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/CraftRegistry.java b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java index b12b99253543445475b73a1d3d7c6364856b49e8..4fc02698a9312496e7f9bce1c64f317374d2a42f 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java @@ -131,6 +131,7 @@ public class CraftRegistry implements Registry { private final Class bukkitClass; 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 BiFunction minecraftToBukkit; private boolean init; @@ -175,6 +176,7 @@ public class CraftRegistry implements Registry { } this.cache.put(namespacedKey, bukkit); + this.byValue.put(bukkit, namespacedKey); // Paper - improve Registry return bukkit; } @@ -197,4 +199,11 @@ public class CraftRegistry implements Registry { return this.minecraftToBukkit.apply(namespacedKey, minecraft); } + + // 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/inventory/trim/CraftTrimMaterial.java b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java index 147950b8a1c853e1b52fdb29b26e839c2dd7bd01..8a20186618de8de8dd941a12aeb7d4f3b7e70c18 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java @@ -35,6 +35,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 88e9acd2084c5e7738017a91a454fc5407ab833b..dbb1619f8e8d98feb30f76beb8c23ebcd3ddca1f 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java @@ -35,6 +35,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 1c4966520b6401e6571aa44d5934dfa280bc80e3..010de6fbb75eb5d51639695d260f916072fdb22d 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 extends AbstractTestingBase { @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 extends AbstractTestingBase { }); } - 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); }