From 5b1ab02f3927e02833a0212c7b284e04a69298bb Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Wed, 20 Dec 2023 02:03:05 -0800 Subject: [PATCH] Improve Registry --- .../main/java/org/bukkit/craftbukkit/CraftArt.java | 1 + .../bukkit/craftbukkit/CraftMusicInstrument.java | 1 + .../java/org/bukkit/craftbukkit/CraftRegistry.java | 9 +++++++++ .../java/org/bukkit/craftbukkit/CraftSound.java | 1 + .../craftbukkit/block/banner/CraftPatternType.java | 1 + .../generator/structure/CraftStructure.java | 1 + .../inventory/trim/CraftTrimMaterial.java | 1 + .../inventory/trim/CraftTrimPattern.java | 1 + .../java/org/bukkit/registry/PerRegistryTest.java | 13 ++++++++----- 9 files changed, 24 insertions(+), 5 deletions(-) diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftArt.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftArt.java index 40af940193..a32874e415 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftArt.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftArt.java @@ -83,6 +83,7 @@ public class CraftArt implements Art, Handleable { @NotNull @Override public NamespacedKey getKey() { + if (true) return java.util.Objects.requireNonNull(org.bukkit.Registry.ART.getKey(this), () -> this + " doesn't have a key"); // Paper return this.key; } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java index fb8f68f1ae..2838ef9d89 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java @@ -65,6 +65,7 @@ public class CraftMusicInstrument extends MusicInstrument implements Handleable< @NotNull @Override public NamespacedKey getKey() { + if (true) return java.util.Objects.requireNonNull(org.bukkit.Registry.INSTRUMENT.getKey(this), () -> this + " doesn't have a key"); // Paper return this.key; } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java index f3b971d505..e23e92331a 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java @@ -184,6 +184,7 @@ public class CraftRegistry implements Registry { private final Class bukkitClass; // Paper - relax preload class private final Map cache = new HashMap<>(); + private final Map byValue = new java.util.IdentityHashMap<>(); // Paper - improve Registry private final net.minecraft.core.Registry minecraftRegistry; private final io.papermc.paper.registry.entry.RegistryTypeMapper minecraftToBukkit; // Paper - switch to Holder private final BiFunction serializationUpdater; // Paper - rename to make it *clear* what it is *only* for @@ -237,6 +238,7 @@ public class CraftRegistry implements Registry { } this.cache.put(namespacedKey, bukkit); + this.byValue.put(bukkit, namespacedKey); // Paper - improve Registry return bukkit; } @@ -279,4 +281,11 @@ public class CraftRegistry implements Registry { return this.minecraftToBukkit.convertDirectHolder(holder); } // Paper end - support Direct Holders + + // Paper start - improve Registry + @Override + public NamespacedKey getKey(final B value) { + return this.byValue.get(value); + } + // Paper end - improve Registry } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftSound.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftSound.java index 5b6e6902db..e24d784674 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftSound.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftSound.java @@ -64,6 +64,7 @@ public class CraftSound implements Sound, Handleable { @NotNull @Override public NamespacedKey getKey() { + if (true) return java.util.Objects.requireNonNull(org.bukkit.Registry.SOUNDS.getKey(this), () -> this + " doesn't have a key"); // Paper return this.key; } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/block/banner/CraftPatternType.java b/paper-server/src/main/java/org/bukkit/craftbukkit/block/banner/CraftPatternType.java index 456a7b45c2..3addb382d0 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/block/banner/CraftPatternType.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/block/banner/CraftPatternType.java @@ -67,6 +67,7 @@ public class CraftPatternType implements PatternType, Handleable @Override public NamespacedKey getKey() { + if (true) return java.util.Objects.requireNonNull(org.bukkit.Registry.BANNER_PATTERN.getKey(this), () -> this + " doesn't have a key"); // Paper return this.key; } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/generator/structure/CraftStructure.java b/paper-server/src/main/java/org/bukkit/craftbukkit/generator/structure/CraftStructure.java index 09fd006244..7ef679fdd2 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/generator/structure/CraftStructure.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/generator/structure/CraftStructure.java @@ -42,6 +42,7 @@ public class CraftStructure extends Structure implements Handleable this + " doesn't have a key"); // Paper return this.key; } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java index caf7e4312e..38578ef887 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java @@ -54,6 +54,7 @@ public class CraftTrimMaterial implements TrimMaterial, Handleable this + " doesn't have a key"); // Paper return this.key; } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java index f91577c923..36df80a8be 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java @@ -54,6 +54,7 @@ public class CraftTrimPattern implements TrimPattern, Handleable this + " doesn't have a key"); // Paper return this.key; } diff --git a/paper-server/src/test/java/org/bukkit/registry/PerRegistryTest.java b/paper-server/src/test/java/org/bukkit/registry/PerRegistryTest.java index 18859eea52..319e000519 100644 --- a/paper-server/src/test/java/org/bukkit/registry/PerRegistryTest.java +++ b/paper-server/src/test/java/org/bukkit/registry/PerRegistryTest.java @@ -49,19 +49,22 @@ public class PerRegistryTest { @ParameterizedTest @MethodSource("data") - public void testGet(Registry registry) { + public void testGet(Registry registry) { // Paper - improve Registry registry.forEach(element -> { + NamespacedKey key = registry.getKey(element); // Paper - improve Registry + assertNotNull(key); // Paper - improve Registry // Values in the registry should be referentially equal to what is returned with #get() // This ensures that new instances are not created each time #get() is invoked - assertSame(element, registry.get(element.getKey())); + assertSame(element, registry.get(key)); // Paper - improve Registry }); } @ParameterizedTest @MethodSource("data") - public void testMatch(Registry registry) { + public void testMatch(Registry registry) { // Paper - improve Registry registry.forEach(element -> { - NamespacedKey key = element.getKey(); + NamespacedKey key = registry.getKey(element); // Paper - improve Registry + assertNotNull(key); // Paper - improve Registry this.assertSameMatchWithKeyMessage(registry, element, key.toString()); // namespace:key this.assertSameMatchWithKeyMessage(registry, element, key.getKey()); // key @@ -72,7 +75,7 @@ public class PerRegistryTest { }); } - private void assertSameMatchWithKeyMessage(Registry registry, Keyed element, String key) { + private void assertSameMatchWithKeyMessage(Registry registry, T element, String key) { // Paper - improve Registry assertSame(element, registry.match(key), key); }