From a1b891dd884b85ebb70ec71b9384d84fdf843b8d Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Mon, 27 Feb 2023 18:28:39 -0800 Subject: [PATCH] Registry Modification API == AT == public net.minecraft.core.MappedRegistry validateWrite(Lnet/minecraft/resources/ResourceKey;)V public net.minecraft.resources.RegistryOps lookupProvider public net.minecraft.resources.RegistryOps$HolderLookupAdapter --- .../minecraft/core/MappedRegistry.java.patch | 14 ++ .../registries/BuiltInRegistries.java.patch | 29 ++- .../resources/RegistryDataLoader.java.patch | 60 +++++- .../ReloadableServerRegistries.java.patch | 24 ++- .../paper/registry/PaperRegistries.java | 10 + .../paper/registry/PaperRegistryAccess.java | 8 + .../paper/registry/PaperRegistryBuilder.java | 25 +++ .../PaperRegistryListenerManager.java | 183 ++++++++++++++++++ .../paper/registry/PaperSimpleRegistry.java | 16 ++ .../paper/registry/WritableCraftRegistry.java | 77 ++++++++ .../paper/registry/data/util/Conversions.java | 58 ++++++ .../registry/data/util/package-info.java | 4 + .../registry/entry/AddableRegistryEntry.java | 41 ++++ .../entry/ModifiableRegistryEntry.java | 29 +++ .../paper/registry/entry/RegistryEntry.java | 66 +++++++ .../registry/entry/RegistryEntryBuilder.java | 13 ++ .../registry/entry/WritableRegistryEntry.java | 25 +++ .../event/RegistryEntryAddEventImpl.java | 29 +++ .../registry/event/RegistryEventMap.java | 45 +++++ .../event/RegistryEventTypeProviderImpl.java | 24 +++ .../event/RegistryFreezeEventImpl.java | 27 +++ .../paper/registry/event/package-info.java | 4 + .../type/RegistryEntryAddEventTypeImpl.java | 37 ++++ .../RegistryEntryAddHandlerConfiguration.java | 42 ++++ .../type/RegistryLifecycleEventType.java | 18 ++ .../registry/event/type/package-info.java | 4 + .../registry/legacy/DelayedRegistry.java | 13 ++ .../registry/set/NamedRegistryKeySetImpl.java | 74 +++++++ .../paper/registry/set/PaperRegistrySets.java | 45 +++++ .../paper/registry/set/package-info.java | 4 + .../org/bukkit/craftbukkit/CraftRegistry.java | 13 ++ ...r.registry.event.RegistryEventTypeProvider | 1 + .../paper/registry/RegistryBuilderTest.java | 44 +++++ .../bukkit/registry/RegistryClassTest.java | 12 +- .../support/extension/NormalExtension.java | 2 +- 35 files changed, 1109 insertions(+), 11 deletions(-) create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryBuilder.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryListenerManager.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/data/util/Conversions.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/data/util/package-info.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/entry/AddableRegistryEntry.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/entry/ModifiableRegistryEntry.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/entry/WritableRegistryEntry.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEventImpl.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventTypeProviderImpl.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEventImpl.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/event/package-info.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddEventTypeImpl.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddHandlerConfiguration.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryLifecycleEventType.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/event/type/package-info.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/set/NamedRegistryKeySetImpl.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/set/PaperRegistrySets.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/set/package-info.java create mode 100644 paper-server/src/main/resources/META-INF/services/io.papermc.paper.registry.event.RegistryEventTypeProvider create mode 100644 paper-server/src/test/java/io/papermc/paper/registry/RegistryBuilderTest.java diff --git a/paper-server/patches/sources/net/minecraft/core/MappedRegistry.java.patch b/paper-server/patches/sources/net/minecraft/core/MappedRegistry.java.patch index 8599466b69..4d3c8b552e 100644 --- a/paper-server/patches/sources/net/minecraft/core/MappedRegistry.java.patch +++ b/paper-server/patches/sources/net/minecraft/core/MappedRegistry.java.patch @@ -17,3 +17,17 @@ private Lifecycle registryLifecycle; private final Map, HolderSet.Named> frozenTags = new IdentityHashMap<>(); MappedRegistry.TagSet allTags = MappedRegistry.TagSet.unbound(); +@@ -508,5 +508,13 @@ + void forEach(BiConsumer, ? super HolderSet.Named> consumer); + + Stream> getTags(); ++ } ++ // Paper start ++ // used to clear intrusive holders from GameEvent, Item, Block, EntityType, and Fluid from unused instances of those types ++ public void clearIntrusiveHolder(final T instance) { ++ if (this.unregisteredIntrusiveHolders != null) { ++ this.unregisteredIntrusiveHolders.remove(instance); ++ } + } ++ // Paper end + } diff --git a/paper-server/patches/sources/net/minecraft/core/registries/BuiltInRegistries.java.patch b/paper-server/patches/sources/net/minecraft/core/registries/BuiltInRegistries.java.patch index 9ebbb08908..d363add98b 100644 --- a/paper-server/patches/sources/net/minecraft/core/registries/BuiltInRegistries.java.patch +++ b/paper-server/patches/sources/net/minecraft/core/registries/BuiltInRegistries.java.patch @@ -1,6 +1,24 @@ --- a/net/minecraft/core/registries/BuiltInRegistries.java +++ b/net/minecraft/core/registries/BuiltInRegistries.java -@@ -323,14 +323,21 @@ +@@ -296,6 +296,17 @@ + public static final Registry> SLOT_DISPLAY = registerSimple(Registries.SLOT_DISPLAY, SlotDisplays::bootstrap); + public static final Registry RECIPE_BOOK_CATEGORY = registerSimple(Registries.RECIPE_BOOK_CATEGORY, RecipeBookCategories::bootstrap); + public static final Registry> REGISTRY = WRITABLE_REGISTRY; ++ // Paper start - add built-in registry conversions ++ public static final io.papermc.paper.registry.data.util.Conversions BUILT_IN_CONVERSIONS = new io.papermc.paper.registry.data.util.Conversions(new net.minecraft.resources.RegistryOps.RegistryInfoLookup() { ++ @Override ++ public java.util.Optional> lookup(final ResourceKey> registryRef) { ++ final Registry registry = net.minecraft.server.RegistryLayer.STATIC_ACCESS.lookupOrThrow(registryRef); ++ return java.util.Optional.of( ++ new net.minecraft.resources.RegistryOps.RegistryInfo<>(registry, registry, Lifecycle.experimental()) ++ ); ++ } ++ }); ++ // Paper end - add built-in registry conversions + + private static Registry registerSimple(ResourceKey> key, BuiltInRegistries.RegistryBootstrap initializer) { + return internalRegister(key, new MappedRegistry<>(key, Lifecycle.stable(), false), initializer); +@@ -323,14 +334,22 @@ ResourceKey> key, R registry, BuiltInRegistries.RegistryBootstrap initializer ) { Bootstrap.checkBootstrapCalled(() -> "registry " + key.location()); @@ -18,8 +36,17 @@ + } + public static void bootStrap(Runnable runnable) { + // Paper end ++ REGISTRY.freeze(); // Paper - freeze main registry early createContents(); + runnable.run(); // Paper freeze(); validate(REGISTRY); } +@@ -348,6 +367,7 @@ + + for (Registry registry : REGISTRY) { + bindBootstrappedTagsToEmpty(registry); ++ io.papermc.paper.registry.PaperRegistryListenerManager.INSTANCE.runFreezeListeners(registry.key(), BUILT_IN_CONVERSIONS); // Paper + registry.freeze(); + } + } diff --git a/paper-server/patches/sources/net/minecraft/resources/RegistryDataLoader.java.patch b/paper-server/patches/sources/net/minecraft/resources/RegistryDataLoader.java.patch index 4aab234f05..055ca49e4b 100644 --- a/paper-server/patches/sources/net/minecraft/resources/RegistryDataLoader.java.patch +++ b/paper-server/patches/sources/net/minecraft/resources/RegistryDataLoader.java.patch @@ -9,7 +9,65 @@ private static final RegistrationInfo NETWORK_REGISTRATION_INFO = new RegistrationInfo(Optional.empty(), Lifecycle.experimental()); private static final Function, RegistrationInfo> REGISTRATION_INFO_CACHE = Util.memoize(knownPacks -> { Lifecycle lifecycle = knownPacks.map(KnownPack::isVanilla).map(vanilla -> Lifecycle.stable()).orElse(Lifecycle.experimental()); -@@ -349,6 +349,7 @@ +@@ -238,13 +238,13 @@ + } + + private static void loadElementFromResource( +- WritableRegistry registry, Decoder decoder, RegistryOps ops, ResourceKey key, Resource resource, RegistrationInfo entryInfo ++ WritableRegistry registry, Decoder decoder, RegistryOps ops, ResourceKey key, Resource resource, RegistrationInfo entryInfo, io.papermc.paper.registry.data.util.Conversions conversions // Paper - pass conversions + ) throws IOException { + try (Reader reader = resource.openAsReader()) { + JsonElement jsonElement = JsonParser.parseReader(reader); + DataResult dataResult = decoder.parse(ops, jsonElement); + E object = dataResult.getOrThrow(); +- registry.register(key, object, entryInfo); ++ io.papermc.paper.registry.PaperRegistryListenerManager.INSTANCE.registerWithListeners(registry, key, object, entryInfo, conversions); // Paper - register with listeners + } + } + +@@ -258,6 +258,7 @@ + FileToIdConverter fileToIdConverter = FileToIdConverter.registry(registry.key()); + RegistryOps registryOps = RegistryOps.create(JsonOps.INSTANCE, infoGetter); + ++ final io.papermc.paper.registry.data.util.Conversions conversions = new io.papermc.paper.registry.data.util.Conversions(infoGetter); // Paper - create conversions + for (Entry entry : fileToIdConverter.listMatchingResources(resourceManager).entrySet()) { + ResourceLocation resourceLocation = entry.getKey(); + ResourceKey resourceKey = ResourceKey.create(registry.key(), fileToIdConverter.fileToId(resourceLocation)); +@@ -265,7 +266,7 @@ + RegistrationInfo registrationInfo = REGISTRATION_INFO_CACHE.apply(resource.knownPackInfo()); + + try { +- loadElementFromResource(registry, elementDecoder, registryOps, resourceKey, resource, registrationInfo); ++ loadElementFromResource(registry, elementDecoder, registryOps, resourceKey, resource, registrationInfo, conversions); // Paper - pass conversions + } catch (Exception var14) { + errors.put( + resourceKey, +@@ -274,6 +275,7 @@ + } + } + ++ io.papermc.paper.registry.PaperRegistryListenerManager.INSTANCE.runFreezeListeners(registry.key(), conversions); // Paper - run pre-freeze listeners + TagLoader.loadTagsForRegistry(resourceManager, registry); + } + +@@ -291,6 +293,7 @@ + RegistryOps registryOps2 = RegistryOps.create(JsonOps.INSTANCE, infoGetter); + FileToIdConverter fileToIdConverter = FileToIdConverter.registry(registry.key()); + ++ final io.papermc.paper.registry.data.util.Conversions conversions = new io.papermc.paper.registry.data.util.Conversions(infoGetter); // Paper - create conversions + for (RegistrySynchronization.PackedRegistryEntry packedRegistryEntry : networkedRegistryData.elements) { + ResourceKey resourceKey = ResourceKey.create(registry.key(), packedRegistryEntry.id()); + Optional optional = packedRegistryEntry.data(); +@@ -309,7 +312,7 @@ + + try { + Resource resource = factory.getResourceOrThrow(resourceLocation); +- loadElementFromResource(registry, decoder, registryOps2, resourceKey, resource, NETWORK_REGISTRATION_INFO); ++ loadElementFromResource(registry, decoder, registryOps2, resourceKey, resource, NETWORK_REGISTRATION_INFO, conversions); // Paper - pass conversions + } catch (Exception var17) { + loadingErrors.put(resourceKey, new IllegalStateException("Failed to parse local data", var17)); + } +@@ -349,6 +352,7 @@ RegistryDataLoader.Loader create(Lifecycle lifecycle, Map, Exception> errors) { WritableRegistry writableRegistry = new MappedRegistry<>(this.key, lifecycle); diff --git a/paper-server/patches/sources/net/minecraft/server/ReloadableServerRegistries.java.patch b/paper-server/patches/sources/net/minecraft/server/ReloadableServerRegistries.java.patch index 090b4129c0..a41fb37ed3 100644 --- a/paper-server/patches/sources/net/minecraft/server/ReloadableServerRegistries.java.patch +++ b/paper-server/patches/sources/net/minecraft/server/ReloadableServerRegistries.java.patch @@ -1,10 +1,30 @@ --- a/net/minecraft/server/ReloadableServerRegistries.java +++ b/net/minecraft/server/ReloadableServerRegistries.java -@@ -64,6 +64,7 @@ +@@ -50,8 +50,9 @@ + ); + HolderLookup.Provider provider = HolderLookup.Provider.create(list.stream()); + RegistryOps registryOps = provider.createSerializationContext(JsonOps.INSTANCE); ++ final io.papermc.paper.registry.data.util.Conversions conversions = new io.papermc.paper.registry.data.util.Conversions(registryOps.lookupProvider); // Paper + List>> list2 = LootDataType.values() +- .map(type -> scheduleRegistryLoad((LootDataType)type, registryOps, resourceManager, prepareExecutor)) ++ .map(type -> scheduleRegistryLoad((LootDataType)type, registryOps, resourceManager, prepareExecutor, conversions)) // Paper + .toList(); + CompletableFuture>> completableFuture = Util.sequence(list2); + return completableFuture.thenApplyAsync( +@@ -60,13 +61,14 @@ + } + + private static CompletableFuture> scheduleRegistryLoad( +- LootDataType type, RegistryOps ops, ResourceManager resourceManager, Executor prepareExecutor ++ LootDataType type, RegistryOps ops, ResourceManager resourceManager, Executor prepareExecutor, io.papermc.paper.registry.data.util.Conversions conversions // Paper ) { return CompletableFuture.supplyAsync(() -> { WritableRegistry writableRegistry = new MappedRegistry<>(type.registryKey(), Lifecycle.experimental()); + io.papermc.paper.registry.PaperRegistryAccess.instance().registerReloadableRegistry(type.registryKey(), writableRegistry); // Paper - register reloadable registry Map map = new HashMap<>(); SimpleJsonResourceReloadListener.scanDirectory(resourceManager, type.registryKey(), ops, type.codec(), map); - map.forEach((id, value) -> writableRegistry.register(ResourceKey.create(type.registryKey(), id), (T)value, DEFAULT_REGISTRATION_INFO)); +- map.forEach((id, value) -> writableRegistry.register(ResourceKey.create(type.registryKey(), id), (T)value, DEFAULT_REGISTRATION_INFO)); ++ map.forEach((id, value) -> io.papermc.paper.registry.PaperRegistryListenerManager.INSTANCE.registerWithListeners(writableRegistry, ResourceKey.create(type.registryKey(), id), value, DEFAULT_REGISTRATION_INFO, conversions)); // Paper - register with listeners + TagLoader.loadTagsForRegistry(resourceManager, writableRegistry); + return writableRegistry; + }, prepareExecutor); diff --git a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java index ea99f30a31..875b3e761d 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java @@ -3,6 +3,7 @@ package io.papermc.paper.registry; import com.google.common.base.Preconditions; import io.papermc.paper.adventure.PaperAdventure; import io.papermc.paper.registry.entry.RegistryEntry; +import io.papermc.paper.registry.tag.TagKey; import java.util.Collections; import java.util.IdentityHashMap; import java.util.List; @@ -148,6 +149,15 @@ public final class PaperRegistries { return ResourceKey.create((ResourceKey>) PaperRegistries.registryToNms(typedKey.registryKey()), PaperAdventure.asVanilla(typedKey.key())); } + public static TagKey fromNms(final net.minecraft.tags.TagKey tagKey) { + return TagKey.create(registryFromNms(tagKey.registry()), CraftNamespacedKey.fromMinecraft(tagKey.location())); + } + + @SuppressWarnings({"unchecked", "RedundantCast"}) + public static net.minecraft.tags.TagKey toNms(final TagKey tagKey) { + return net.minecraft.tags.TagKey.create((ResourceKey>) registryToNms(tagKey.registryKey()), PaperAdventure.asVanilla(tagKey.key())); + } + private PaperRegistries() { } } diff --git a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java index 4bf7915867..ed071ed34e 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java @@ -78,6 +78,14 @@ public class PaperRegistryAccess implements RegistryAccess { return possiblyUnwrap(registryHolder.get()); } + public > WritableCraftRegistry getWritableRegistry(final RegistryKey key) { + final Registry registry = this.getRegistry(key); + if (registry instanceof WritableCraftRegistry) { + return (WritableCraftRegistry) registry; + } + throw new IllegalArgumentException(key + " does not point to a writable registry"); + } + private static Registry possiblyUnwrap(final Registry registry) { if (registry instanceof final DelayedRegistry delayedRegistry) { // if not coming from legacy, unwrap the delayed registry return delayedRegistry.delegate(); diff --git a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryBuilder.java b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryBuilder.java new file mode 100644 index 0000000000..6a60d7b7ed --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryBuilder.java @@ -0,0 +1,25 @@ +package io.papermc.paper.registry; + +import io.papermc.paper.registry.data.util.Conversions; +import org.jspecify.annotations.Nullable; + +public interface PaperRegistryBuilder extends RegistryBuilder { + + M build(); + + @FunctionalInterface + interface Filler> { + + B fill(Conversions conversions, @Nullable M nms); + + default Factory asFactory() { + return (lookup) -> this.fill(lookup, null); + } + } + + @FunctionalInterface + interface Factory> { + + B create(Conversions conversions); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryListenerManager.java b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryListenerManager.java new file mode 100644 index 0000000000..4f98c15d40 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryListenerManager.java @@ -0,0 +1,183 @@ +package io.papermc.paper.registry; + +import com.google.common.base.Preconditions; +import com.mojang.serialization.Lifecycle; +import io.papermc.paper.plugin.bootstrap.BootstrapContext; +import io.papermc.paper.plugin.entrypoint.Entrypoint; +import io.papermc.paper.plugin.entrypoint.LaunchEntryPointHandler; +import io.papermc.paper.plugin.lifecycle.event.LifecycleEventRunner; +import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEventType; +import io.papermc.paper.registry.data.util.Conversions; +import io.papermc.paper.registry.entry.RegistryEntry; +import io.papermc.paper.registry.entry.RegistryEntryInfo; +import io.papermc.paper.registry.event.RegistryEntryAddEventImpl; +import io.papermc.paper.registry.event.RegistryEventMap; +import io.papermc.paper.registry.event.RegistryEventProvider; +import io.papermc.paper.registry.event.RegistryFreezeEvent; +import io.papermc.paper.registry.event.RegistryFreezeEventImpl; +import io.papermc.paper.registry.event.type.RegistryEntryAddEventType; +import io.papermc.paper.registry.event.type.RegistryEntryAddEventTypeImpl; +import io.papermc.paper.registry.event.type.RegistryLifecycleEventType; +import java.util.Optional; +import net.kyori.adventure.key.Key; +import net.minecraft.core.Holder; +import net.minecraft.core.MappedRegistry; +import net.minecraft.core.RegistrationInfo; +import net.minecraft.core.Registry; +import net.minecraft.core.WritableRegistry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import org.intellij.lang.annotations.Subst; +import org.jspecify.annotations.Nullable; + +public class PaperRegistryListenerManager { + + public static final PaperRegistryListenerManager INSTANCE = new PaperRegistryListenerManager(); + + public final RegistryEventMap valueAddEventTypes = new RegistryEventMap("value add"); + public final RegistryEventMap freezeEventTypes = new RegistryEventMap("freeze"); + + private PaperRegistryListenerManager() { + } + + /** + * For {@link Registry#register(Registry, String, Object)} + */ + public M registerWithListeners(final Registry registry, final String id, final M nms) { + return this.registerWithListeners(registry, ResourceLocation.withDefaultNamespace(id), nms); + } + + /** + * For {@link Registry#register(Registry, ResourceLocation, Object)} + */ + public M registerWithListeners(final Registry registry, final ResourceLocation loc, final M nms) { + return this.registerWithListeners(registry, ResourceKey.create(registry.key(), loc), nms); + } + + /** + * For {@link Registry#register(Registry, ResourceKey, Object)} + */ + public M registerWithListeners(final Registry registry, final ResourceKey key, final M nms) { + return this.registerWithListeners(registry, key, nms, RegistrationInfo.BUILT_IN, PaperRegistryListenerManager::registerWithInstance, BuiltInRegistries.BUILT_IN_CONVERSIONS); + } + + /** + * For {@link Registry#registerForHolder(Registry, ResourceLocation, Object)} + */ + public Holder.Reference registerForHolderWithListeners(final Registry registry, final ResourceLocation loc, final M nms) { + return this.registerForHolderWithListeners(registry, ResourceKey.create(registry.key(), loc), nms); + } + + /** + * For {@link Registry#registerForHolder(Registry, ResourceKey, Object)} + */ + public Holder.Reference registerForHolderWithListeners(final Registry registry, final ResourceKey key, final M nms) { + return this.registerWithListeners(registry, key, nms, RegistrationInfo.BUILT_IN, WritableRegistry::register, BuiltInRegistries.BUILT_IN_CONVERSIONS); + } + + public void registerWithListeners( + final Registry registry, + final ResourceKey key, + final M nms, + final RegistrationInfo registrationInfo, + final Conversions conversions + ) { + this.registerWithListeners(registry, key, nms, registrationInfo, WritableRegistry::register, conversions); + } + + // TODO remove Keyed + public , R> R registerWithListeners( + final Registry registry, + final ResourceKey key, + final M nms, + final RegistrationInfo registrationInfo, + final RegisterMethod registerMethod, + final Conversions conversions + ) { + Preconditions.checkState(LaunchEntryPointHandler.INSTANCE.hasEntered(Entrypoint.BOOTSTRAPPER), registry.key() + " tried to run modification listeners before bootstrappers have been called"); // verify that bootstrappers have been called + final RegistryEntryInfo entry = PaperRegistries.getEntry(registry.key()); + if (!RegistryEntry.Modifiable.isModifiable(entry) || !this.valueAddEventTypes.hasHandlers(entry.apiKey())) { + return registerMethod.register((WritableRegistry) registry, key, nms, registrationInfo); + } + final RegistryEntry.Modifiable modifiableEntry = RegistryEntry.Modifiable.asModifiable(entry); + @SuppressWarnings("PatternValidation") final TypedKey typedKey = TypedKey.create(entry.apiKey(), Key.key(key.location().getNamespace(), key.location().getPath())); + final B builder = modifiableEntry.fillBuilder(conversions, nms); + return this.registerWithListeners(registry, modifiableEntry, key, nms, builder, registrationInfo, registerMethod, conversions); + } + + > void registerWithListeners( // TODO remove Keyed + final WritableRegistry registry, + final RegistryEntryInfo entry, + final ResourceKey key, + final B builder, + final RegistrationInfo registrationInfo, + final Conversions conversions + ) { + if (!RegistryEntry.Modifiable.isModifiable(entry) || !this.valueAddEventTypes.hasHandlers(entry.apiKey())) { + registry.register(key, builder.build(), registrationInfo); + return; + } + this.registerWithListeners(registry, RegistryEntry.Modifiable.asModifiable(entry), key, null, builder, registrationInfo, WritableRegistry::register, conversions); + } + + public , R> R registerWithListeners( // TODO remove Keyed + final Registry registry, + final RegistryEntry.Modifiable entry, + final ResourceKey key, + final @Nullable M oldNms, + final B builder, + RegistrationInfo registrationInfo, + final RegisterMethod registerMethod, + final Conversions conversions + ) { + @Subst("namespace:key") final ResourceLocation beingAdded = key.location(); + @SuppressWarnings("PatternValidation") final TypedKey typedKey = TypedKey.create(entry.apiKey(), Key.key(beingAdded.getNamespace(), beingAdded.getPath())); + final RegistryEntryAddEventImpl event = entry.createEntryAddEvent(typedKey, builder, conversions); + LifecycleEventRunner.INSTANCE.callEvent(this.valueAddEventTypes.getEventType(entry.apiKey()), event); + if (oldNms != null) { + ((MappedRegistry) registry).clearIntrusiveHolder(oldNms); + } + final M newNms = event.builder().build(); + if (oldNms != null && !newNms.equals(oldNms)) { + registrationInfo = new RegistrationInfo(Optional.empty(), Lifecycle.experimental()); + } + return registerMethod.register((WritableRegistry) registry, key, newNms, registrationInfo); + } + + private static M registerWithInstance(final WritableRegistry writableRegistry, final ResourceKey key, final M value, final RegistrationInfo registrationInfo) { + writableRegistry.register(key, value, registrationInfo); + return value; + } + + @FunctionalInterface + public interface RegisterMethod { + + R register(WritableRegistry writableRegistry, ResourceKey key, M value, RegistrationInfo registrationInfo); + } + + public > void runFreezeListeners(final ResourceKey> resourceKey, final Conversions conversions) { + final RegistryEntryInfo entry = PaperRegistries.getEntry(resourceKey); + if (!RegistryEntry.Addable.isAddable(entry) || !this.freezeEventTypes.hasHandlers(entry.apiKey())) { + return; + } + final RegistryEntry.Addable writableEntry = RegistryEntry.Addable.asAddable(entry); + final WritableCraftRegistry writableRegistry = PaperRegistryAccess.instance().getWritableRegistry(entry.apiKey()); + final RegistryFreezeEventImpl event = writableEntry.createFreezeEvent(writableRegistry, conversions); + LifecycleEventRunner.INSTANCE.callEvent(this.freezeEventTypes.getEventType(entry.apiKey()), event); + } + + public > RegistryEntryAddEventType getRegistryValueAddEventType(final RegistryEventProvider type) { + if (!RegistryEntry.Modifiable.isModifiable(PaperRegistries.getEntry(type.registryKey()))) { + throw new IllegalArgumentException(type.registryKey() + " does not support RegistryEntryAddEvent"); + } + return this.valueAddEventTypes.getOrCreate(type.registryKey(), RegistryEntryAddEventTypeImpl::new); + } + + public > LifecycleEventType.Prioritizable> getRegistryFreezeEventType(final RegistryEventProvider type) { + if (!RegistryEntry.Addable.isAddable(PaperRegistries.getEntry(type.registryKey()))) { + throw new IllegalArgumentException(type.registryKey() + " does not support RegistryFreezeEvent"); + } + return this.freezeEventTypes.getOrCreate(type.registryKey(), RegistryLifecycleEventType::new); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java b/paper-server/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java index 6d134ace04..cc39bc68d2 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java @@ -1,6 +1,10 @@ package io.papermc.paper.registry; +import io.papermc.paper.registry.set.NamedRegistryKeySetImpl; +import io.papermc.paper.registry.tag.Tag; +import io.papermc.paper.registry.tag.TagKey; import java.util.function.Predicate; +import net.minecraft.core.HolderSet; import net.minecraft.core.registries.BuiltInRegistries; import org.bukkit.Keyed; import org.bukkit.Particle; @@ -35,4 +39,16 @@ public class PaperSimpleRegistry & Keyed, M> extends Registry. super(type, predicate); this.nmsRegistry = nmsRegistry; } + + @Override + public boolean hasTag(final TagKey key) { + final net.minecraft.tags.TagKey nmsKey = PaperRegistries.toNms(key); + return this.nmsRegistry.get(nmsKey).isPresent(); + } + + @Override + public Tag getTag(final TagKey key) { + final HolderSet.Named namedHolderSet = this.nmsRegistry.get(PaperRegistries.toNms(key)).orElseThrow(); + return new NamedRegistryKeySetImpl<>(key, namedHolderSet); + } } diff --git a/paper-server/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java b/paper-server/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java new file mode 100644 index 0000000000..f201f14250 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java @@ -0,0 +1,77 @@ +package io.papermc.paper.registry; + +import com.mojang.serialization.Lifecycle; +import io.papermc.paper.registry.data.util.Conversions; +import io.papermc.paper.registry.entry.RegistryEntry; +import io.papermc.paper.registry.entry.RegistryTypeMapper; +import io.papermc.paper.registry.event.WritableRegistry; +import java.util.Optional; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import net.minecraft.core.MappedRegistry; +import net.minecraft.core.RegistrationInfo; +import net.minecraft.resources.ResourceKey; +import org.bukkit.Keyed; +import org.bukkit.NamespacedKey; +import org.bukkit.craftbukkit.CraftRegistry; +import org.bukkit.craftbukkit.util.ApiVersion; + +public class WritableCraftRegistry> extends CraftRegistry { + + private static final RegistrationInfo FROM_PLUGIN = new RegistrationInfo(Optional.empty(), Lifecycle.experimental()); + + private final RegistryEntry.BuilderHolder entry; + private final MappedRegistry registry; + private final PaperRegistryBuilder.Factory builderFactory; + + public WritableCraftRegistry( + final RegistryEntry.BuilderHolder entry, + final Class classToPreload, + final MappedRegistry registry, + final BiFunction serializationUpdater, + final PaperRegistryBuilder.Factory builderFactory, + final RegistryTypeMapper minecraftToBukkit + ) { + super(classToPreload, registry, minecraftToBukkit, serializationUpdater); + this.entry = entry; + this.registry = registry; + this.builderFactory = builderFactory; + } + + public void register(final TypedKey key, final Consumer value, final Conversions conversions) { + final ResourceKey resourceKey = PaperRegistries.toNms(key); + this.registry.validateWrite(resourceKey); + final B builder = this.newBuilder(conversions); + value.accept(builder); + PaperRegistryListenerManager.INSTANCE.registerWithListeners( + this.registry, + RegistryEntry.Modifiable.asModifiable(this.entry), + resourceKey, + builder, + FROM_PLUGIN, + conversions + ); + } + + public WritableRegistry createApiWritableRegistry(final Conversions conversions) { + return new ApiWritableRegistry(conversions); + } + + protected B newBuilder(final Conversions conversions) { + return this.builderFactory.create(conversions); + } + + public class ApiWritableRegistry implements WritableRegistry { + + private final Conversions conversions; + + public ApiWritableRegistry(final Conversions conversions) { + this.conversions = conversions; + } + + @Override + public void register(final TypedKey key, final Consumer value) { + WritableCraftRegistry.this.register(key, value, this.conversions); + } + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/data/util/Conversions.java b/paper-server/src/main/java/io/papermc/paper/registry/data/util/Conversions.java new file mode 100644 index 0000000000..b1710da835 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/data/util/Conversions.java @@ -0,0 +1,58 @@ +package io.papermc.paper.registry.data.util; + +import com.google.common.base.Preconditions; +import com.mojang.serialization.JavaOps; +import io.papermc.paper.adventure.WrapperAwareSerializer; +import java.util.Optional; +import net.kyori.adventure.text.Component; +import net.minecraft.core.Registry; +import net.minecraft.core.RegistryAccess; +import net.minecraft.resources.RegistryOps; +import net.minecraft.resources.ResourceKey; +import org.bukkit.craftbukkit.CraftRegistry; +import org.jetbrains.annotations.Contract; +import org.jspecify.annotations.Nullable; + +public class Conversions { + + private static @Nullable Conversions globalInstance; + public static Conversions global() { + if (globalInstance == null) { + final RegistryAccess globalAccess = CraftRegistry.getMinecraftRegistry(); + Preconditions.checkState(globalAccess != null, "Global registry access is not available"); + globalInstance = new Conversions(new RegistryOps.RegistryInfoLookup() { + @Override + public Optional> lookup(final ResourceKey> registryRef) { + final Registry registry = globalAccess.lookupOrThrow(registryRef); + return Optional.of( + new RegistryOps.RegistryInfo<>(registry, registry, registry.registryLifecycle()) + ); + } + }); + } + return globalInstance; + } + + + private final RegistryOps.RegistryInfoLookup lookup; + private final WrapperAwareSerializer serializer; + + public Conversions(final RegistryOps.RegistryInfoLookup lookup) { + this.lookup = lookup; + this.serializer = new WrapperAwareSerializer(() -> RegistryOps.create(JavaOps.INSTANCE, lookup)); + } + + public RegistryOps.RegistryInfoLookup lookup() { + return this.lookup; + } + + @Contract("null -> null; !null -> !null") + public net.minecraft.network.chat.@Nullable Component asVanilla(final @Nullable Component adventure) { + if (adventure == null) return null; + return this.serializer.serialize(adventure); + } + + public Component asAdventure(final net.minecraft.network.chat.@Nullable Component vanilla) { + return vanilla == null ? Component.empty() : this.serializer.deserialize(vanilla); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/data/util/package-info.java b/paper-server/src/main/java/io/papermc/paper/registry/data/util/package-info.java new file mode 100644 index 0000000000..5b88be976c --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/data/util/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package io.papermc.paper.registry.data.util; + +import org.jspecify.annotations.NullMarked; diff --git a/paper-server/src/main/java/io/papermc/paper/registry/entry/AddableRegistryEntry.java b/paper-server/src/main/java/io/papermc/paper/registry/entry/AddableRegistryEntry.java new file mode 100644 index 0000000000..c44edcf13e --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/entry/AddableRegistryEntry.java @@ -0,0 +1,41 @@ +package io.papermc.paper.registry.entry; + +import io.papermc.paper.registry.PaperRegistryBuilder; +import io.papermc.paper.registry.RegistryHolder; +import io.papermc.paper.registry.RegistryKey; +import io.papermc.paper.registry.WritableCraftRegistry; +import io.papermc.paper.registry.data.util.Conversions; +import net.minecraft.core.MappedRegistry; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; +import org.bukkit.Keyed; + +public class AddableRegistryEntry> extends CraftRegistryEntry implements RegistryEntry.Addable { + + private final PaperRegistryBuilder.Filler builderFiller; + + protected AddableRegistryEntry( + final ResourceKey> mcKey, + final RegistryKey apiKey, + final Class classToPreload, + final RegistryTypeMapper minecraftToBukkit, + final PaperRegistryBuilder.Filler builderFiller + ) { + super(mcKey, apiKey, classToPreload, minecraftToBukkit); + this.builderFiller = builderFiller; + } + + private WritableCraftRegistry createRegistry(final Registry registry) { + return new WritableCraftRegistry<>(this, this.classToPreload, (MappedRegistry) registry, this.updater, this.builderFiller.asFactory(), this.minecraftToBukkit); + } + + @Override + public RegistryHolder createRegistryHolder(final Registry nmsRegistry) { + return new RegistryHolder.Memoized<>(() -> this.createRegistry(nmsRegistry)); + } + + @Override + public B fillBuilder(final Conversions conversions, final M nms) { + return this.builderFiller.fill(conversions, nms); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/entry/ModifiableRegistryEntry.java b/paper-server/src/main/java/io/papermc/paper/registry/entry/ModifiableRegistryEntry.java new file mode 100644 index 0000000000..4254335e55 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/entry/ModifiableRegistryEntry.java @@ -0,0 +1,29 @@ +package io.papermc.paper.registry.entry; + +import io.papermc.paper.registry.PaperRegistryBuilder; +import io.papermc.paper.registry.RegistryKey; +import io.papermc.paper.registry.data.util.Conversions; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; +import org.bukkit.Keyed; + +public class ModifiableRegistryEntry> extends CraftRegistryEntry implements RegistryEntry.Modifiable { + + protected final PaperRegistryBuilder.Filler builderFiller; + + protected ModifiableRegistryEntry( + final ResourceKey> mcKey, + final RegistryKey apiKey, + final Class toPreload, + final RegistryTypeMapper minecraftToBukkit, + final PaperRegistryBuilder.Filler builderFiller + ) { + super(mcKey, apiKey, toPreload, minecraftToBukkit); + this.builderFiller = builderFiller; + } + + @Override + public B fillBuilder(final Conversions conversions, final M nms) { + return this.builderFiller.fill(conversions, nms); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntry.java b/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntry.java index f0a81a8b88..32089721ed 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntry.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntry.java @@ -1,13 +1,20 @@ package io.papermc.paper.registry.entry; +import io.papermc.paper.registry.PaperRegistryBuilder; import io.papermc.paper.registry.RegistryHolder; 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.RegistryEntryAddEventImpl; +import io.papermc.paper.registry.event.RegistryFreezeEventImpl; import io.papermc.paper.registry.legacy.DelayedRegistryEntry; import java.util.function.BiFunction; import net.minecraft.core.Registry; import org.bukkit.Keyed; import org.bukkit.NamespacedKey; import org.bukkit.craftbukkit.util.ApiVersion; +import org.jspecify.annotations.Nullable; public interface RegistryEntry extends RegistryEntryInfo { // TODO remove Keyed @@ -26,4 +33,63 @@ public interface RegistryEntry extends RegistryEntryInfo delayed() { return new DelayedRegistryEntry<>(this); } + + interface BuilderHolder> extends RegistryEntryInfo { + + B fillBuilder(Conversions conversions, M nms); + } + + /** + * Can mutate values being added to the registry + */ + interface Modifiable> extends BuilderHolder { + + static boolean isModifiable(final @Nullable RegistryEntryInfo entry) { + return entry instanceof RegistryEntry.Modifiable || (entry instanceof final DelayedRegistryEntry delayed && delayed.delegate() instanceof RegistryEntry.Modifiable); + } + + static > Modifiable asModifiable(final RegistryEntryInfo entry) { // TODO remove Keyed + return (Modifiable) possiblyUnwrap(entry); + } + + default RegistryEntryAddEventImpl createEntryAddEvent(final TypedKey key, final B initialBuilder, final Conversions conversions) { + return new RegistryEntryAddEventImpl<>(key, initialBuilder, this.apiKey(), conversions); + } + } + + /** + * Can only add new values to the registry, not modify any values. + */ + interface Addable> extends BuilderHolder { // TODO remove Keyed + + default RegistryFreezeEventImpl createFreezeEvent(final WritableCraftRegistry writableRegistry, final Conversions conversions) { + return new RegistryFreezeEventImpl<>(this.apiKey(), writableRegistry.createApiWritableRegistry(conversions), conversions); + } + + static boolean isAddable(final @Nullable RegistryEntryInfo entry) { + return entry instanceof RegistryEntry.Addable || (entry instanceof final DelayedRegistryEntry delayed && delayed.delegate() instanceof RegistryEntry.Addable); + } + + static > Addable asAddable(final RegistryEntryInfo entry) { + return (Addable) possiblyUnwrap(entry); + } + } + + /** + * Can mutate values and add new values. + */ + interface Writable> extends Modifiable, Addable { // TODO remove Keyed + + static boolean isWritable(final @Nullable RegistryEntryInfo entry) { + return entry instanceof RegistryEntry.Writable || (entry instanceof final DelayedRegistryEntry delayed && delayed.delegate() instanceof RegistryEntry.Writable); + } + + static > Writable asWritable(final RegistryEntryInfo entry) { // TODO remove Keyed + return (Writable) possiblyUnwrap(entry); + } + } + + private static RegistryEntryInfo possiblyUnwrap(final RegistryEntryInfo entry) { + return entry instanceof final DelayedRegistryEntry delayed ? delayed.delegate() : entry; + } } diff --git a/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntryBuilder.java b/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntryBuilder.java index 6d8f08d611..5352ec936c 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntryBuilder.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntryBuilder.java @@ -1,6 +1,7 @@ package io.papermc.paper.registry.entry; import com.mojang.datafixers.util.Either; +import io.papermc.paper.registry.PaperRegistryBuilder; import io.papermc.paper.registry.RegistryKey; import java.util.function.BiFunction; import java.util.function.Function; @@ -59,5 +60,17 @@ public class RegistryEntryBuilder { // TODO remove Keyed public RegistryEntry build() { return new CraftRegistryEntry<>(this.mcKey, this.apiKey, this.classToPreload, this.minecraftToBukkit); } + + public > RegistryEntry modifiable(final PaperRegistryBuilder.Filler filler) { + return new ModifiableRegistryEntry<>(this.mcKey, this.apiKey, this.classToPreload, this.minecraftToBukkit, filler); + } + + public > RegistryEntry addable(final PaperRegistryBuilder.Filler filler) { + return new AddableRegistryEntry<>(this.mcKey, this.apiKey, this.classToPreload, this.minecraftToBukkit, filler); + } + + public > RegistryEntry writable(final PaperRegistryBuilder.Filler filler) { + return new WritableRegistryEntry<>(this.mcKey, this.apiKey, this.classToPreload, this.minecraftToBukkit, filler); + } } } diff --git a/paper-server/src/main/java/io/papermc/paper/registry/entry/WritableRegistryEntry.java b/paper-server/src/main/java/io/papermc/paper/registry/entry/WritableRegistryEntry.java new file mode 100644 index 0000000000..7ff5dbea3f --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/entry/WritableRegistryEntry.java @@ -0,0 +1,25 @@ +package io.papermc.paper.registry.entry; + +import com.mojang.datafixers.util.Either; +import io.papermc.paper.registry.PaperRegistryBuilder; +import io.papermc.paper.registry.RegistryKey; +import java.util.function.BiFunction; +import java.util.function.Function; +import net.minecraft.core.Holder; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; +import org.bukkit.Keyed; +import org.bukkit.NamespacedKey; + +public class WritableRegistryEntry> extends AddableRegistryEntry implements RegistryEntry.Writable { // TODO remove Keyed + + protected WritableRegistryEntry( + final ResourceKey> mcKey, + final RegistryKey apiKey, + final Class classToPreload, + final RegistryTypeMapper minecraftToBukkit, + final PaperRegistryBuilder.Filler builderFiller + ) { + super(mcKey, apiKey, classToPreload, minecraftToBukkit, builderFiller); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEventImpl.java b/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEventImpl.java new file mode 100644 index 0000000000..332829c65e --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEventImpl.java @@ -0,0 +1,29 @@ +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>( + TypedKey key, + B builder, + RegistryKey registryKey, + Conversions conversions +) implements RegistryEntryAddEvent, PaperLifecycleEvent { + + @Override + public Tag getOrCreateTag(final TagKey tagKey) { + final RegistryOps.RegistryInfo 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); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java b/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java new file mode 100644 index 0000000000..bfcd0884d7 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java @@ -0,0 +1,45 @@ +package io.papermc.paper.registry.event; + +import io.papermc.paper.plugin.bootstrap.BootstrapContext; +import io.papermc.paper.plugin.lifecycle.event.LifecycleEvent; +import io.papermc.paper.plugin.lifecycle.event.LifecycleEventRunner; +import io.papermc.paper.plugin.lifecycle.event.types.AbstractLifecycleEventType; +import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEventType; +import io.papermc.paper.registry.RegistryKey; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.function.BiFunction; + +public final class RegistryEventMap { + + private final Map, LifecycleEventType> eventTypes = new HashMap<>(); + private final String name; + + public RegistryEventMap(final String name) { + this.name = name; + } + + @SuppressWarnings("unchecked") + public > ET getOrCreate(final RegistryKey registryKey, final BiFunction, ? super String, ET> eventTypeCreator) { + final ET eventType; + if (this.eventTypes.containsKey(registryKey)) { + eventType = (ET) this.eventTypes.get(registryKey); + } else { + eventType = eventTypeCreator.apply(registryKey, this.name); + this.eventTypes.put(registryKey, eventType); + } + return eventType; + } + + @SuppressWarnings("unchecked") + public LifecycleEventType getEventType(final RegistryKey registryKey) { + return (LifecycleEventType) Objects.requireNonNull(this.eventTypes.get(registryKey), "No hook for " + registryKey); + } + + public boolean hasHandlers(final RegistryKey registryKey) { + final AbstractLifecycleEventType type = ((AbstractLifecycleEventType) this.eventTypes.get(registryKey)); + return type != null && type.hasHandlers(); + } + +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventTypeProviderImpl.java b/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventTypeProviderImpl.java new file mode 100644 index 0000000000..34c842ffa3 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventTypeProviderImpl.java @@ -0,0 +1,24 @@ +package io.papermc.paper.registry.event; + +import io.papermc.paper.plugin.bootstrap.BootstrapContext; +import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEventType; +import io.papermc.paper.registry.PaperRegistryListenerManager; +import io.papermc.paper.registry.RegistryBuilder; +import io.papermc.paper.registry.event.type.RegistryEntryAddEventType; + +public class RegistryEventTypeProviderImpl implements RegistryEventTypeProvider { + + public static RegistryEventTypeProviderImpl instance() { + return (RegistryEventTypeProviderImpl) RegistryEventTypeProvider.provider(); + } + + @Override + public > RegistryEntryAddEventType registryEntryAdd(final RegistryEventProvider type) { + return PaperRegistryListenerManager.INSTANCE.getRegistryValueAddEventType(type); + } + + @Override + public > LifecycleEventType.Prioritizable> registryFreeze(final RegistryEventProvider type) { + return PaperRegistryListenerManager.INSTANCE.getRegistryFreezeEventType(type); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEventImpl.java b/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEventImpl.java new file mode 100644 index 0000000000..1b45802f9a --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEventImpl.java @@ -0,0 +1,27 @@ +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 RegistryFreezeEventImpl>( + RegistryKey registryKey, + WritableRegistry registry, + Conversions conversions +) implements RegistryFreezeEvent, PaperLifecycleEvent { + + @Override + public Tag getOrCreateTag(final TagKey tagKey) { + final RegistryOps.RegistryInfo 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); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/event/package-info.java b/paper-server/src/main/java/io/papermc/paper/registry/event/package-info.java new file mode 100644 index 0000000000..1d20e0d940 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/event/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package io.papermc.paper.registry.event; + +import org.jspecify.annotations.NullMarked; diff --git a/paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddEventTypeImpl.java b/paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddEventTypeImpl.java new file mode 100644 index 0000000000..fbf853bf1c --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddEventTypeImpl.java @@ -0,0 +1,37 @@ +package io.papermc.paper.registry.event.type; + +import io.papermc.paper.plugin.bootstrap.BootstrapContext; +import io.papermc.paper.plugin.lifecycle.event.handler.LifecycleEventHandler; +import io.papermc.paper.plugin.lifecycle.event.types.PrioritizableLifecycleEventType; +import io.papermc.paper.registry.RegistryBuilder; +import io.papermc.paper.registry.RegistryKey; +import io.papermc.paper.registry.event.RegistryEntryAddEvent; +import java.util.function.Consumer; +import java.util.function.Predicate; + +public class RegistryEntryAddEventTypeImpl> extends PrioritizableLifecycleEventType, RegistryEntryAddConfiguration> implements RegistryEntryAddEventType { + + public RegistryEntryAddEventTypeImpl(final RegistryKey registryKey, final String eventName) { + super(registryKey + " / " + eventName, BootstrapContext.class); + } + + @Override + public boolean blocksReloading(final BootstrapContext eventOwner) { + return false; // only runs once + } + + @Override + public RegistryEntryAddConfiguration newHandler(final LifecycleEventHandler> handler) { + return new RegistryEntryAddHandlerConfiguration<>(handler, this); + } + + @Override + public void forEachHandler(final RegistryEntryAddEvent event, final Consumer>> consumer, final Predicate>> predicate) { + super.forEachHandler(event, consumer, predicate.and(handler -> this.matchesTarget(event, handler))); + } + + private boolean matchesTarget(final RegistryEntryAddEvent event, final RegisteredHandler> handler) { + final RegistryEntryAddHandlerConfiguration config = (RegistryEntryAddHandlerConfiguration) handler.config(); + return config.filter() == null || config.filter().test(event.key()); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddHandlerConfiguration.java b/paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddHandlerConfiguration.java new file mode 100644 index 0000000000..3cc0f7688f --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddHandlerConfiguration.java @@ -0,0 +1,42 @@ +package io.papermc.paper.registry.event.type; + +import io.papermc.paper.plugin.bootstrap.BootstrapContext; +import io.papermc.paper.plugin.lifecycle.event.handler.LifecycleEventHandler; +import io.papermc.paper.plugin.lifecycle.event.handler.configuration.PrioritizedLifecycleEventHandlerConfigurationImpl; +import io.papermc.paper.plugin.lifecycle.event.types.AbstractLifecycleEventType; +import io.papermc.paper.registry.RegistryBuilder; +import io.papermc.paper.registry.TypedKey; +import io.papermc.paper.registry.event.RegistryEntryAddEvent; +import java.util.function.Predicate; +import org.jetbrains.annotations.Contract; +import org.jspecify.annotations.Nullable; + +public class RegistryEntryAddHandlerConfiguration> extends PrioritizedLifecycleEventHandlerConfigurationImpl> implements RegistryEntryAddConfiguration { + + private @Nullable Predicate> filter; + + public RegistryEntryAddHandlerConfiguration(final LifecycleEventHandler> handler, final AbstractLifecycleEventType, ?> eventType) { + super(handler, eventType); + } + + @Contract(pure = true) + public @Nullable Predicate> filter() { + return this.filter; + } + + @Override + public RegistryEntryAddConfiguration filter(final Predicate> filter) { + this.filter = filter; + return this; + } + + @Override + public RegistryEntryAddConfiguration priority(final int priority) { + return (RegistryEntryAddConfiguration) super.priority(priority); + } + + @Override + public RegistryEntryAddConfiguration monitor() { + return (RegistryEntryAddConfiguration) super.monitor(); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryLifecycleEventType.java b/paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryLifecycleEventType.java new file mode 100644 index 0000000000..7ee7702219 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/event/type/RegistryLifecycleEventType.java @@ -0,0 +1,18 @@ +package io.papermc.paper.registry.event.type; + +import io.papermc.paper.plugin.bootstrap.BootstrapContext; +import io.papermc.paper.plugin.lifecycle.event.types.PrioritizableLifecycleEventType; +import io.papermc.paper.registry.RegistryKey; +import io.papermc.paper.registry.event.RegistryEvent; + +public final class RegistryLifecycleEventType> extends PrioritizableLifecycleEventType.Simple { + + public RegistryLifecycleEventType(final RegistryKey registryKey, final String eventName) { + super(registryKey + " / " + eventName, BootstrapContext.class); + } + + @Override + public boolean blocksReloading(final BootstrapContext eventOwner) { + return false; // only runs once + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/event/type/package-info.java b/paper-server/src/main/java/io/papermc/paper/registry/event/type/package-info.java new file mode 100644 index 0000000000..f9f63926a5 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/event/type/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package io.papermc.paper.registry.event.type; + +import org.jspecify.annotations.NullMarked; diff --git a/paper-server/src/main/java/io/papermc/paper/registry/legacy/DelayedRegistry.java b/paper-server/src/main/java/io/papermc/paper/registry/legacy/DelayedRegistry.java index ca829b162d..fdc475f2b1 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/legacy/DelayedRegistry.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/legacy/DelayedRegistry.java @@ -1,11 +1,14 @@ package io.papermc.paper.registry.legacy; +import io.papermc.paper.registry.tag.Tag; +import io.papermc.paper.registry.tag.TagKey; import java.util.Iterator; import java.util.function.Supplier; import java.util.stream.Stream; import org.bukkit.Keyed; import org.bukkit.NamespacedKey; import org.bukkit.Registry; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; /** @@ -54,4 +57,14 @@ public final class DelayedRegistry> imple public @Nullable NamespacedKey getKey(final T value) { return this.delegate().getKey(value); } + + @Override + public boolean hasTag(final TagKey key) { + return this.delegate().hasTag(key); + } + + @Override + public @NonNull Tag getTag(final TagKey key) { + return this.delegate().getTag(key); + } } diff --git a/paper-server/src/main/java/io/papermc/paper/registry/set/NamedRegistryKeySetImpl.java b/paper-server/src/main/java/io/papermc/paper/registry/set/NamedRegistryKeySetImpl.java new file mode 100644 index 0000000000..7b15640c2f --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/set/NamedRegistryKeySetImpl.java @@ -0,0 +1,74 @@ +package io.papermc.paper.registry.set; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +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.tag.Tag; +import io.papermc.paper.registry.tag.TagKey; +import java.util.Collection; +import java.util.Set; +import net.kyori.adventure.key.Key; +import net.minecraft.core.Holder; +import net.minecraft.core.HolderSet; +import org.bukkit.Keyed; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; +import org.bukkit.craftbukkit.util.CraftNamespacedKey; +import org.jetbrains.annotations.Unmodifiable; +import org.jspecify.annotations.NullMarked; + +@NullMarked +public record NamedRegistryKeySetImpl( // TODO remove Keyed + TagKey tagKey, + HolderSet.Named namedSet +) implements Tag, org.bukkit.Tag { + + @Override + public @Unmodifiable Collection> values() { + final ImmutableList.Builder> builder = ImmutableList.builder(); + for (final Holder holder : this.namedSet) { + builder.add(TypedKey.create(this.tagKey.registryKey(), CraftNamespacedKey.fromMinecraft(((Holder.Reference) holder).key().location()))); + } + return builder.build(); + } + + @Override + public RegistryKey registryKey() { + return this.tagKey.registryKey(); + } + + @Override + public boolean contains(final TypedKey valueKey) { + return Iterables.any(this.namedSet, h -> { + return PaperRegistries.fromNms(((Holder.Reference) h).key()).equals(valueKey); + }); + } + + @Override + public @Unmodifiable Collection resolve(final Registry registry) { + final ImmutableList.Builder builder = ImmutableList.builder(); + for (final Holder holder : this.namedSet) { + builder.add(registry.getOrThrow(CraftNamespacedKey.fromMinecraft(((Holder.Reference) holder).key().location()))); + } + return builder.build(); + } + + @Override + public boolean isTagged(final T item) { + return this.getValues().contains(item); + } + + @Override + public Set getValues() { + return Set.copyOf(this.resolve(RegistryAccess.registryAccess().getRegistry(this.registryKey()))); + } + + @Override + public NamespacedKey getKey() { + final Key key = this.tagKey().key(); + return new NamespacedKey(key.namespace(), key.value()); + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/set/PaperRegistrySets.java b/paper-server/src/main/java/io/papermc/paper/registry/set/PaperRegistrySets.java new file mode 100644 index 0000000000..cb92f53cf9 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/set/PaperRegistrySets.java @@ -0,0 +1,45 @@ +package io.papermc.paper.registry.set; + +import io.papermc.paper.registry.PaperRegistries; +import io.papermc.paper.registry.RegistryKey; +import io.papermc.paper.registry.TypedKey; +import java.util.ArrayList; +import java.util.List; +import net.minecraft.core.Holder; +import net.minecraft.core.HolderSet; +import net.minecraft.core.Registry; +import net.minecraft.resources.RegistryOps; +import net.minecraft.resources.ResourceKey; +import org.bukkit.Keyed; + +public final class PaperRegistrySets { + + public static HolderSet convertToNms(final ResourceKey> resourceKey, final RegistryOps.RegistryInfoLookup lookup, final RegistryKeySet registryKeySet) { // TODO remove Keyed + if (registryKeySet instanceof NamedRegistryKeySetImpl) { + return ((NamedRegistryKeySetImpl) registryKeySet).namedSet(); + } else { + final RegistryOps.RegistryInfo registryInfo = lookup.lookup(resourceKey).orElseThrow(); + return HolderSet.direct(key -> { + return registryInfo.getter().getOrThrow(PaperRegistries.toNms(key)); + }, registryKeySet.values()); + } + } + + public static RegistryKeySet convertToApi(final RegistryKey registryKey, final HolderSet holders) { // TODO remove Keyed + if (holders instanceof final HolderSet.Named named) { + return new NamedRegistryKeySetImpl<>(PaperRegistries.fromNms(named.key()), named); + } else { + final List> keys = new ArrayList<>(); + for (final Holder holder : holders) { + if (!(holder instanceof final Holder.Reference reference)) { + throw new UnsupportedOperationException("Cannot convert a holder set containing direct holders"); + } + keys.add(PaperRegistries.fromNms(reference.key())); + } + return RegistrySet.keySet(registryKey, keys); + } + } + + private PaperRegistrySets() { + } +} diff --git a/paper-server/src/main/java/io/papermc/paper/registry/set/package-info.java b/paper-server/src/main/java/io/papermc/paper/registry/set/package-info.java new file mode 100644 index 0000000000..516b072428 --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/set/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package io.papermc.paper.registry.set; + +import org.jspecify.annotations.NullMarked; diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java index 624f627ead..a7e745bc13 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java @@ -301,4 +301,17 @@ public class CraftRegistry implements Registry { return this.byValue.get(value); } // Paper end - improve Registry + + // Paper start - RegistrySet API + @Override + public boolean hasTag(final io.papermc.paper.registry.tag.TagKey key) { + return this.minecraftRegistry.get(net.minecraft.tags.TagKey.create(this.minecraftRegistry.key(), io.papermc.paper.adventure.PaperAdventure.asVanilla(key.key()))).isPresent(); + } + + @Override + public io.papermc.paper.registry.tag.Tag getTag(final io.papermc.paper.registry.tag.TagKey key) { + final net.minecraft.core.HolderSet.Named namedHolderSet = this.minecraftRegistry.get(io.papermc.paper.registry.PaperRegistries.toNms(key)).orElseThrow(); + return new io.papermc.paper.registry.set.NamedRegistryKeySetImpl<>(key, namedHolderSet); + } + // Paper end - RegistrySet API } diff --git a/paper-server/src/main/resources/META-INF/services/io.papermc.paper.registry.event.RegistryEventTypeProvider b/paper-server/src/main/resources/META-INF/services/io.papermc.paper.registry.event.RegistryEventTypeProvider new file mode 100644 index 0000000000..8bee1a5ed8 --- /dev/null +++ b/paper-server/src/main/resources/META-INF/services/io.papermc.paper.registry.event.RegistryEventTypeProvider @@ -0,0 +1 @@ +io.papermc.paper.registry.event.RegistryEventTypeProviderImpl diff --git a/paper-server/src/test/java/io/papermc/paper/registry/RegistryBuilderTest.java b/paper-server/src/test/java/io/papermc/paper/registry/RegistryBuilderTest.java new file mode 100644 index 0000000000..55f5fc45cf --- /dev/null +++ b/paper-server/src/test/java/io/papermc/paper/registry/RegistryBuilderTest.java @@ -0,0 +1,44 @@ +package io.papermc.paper.registry; + +import io.papermc.paper.registry.data.util.Conversions; +import io.papermc.paper.registry.entry.RegistryEntry; +import io.papermc.paper.registry.entry.RegistryEntryInfo; +import io.papermc.paper.registry.legacy.DelayedRegistryEntry; +import java.util.Map; +import java.util.stream.Stream; +import net.minecraft.core.Registry; +import net.minecraft.resources.RegistryOps; +import net.minecraft.resources.ResourceKey; +import org.bukkit.Keyed; +import org.bukkit.support.RegistryHelper; +import org.bukkit.support.environment.AllFeatures; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@AllFeatures +class RegistryBuilderTest { + + static Stream registries() { + return PaperRegistries.REGISTRY_ENTRIES.stream() + .map(RegistryBuilderTest::possiblyUnwrap) + .filter(RegistryEntry.BuilderHolder.class::isInstance) + .map(Arguments::arguments); + } + + private static RegistryEntryInfo possiblyUnwrap(final RegistryEntryInfo entry) { + return entry instanceof final DelayedRegistryEntry delayed ? delayed.delegate() : entry; + } + + @ParameterizedTest + @MethodSource("registries") + void testEquality(final RegistryEntry.BuilderHolder registryEntry) { + final Registry registry = RegistryHelper.getRegistry().lookupOrThrow(registryEntry.mcKey()); + for (final Map.Entry, M> entry : registry.entrySet()) { + final M built = registryEntry.fillBuilder(new Conversions(new RegistryOps.HolderLookupAdapter(RegistryHelper.getRegistry())), entry.getValue()).build(); + assertEquals(entry.getValue(), built); + } + } +} diff --git a/paper-server/src/test/java/org/bukkit/registry/RegistryClassTest.java b/paper-server/src/test/java/org/bukkit/registry/RegistryClassTest.java index ea3d37f387..c118c91197 100644 --- a/paper-server/src/test/java/org/bukkit/registry/RegistryClassTest.java +++ b/paper-server/src/test/java/org/bukkit/registry/RegistryClassTest.java @@ -111,7 +111,7 @@ public class RegistryClassTest { outsideRequest.clear(); MockUtil.resetMock(spyRegistry); doAnswer(invocation -> { - Keyed item = realRegistry.get(invocation.getArgument(0)); + Keyed item = realRegistry.get((NamespacedKey) invocation.getArgument(0)); // Paper - registry modification api - specifically call namespaced key overload if (item == null) { nullValue.add(invocation.getArgument(0)); @@ -120,10 +120,10 @@ public class RegistryClassTest { nullAble.add(invocation.getArgument(0)); return item; - }).when(spyRegistry).get(any()); + }).when(spyRegistry).get((NamespacedKey) any()); // Paper - registry modification api - specifically call namespaced key overload doAnswer(invocation -> { - Keyed item = realRegistry.get(invocation.getArgument(0)); + Keyed item = realRegistry.get((NamespacedKey) invocation.getArgument(0)); // Paper - registry modification api - specifically call namespaced key overload if (item == null) { nullValue.add(invocation.getArgument(0)); @@ -138,7 +138,7 @@ public class RegistryClassTest { notNullAble.add(invocation.getArgument(0)); return item; - }).when(spyRegistry).getOrThrow(any()); + }).when(spyRegistry).getOrThrow((NamespacedKey) any()); // Paper - registry modification api - specifically call namespaced key overload // Load class try { @@ -171,13 +171,13 @@ public class RegistryClassTest { outsideRequest .computeIfAbsent(type, ty -> new ArrayList<>()).add(invocation.getArgument(0)); return mock(type); - }).when(spyRegistry).get(any()); + }).when(spyRegistry).get((NamespacedKey) any()); // Paper - registry modification api - specifically call namespaced key overload doAnswer(invocation -> { outsideRequest .computeIfAbsent(type, ty -> new ArrayList<>()).add(invocation.getArgument(0)); return mock(type); - }).when(spyRegistry).getOrThrow(any()); + }).when(spyRegistry).getOrThrow((NamespacedKey) any()); // Paper - registry modification api - specifically call namespaced key overload } private static void initFieldDataCache() { diff --git a/paper-server/src/test/java/org/bukkit/support/extension/NormalExtension.java b/paper-server/src/test/java/org/bukkit/support/extension/NormalExtension.java index 8b5dcc4d0e..a809ea2f0d 100644 --- a/paper-server/src/test/java/org/bukkit/support/extension/NormalExtension.java +++ b/paper-server/src/test/java/org/bukkit/support/extension/NormalExtension.java @@ -62,7 +62,7 @@ public class NormalExtension extends BaseExtension { doAnswer(invocation -> mocks.computeIfAbsent(invocation.getArgument(0), k -> mock(RegistryHelper.updateClass(keyed, invocation.getArgument(0)), withSettings().stubOnly().defaultAnswer(DEFAULT_ANSWER))) - ).when(registry).get(any()); // Allow static classes to fill there fields, so that it does not error out, just by loading them + ).when(registry).get((NamespacedKey) any()); // Allow static classes to fill there fields, so that it does not error out, just by loading them // Paper - registry modification api - specifically call namespaced key overload return registry; }