Compare commits

...
Author SHA1 Message Date
Jake Potrebic 592f013c5c Add RegistryRetriever for future use in registry API 2026-05-08 21:56:30 +02:00
9 changed files with 138 additions and 34 deletions

No files matched your search

@@ -5,6 +5,7 @@ import io.papermc.paper.registry.tag.Tag;
import io.papermc.paper.registry.tag.TagKey;
import org.bukkit.Keyed;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
/**
* Event object for {@link RegistryEventProvider#compose()}. This
@@ -32,6 +33,19 @@ public interface RegistryComposeEvent<T, B extends RegistryBuilder<T>> extends R
* @param tagKey the tag key
* @return the tag
* @param <V> the tag value type
* @deprecated use {@link #retriever()}
*/
<V extends Keyed> Tag<V> getOrCreateTag(TagKey<V> tagKey); // TODO remove Keyed
@Deprecated(forRemoval = true)
default <V extends Keyed> Tag<V> getOrCreateTag(TagKey<V> tagKey) { // TODO remove Keyed
return this.retriever().getOrCreateTag(tagKey);
}
/**
* Gets the registry retriever, which can be used to retrieve or create
* entries in the registry.
*
* @return the registry retriever
*/
@Contract(pure = true)
RegistryRetriever retriever();
}
@@ -6,6 +6,7 @@ import io.papermc.paper.registry.tag.Tag;
import io.papermc.paper.registry.tag.TagKey;
import org.bukkit.Keyed;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
/**
* Event object for {@link RegistryEventProvider#entryAdd()}. This
@@ -40,6 +41,19 @@ public interface RegistryEntryAddEvent<T, B extends RegistryBuilder<T>> extends
* @param tagKey the tag key
* @return the tag
* @param <V> the tag value type
* @deprecated use {@link #retriever()}
*/
<V extends Keyed> Tag<V> getOrCreateTag(TagKey<V> tagKey);
@Deprecated(forRemoval = true)
default <V extends Keyed> Tag<V> getOrCreateTag(final TagKey<V> tagKey) { // TODO remove Keyed
return this.retriever().getOrCreateTag(tagKey);
}
/**
* Gets the registry retriever, which can be used to retrieve or create
* entries in the registry.
*
* @return the registry retriever
*/
@Contract(pure = true)
RegistryRetriever retriever();
}
@@ -0,0 +1,35 @@
package io.papermc.paper.registry.event;
import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.tag.Tag;
import io.papermc.paper.registry.tag.TagKey;
import org.bukkit.Keyed;
import org.jetbrains.annotations.ApiStatus;
@ApiStatus.NonExtendable
public interface RegistryRetriever {
/**
* Gets or creates a tag for the given tag key. This tag
* is then required to be filled either from the built-in or
* custom datapack.
*
* @param tagKey the tag key
* @param <V> the tag value type
* @return the tag
*/
<V extends Keyed> Tag<V> getOrCreateTag(TagKey<V> tagKey); // TODO remove Keyed
/**
* Gets or creates an empty instance of the given {@link TypedKey}.
* <br>
* Can only create instances if the applicable registry still supports adding
* new references. If the registry doesn't, this method will throw an exception.
*
* @param key the key to get or create an instance for
* @param <V> the type of the value associated with the key
* @return an instance of the given key, or a new instance if it does not exist
* @throws IllegalStateException if this registry does not support adding new references and the key does not exist
*/
<V extends Keyed> V getOrCreate(TypedKey<V> key); // TODO remove Keyed
}
@@ -6,6 +6,7 @@ import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.WritableCraftRegistry;
import io.papermc.paper.registry.data.util.Conversions;
import io.papermc.paper.registry.event.PaperRegistryRetriever;
import io.papermc.paper.registry.event.RegistryEntryAddEventImpl;
import io.papermc.paper.registry.event.RegistryComposeEventImpl;
import java.util.function.BiFunction;
@@ -103,11 +104,11 @@ public sealed interface RegistryEntryMeta<M, A extends Keyed> permits RegistryEn
) implements ServerSide<M, A> {
public RegistryEntryAddEventImpl<A, B> createEntryAddEvent(final TypedKey<A> key, final B initialBuilder, final Conversions conversions) {
return new RegistryEntryAddEventImpl<>(key, initialBuilder, this.apiKey(), conversions);
return new RegistryEntryAddEventImpl<>(key, initialBuilder, this.apiKey(), conversions, new PaperRegistryRetriever(conversions));
}
public RegistryComposeEventImpl<A, B> createPostLoadEvent(final WritableCraftRegistry<M, A, B> writableRegistry, final Conversions conversions) {
return new RegistryComposeEventImpl<>(this.apiKey(), writableRegistry.createApiWritableRegistry(conversions), conversions);
return new RegistryComposeEventImpl<>(this.apiKey(), writableRegistry.createApiWritableRegistry(conversions), conversions, new PaperRegistryRetriever(conversions));
}
@Override
@@ -0,0 +1,51 @@
package io.papermc.paper.registry.event;
import io.papermc.paper.registry.PaperRegistries;
import io.papermc.paper.registry.PaperRegistryAccess;
import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.data.util.Conversions;
import io.papermc.paper.registry.set.NamedRegistryKeySetImpl;
import io.papermc.paper.registry.tag.Tag;
import io.papermc.paper.registry.tag.TagKey;
import java.util.Optional;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceKey;
import org.bukkit.Keyed;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
public final class PaperRegistryRetriever implements RegistryRetriever {
private final Conversions conversions;
public PaperRegistryRetriever(final Conversions conversions) {
this.conversions = conversions;
}
@Override
public <V extends Keyed> Tag<V> getOrCreateTag(final TagKey<V> tagKey) {
final RegistryOps.RegistryInfo<Object> registryInfo = this.conversions.lookup().lookup(PaperRegistries.registryToNms(tagKey.registryKey())).orElseThrow();
final HolderSet.Named<?> tagSet = registryInfo.getter().getOrThrow(PaperRegistries.toNms(tagKey));
return new NamedRegistryKeySetImpl<>(tagKey, tagSet);
}
@Override
public <V extends Keyed> V getOrCreate(final TypedKey<V> key) {
final Registry<V> registry = PaperRegistryAccess.instance().getRegistry(key.registryKey());
final V value = registry.get(key);
if (value != null) {
return value;
} else if (!(registry instanceof final CraftRegistry<V, ?> craftRegistry)) {
throw new IllegalStateException("Cannot create instance for key " + key + " as the registry is not a CraftRegistry");
} else if (!craftRegistry.constructorUsesHolder()) {
throw new IllegalStateException("Cannot create instance for key " + key + " as the registry does not yet support creating instances with Holder references");
} else {
final ResourceKey<? extends net.minecraft.core.Registry<Object>> resourceKey = PaperRegistries.registryToNms(key.registryKey());
final RegistryOps.RegistryInfo<?> registryInfo = this.conversions.lookup().lookup(resourceKey).orElseThrow(() -> new IllegalStateException("Cannot create instance for key " + key + " as the registry is not registered in the lookup"));
registryInfo.getter().get(PaperRegistries.toNms(key)).orElseThrow(() -> new IllegalStateException("Cannot create instance for key " + key + " as it does not exist in the registry " + resourceKey));
return craftRegistry.getOrThrow(key);
}
}
}
@@ -1,27 +1,14 @@
package io.papermc.paper.registry.event;
import io.papermc.paper.plugin.lifecycle.event.PaperLifecycleEvent;
import io.papermc.paper.registry.PaperRegistries;
import io.papermc.paper.registry.RegistryBuilder;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.data.util.Conversions;
import io.papermc.paper.registry.set.NamedRegistryKeySetImpl;
import io.papermc.paper.registry.tag.Tag;
import io.papermc.paper.registry.tag.TagKey;
import net.minecraft.core.HolderSet;
import net.minecraft.resources.RegistryOps;
import org.bukkit.Keyed;
public record RegistryComposeEventImpl<T, B extends RegistryBuilder<T>>(
RegistryKey<T> registryKey,
WritableRegistry<T, B> registry,
Conversions conversions
Conversions conversions,
RegistryRetriever retriever
) implements RegistryComposeEvent<T, B>, PaperLifecycleEvent {
@Override
public <V extends Keyed> Tag<V> getOrCreateTag(final TagKey<V> tagKey) {
final RegistryOps.RegistryInfo<Object> registryInfo = this.conversions.lookup().lookup(PaperRegistries.registryToNms(tagKey.registryKey())).orElseThrow();
final HolderSet.Named<?> tagSet = registryInfo.getter().getOrThrow(PaperRegistries.toNms(tagKey));
return new NamedRegistryKeySetImpl<>(tagKey, tagSet);
}
}
@@ -1,29 +1,16 @@
package io.papermc.paper.registry.event;
import io.papermc.paper.plugin.lifecycle.event.PaperLifecycleEvent;
import io.papermc.paper.registry.PaperRegistries;
import io.papermc.paper.registry.RegistryBuilder;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.data.util.Conversions;
import io.papermc.paper.registry.set.NamedRegistryKeySetImpl;
import io.papermc.paper.registry.tag.Tag;
import io.papermc.paper.registry.tag.TagKey;
import net.minecraft.core.HolderSet;
import net.minecraft.resources.RegistryOps;
import org.bukkit.Keyed;
public record RegistryEntryAddEventImpl<T, B extends RegistryBuilder<T>>(
TypedKey<T> key,
B builder,
RegistryKey<T> registryKey,
Conversions conversions
Conversions conversions,
RegistryRetriever retriever
) implements RegistryEntryAddEvent<T, B>, PaperLifecycleEvent {
@Override
public <V extends Keyed> Tag<V> getOrCreateTag(final TagKey<V> tagKey) {
final RegistryOps.RegistryInfo<Object> registryInfo = this.conversions.lookup().lookup(PaperRegistries.registryToNms(tagKey.registryKey())).orElseThrow();
final HolderSet.Named<?> tagSet = registryInfo.getter().getOrThrow(PaperRegistries.toNms(tagKey));
return new NamedRegistryKeySetImpl<>(tagKey, tagSet);
}
}
@@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import io.papermc.paper.registry.PaperRegistries;
import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.entry.RegistryEntryMeta;
import io.papermc.paper.registry.set.NamedRegistryKeySetImpl;
import io.papermc.paper.registry.tag.Tag;
@@ -245,6 +246,10 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
return this.minecraftToBukkit.supportsDirectHolders();
}
public boolean constructorUsesHolder() {
return this.minecraftToBukkit.constructorUsesHolder();
}
@Override
public NamespacedKey getKey(final B value) {
if (value instanceof Holderable<?> holderable) {
@@ -2,6 +2,11 @@ package io.papermc.testplugin;
import io.papermc.paper.plugin.bootstrap.BootstrapContext;
import io.papermc.paper.plugin.bootstrap.PluginBootstrap;
import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.event.RegistryEvents;
import io.papermc.paper.registry.keys.WolfVariantKeys;
import net.kyori.adventure.key.Key;
import org.bukkit.entity.Wolf;
import org.jetbrains.annotations.NotNull;
public class TestPluginBootstrap implements PluginBootstrap {
@@ -9,6 +14,11 @@ public class TestPluginBootstrap implements PluginBootstrap {
@Override
public void bootstrap(@NotNull BootstrapContext context) {
// io.papermc.testplugin.brigtests.Registration.registerViaBootstrap(context);
context.getLifecycleManager().registerEventHandler(RegistryEvents.ENCHANTMENT.compose(), event -> {
final Wolf.Variant v = event.retriever().getOrCreate(WolfVariantKeys.create(Key.key("mm:test")));
System.out.println("test");
});
}
}