2023-12-23 22:16:27 +01:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Jake Potrebic <jake.m.potrebic@gmail.com>
|
|
|
|
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
|
2024-10-24 12:11:32 +02:00
|
|
|
index 09929f580164abcd1c04061d04c6aa992767e256..aa66fd8dca886c1f064d8cb4a3d15c2086c1719a 100644
|
2023-12-23 22:16:27 +01:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
|
2024-09-15 21:39:53 +02:00
|
|
|
@@ -155,6 +155,7 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
2023-12-23 22:16:27 +01:00
|
|
|
|
2024-05-04 20:22:35 +02:00
|
|
|
private final Class<?> bukkitClass; // Paper - relax preload class
|
2023-12-23 22:16:27 +01: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;
|
2024-05-11 23:48:37 +02:00
|
|
|
private final BiFunction<NamespacedKey, ApiVersion, NamespacedKey> serializationUpdater; // Paper - rename to make it *clear* what it is *only* for
|
2024-09-15 21:39:53 +02:00
|
|
|
@@ -203,6 +204,7 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
2023-12-23 22:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.cache.put(namespacedKey, bukkit);
|
|
|
|
+ this.byValue.put(bukkit, namespacedKey); // Paper - improve Registry
|
|
|
|
|
|
|
|
return bukkit;
|
|
|
|
}
|
2024-10-21 00:06:54 +02:00
|
|
|
@@ -235,4 +237,11 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
2023-12-23 22:16:27 +01:00
|
|
|
|
|
|
|
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
|
2024-10-24 12:11:32 +02:00
|
|
|
index caf7e4312e95e90dd0822355c8832006e69a2700..38578ef887227ecc8e8bba281eae17a849b4fbe6 100644
|
2023-12-23 22:16:27 +01:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java
|
2024-04-25 01:25:57 +02:00
|
|
|
@@ -54,6 +54,7 @@ public class CraftTrimMaterial implements TrimMaterial, Handleable<net.minecraft
|
2023-12-23 22:16:27 +01:00
|
|
|
@Override
|
|
|
|
@NotNull
|
|
|
|
public NamespacedKey getKey() {
|
|
|
|
+ if (true) return java.util.Objects.requireNonNull(org.bukkit.Registry.TRIM_MATERIAL.getKey(this), () -> this + " doesn't have a key"); // Paper
|
|
|
|
return this.key;
|
|
|
|
}
|
2024-04-06 21:53:39 +02:00
|
|
|
|
2023-12-23 22:16:27 +01:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java
|
2024-10-24 12:11:32 +02:00
|
|
|
index f91577c9239c8d5ed4b72b23dde9c053b4beae0b..36df80a8be8a2485823f699e45c99674dbe71507 100644
|
2023-12-23 22:16:27 +01:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java
|
2024-04-25 01:25:57 +02:00
|
|
|
@@ -54,6 +54,7 @@ public class CraftTrimPattern implements TrimPattern, Handleable<net.minecraft.w
|
2023-12-23 22:16:27 +01:00
|
|
|
@Override
|
|
|
|
@NotNull
|
|
|
|
public NamespacedKey getKey() {
|
|
|
|
+ if (true) return java.util.Objects.requireNonNull(org.bukkit.Registry.TRIM_PATTERN.getKey(this), () -> this + " doesn't have a key"); // Paper
|
|
|
|
return this.key;
|
|
|
|
}
|
2024-04-06 21:53:39 +02:00
|
|
|
|
2024-01-26 20:17:06 +01:00
|
|
|
diff --git a/src/test/java/org/bukkit/registry/PerRegistryTest.java b/src/test/java/org/bukkit/registry/PerRegistryTest.java
|
2024-10-21 00:06:54 +02:00
|
|
|
index 18859eea522ff26cbefb5bbc5065b5369ed6c189..319e000519fd719cea0e6daf2ba9cfa67e6958a3 100644
|
2024-01-26 20:17:06 +01:00
|
|
|
--- a/src/test/java/org/bukkit/registry/PerRegistryTest.java
|
|
|
|
+++ b/src/test/java/org/bukkit/registry/PerRegistryTest.java
|
2024-10-21 00:06:54 +02:00
|
|
|
@@ -49,19 +49,22 @@ public class PerRegistryTest {
|
2023-12-23 22:16:27 +01:00
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("data")
|
|
|
|
- public void testGet(Registry<?> registry) {
|
|
|
|
+ public <T extends Keyed> void testGet(Registry<T> 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 <T extends Keyed> void testMatch(Registry<T> 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
|
2024-10-21 00:06:54 +02:00
|
|
|
@@ -72,7 +75,7 @@ public class PerRegistryTest {
|
2023-12-23 22:16:27 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- private void assertSameMatchWithKeyMessage(Registry<?> registry, Keyed element, String key) {
|
|
|
|
+ private <T extends Keyed> void assertSameMatchWithKeyMessage(Registry<T> registry, T element, String key) { // Paper - improve Registry
|
|
|
|
assertSame(element, registry.match(key), key);
|
|
|
|
}
|
|
|
|
|