2024-06-16 21:39:59 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Mon, 27 Feb 2023 18:28:39 -0800
Subject: [PATCH] Registry Modification API
2024-11-09 21:44:55 +01:00
Feature patch
2024-06-16 21:39:59 +02:00
== AT ==
public net.minecraft.core.MappedRegistry validateWrite(Lnet/minecraft/resources/ResourceKey;)V
public net.minecraft.resources.RegistryOps lookupProvider
public net.minecraft.resources.RegistryOps$HolderLookupAdapter
diff --git a/src/main/java/io/papermc/paper/registry/PaperRegistries.java b/src/main/java/io/papermc/paper/registry/PaperRegistries.java
2024-11-25 04:26:32 +01:00
index cd8a6a4c2a63029f8f859765088c227bbd456813..86c9f87cdb41c0d1ccc2a61b501f969cfaae47bc 100644
2024-06-16 21:39:59 +02:00
--- a/src/main/java/io/papermc/paper/registry/PaperRegistries.java
+++ b/src/main/java/io/papermc/paper/registry/PaperRegistries.java
2024-11-23 20:26:51 +01:00
@@ -3,6 +3,7 @@ package io.papermc.paper.registry;
import com.google.common.base.Preconditions;
2024-06-16 21:39:59 +02:00
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;
2024-11-25 04:26:32 +01:00
@@ -67,6 +68,7 @@ import org.jspecify.annotations.Nullable;
2024-06-16 21:39:59 +02:00
import static io.papermc.paper.registry.entry.RegistryEntry.apiOnly;
import static io.papermc.paper.registry.entry.RegistryEntry.entry;
+import static io.papermc.paper.registry.entry.RegistryEntry.writable;
public final class PaperRegistries {
2024-11-25 04:26:32 +01:00
@@ -149,6 +151,15 @@ public final class PaperRegistries {
2024-06-16 21:39:59 +02:00
return ResourceKey.create((ResourceKey<? extends Registry<M>>) PaperRegistries.registryToNms(typedKey.registryKey()), PaperAdventure.asVanilla(typedKey.key()));
}
+ public static <M, T> TagKey<T> fromNms(final net.minecraft.tags.TagKey<M> tagKey) {
+ return TagKey.create(registryFromNms(tagKey.registry()), CraftNamespacedKey.fromMinecraft(tagKey.location()));
+ }
+
+ @SuppressWarnings({"unchecked", "RedundantCast"})
+ public static <M, T> net.minecraft.tags.TagKey<M> toNms(final TagKey<T> tagKey) {
+ return net.minecraft.tags.TagKey.create((ResourceKey<? extends Registry<M>>) registryToNms(tagKey.registryKey()), PaperAdventure.asVanilla(tagKey.key()));
+ }
+
private PaperRegistries() {
}
}
diff --git a/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java b/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java
2024-11-25 04:26:32 +01:00
index 4bf7915867dbe762ef0b070d67d5f7b7d1ee4f03..ed071ed34e16812f133102b0d66a5201a94639f2 100644
2024-06-16 21:39:59 +02:00
--- a/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java
+++ b/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java
2024-11-25 04:26:32 +01:00
@@ -78,6 +78,14 @@ public class PaperRegistryAccess implements RegistryAccess {
2024-06-16 21:39:59 +02:00
return possiblyUnwrap(registryHolder.get());
}
+ public <M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> WritableCraftRegistry<M, T, B> getWritableRegistry(final RegistryKey<T> key) {
+ final Registry<T> registry = this.getRegistry(key);
+ if (registry instanceof WritableCraftRegistry<?, T, ?>) {
+ return (WritableCraftRegistry<M, T, B>) registry;
+ }
+ throw new IllegalArgumentException(key + " does not point to a writable registry");
+ }
+
private static <T extends Keyed> Registry<T> possiblyUnwrap(final Registry<T> registry) {
if (registry instanceof final DelayedRegistry<T, ?> delayedRegistry) { // if not coming from legacy, unwrap the delayed registry
return delayedRegistry.delegate();
diff --git a/src/main/java/io/papermc/paper/registry/PaperRegistryBuilder.java b/src/main/java/io/papermc/paper/registry/PaperRegistryBuilder.java
new file mode 100644
2024-11-25 04:26:32 +01:00
index 0000000000000000000000000000000000000000..69e63689083207de2f409557d4a9a17f8ad7ae3e
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/PaperRegistryBuilder.java
2024-11-25 04:26:32 +01:00
@@ -0,0 +1,25 @@
2024-06-16 21:39:59 +02:00
+package io.papermc.paper.registry;
+
+import io.papermc.paper.registry.data.util.Conversions;
2024-11-25 04:26:32 +01:00
+import org.jspecify.annotations.Nullable;
2024-06-16 21:39:59 +02:00
+
+public interface PaperRegistryBuilder<M, T> extends RegistryBuilder<T> {
+
+ M build();
+
+ @FunctionalInterface
+ interface Filler<M, T, B extends PaperRegistryBuilder<M, T>> {
+
2024-06-17 00:00:12 +02:00
+ B fill(Conversions conversions, TypedKey<T> key, @Nullable M nms);
2024-06-16 21:39:59 +02:00
+
+ default Factory<M, T, B> asFactory() {
+ return (lookup, key) -> this.fill(lookup, key, null);
+ }
+ }
+
+ @FunctionalInterface
+ interface Factory<M, T, B extends PaperRegistryBuilder<M, T>> {
+
2024-06-17 00:00:12 +02:00
+ B create(Conversions conversions, TypedKey<T> key);
2024-06-16 21:39:59 +02:00
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/PaperRegistryListenerManager.java b/src/main/java/io/papermc/paper/registry/PaperRegistryListenerManager.java
new file mode 100644
2024-11-25 04:26:32 +01:00
index 0000000000000000000000000000000000000000..60d9b806a09bb103e36ce1f932a5a34ef408770a
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/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;
2024-11-25 04:26:32 +01:00
+import org.jspecify.annotations.Nullable;
2024-06-16 21:39:59 +02:00
+
2024-09-08 20:56:09 +02:00
+public class PaperRegistryListenerManager {
2024-06-16 21:39:59 +02:00
+
+ public static final PaperRegistryListenerManager INSTANCE = new PaperRegistryListenerManager();
+
2024-09-08 20:56:09 +02:00
+ public final RegistryEventMap valueAddEventTypes = new RegistryEventMap("value add");
+ public final RegistryEventMap freezeEventTypes = new RegistryEventMap("freeze");
2024-06-16 21:39:59 +02:00
+
+ private PaperRegistryListenerManager() {
+ }
+
+ /**
+ * For {@link Registry#register(Registry, String, Object)}
+ */
+ public <M> M registerWithListeners(final Registry<M> registry, final String id, final M nms) {
+ return this.registerWithListeners(registry, ResourceLocation.withDefaultNamespace(id), nms);
+ }
+
+ /**
+ * For {@link Registry#register(Registry, ResourceLocation, Object)}
+ */
+ public <M> M registerWithListeners(final Registry<M> 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> M registerWithListeners(final Registry<M> registry, final ResourceKey<M> 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 <M> Holder.Reference<M> registerForHolderWithListeners(final Registry<M> 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 <M> Holder.Reference<M> registerForHolderWithListeners(final Registry<M> registry, final ResourceKey<M> key, final M nms) {
+ return this.registerWithListeners(registry, key, nms, RegistrationInfo.BUILT_IN, WritableRegistry::register, BuiltInRegistries.BUILT_IN_CONVERSIONS);
+ }
+
+ public <M> void registerWithListeners(
+ final Registry<M> registry,
+ final ResourceKey<M> key,
+ final M nms,
+ final RegistrationInfo registrationInfo,
+ final Conversions conversions
+ ) {
+ this.registerWithListeners(registry, key, nms, registrationInfo, WritableRegistry::register, conversions);
+ }
+
+ // TODO remove Keyed
+ public <M, T extends org.bukkit.Keyed, B extends PaperRegistryBuilder<M, T>, R> R registerWithListeners(
+ final Registry<M> registry,
+ final ResourceKey<M> key,
+ final M nms,
+ final RegistrationInfo registrationInfo,
+ final RegisterMethod<M, R> 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
2024-11-25 04:26:32 +01:00
+ final RegistryEntryInfo<M, T> entry = PaperRegistries.getEntry(registry.key());
2024-09-08 20:56:09 +02:00
+ if (!RegistryEntry.Modifiable.isModifiable(entry) || !this.valueAddEventTypes.hasHandlers(entry.apiKey())) {
2024-06-16 21:39:59 +02:00
+ return registerMethod.register((WritableRegistry<M>) registry, key, nms, registrationInfo);
+ }
+ final RegistryEntry.Modifiable<M, T, B> modifiableEntry = RegistryEntry.Modifiable.asModifiable(entry);
+ @SuppressWarnings("PatternValidation") final TypedKey<T> typedKey = TypedKey.create(entry.apiKey(), Key.key(key.location().getNamespace(), key.location().getPath()));
+ final B builder = modifiableEntry.fillBuilder(conversions, typedKey, nms);
+ return this.registerWithListeners(registry, modifiableEntry, key, nms, builder, registrationInfo, registerMethod, conversions);
+ }
+
+ <M, T extends org.bukkit.Keyed, B extends PaperRegistryBuilder<M, T>> void registerWithListeners( // TODO remove Keyed
+ final WritableRegistry<M> registry,
+ final RegistryEntryInfo<M, T> entry,
+ final ResourceKey<M> key,
+ final B builder,
+ final RegistrationInfo registrationInfo,
+ final Conversions conversions
+ ) {
2024-09-08 20:56:09 +02:00
+ if (!RegistryEntry.Modifiable.isModifiable(entry) || !this.valueAddEventTypes.hasHandlers(entry.apiKey())) {
2024-06-16 21:39:59 +02:00
+ registry.register(key, builder.build(), registrationInfo);
+ return;
+ }
+ this.registerWithListeners(registry, RegistryEntry.Modifiable.asModifiable(entry), key, null, builder, registrationInfo, WritableRegistry::register, conversions);
+ }
+
+ public <M, T extends org.bukkit.Keyed, B extends PaperRegistryBuilder<M, T>, R> R registerWithListeners( // TODO remove Keyed
+ final Registry<M> registry,
+ final RegistryEntry.Modifiable<M, T, B> entry,
+ final ResourceKey<M> key,
+ final @Nullable M oldNms,
+ final B builder,
+ RegistrationInfo registrationInfo,
+ final RegisterMethod<M, R> registerMethod,
+ final Conversions conversions
+ ) {
+ @Subst("namespace:key") final ResourceLocation beingAdded = key.location();
+ @SuppressWarnings("PatternValidation") final TypedKey<T> typedKey = TypedKey.create(entry.apiKey(), Key.key(beingAdded.getNamespace(), beingAdded.getPath()));
+ final RegistryEntryAddEventImpl<T, B> event = entry.createEntryAddEvent(typedKey, builder, conversions);
2024-09-08 20:56:09 +02:00
+ LifecycleEventRunner.INSTANCE.callEvent(this.valueAddEventTypes.getEventType(entry.apiKey()), event);
2024-06-16 21:39:59 +02:00
+ if (oldNms != null) {
+ ((MappedRegistry<M>) 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<M>) registry, key, newNms, registrationInfo);
+ }
+
+ private static <M> M registerWithInstance(final WritableRegistry<M> writableRegistry, final ResourceKey<M> key, final M value, final RegistrationInfo registrationInfo) {
+ writableRegistry.register(key, value, registrationInfo);
+ return value;
+ }
+
+ @FunctionalInterface
+ public interface RegisterMethod<M, R> {
+
+ R register(WritableRegistry<M> writableRegistry, ResourceKey<M> key, M value, RegistrationInfo registrationInfo);
+ }
+
+ public <M, T extends org.bukkit.Keyed, B extends PaperRegistryBuilder<M, T>> void runFreezeListeners(final ResourceKey<? extends Registry<M>> resourceKey, final Conversions conversions) {
2024-11-25 04:26:32 +01:00
+ final RegistryEntryInfo<M, T> entry = PaperRegistries.getEntry(resourceKey);
2024-09-08 20:56:09 +02:00
+ if (!RegistryEntry.Addable.isAddable(entry) || !this.freezeEventTypes.hasHandlers(entry.apiKey())) {
2024-06-16 21:39:59 +02:00
+ return;
+ }
+ final RegistryEntry.Addable<M, T, B> writableEntry = RegistryEntry.Addable.asAddable(entry);
+ final WritableCraftRegistry<M, T, B> writableRegistry = PaperRegistryAccess.instance().getWritableRegistry(entry.apiKey());
+ final RegistryFreezeEventImpl<T, B> event = writableEntry.createFreezeEvent(writableRegistry, conversions);
2024-09-08 20:56:09 +02:00
+ LifecycleEventRunner.INSTANCE.callEvent(this.freezeEventTypes.getEventType(entry.apiKey()), event);
2024-06-16 21:39:59 +02:00
+ }
+
+ public <T, B extends RegistryBuilder<T>> RegistryEntryAddEventType<T, B> getRegistryValueAddEventType(final RegistryEventProvider<T, B> type) {
+ if (!RegistryEntry.Modifiable.isModifiable(PaperRegistries.getEntry(type.registryKey()))) {
+ throw new IllegalArgumentException(type.registryKey() + " does not support RegistryEntryAddEvent");
+ }
2024-09-08 20:56:09 +02:00
+ return this.valueAddEventTypes.getOrCreate(type.registryKey(), RegistryEntryAddEventTypeImpl::new);
2024-06-16 21:39:59 +02:00
+ }
+
+ public <T, B extends RegistryBuilder<T>> LifecycleEventType.Prioritizable<BootstrapContext, RegistryFreezeEvent<T, B>> getRegistryFreezeEventType(final RegistryEventProvider<T, B> type) {
+ if (!RegistryEntry.Addable.isAddable(PaperRegistries.getEntry(type.registryKey()))) {
+ throw new IllegalArgumentException(type.registryKey() + " does not support RegistryFreezeEvent");
+ }
2024-09-08 20:56:09 +02:00
+ return this.freezeEventTypes.getOrCreate(type.registryKey(), RegistryLifecycleEventType::new);
2024-06-16 21:39:59 +02:00
+ }
+}
2024-11-23 20:26:51 +01:00
diff --git a/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java b/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java
index 6d134ace042758da722960cbcb48e52508dafd61..cc39bc68d29055ef6429f08f975412bd9fe68dbc 100644
--- a/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java
+++ b/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<T extends Enum<T> & Keyed, M> extends Registry.
super(type, predicate);
this.nmsRegistry = nmsRegistry;
}
+
+ @Override
+ public boolean hasTag(final TagKey<T> key) {
+ final net.minecraft.tags.TagKey<M> nmsKey = PaperRegistries.toNms(key);
+ return this.nmsRegistry.get(nmsKey).isPresent();
+ }
+
+ @Override
+ public Tag<T> getTag(final TagKey<T> key) {
+ final HolderSet.Named<M> namedHolderSet = this.nmsRegistry.get(PaperRegistries.toNms(key)).orElseThrow();
+ return new NamedRegistryKeySetImpl<>(key, namedHolderSet);
+ }
}
2024-06-16 21:39:59 +02:00
diff --git a/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java b/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java
new file mode 100644
2024-11-25 04:26:32 +01:00
index 0000000000000000000000000000000000000000..ef91352d442d9ddd666af6bde9a186ce15e5aa04
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java
@@ -0,0 +1,92 @@
+package io.papermc.paper.registry;
+
+import com.mojang.serialization.Lifecycle;
+import io.papermc.paper.adventure.PaperAdventure;
+import io.papermc.paper.registry.data.util.Conversions;
+import io.papermc.paper.registry.entry.RegistryEntry;
+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;
2024-11-25 04:26:32 +01:00
+import org.jspecify.annotations.Nullable;
2024-06-16 21:39:59 +02:00
+
+public class WritableCraftRegistry<M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> extends CraftRegistry<T, M> {
+
+ private static final RegistrationInfo FROM_PLUGIN = new RegistrationInfo(Optional.empty(), Lifecycle.experimental());
+
+ private final RegistryEntry.BuilderHolder<M, T, B> entry;
+ private final MappedRegistry<M> registry;
+ private final PaperRegistryBuilder.Factory<M, T, ? extends B> builderFactory;
+ private final BiFunction<? super NamespacedKey, M, T> minecraftToBukkit;
+
+ public WritableCraftRegistry(
+ final RegistryEntry.BuilderHolder<M, T, B> entry,
+ final Class<?> classToPreload,
+ final MappedRegistry<M> registry,
+ final BiFunction<NamespacedKey, ApiVersion, NamespacedKey> serializationUpdater,
+ final PaperRegistryBuilder.Factory<M, T, ? extends B> builderFactory,
+ final BiFunction<? super NamespacedKey, M, T> minecraftToBukkit
+ ) {
+ super(classToPreload, registry, null, serializationUpdater);
+ this.entry = entry;
+ this.registry = registry;
+ this.builderFactory = builderFactory;
+ this.minecraftToBukkit = minecraftToBukkit;
+ }
+
+ public void register(final TypedKey<T> key, final Consumer<? super B> value, final Conversions conversions) {
+ final ResourceKey<M> resourceKey = ResourceKey.create(this.registry.key(), PaperAdventure.asVanilla(key.key()));
+ this.registry.validateWrite(resourceKey);
+ final B builder = this.newBuilder(conversions, key);
+ value.accept(builder);
+ PaperRegistryListenerManager.INSTANCE.registerWithListeners(
+ this.registry,
+ RegistryEntry.Modifiable.asModifiable(this.entry),
+ resourceKey,
+ builder,
+ FROM_PLUGIN,
+ conversions
+ );
+ }
+
+ @Override
+ public final @Nullable T createBukkit(final NamespacedKey namespacedKey, final @Nullable M minecraft) {
+ if (minecraft == null) {
+ return null;
+ }
+ return this.minecraftToBukkit(namespacedKey, minecraft);
+ }
+
+ public WritableRegistry<T, B> createApiWritableRegistry(final Conversions conversions) {
+ return new ApiWritableRegistry(conversions);
+ }
+
+ public T minecraftToBukkit(final NamespacedKey namespacedKey, final M minecraft) {
+ return this.minecraftToBukkit.apply(namespacedKey, minecraft);
+ }
+
+ protected B newBuilder(final Conversions conversions, final TypedKey<T> key) {
+ return this.builderFactory.create(conversions, key);
+ }
+
+ public class ApiWritableRegistry implements WritableRegistry<T, B> {
+
+ private final Conversions conversions;
+
+ public ApiWritableRegistry(final Conversions conversions) {
+ this.conversions = conversions;
+ }
+
+ @Override
+ public void register(final TypedKey<T> key, final Consumer<? super B> value) {
+ WritableCraftRegistry.this.register(key, value, this.conversions);
+ }
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/data/util/Conversions.java b/src/main/java/io/papermc/paper/registry/data/util/Conversions.java
new file mode 100644
2024-11-25 04:26:32 +01:00
index 0000000000000000000000000000000000000000..1e9a0143bc3b70dfc5e129a0fe849ff3e01447ab
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/data/util/Conversions.java
2024-11-25 04:26:32 +01:00
@@ -0,0 +1,33 @@
2024-06-16 21:39:59 +02:00
+package io.papermc.paper.registry.data.util;
+
+import com.mojang.serialization.JavaOps;
+import io.papermc.paper.adventure.WrapperAwareSerializer;
+import net.kyori.adventure.text.Component;
+import net.minecraft.resources.RegistryOps;
+import org.jetbrains.annotations.Contract;
2024-11-25 04:26:32 +01:00
+import org.jspecify.annotations.Nullable;
2024-06-16 21:39:59 +02:00
+
+public class Conversions {
+
+ 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);
+ }
+}
2024-11-25 04:26:32 +01:00
diff --git a/src/main/java/io/papermc/paper/registry/data/util/package-info.java b/src/main/java/io/papermc/paper/registry/data/util/package-info.java
new file mode 100644
index 0000000000000000000000000000000000000000..5b88be976c7773459ce1b6daf58d7ea7c806f21b
--- /dev/null
+++ b/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;
2024-06-16 21:39:59 +02:00
diff --git a/src/main/java/io/papermc/paper/registry/entry/AddableRegistryEntry.java b/src/main/java/io/papermc/paper/registry/entry/AddableRegistryEntry.java
new file mode 100644
2024-06-17 00:00:12 +02:00
index 0000000000000000000000000000000000000000..aeec9b3ae2911f041d000b3db72f37974020ba60
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/entry/AddableRegistryEntry.java
2024-06-17 00:00:12 +02:00
@@ -0,0 +1,44 @@
2024-06-16 21:39:59 +02:00
+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 java.util.function.BiFunction;
+import net.minecraft.core.MappedRegistry;
+import net.minecraft.core.Registry;
+import net.minecraft.resources.ResourceKey;
+import org.bukkit.Keyed;
+import org.bukkit.NamespacedKey;
+
+public class AddableRegistryEntry<M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> extends CraftRegistryEntry<M, T> implements RegistryEntry.Addable<M, T, B> {
+
+ private final PaperRegistryBuilder.Filler<M, T, B> builderFiller;
+
+ protected AddableRegistryEntry(
+ final ResourceKey<? extends Registry<M>> mcKey,
+ final RegistryKey<T> apiKey,
+ final Class<?> classToPreload,
+ final BiFunction<NamespacedKey, M, T> minecraftToBukkit,
+ final PaperRegistryBuilder.Filler<M, T, B> builderFiller
+ ) {
+ super(mcKey, apiKey, classToPreload, minecraftToBukkit);
+ this.builderFiller = builderFiller;
+ }
+
+ private WritableCraftRegistry<M, T, B> createRegistry(final Registry<M> registry) {
+ return new WritableCraftRegistry<>(this, this.classToPreload, (MappedRegistry<M>) registry, this.updater, this.builderFiller.asFactory(), this.minecraftToBukkit);
+ }
+
+ @Override
+ public RegistryHolder<T> createRegistryHolder(final Registry<M> nmsRegistry) {
+ return new RegistryHolder.Memoized<>(() -> this.createRegistry(nmsRegistry));
+ }
+
+ @Override
2024-06-17 00:00:12 +02:00
+ public B fillBuilder(final Conversions conversions, final TypedKey<T> key, final M nms) {
2024-06-16 21:39:59 +02:00
+ return this.builderFiller.fill(conversions, key, nms);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/entry/ModifiableRegistryEntry.java b/src/main/java/io/papermc/paper/registry/entry/ModifiableRegistryEntry.java
new file mode 100644
2024-06-17 00:00:12 +02:00
index 0000000000000000000000000000000000000000..515a995e3862f8e7cb93d149315ea32e04a08716
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/entry/ModifiableRegistryEntry.java
2024-06-17 00:00:12 +02:00
@@ -0,0 +1,32 @@
2024-06-16 21:39:59 +02:00
+package io.papermc.paper.registry.entry;
+
+import io.papermc.paper.registry.PaperRegistryBuilder;
+import io.papermc.paper.registry.RegistryKey;
+import io.papermc.paper.registry.TypedKey;
+import io.papermc.paper.registry.data.util.Conversions;
+import java.util.function.BiFunction;
+import net.minecraft.core.Registry;
+import net.minecraft.resources.ResourceKey;
+import org.bukkit.Keyed;
+import org.bukkit.NamespacedKey;
+
+public class ModifiableRegistryEntry<M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> extends CraftRegistryEntry<M, T> implements RegistryEntry.Modifiable<M, T, B> {
+
+ protected final PaperRegistryBuilder.Filler<M, T, B> builderFiller;
+
+ protected ModifiableRegistryEntry(
+ final ResourceKey<? extends Registry<M>> mcKey,
+ final RegistryKey<T> apiKey,
+ final Class<?> toPreload,
+ final BiFunction<NamespacedKey, M, T> minecraftToBukkit,
+ final PaperRegistryBuilder.Filler<M, T, B> builderFiller
+ ) {
+ super(mcKey, apiKey, toPreload, minecraftToBukkit);
+ this.builderFiller = builderFiller;
+ }
+
+ @Override
2024-06-17 00:00:12 +02:00
+ public B fillBuilder(final Conversions conversions, final TypedKey<T> key, final M nms) {
2024-06-16 21:39:59 +02:00
+ return this.builderFiller.fill(conversions, key, nms);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/entry/RegistryEntry.java b/src/main/java/io/papermc/paper/registry/entry/RegistryEntry.java
2024-11-25 04:26:32 +01:00
index 2889d87f0989ae5744cd4c1e57240830aa574155..9494bcf82101340e04559d850570698947b7a9ff 100644
2024-06-16 21:39:59 +02:00
--- a/src/main/java/io/papermc/paper/registry/entry/RegistryEntry.java
+++ b/src/main/java/io/papermc/paper/registry/entry/RegistryEntry.java
2024-06-17 00:00:12 +02:00
@@ -1,7 +1,13 @@
2024-06-16 21:39:59 +02:00
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 java.util.function.Supplier;
2024-11-25 04:26:32 +01:00
@@ -10,6 +16,7 @@ import net.minecraft.resources.ResourceKey;
import org.bukkit.Keyed;
2024-06-16 21:39:59 +02:00
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.util.ApiVersion;
2024-11-25 04:26:32 +01:00
+import org.jspecify.annotations.Nullable;
public interface RegistryEntry<M, B extends Keyed> extends RegistryEntryInfo<M, B> { // TODO remove Keyed
2024-06-16 21:39:59 +02:00
2024-11-25 04:26:32 +01:00
@@ -29,6 +36,65 @@ public interface RegistryEntry<M, B extends Keyed> extends RegistryEntryInfo<M,
2024-06-16 21:39:59 +02:00
return new DelayedRegistryEntry<>(this);
}
+ interface BuilderHolder<M, T, B extends PaperRegistryBuilder<M, T>> extends RegistryEntryInfo<M, T> {
+
2024-06-17 00:00:12 +02:00
+ B fillBuilder(Conversions conversions, TypedKey<T> key, M nms);
2024-06-16 21:39:59 +02:00
+ }
+
+ /**
+ * Can mutate values being added to the registry
+ */
+ interface Modifiable<M, T, B extends PaperRegistryBuilder<M, T>> extends BuilderHolder<M, T, B> {
+
+ static boolean isModifiable(final @Nullable RegistryEntryInfo<?, ?> entry) {
+ return entry instanceof RegistryEntry.Modifiable<?, ?, ?> || (entry instanceof final DelayedRegistryEntry<?, ?> delayed && delayed.delegate() instanceof RegistryEntry.Modifiable<?, ?, ?>);
+ }
+
+ static <M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> Modifiable<M, T, B> asModifiable(final RegistryEntryInfo<M, T> entry) { // TODO remove Keyed
+ return (Modifiable<M, T, B>) possiblyUnwrap(entry);
+ }
+
+ default RegistryEntryAddEventImpl<T, B> createEntryAddEvent(final TypedKey<T> 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<M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> extends BuilderHolder<M, T, B> { // TODO remove Keyed
+
+ default RegistryFreezeEventImpl<T, B> createFreezeEvent(final WritableCraftRegistry<M, T, B> 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 <M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> Addable<M, T, B> asAddable(final RegistryEntryInfo<M, T> entry) {
+ return (Addable<M, T, B>) possiblyUnwrap(entry);
+ }
+ }
+
+ /**
+ * Can mutate values and add new values.
+ */
+ interface Writable<M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> extends Modifiable<M, T, B>, Addable<M, T, B> { // 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 <M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> Writable<M, T, B> asWritable(final RegistryEntryInfo<M, T> entry) { // TODO remove Keyed
+ return (Writable<M, T, B>) possiblyUnwrap(entry);
+ }
+ }
+
+ private static <M, B extends Keyed> RegistryEntryInfo<M, B> possiblyUnwrap(final RegistryEntryInfo<M, B> entry) {
+ return entry instanceof final DelayedRegistryEntry<M, B> delayed ? delayed.delegate() : entry;
+ }
+
static <M, B extends Keyed> RegistryEntry<M, B> entry(
final ResourceKey<? extends Registry<M>> mcKey,
final RegistryKey<B> apiKey,
2024-11-25 04:26:32 +01:00
@@ -45,4 +111,24 @@ public interface RegistryEntry<M, B extends Keyed> extends RegistryEntryInfo<M,
2024-06-16 21:39:59 +02:00
) {
return new ApiRegistryEntry<>(mcKey, apiKey, apiRegistrySupplier);
}
+
+ static <M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> RegistryEntry<M, T> modifiable(
+ final ResourceKey<? extends Registry<M>> mcKey,
+ final RegistryKey<T> apiKey,
+ final Class<?> toPreload,
+ final BiFunction<NamespacedKey, M, T> minecraftToBukkit,
+ final PaperRegistryBuilder.Filler<M, T, B> filler
+ ) {
+ return new ModifiableRegistryEntry<>(mcKey, apiKey, toPreload, minecraftToBukkit, filler);
+ }
+
+ static <M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> RegistryEntry<M, T> writable(
+ final ResourceKey<? extends Registry<M>> mcKey,
+ final RegistryKey<T> apiKey,
+ final Class<?> toPreload,
+ final BiFunction<NamespacedKey, M, T> minecraftToBukkit,
+ final PaperRegistryBuilder.Filler<M, T, B> filler
+ ) {
+ return new WritableRegistryEntry<>(mcKey, apiKey, toPreload, minecraftToBukkit, filler);
+ }
}
diff --git a/src/main/java/io/papermc/paper/registry/entry/WritableRegistryEntry.java b/src/main/java/io/papermc/paper/registry/entry/WritableRegistryEntry.java
new file mode 100644
index 0000000000000000000000000000000000000000..562accce731630327d116afd1c9d559df7e386bd
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/entry/WritableRegistryEntry.java
@@ -0,0 +1,22 @@
+package io.papermc.paper.registry.entry;
+
+import io.papermc.paper.registry.PaperRegistryBuilder;
+import io.papermc.paper.registry.RegistryKey;
+import java.util.function.BiFunction;
+import net.minecraft.core.Registry;
+import net.minecraft.resources.ResourceKey;
+import org.bukkit.Keyed;
+import org.bukkit.NamespacedKey;
+
+public class WritableRegistryEntry<M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> extends AddableRegistryEntry<M, T, B> implements RegistryEntry.Writable<M, T, B> { // TODO remove Keyed
+
+ protected WritableRegistryEntry(
+ final ResourceKey<? extends Registry<M>> mcKey,
+ final RegistryKey<T> apiKey,
+ final Class<?> classToPreload,
+ final BiFunction<NamespacedKey, M, T> minecraftToBukkit,
+ final PaperRegistryBuilder.Filler<M, T, B> builderFiller
+ ) {
+ super(mcKey, apiKey, classToPreload, minecraftToBukkit, builderFiller);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEventImpl.java b/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEventImpl.java
new file mode 100644
2024-11-25 04:26:32 +01:00
index 0000000000000000000000000000000000000000..332829c65ef45966dffcf5f1c59422a801179759
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEventImpl.java
2024-11-25 04:26:32 +01:00
@@ -0,0 +1,29 @@
2024-06-16 21:39:59 +02:00
+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
+) implements RegistryEntryAddEvent<T, B>, PaperLifecycleEvent {
+
+ @Override
2024-11-25 04:26:32 +01:00
+ public <V extends Keyed> Tag<V> getOrCreateTag(final TagKey<V> tagKey) {
2024-06-16 21:39:59 +02:00
+ 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);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java b/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java
new file mode 100644
2024-09-25 04:33:30 +02:00
index 0000000000000000000000000000000000000000..bfcd0884d778ca62817fb1d22f0f2ed1f2abe855
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java
2024-09-25 04:33:30 +02:00
@@ -0,0 +1,45 @@
2024-06-16 21:39:59 +02:00
+package io.papermc.paper.registry.event;
+
+import io.papermc.paper.plugin.bootstrap.BootstrapContext;
2024-09-08 20:56:09 +02:00
+import io.papermc.paper.plugin.lifecycle.event.LifecycleEvent;
2024-06-16 21:39:59 +02:00
+import io.papermc.paper.plugin.lifecycle.event.LifecycleEventRunner;
2024-09-08 20:56:09 +02:00
+import io.papermc.paper.plugin.lifecycle.event.types.AbstractLifecycleEventType;
2024-06-16 21:39:59 +02:00
+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 {
+
2024-09-08 20:56:09 +02:00
+ private final Map<RegistryKey<?>, LifecycleEventType<BootstrapContext, ? extends LifecycleEvent, ?>> eventTypes = new HashMap<>();
2024-06-16 21:39:59 +02:00
+ private final String name;
+
+ public RegistryEventMap(final String name) {
+ this.name = name;
+ }
+
+ @SuppressWarnings("unchecked")
2024-09-08 20:56:09 +02:00
+ public <T, E extends LifecycleEvent, ET extends LifecycleEventType<BootstrapContext, E, ?>> ET getOrCreate(final RegistryKey<T> registryKey, final BiFunction<? super RegistryKey<T>, ? super String, ET> eventTypeCreator) {
+ final ET eventType;
+ if (this.eventTypes.containsKey(registryKey)) {
+ eventType = (ET) this.eventTypes.get(registryKey);
2024-06-16 21:39:59 +02:00
+ } else {
2024-09-08 20:56:09 +02:00
+ eventType = eventTypeCreator.apply(registryKey, this.name);
+ this.eventTypes.put(registryKey, eventType);
2024-06-16 21:39:59 +02:00
+ }
2024-09-08 20:56:09 +02:00
+ return eventType;
2024-06-16 21:39:59 +02:00
+ }
+
+ @SuppressWarnings("unchecked")
2024-09-08 20:56:09 +02:00
+ public <T, E extends LifecycleEvent> LifecycleEventType<BootstrapContext, E, ?> getEventType(final RegistryKey<T> registryKey) {
+ return (LifecycleEventType<BootstrapContext, E, ?>) Objects.requireNonNull(this.eventTypes.get(registryKey), "No hook for " + registryKey);
2024-06-16 21:39:59 +02:00
+ }
+
2024-09-08 20:56:09 +02:00
+ public boolean hasHandlers(final RegistryKey<?> registryKey) {
+ final AbstractLifecycleEventType<?, ?, ?> type = ((AbstractLifecycleEventType<?, ?, ?>) this.eventTypes.get(registryKey));
+ return type != null && type.hasHandlers();
2024-06-16 21:39:59 +02:00
+ }
+
+}
diff --git a/src/main/java/io/papermc/paper/registry/event/RegistryEventTypeProviderImpl.java b/src/main/java/io/papermc/paper/registry/event/RegistryEventTypeProviderImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..34c842ffa355e3c8001dd7b8551bcb49229a6391
--- /dev/null
+++ b/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 <T, B extends RegistryBuilder<T>> RegistryEntryAddEventType<T, B> registryEntryAdd(final RegistryEventProvider<T, B> type) {
+ return PaperRegistryListenerManager.INSTANCE.getRegistryValueAddEventType(type);
+ }
+
+ @Override
+ public <T, B extends RegistryBuilder<T>> LifecycleEventType.Prioritizable<BootstrapContext, RegistryFreezeEvent<T, B>> registryFreeze(final RegistryEventProvider<T, B> type) {
+ return PaperRegistryListenerManager.INSTANCE.getRegistryFreezeEventType(type);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEventImpl.java b/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEventImpl.java
new file mode 100644
2024-11-25 04:26:32 +01:00
index 0000000000000000000000000000000000000000..1b45802f9afc97fd59c6dc77964dedea1016cd6c
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEventImpl.java
2024-11-25 04:26:32 +01:00
@@ -0,0 +1,27 @@
2024-06-16 21:39:59 +02:00
+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<T, B extends RegistryBuilder<T>>(
+ RegistryKey<T> registryKey,
+ WritableRegistry<T, B> registry,
+ Conversions conversions
+) implements RegistryFreezeEvent<T, B>, PaperLifecycleEvent {
+
+ @Override
2024-11-25 04:26:32 +01:00
+ public <V extends Keyed> Tag<V> getOrCreateTag(final TagKey<V> tagKey) {
2024-06-16 21:39:59 +02:00
+ 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);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/event/package-info.java b/src/main/java/io/papermc/paper/registry/event/package-info.java
new file mode 100644
2024-11-25 04:26:32 +01:00
index 0000000000000000000000000000000000000000..1d20e0d940ae498b96fe33f6176c140f816921f1
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/event/package-info.java
2024-11-25 04:26:32 +01:00
@@ -0,0 +1,4 @@
+@NullMarked
2024-06-16 21:39:59 +02:00
+package io.papermc.paper.registry.event;
+
2024-11-25 04:26:32 +01:00
+import org.jspecify.annotations.NullMarked;
2024-06-16 21:39:59 +02:00
diff --git a/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddEventTypeImpl.java b/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddEventTypeImpl.java
new file mode 100644
2024-09-25 04:33:30 +02:00
index 0000000000000000000000000000000000000000..fbf853bf1cbb3c7bbef531afe185818b9454299b
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddEventTypeImpl.java
2024-09-25 04:33:30 +02:00
@@ -0,0 +1,37 @@
2024-06-16 21:39:59 +02:00
+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;
2024-09-08 20:56:09 +02:00
+import io.papermc.paper.registry.RegistryKey;
2024-06-16 21:39:59 +02:00
+import io.papermc.paper.registry.event.RegistryEntryAddEvent;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+
+public class RegistryEntryAddEventTypeImpl<T, B extends RegistryBuilder<T>> extends PrioritizableLifecycleEventType<BootstrapContext, RegistryEntryAddEvent<T, B>, RegistryEntryAddConfiguration<T>> implements RegistryEntryAddEventType<T, B> {
+
2024-09-08 20:56:09 +02:00
+ public RegistryEntryAddEventTypeImpl(final RegistryKey<T> registryKey, final String eventName) {
+ super(registryKey + " / " + eventName, BootstrapContext.class);
2024-06-16 21:39:59 +02:00
+ }
+
+ @Override
2024-09-25 04:33:30 +02:00
+ public boolean blocksReloading(final BootstrapContext eventOwner) {
+ return false; // only runs once
+ }
+
+ @Override
2024-06-16 21:39:59 +02:00
+ public RegistryEntryAddConfiguration<T> newHandler(final LifecycleEventHandler<? super RegistryEntryAddEvent<T, B>> handler) {
+ return new RegistryEntryAddHandlerConfiguration<>(handler, this);
+ }
+
+ @Override
+ public void forEachHandler(final RegistryEntryAddEvent<T, B> event, final Consumer<RegisteredHandler<BootstrapContext, RegistryEntryAddEvent<T, B>>> consumer, final Predicate<RegisteredHandler<BootstrapContext, RegistryEntryAddEvent<T, B>>> predicate) {
+ super.forEachHandler(event, consumer, predicate.and(handler -> this.matchesTarget(event, handler)));
+ }
+
+ private boolean matchesTarget(final RegistryEntryAddEvent<T, B> event, final RegisteredHandler<BootstrapContext, RegistryEntryAddEvent<T, B>> handler) {
+ final RegistryEntryAddHandlerConfiguration<T, B> config = (RegistryEntryAddHandlerConfiguration<T, B>) handler.config();
+ return config.filter() == null || config.filter().test(event.key());
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddHandlerConfiguration.java b/src/main/java/io/papermc/paper/registry/event/type/RegistryEntryAddHandlerConfiguration.java
new file mode 100644
2024-11-25 04:26:32 +01:00
index 0000000000000000000000000000000000000000..3cc0f7688f61c5fdbd98d0134741c98a8b9748b9
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/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;
2024-11-25 04:26:32 +01:00
+import org.jspecify.annotations.Nullable;
2024-06-16 21:39:59 +02:00
+
+public class RegistryEntryAddHandlerConfiguration<T, B extends RegistryBuilder<T>> extends PrioritizedLifecycleEventHandlerConfigurationImpl<BootstrapContext, RegistryEntryAddEvent<T, B>> implements RegistryEntryAddConfiguration<T> {
+
+ private @Nullable Predicate<TypedKey<T>> filter;
+
+ public RegistryEntryAddHandlerConfiguration(final LifecycleEventHandler<? super RegistryEntryAddEvent<T, B>> handler, final AbstractLifecycleEventType<BootstrapContext, RegistryEntryAddEvent<T, B>, ?> eventType) {
+ super(handler, eventType);
+ }
+
+ @Contract(pure = true)
+ public @Nullable Predicate<TypedKey<T>> filter() {
+ return this.filter;
+ }
+
+ @Override
+ public RegistryEntryAddConfiguration<T> filter(final Predicate<TypedKey<T>> filter) {
+ this.filter = filter;
+ return this;
+ }
+
+ @Override
+ public RegistryEntryAddConfiguration<T> priority(final int priority) {
+ return (RegistryEntryAddConfiguration<T>) super.priority(priority);
+ }
+
+ @Override
+ public RegistryEntryAddConfiguration<T> monitor() {
+ return (RegistryEntryAddConfiguration<T>) super.monitor();
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/event/type/RegistryLifecycleEventType.java b/src/main/java/io/papermc/paper/registry/event/type/RegistryLifecycleEventType.java
new file mode 100644
2024-09-25 04:33:30 +02:00
index 0000000000000000000000000000000000000000..7ee77022198bf5f9f88c6a1917a1da30b1863883
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/event/type/RegistryLifecycleEventType.java
2024-09-25 04:33:30 +02:00
@@ -0,0 +1,18 @@
2024-06-16 21:39:59 +02:00
+package io.papermc.paper.registry.event.type;
+
+import io.papermc.paper.plugin.bootstrap.BootstrapContext;
+import io.papermc.paper.plugin.lifecycle.event.types.PrioritizableLifecycleEventType;
2024-09-08 20:56:09 +02:00
+import io.papermc.paper.registry.RegistryKey;
2024-06-16 21:39:59 +02:00
+import io.papermc.paper.registry.event.RegistryEvent;
+
2024-09-08 20:56:09 +02:00
+public final class RegistryLifecycleEventType<T, E extends RegistryEvent<T>> extends PrioritizableLifecycleEventType.Simple<BootstrapContext, E> {
2024-06-16 21:39:59 +02:00
+
2024-09-08 20:56:09 +02:00
+ public RegistryLifecycleEventType(final RegistryKey<T> registryKey, final String eventName) {
+ super(registryKey + " / " + eventName, BootstrapContext.class);
2024-06-16 21:39:59 +02:00
+ }
2024-09-25 04:33:30 +02:00
+
+ @Override
+ public boolean blocksReloading(final BootstrapContext eventOwner) {
+ return false; // only runs once
+ }
2024-06-16 21:39:59 +02:00
+}
2024-11-25 04:26:32 +01:00
diff --git a/src/main/java/io/papermc/paper/registry/event/type/package-info.java b/src/main/java/io/papermc/paper/registry/event/type/package-info.java
new file mode 100644
index 0000000000000000000000000000000000000000..f9f63926a5aaf84e0d23bac3422c5800683e37f9
--- /dev/null
+++ b/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;
2024-06-16 21:39:59 +02:00
diff --git a/src/main/java/io/papermc/paper/registry/legacy/DelayedRegistry.java b/src/main/java/io/papermc/paper/registry/legacy/DelayedRegistry.java
2024-11-25 11:18:05 +01:00
index ca829b162d4369f845e59b62bb8779fd83fe2ef3..fdc475f2b112ba88ff1d89cb0c4eaa465b2d034c 100644
2024-06-16 21:39:59 +02:00
--- a/src/main/java/io/papermc/paper/registry/legacy/DelayedRegistry.java
+++ b/src/main/java/io/papermc/paper/registry/legacy/DelayedRegistry.java
2024-11-25 11:18:05 +01:00
@@ -1,11 +1,14 @@
2024-06-16 21:39:59 +02:00
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;
2024-11-25 11:18:05 +01:00
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<T extends Keyed, R extends Registry<T>> imple
2024-11-25 04:26:32 +01:00
public @Nullable NamespacedKey getKey(final T value) {
2024-06-16 21:39:59 +02:00
return this.delegate().getKey(value);
}
+
+ @Override
+ public boolean hasTag(final TagKey<T> key) {
+ return this.delegate().hasTag(key);
+ }
+
+ @Override
2024-11-25 11:18:05 +01:00
+ public @NonNull Tag<T> getTag(final TagKey<T> key) {
2024-06-16 21:39:59 +02:00
+ return this.delegate().getTag(key);
+ }
}
diff --git a/src/main/java/io/papermc/paper/registry/set/NamedRegistryKeySetImpl.java b/src/main/java/io/papermc/paper/registry/set/NamedRegistryKeySetImpl.java
new file mode 100644
2024-11-25 04:26:32 +01:00
index 0000000000000000000000000000000000000000..7b15640c2f10c72f2612ab2270adc5689dfd9e5a
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/set/NamedRegistryKeySetImpl.java
2024-11-25 04:26:32 +01:00
@@ -0,0 +1,74 @@
2024-06-16 21:39:59 +02:00
+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;
2024-11-25 04:26:32 +01:00
+import org.jspecify.annotations.NullMarked;
2024-06-16 21:39:59 +02:00
+
2024-11-25 04:26:32 +01:00
+@NullMarked
2024-06-16 21:39:59 +02:00
+public record NamedRegistryKeySetImpl<T extends Keyed, M>( // TODO remove Keyed
+ TagKey<T> tagKey,
+ HolderSet.Named<M> namedSet
+) implements Tag<T>, org.bukkit.Tag<T> {
+
+ @Override
+ public @Unmodifiable Collection<TypedKey<T>> values() {
+ final ImmutableList.Builder<TypedKey<T>> builder = ImmutableList.builder();
+ for (final Holder<M> holder : this.namedSet) {
+ builder.add(TypedKey.create(this.tagKey.registryKey(), CraftNamespacedKey.fromMinecraft(((Holder.Reference<?>) holder).key().location())));
+ }
+ return builder.build();
+ }
+
+ @Override
+ public RegistryKey<T> registryKey() {
+ return this.tagKey.registryKey();
+ }
+
+ @Override
+ public boolean contains(final TypedKey<T> valueKey) {
+ return Iterables.any(this.namedSet, h -> {
+ return PaperRegistries.fromNms(((Holder.Reference<?>) h).key()).equals(valueKey);
+ });
+ }
+
+ @Override
+ public @Unmodifiable Collection<T> resolve(final Registry<T> registry) {
+ final ImmutableList.Builder<T> builder = ImmutableList.builder();
+ for (final Holder<M> 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<T> getValues() {
+ return Set.copyOf(this.resolve(RegistryAccess.registryAccess().getRegistry(this.registryKey())));
+ }
+
+ @Override
2024-11-25 04:26:32 +01:00
+ public NamespacedKey getKey() {
2024-06-16 21:39:59 +02:00
+ final Key key = this.tagKey().key();
+ return new NamespacedKey(key.namespace(), key.value());
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/set/PaperRegistrySets.java b/src/main/java/io/papermc/paper/registry/set/PaperRegistrySets.java
new file mode 100644
2024-11-25 04:26:32 +01:00
index 0000000000000000000000000000000000000000..cb92f53cf9d139828acd016499470212a198b722
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/set/PaperRegistrySets.java
2024-11-25 04:26:32 +01:00
@@ -0,0 +1,45 @@
2024-06-16 21:39:59 +02:00
+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 <A extends Keyed, M> HolderSet<M> convertToNms(final ResourceKey<? extends Registry<M>> resourceKey, final RegistryOps.RegistryInfoLookup lookup, final RegistryKeySet<A> registryKeySet) { // TODO remove Keyed
+ if (registryKeySet instanceof NamedRegistryKeySetImpl<A, ?>) {
+ return ((NamedRegistryKeySetImpl<A, M>) registryKeySet).namedSet();
+ } else {
+ final RegistryOps.RegistryInfo<M> registryInfo = lookup.lookup(resourceKey).orElseThrow();
+ return HolderSet.direct(key -> {
+ return registryInfo.getter().getOrThrow(PaperRegistries.toNms(key));
+ }, registryKeySet.values());
+ }
+ }
+
+ public static <A extends Keyed, M> RegistryKeySet<A> convertToApi(final RegistryKey<A> registryKey, final HolderSet<M> holders) { // TODO remove Keyed
+ if (holders instanceof final HolderSet.Named<M> named) {
+ return new NamedRegistryKeySetImpl<>(PaperRegistries.fromNms(named.key()), named);
+ } else {
+ final List<TypedKey<A>> keys = new ArrayList<>();
+ for (final Holder<M> holder : holders) {
+ if (!(holder instanceof final Holder.Reference<M> 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() {
+ }
+}
2024-11-25 04:26:32 +01:00
diff --git a/src/main/java/io/papermc/paper/registry/set/package-info.java b/src/main/java/io/papermc/paper/registry/set/package-info.java
new file mode 100644
index 0000000000000000000000000000000000000000..516b072428dcc8a28d13bcc990493cf4c22ad948
--- /dev/null
+++ b/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;
2024-06-16 21:39:59 +02:00
diff --git a/src/main/java/net/minecraft/core/MappedRegistry.java b/src/main/java/net/minecraft/core/MappedRegistry.java
2024-10-24 23:03:27 +02:00
index 71e04e5c1bc0722abf8ca2e0738bd60b6d7ae21c..063630c1ffcce099139c59d598fc5a210e21f640 100644
2024-06-16 21:39:59 +02:00
--- a/src/main/java/net/minecraft/core/MappedRegistry.java
+++ b/src/main/java/net/minecraft/core/MappedRegistry.java
2024-10-24 23:03:27 +02:00
@@ -509,4 +509,12 @@ public class MappedRegistry<T> implements WritableRegistry<T> {
Stream<HolderSet.Named<T>> getTags();
2024-06-16 21:39:59 +02:00
}
+ // 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/src/main/java/net/minecraft/core/registries/BuiltInRegistries.java b/src/main/java/net/minecraft/core/registries/BuiltInRegistries.java
2024-10-25 14:55:49 +02:00
index 4638ba98dbbdb0f880337347be85a6e0fbed2191..bc448f8511c629d1f13d4baf717a11e6a6ad24f9 100644
2024-06-16 21:39:59 +02:00
--- a/src/main/java/net/minecraft/core/registries/BuiltInRegistries.java
+++ b/src/main/java/net/minecraft/core/registries/BuiltInRegistries.java
2024-10-24 23:03:27 +02:00
@@ -296,6 +296,17 @@ public class BuiltInRegistries {
public static final Registry<SlotDisplay.Type<?>> SLOT_DISPLAY = registerSimple(Registries.SLOT_DISPLAY, SlotDisplays::bootstrap);
public static final Registry<RecipeBookCategory> RECIPE_BOOK_CATEGORY = registerSimple(Registries.RECIPE_BOOK_CATEGORY, RecipeBookCategories::bootstrap);
2024-06-16 21:39:59 +02:00
public static final Registry<? extends 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 <T> java.util.Optional<net.minecraft.resources.RegistryOps.RegistryInfo<T>> lookup(final ResourceKey<? extends Registry<? extends T>> registryRef) {
2024-10-25 14:55:49 +02:00
+ final Registry<T> registry = net.minecraft.server.RegistryLayer.STATIC_ACCESS.lookupOrThrow(registryRef);
2024-06-16 21:39:59 +02:00
+ return java.util.Optional.of(
2024-10-25 14:55:49 +02:00
+ new net.minecraft.resources.RegistryOps.RegistryInfo<>(registry, registry, Lifecycle.experimental())
2024-06-16 21:39:59 +02:00
+ );
+ }
+ });
+ // Paper end - add built-in registry conversions
private static <T> Registry<T> registerSimple(ResourceKey<? extends Registry<T>> key, BuiltInRegistries.RegistryBootstrap<T> initializer) {
return internalRegister(key, new MappedRegistry<>(key, Lifecycle.stable(), false), initializer);
2024-10-24 23:03:27 +02:00
@@ -336,6 +347,7 @@ public class BuiltInRegistries {
2024-06-16 21:39:59 +02:00
}
public static void bootStrap(Runnable runnable) {
// Paper end
+ REGISTRY.freeze(); // Paper - freeze main registry early
createContents();
runnable.run(); // Paper
freeze();
2024-10-24 23:03:27 +02:00
@@ -355,6 +367,7 @@ public class BuiltInRegistries {
2024-06-16 21:39:59 +02:00
for (Registry<?> registry : REGISTRY) {
2024-10-24 23:03:27 +02:00
bindBootstrappedTagsToEmpty(registry);
2024-06-16 21:39:59 +02:00
+ io.papermc.paper.registry.PaperRegistryListenerManager.INSTANCE.runFreezeListeners(registry.key(), BUILT_IN_CONVERSIONS); // Paper
registry.freeze();
}
}
diff --git a/src/main/java/net/minecraft/resources/RegistryDataLoader.java b/src/main/java/net/minecraft/resources/RegistryDataLoader.java
2024-11-23 19:52:00 +01:00
index 61774165a25209ee6d26cf8d80149b220c3874e6..c5e279216f6cb880397ea2768ee6e99a8798e3e1 100644
2024-06-16 21:39:59 +02:00
--- a/src/main/java/net/minecraft/resources/RegistryDataLoader.java
+++ b/src/main/java/net/minecraft/resources/RegistryDataLoader.java
2024-10-24 23:03:27 +02:00
@@ -130,7 +130,7 @@ public class RegistryDataLoader {
public static RegistryAccess.Frozen load(
ResourceManager resourceManager, List<HolderLookup.RegistryLookup<?>> registries, List<RegistryDataLoader.RegistryData<?>> entries
) {
- return load((loader, infoGetter) -> loader.loadFromResources(resourceManager, infoGetter), registries, entries);
+ return load((loader, infoGetter, conversions) -> loader.loadFromResources(resourceManager, infoGetter, conversions), registries, entries); // Paper - pass conversions
2024-06-16 21:39:59 +02:00
}
public static RegistryAccess.Frozen load(
2024-10-24 23:03:27 +02:00
@@ -139,7 +139,7 @@ public class RegistryDataLoader {
List<HolderLookup.RegistryLookup<?>> registries,
2024-06-16 21:39:59 +02:00
List<RegistryDataLoader.RegistryData<?>> entries
) {
2024-10-24 23:03:27 +02:00
- return load((loader, infoGetter) -> loader.loadFromNetwork(data, factory, infoGetter), registries, entries);
+ return load((loader, infoGetter, conversions) -> loader.loadFromNetwork(data, factory, infoGetter, conversions), registries, entries); // Paper - pass conversions
2024-06-16 21:39:59 +02:00
}
private static RegistryAccess.Frozen load(
2024-11-23 19:52:00 +01:00
@@ -148,7 +148,8 @@ public class RegistryDataLoader {
2024-06-16 21:39:59 +02:00
Map<ResourceKey<?>, Exception> map = new HashMap<>();
List<RegistryDataLoader.Loader<?>> list = entries.stream().map(entry -> entry.create(Lifecycle.stable(), map)).collect(Collectors.toUnmodifiableList());
2024-10-24 23:03:27 +02:00
RegistryOps.RegistryInfoLookup registryInfoLookup = createContext(registries, list);
2024-06-16 21:39:59 +02:00
- list.forEach(loader -> loadable.apply((RegistryDataLoader.Loader<?>)loader, registryInfoLookup));
+ final io.papermc.paper.registry.data.util.Conversions conversions = new io.papermc.paper.registry.data.util.Conversions(registryInfoLookup); // Paper - create conversions
+ list.forEach(loader -> loadable.apply((RegistryDataLoader.Loader<?>)loader, registryInfoLookup, conversions));
list.forEach(loader -> {
Registry<?> registry = loader.registry();
2024-11-23 19:52:00 +01:00
@@ -238,13 +239,13 @@ public class RegistryDataLoader {
2024-06-16 21:39:59 +02:00
}
private static <E> void loadElementFromResource(
- WritableRegistry<E> registry, Decoder<E> decoder, RegistryOps<JsonElement> ops, ResourceKey<E> key, Resource resource, RegistrationInfo entryInfo
+ WritableRegistry<E> registry, Decoder<E> decoder, RegistryOps<JsonElement> ops, ResourceKey<E> key, Resource resource, RegistrationInfo entryInfo, io.papermc.paper.registry.data.util.Conversions conversions
) throws IOException {
try (Reader reader = resource.openAsReader()) {
JsonElement jsonElement = JsonParser.parseReader(reader);
DataResult<E> 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
}
}
2024-11-23 19:52:00 +01:00
@@ -253,7 +254,8 @@ public class RegistryDataLoader {
2024-06-16 21:39:59 +02:00
RegistryOps.RegistryInfoLookup infoGetter,
WritableRegistry<E> registry,
Decoder<E> elementDecoder,
- Map<ResourceKey<?>, Exception> errors
+ Map<ResourceKey<?>, Exception> errors,
+ io.papermc.paper.registry.data.util.Conversions conversions // Paper - pass conversions
) {
String string = Registries.elementsDirPath(registry.key());
FileToIdConverter fileToIdConverter = FileToIdConverter.json(string);
2024-11-23 19:52:00 +01:00
@@ -266,7 +268,7 @@ public class RegistryDataLoader {
2024-06-16 21:39:59 +02:00
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 var15) {
errors.put(
resourceKey,
2024-11-23 19:52:00 +01:00
@@ -275,6 +277,7 @@ public class RegistryDataLoader {
}
}
+ io.papermc.paper.registry.PaperRegistryListenerManager.INSTANCE.runFreezeListeners(registry.key(), conversions); // Paper - run pre-freeze listeners
TagLoader.loadTagsForRegistry(resourceManager, registry);
}
2024-10-24 23:03:27 +02:00
@@ -284,7 +287,8 @@ public class RegistryDataLoader {
2024-06-16 21:39:59 +02:00
RegistryOps.RegistryInfoLookup infoGetter,
WritableRegistry<E> registry,
Decoder<E> decoder,
- Map<ResourceKey<?>, Exception> loadingErrors
+ Map<ResourceKey<?>, Exception> loadingErrors,
+ io.papermc.paper.registry.data.util.Conversions conversions // Paper - pass conversions
) {
2024-10-24 23:03:27 +02:00
RegistryDataLoader.NetworkedRegistryData networkedRegistryData = data.get(registry.key());
if (networkedRegistryData != null) {
@@ -311,7 +315,7 @@ public class RegistryDataLoader {
2024-06-16 21:39:59 +02:00
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 var18) {
loadingErrors.put(resourceKey, new IllegalStateException("Failed to parse local data", var18));
}
2024-10-24 23:03:27 +02:00
@@ -323,22 +327,23 @@ public class RegistryDataLoader {
2024-06-16 21:39:59 +02:00
}
static record Loader<T>(RegistryDataLoader.RegistryData<T> data, WritableRegistry<T> registry, Map<ResourceKey<?>, Exception> loadingErrors) {
- public void loadFromResources(ResourceManager resourceManager, RegistryOps.RegistryInfoLookup infoGetter) {
- RegistryDataLoader.loadContentsFromManager(resourceManager, infoGetter, this.registry, this.data.elementCodec, this.loadingErrors);
+ public void loadFromResources(ResourceManager resourceManager, RegistryOps.RegistryInfoLookup infoGetter, io.papermc.paper.registry.data.util.Conversions conversions) { // Paper - pass conversions
+ RegistryDataLoader.loadContentsFromManager(resourceManager, infoGetter, this.registry, this.data.elementCodec, this.loadingErrors, conversions); // Paper - pass conversions
}
public void loadFromNetwork(
2024-10-24 23:03:27 +02:00
Map<ResourceKey<? extends Registry<?>>, RegistryDataLoader.NetworkedRegistryData> data,
2024-06-16 21:39:59 +02:00
ResourceProvider factory,
- RegistryOps.RegistryInfoLookup infoGetter
+ RegistryOps.RegistryInfoLookup infoGetter,
+ io.papermc.paper.registry.data.util.Conversions conversions // Paper
) {
- RegistryDataLoader.loadContentsFromNetwork(data, factory, infoGetter, this.registry, this.data.elementCodec, this.loadingErrors);
+ RegistryDataLoader.loadContentsFromNetwork(data, factory, infoGetter, this.registry, this.data.elementCodec, this.loadingErrors, conversions); // Paper - pass conversions
}
}
@FunctionalInterface
interface LoadingFunction {
- void apply(RegistryDataLoader.Loader<?> loader, RegistryOps.RegistryInfoLookup infoGetter);
+ void apply(RegistryDataLoader.Loader<?> loader, RegistryOps.RegistryInfoLookup infoGetter, io.papermc.paper.registry.data.util.Conversions conversions); // Paper - pass conversions
}
2024-10-24 23:03:27 +02:00
public static record NetworkedRegistryData(List<RegistrySynchronization.PackedRegistryEntry> elements, TagNetworkSerialization.NetworkPayload tags) {
2024-06-16 21:39:59 +02:00
diff --git a/src/main/java/net/minecraft/server/ReloadableServerRegistries.java b/src/main/java/net/minecraft/server/ReloadableServerRegistries.java
2024-10-24 23:03:27 +02:00
index 6fddef967b6314ca0158f5bd4b8898670ea5e9ec..b5ca1a0acb16d0cd8dccc854f309d425a48b070d 100644
2024-06-16 21:39:59 +02:00
--- a/src/main/java/net/minecraft/server/ReloadableServerRegistries.java
+++ b/src/main/java/net/minecraft/server/ReloadableServerRegistries.java
2024-10-24 23:03:27 +02:00
@@ -50,8 +50,9 @@ public class ReloadableServerRegistries {
);
HolderLookup.Provider provider = HolderLookup.Provider.create(list.stream());
RegistryOps<JsonElement> registryOps = provider.createSerializationContext(JsonOps.INSTANCE);
2024-06-16 21:39:59 +02:00
+ final io.papermc.paper.registry.data.util.Conversions conversions = new io.papermc.paper.registry.data.util.Conversions(registryOps.lookupProvider); // Paper
2024-10-24 23:03:27 +02:00
List<CompletableFuture<WritableRegistry<?>>> list2 = LootDataType.values()
- .map(type -> scheduleRegistryLoad((LootDataType<?>)type, registryOps, resourceManager, prepareExecutor))
+ .map(type -> scheduleRegistryLoad((LootDataType<?>)type, registryOps, resourceManager, prepareExecutor, conversions)) // Paper
2024-06-16 21:39:59 +02:00
.toList();
2024-10-24 23:03:27 +02:00
CompletableFuture<List<WritableRegistry<?>>> completableFuture = Util.sequence(list2);
return completableFuture.thenApplyAsync(
@@ -60,7 +61,7 @@ public class ReloadableServerRegistries {
2024-06-16 21:39:59 +02:00
}
2024-10-24 23:03:27 +02:00
private static <T> CompletableFuture<WritableRegistry<?>> scheduleRegistryLoad(
2024-06-16 21:39:59 +02:00
- LootDataType<T> type, RegistryOps<JsonElement> ops, ResourceManager resourceManager, Executor prepareExecutor
+ LootDataType<T> type, RegistryOps<JsonElement> ops, ResourceManager resourceManager, Executor prepareExecutor, io.papermc.paper.registry.data.util.Conversions conversions // Paper
) {
2024-10-24 23:03:27 +02:00
return CompletableFuture.supplyAsync(() -> {
WritableRegistry<T> writableRegistry = new MappedRegistry<>(type.registryKey(), Lifecycle.experimental());
@@ -68,7 +69,7 @@ public class ReloadableServerRegistries {
Map<ResourceLocation, T> map = new HashMap<>();
String string = Registries.elementsDirPath(type.registryKey());
SimpleJsonResourceReloadListener.scanDirectory(resourceManager, string, ops, type.codec(), map);
- 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);
2024-06-16 21:39:59 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
2024-11-04 18:42:38 +01:00
index 45c78c113e881b277e1216293ad918ee40b44325..8314059455d91f01b986c5c0a239f41817834bd6 100644
2024-06-16 21:39:59 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
2024-11-04 18:42:38 +01:00
@@ -164,11 +164,11 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
2024-06-16 21:39:59 +02:00
private final Map<NamespacedKey, B> cache = new HashMap<>();
private final Map<B, NamespacedKey> byValue = new java.util.IdentityHashMap<>(); // Paper - improve Registry
private final net.minecraft.core.Registry<M> minecraftRegistry;
- private final BiFunction<NamespacedKey, M, B> minecraftToBukkit;
+ private final BiFunction<? super NamespacedKey, M, B> minecraftToBukkit; // Paper
private final BiFunction<NamespacedKey, ApiVersion, NamespacedKey> serializationUpdater; // Paper - rename to make it *clear* what it is *only* for
private boolean init;
- public CraftRegistry(Class<?> bukkitClass, net.minecraft.core.Registry<M> minecraftRegistry, BiFunction<NamespacedKey, M, B> minecraftToBukkit, BiFunction<NamespacedKey, ApiVersion, NamespacedKey> serializationUpdater) { // Paper - relax preload class
+ public CraftRegistry(Class<?> bukkitClass, net.minecraft.core.Registry<M> minecraftRegistry, BiFunction<? super NamespacedKey, M, B> minecraftToBukkit, BiFunction<NamespacedKey, ApiVersion, NamespacedKey> serializationUpdater) { // Paper - relax preload class
this.bukkitClass = bukkitClass;
this.minecraftRegistry = minecraftRegistry;
this.minecraftToBukkit = minecraftToBukkit;
2024-11-04 18:42:38 +01:00
@@ -251,4 +251,17 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
2024-06-16 21:39:59 +02:00
return this.byValue.get(value);
}
// Paper end - improve Registry
+
+ // Paper start - RegistrySet API
+ @Override
+ public boolean hasTag(final io.papermc.paper.registry.tag.TagKey<B> key) {
2024-10-25 14:55:49 +02:00
+ return this.minecraftRegistry.get(net.minecraft.tags.TagKey.create(this.minecraftRegistry.key(), io.papermc.paper.adventure.PaperAdventure.asVanilla(key.key()))).isPresent();
2024-06-16 21:39:59 +02:00
+ }
+
+ @Override
+ public io.papermc.paper.registry.tag.Tag<B> getTag(final io.papermc.paper.registry.tag.TagKey<B> key) {
2024-10-25 14:55:49 +02:00
+ final net.minecraft.core.HolderSet.Named<M> namedHolderSet = this.minecraftRegistry.get(io.papermc.paper.registry.PaperRegistries.toNms(key)).orElseThrow();
2024-06-16 21:39:59 +02:00
+ return new io.papermc.paper.registry.set.NamedRegistryKeySetImpl<>(key, namedHolderSet);
+ }
+ // Paper end - RegistrySet API
}
diff --git a/src/main/resources/META-INF/services/io.papermc.paper.registry.event.RegistryEventTypeProvider b/src/main/resources/META-INF/services/io.papermc.paper.registry.event.RegistryEventTypeProvider
new file mode 100644
index 0000000000000000000000000000000000000000..8bee1a5ed877a04e4d027593df1f42cefdd824e7
--- /dev/null
+++ b/src/main/resources/META-INF/services/io.papermc.paper.registry.event.RegistryEventTypeProvider
@@ -0,0 +1 @@
+io.papermc.paper.registry.event.RegistryEventTypeProviderImpl
diff --git a/src/test/java/io/papermc/paper/registry/RegistryBuilderTest.java b/src/test/java/io/papermc/paper/registry/RegistryBuilderTest.java
new file mode 100644
2024-11-24 20:12:08 +01:00
index 0000000000000000000000000000000000000000..f2f1dfe4277ce1e84a9494bee285badc958c8d3f
2024-06-16 21:39:59 +02:00
--- /dev/null
+++ b/src/test/java/io/papermc/paper/registry/RegistryBuilderTest.java
2024-11-24 20:12:08 +01:00
@@ -0,0 +1,44 @@
2024-06-16 21:39:59 +02:00
+package io.papermc.paper.registry;
+
+import io.papermc.paper.registry.data.util.Conversions;
2024-11-24 20:12:08 +01:00
+import io.papermc.paper.registry.entry.RegistryEntry;
+import io.papermc.paper.registry.entry.RegistryEntryInfo;
+import io.papermc.paper.registry.legacy.DelayedRegistryEntry;
2024-06-16 21:39:59 +02:00
+import java.util.Map;
2024-11-24 20:12:08 +01:00
+import java.util.stream.Stream;
2024-06-16 21:39:59 +02:00
+import net.minecraft.core.Registry;
+import net.minecraft.resources.RegistryOps;
+import net.minecraft.resources.ResourceKey;
2024-11-24 20:12:08 +01:00
+import org.bukkit.Keyed;
2024-10-21 00:06:54 +02:00
+import org.bukkit.support.RegistryHelper;
+import org.bukkit.support.environment.AllFeatures;
2024-06-16 21:39:59 +02:00
+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;
+
2024-10-21 00:06:54 +02:00
+@AllFeatures
+class RegistryBuilderTest {
2024-06-16 21:39:59 +02:00
+
2024-11-24 20:12:08 +01:00
+ static Stream<Arguments> registries() {
+ return PaperRegistries.REGISTRY_ENTRIES.stream()
+ .map(RegistryBuilderTest::possiblyUnwrap)
+ .filter(RegistryEntry.BuilderHolder.class::isInstance)
+ .map(Arguments::arguments);
+ }
+
+ private static <M, B extends Keyed> RegistryEntryInfo<M, B> possiblyUnwrap(final RegistryEntryInfo<M, B> entry) {
+ return entry instanceof final DelayedRegistryEntry<M, B> delayed ? delayed.delegate() : entry;
2024-06-16 21:39:59 +02:00
+ }
+
+ @ParameterizedTest
+ @MethodSource("registries")
2024-11-24 20:12:08 +01:00
+ <M, T> void testEquality(final RegistryEntry.BuilderHolder<M, T, ?> registryEntry) {
+ final Registry<M> registry = RegistryHelper.getRegistry().lookupOrThrow(registryEntry.mcKey());
2024-06-16 21:39:59 +02:00
+ for (final Map.Entry<ResourceKey<M>, M> entry : registry.entrySet()) {
2024-11-24 20:12:08 +01:00
+ final M built = registryEntry.fillBuilder(new Conversions(new RegistryOps.HolderLookupAdapter(RegistryHelper.getRegistry())), PaperRegistries.fromNms(entry.getKey()), entry.getValue()).build();
2024-06-16 21:39:59 +02:00
+ assertEquals(entry.getValue(), built);
+ }
+ }
+}
2024-10-21 00:06:54 +02:00
diff --git a/src/test/java/org/bukkit/registry/RegistryClassTest.java b/src/test/java/org/bukkit/registry/RegistryClassTest.java
2024-10-24 23:03:27 +02:00
index ea3d37f387bdb0dd5ae3fba9231ace31d0cebd64..c118c911972fe5a9f0c3e306009306f04ae2e821 100644
2024-10-21 00:06:54 +02:00
--- a/src/test/java/org/bukkit/registry/RegistryClassTest.java
+++ b/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/src/test/java/org/bukkit/support/extension/NormalExtension.java b/src/test/java/org/bukkit/support/extension/NormalExtension.java
index 8b5dcc4d0ecf7f9c51f73080c123ca08e31b1898..a809ea2f0d2b477c61857aa02a7e55024b2a7e0d 100644
--- a/src/test/java/org/bukkit/support/extension/NormalExtension.java
+++ b/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;
}