From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Wed, 20 Dec 2023 02:03:05 -0800 Subject: [PATCH] Improve Registry diff --git a/src/main/java/org/bukkit/craftbukkit/CraftArt.java b/src/main/java/org/bukkit/craftbukkit/CraftArt.java index 40af940193d0df66bbcdcf5f46132e304016a4d7..a32874e415264fe5516c951984862657a1422586 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftArt.java +++ b/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/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java b/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java index fb8f68f1aedfb26e4d95fe5bad87f0f2cc91c287..2838ef9d89ec8d7bd8981eff4a2ffe2c11ee9271 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java +++ b/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/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java index 7df1ce34c60ce14beb6acf8135a587e7b4e959b1..2df2f02d1c02f8b4297a60e468e6cd653b2bf271 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java +++ b/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/src/main/java/org/bukkit/craftbukkit/CraftSound.java b/src/main/java/org/bukkit/craftbukkit/CraftSound.java index 5b6e6902db19b078bb8598014a87c06f1267acc4..e24d784674f99bbcd0d9c21bc2629a9a97f5ea9b 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftSound.java +++ b/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/src/main/java/org/bukkit/craftbukkit/block/banner/CraftPatternType.java b/src/main/java/org/bukkit/craftbukkit/block/banner/CraftPatternType.java index 456a7b45c2ce577b334a1c1be7a690e4c59ee1b2..3addb382d00434bab9151c9edddc8999b986653c 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/banner/CraftPatternType.java +++ b/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/src/main/java/org/bukkit/craftbukkit/generator/structure/CraftStructure.java b/src/main/java/org/bukkit/craftbukkit/generator/structure/CraftStructure.java index 09fd006244320ddf1191240b2ad6a373bdf11e58..7ef679fdd2c0e44f357967e51606cdbef2d2cdb3 100644 --- a/src/main/java/org/bukkit/craftbukkit/generator/structure/CraftStructure.java +++ b/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/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java index caf7e4312e95e90dd0822355c8832006e69a2700..38578ef887227ecc8e8bba281eae17a849b4fbe6 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java +++ b/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/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java index f91577c9239c8d5ed4b72b23dde9c053b4beae0b..36df80a8be8a2485823f699e45c99674dbe71507 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java +++ b/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/src/test/java/org/bukkit/registry/PerRegistryTest.java b/src/test/java/org/bukkit/registry/PerRegistryTest.java index 18859eea522ff26cbefb5bbc5065b5369ed6c189..319e000519fd719cea0e6daf2ba9cfa67e6958a3 100644 --- a/src/test/java/org/bukkit/registry/PerRegistryTest.java +++ b/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); }