From 0dc6316e04f7f23d86375e806143a994a487df00 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 23 Nov 2024 11:26:51 -0800 Subject: [PATCH] Support tags for more SimpleRegistry (#11607) --- ...gistryAccess-for-managing-registries.patch | 66 ++++++++++++++++++- .../Proxy-ItemStack-to-CraftItemStack.patch | 4 +- patches/api/Registry-Modification-API.patch | 35 ---------- ...gistryAccess-for-managing-Registries.patch | 55 ++++++++++++++-- .../Add-registry-entry-and-builders.patch | 4 +- patches/server/DataComponent-API.patch | 4 +- .../Proxy-ItemStack-to-CraftItemStack.patch | 2 +- .../server/Registry-Modification-API.patch | 60 +++++++++-------- 8 files changed, 155 insertions(+), 75 deletions(-) diff --git a/patches/api/Add-RegistryAccess-for-managing-registries.patch b/patches/api/Add-RegistryAccess-for-managing-registries.patch index 7d74ff555f..9f42e4bc06 100644 --- a/patches/api/Add-RegistryAccess-for-managing-registries.patch +++ b/patches/api/Add-RegistryAccess-for-managing-registries.patch @@ -206,6 +206,43 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 public static Registry getRegistry(@NotNull Class tClass) { return server.getRegistry(tClass); } +diff --git a/src/main/java/org/bukkit/Particle.java b/src/main/java/org/bukkit/Particle.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/org/bukkit/Particle.java ++++ b/src/main/java/org/bukkit/Particle.java +@@ -0,0 +0,0 @@ public enum Particle implements Keyed { + + private final NamespacedKey key; + private final Class dataType; +- final boolean register; ++ // Paper - all particles are registered + + Particle(String key) { + this(key, Void.class); + } + +- Particle(String key, boolean register) { +- this(key, Void.class, register); +- } ++ // Paper - all particles are registered + + Particle(String key, /*@NotNull*/ Class data) { +- this(key, data, true); +- } +- +- Particle(String key, /*@NotNull*/ Class data, boolean register) { ++ // Paper - all particles are registered + if (key != null) { + this.key = NamespacedKey.minecraft(key); + } else { + this.key = null; + } + dataType = data; +- this.register = register; ++ // Paper - all particles are registered + } + + /** diff --git a/src/main/java/org/bukkit/Registry.java b/src/main/java/org/bukkit/Registry.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/Registry.java @@ -274,7 +311,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 /** * Server entity types. * -@@ -0,0 +0,0 @@ public interface Registry extends Iterable { + * @see EntityType + */ +- Registry ENTITY_TYPE = new SimpleRegistry<>(EntityType.class, (entity) -> entity != EntityType.UNKNOWN); ++ Registry ENTITY_TYPE = io.papermc.paper.registry.RegistryAccess.registryAccess().getRegistry(io.papermc.paper.registry.RegistryKey.ENTITY_TYPE); // Paper + /** * Server instruments. * * @see MusicInstrument @@ -311,6 +352,20 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 /** * Server particles. * + * @see Particle + */ +- Registry PARTICLE_TYPE = new SimpleRegistry<>(Particle.class, (par) -> par.register); ++ Registry PARTICLE_TYPE = io.papermc.paper.registry.RegistryAccess.registryAccess().getRegistry(io.papermc.paper.registry.RegistryKey.PARTICLE_TYPE); // Paper + /** + * Server potions. + * + * @see PotionType + */ +- Registry POTION = new SimpleRegistry<>(PotionType.class); ++ Registry POTION = io.papermc.paper.registry.RegistryAccess.registryAccess().getRegistry(io.papermc.paper.registry.RegistryKey.POTION); // Paper + /** + * Server statistics. + * @@ -0,0 +0,0 @@ public interface Registry extends Iterable { * Server structures. * @@ -428,6 +483,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 /** * Get the object by its key. * +@@ -0,0 +0,0 @@ public interface Registry extends Iterable { + return (namespacedKey != null) ? get(namespacedKey) : null; + } + +- static final class SimpleRegistry & Keyed> implements Registry { ++ class SimpleRegistry & Keyed> implements Registry { // Paper - remove final + + private final Class type; + private final Map map; diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/Server.java diff --git a/patches/api/Proxy-ItemStack-to-CraftItemStack.patch b/patches/api/Proxy-ItemStack-to-CraftItemStack.patch index 59370f5889..5e367bf7f4 100644 --- a/patches/api/Proxy-ItemStack-to-CraftItemStack.patch +++ b/patches/api/Proxy-ItemStack-to-CraftItemStack.patch @@ -9,9 +9,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 --- a/src/main/java/org/bukkit/UnsafeValues.java +++ b/src/main/java/org/bukkit/UnsafeValues.java @@ -0,0 +0,0 @@ public interface UnsafeValues { - @NotNull java.util.List computeTooltipLines(@NotNull ItemStack itemStack, @NotNull io.papermc.paper.inventory.tooltip.TooltipContext tooltipContext, @Nullable org.bukkit.entity.Player player); // Paper - expose itemstack tooltip lines + // Paper end - lifecycle event API - io.papermc.paper.registry.tag.@Nullable Tag getTag(io.papermc.paper.registry.tag.@NotNull TagKey tagKey); // Paper - hack to get tags for non-server backed registries + @NotNull java.util.List computeTooltipLines(@NotNull ItemStack itemStack, @NotNull io.papermc.paper.inventory.tooltip.TooltipContext tooltipContext, @Nullable org.bukkit.entity.Player player); // Paper - expose itemstack tooltip lines + + ItemStack createEmptyStack(); // Paper - proxy ItemStack } diff --git a/patches/api/Registry-Modification-API.patch b/patches/api/Registry-Modification-API.patch index e87cdc5a81..933acbbfc2 100644 --- a/patches/api/Registry-Modification-API.patch +++ b/patches/api/Registry-Modification-API.patch @@ -875,38 +875,3 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 /** * Get the object by its key. * -@@ -0,0 +0,0 @@ public interface Registry extends Iterable { - return value.getKey(); - } - // Paper end - improve Registry -+ -+ // Paper start - RegistrySet API -+ @SuppressWarnings("deprecation") -+ @Override -+ public boolean hasTag(final io.papermc.paper.registry.tag.@NotNull TagKey key) { -+ return Bukkit.getUnsafe().getTag(key) != null; -+ } -+ -+ @SuppressWarnings("deprecation") -+ @Override -+ public io.papermc.paper.registry.tag.@NotNull Tag getTag(final io.papermc.paper.registry.tag.@NotNull TagKey key) { -+ final io.papermc.paper.registry.tag.Tag tag = Bukkit.getUnsafe().getTag(key); -+ if (tag == null) { -+ throw new java.util.NoSuchElementException("No tag " + key + " found"); -+ } -+ return tag; -+ } -+ // Paper end - RegistrySet API - } - } -diff --git a/src/main/java/org/bukkit/UnsafeValues.java b/src/main/java/org/bukkit/UnsafeValues.java -index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 ---- a/src/main/java/org/bukkit/UnsafeValues.java -+++ b/src/main/java/org/bukkit/UnsafeValues.java -@@ -0,0 +0,0 @@ public interface UnsafeValues { - // Paper end - lifecycle event API - - @NotNull java.util.List computeTooltipLines(@NotNull ItemStack itemStack, @NotNull io.papermc.paper.inventory.tooltip.TooltipContext tooltipContext, @Nullable org.bukkit.entity.Player player); // Paper - expose itemstack tooltip lines -+ -+ io.papermc.paper.registry.tag.@Nullable Tag getTag(io.papermc.paper.registry.tag.@NotNull TagKey tagKey); // Paper - hack to get tags for non-server backed registries - } diff --git a/patches/server/Add-RegistryAccess-for-managing-Registries.patch b/patches/server/Add-RegistryAccess-for-managing-Registries.patch index 015cf5d030..8ca600b34d 100644 --- a/patches/server/Add-RegistryAccess-for-managing-Registries.patch +++ b/patches/server/Add-RegistryAccess-for-managing-Registries.patch @@ -18,6 +18,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 @@ -0,0 +0,0 @@ +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 java.util.Collections; @@ -125,16 +126,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + entry(Registries.INSTRUMENT, RegistryKey.INSTRUMENT, MusicInstrument.class, CraftMusicInstrument::new).delayed(), + + // api-only -+ apiOnly(Registries.ENTITY_TYPE, RegistryKey.ENTITY_TYPE, () -> org.bukkit.Registry.ENTITY_TYPE), -+ apiOnly(Registries.PARTICLE_TYPE, RegistryKey.PARTICLE_TYPE, () -> org.bukkit.Registry.PARTICLE_TYPE), -+ apiOnly(Registries.POTION, RegistryKey.POTION, () -> org.bukkit.Registry.POTION), ++ apiOnly(Registries.ENTITY_TYPE, RegistryKey.ENTITY_TYPE, PaperSimpleRegistry::entityType), ++ apiOnly(Registries.PARTICLE_TYPE, RegistryKey.PARTICLE_TYPE, PaperSimpleRegistry::particleType), ++ apiOnly(Registries.POTION, RegistryKey.POTION, PaperSimpleRegistry::potion), + apiOnly(Registries.MEMORY_MODULE_TYPE, RegistryKey.MEMORY_MODULE_TYPE, () -> (org.bukkit.Registry>) (org.bukkit.Registry) org.bukkit.Registry.MEMORY_MODULE_TYPE) + ); + final Map, RegistryEntry> byRegistryKey = new IdentityHashMap<>(REGISTRY_ENTRIES.size()); + final Map, RegistryEntry> byResourceKey = new IdentityHashMap<>(REGISTRY_ENTRIES.size()); + for (final RegistryEntry entry : REGISTRY_ENTRIES) { -+ byRegistryKey.put(entry.apiKey(), entry); -+ byResourceKey.put(entry.mcKey(), entry); ++ Preconditions.checkState(byRegistryKey.put(entry.apiKey(), entry) == null, "Duplicate api registry key: %s", entry.apiKey()); ++ Preconditions.checkState(byResourceKey.put(entry.mcKey(), entry) == null, "Duplicate mc registry key: %s", entry.mcKey()); + } + BY_REGISTRY_KEY = Collections.unmodifiableMap(byRegistryKey); + BY_RESOURCE_KEY = Collections.unmodifiableMap(byResourceKey); @@ -305,6 +306,50 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + return (RegistryKey) LegacyRegistryIdentifiers.CLASS_TO_KEY_MAP.get(type); + } +} +diff --git a/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java b/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java +new file mode 100644 +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java +@@ -0,0 +0,0 @@ ++package io.papermc.paper.registry; ++ ++import java.util.function.Predicate; ++import net.minecraft.core.registries.BuiltInRegistries; ++import org.bukkit.Keyed; ++import org.bukkit.Particle; ++import org.bukkit.Registry; ++import org.bukkit.entity.EntityType; ++import org.bukkit.potion.PotionType; ++import org.jspecify.annotations.NullMarked; ++ ++@NullMarked ++public class PaperSimpleRegistry & Keyed, M> extends Registry.SimpleRegistry { ++ ++ static Registry entityType() { ++ return new PaperSimpleRegistry<>(EntityType.class, entity -> entity != EntityType.UNKNOWN, BuiltInRegistries.ENTITY_TYPE); ++ } ++ ++ static Registry particleType() { ++ return new PaperSimpleRegistry<>(Particle.class, BuiltInRegistries.PARTICLE_TYPE); ++ } ++ ++ static Registry potion() { ++ return new PaperSimpleRegistry<>(PotionType.class, BuiltInRegistries.POTION); ++ } ++ ++ private final net.minecraft.core.Registry nmsRegistry; ++ ++ protected PaperSimpleRegistry(final Class type, final net.minecraft.core.Registry nmsRegistry) { ++ super(type); ++ this.nmsRegistry = nmsRegistry; ++ } ++ ++ public PaperSimpleRegistry(final Class type, final Predicate predicate, final net.minecraft.core.Registry nmsRegistry) { ++ super(type, predicate); ++ this.nmsRegistry = nmsRegistry; ++ } ++} diff --git a/src/main/java/io/papermc/paper/registry/RegistryHolder.java b/src/main/java/io/papermc/paper/registry/RegistryHolder.java new file mode 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 diff --git a/patches/server/Add-registry-entry-and-builders.patch b/patches/server/Add-registry-entry-and-builders.patch index b54392743e..a607a80cc2 100644 --- a/patches/server/Add-registry-entry-and-builders.patch +++ b/patches/server/Add-registry-entry-and-builders.patch @@ -9,9 +9,9 @@ diff --git a/src/main/java/io/papermc/paper/registry/PaperRegistries.java b/src/ index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/io/papermc/paper/registry/PaperRegistries.java +++ b/src/main/java/io/papermc/paper/registry/PaperRegistries.java -@@ -0,0 +0,0 @@ - package io.papermc.paper.registry; +@@ -0,0 +0,0 @@ package io.papermc.paper.registry; + import com.google.common.base.Preconditions; import io.papermc.paper.adventure.PaperAdventure; +import io.papermc.paper.registry.data.PaperEnchantmentRegistryEntry; +import io.papermc.paper.registry.data.PaperGameEventRegistryEntry; diff --git a/patches/server/DataComponent-API.patch b/patches/server/DataComponent-API.patch index ea53aaaaa3..ca10995882 100644 --- a/patches/server/DataComponent-API.patch +++ b/patches/server/DataComponent-API.patch @@ -3592,9 +3592,9 @@ diff --git a/src/main/java/io/papermc/paper/registry/PaperRegistries.java b/src/ index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/io/papermc/paper/registry/PaperRegistries.java +++ b/src/main/java/io/papermc/paper/registry/PaperRegistries.java -@@ -0,0 +0,0 @@ - package io.papermc.paper.registry; +@@ -0,0 +0,0 @@ package io.papermc.paper.registry; + import com.google.common.base.Preconditions; import io.papermc.paper.adventure.PaperAdventure; +import io.papermc.paper.datacomponent.DataComponentType; +import io.papermc.paper.datacomponent.PaperComponentType; diff --git a/patches/server/Proxy-ItemStack-to-CraftItemStack.patch b/patches/server/Proxy-ItemStack-to-CraftItemStack.patch index 799a838349..0eae0d1810 100644 --- a/patches/server/Proxy-ItemStack-to-CraftItemStack.patch +++ b/patches/server/Proxy-ItemStack-to-CraftItemStack.patch @@ -210,7 +210,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java @@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues { } - // Paper end - hack to get tags for non server-backed registries + // Paper end - lifecycle event API + // Paper start - proxy ItemStack + @Override diff --git a/patches/server/Registry-Modification-API.patch b/patches/server/Registry-Modification-API.patch index 62e09a7d78..986d20032e 100644 --- a/patches/server/Registry-Modification-API.patch +++ b/patches/server/Registry-Modification-API.patch @@ -15,7 +15,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 --- a/src/main/java/io/papermc/paper/registry/PaperRegistries.java +++ b/src/main/java/io/papermc/paper/registry/PaperRegistries.java @@ -0,0 +0,0 @@ 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; @@ -286,6 +286,38 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + return this.freezeEventTypes.getOrCreate(type.registryKey(), RegistryLifecycleEventType::new); + } +} +diff --git a/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java b/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java ++++ b/src/main/java/io/papermc/paper/registry/PaperSimpleRegistry.java +@@ -0,0 +0,0 @@ + 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; +@@ -0,0 +0,0 @@ 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/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java b/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java new file mode 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 @@ -1365,32 +1397,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + // Paper end - RegistrySet API } -diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 ---- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java -@@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues { - } - // Paper end - lifecycle event API - -+ // Paper start - hack to get tags for non server-backed registries -+ @Override -+ public io.papermc.paper.registry.tag.Tag getTag(final io.papermc.paper.registry.tag.TagKey tagKey) { // TODO remove Keyed -+ if (tagKey.registryKey() != io.papermc.paper.registry.RegistryKey.ENTITY_TYPE && tagKey.registryKey() != io.papermc.paper.registry.RegistryKey.FLUID) { -+ throw new UnsupportedOperationException(tagKey.registryKey() + " doesn't have tags"); -+ } -+ final net.minecraft.resources.ResourceKey> nmsKey = io.papermc.paper.registry.PaperRegistries.registryToNms(tagKey.registryKey()); -+ final net.minecraft.core.Registry nmsRegistry = org.bukkit.craftbukkit.CraftRegistry.getMinecraftRegistry().lookupOrThrow(nmsKey); -+ return nmsRegistry -+ .get(io.papermc.paper.registry.PaperRegistries.toNms(tagKey)) -+ .map(named -> new io.papermc.paper.registry.set.NamedRegistryKeySetImpl<>(tagKey, named)) -+ .orElse(null); -+ } -+ // Paper end - hack to get tags for non server-backed registries -+ - /** - * This helper class represents the different NBT Tags. - *

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..0000000000000000000000000000000000000000