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
|
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
|
|
|
|
@@ -0,0 +0,0 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
|
|
|
|
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;
|
|
|
|
private boolean init;
|
|
|
|
@@ -0,0 +0,0 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cache.put(namespacedKey, bukkit);
|
|
|
|
+ this.byValue.put(bukkit, namespacedKey); // Paper - improve Registry
|
|
|
|
|
|
|
|
return bukkit;
|
|
|
|
}
|
|
|
|
@@ -0,0 +0,0 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
|
|
|
|
|
|
|
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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java
|
2024-01-26 20:17:06 +01:00
|
|
|
@@ -0,0 +0,0 @@ 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
|
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimPattern.java
|
2024-01-26 20:17:06 +01:00
|
|
|
@@ -0,0 +0,0 @@ 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
|
2023-12-23 22:16:27 +01:00
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 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
|
2023-12-23 22:16:27 +01:00
|
|
|
@@ -0,0 +0,0 @@ public class PerRegistryTest extends AbstractTestingBase {
|
|
|
|
|
|
|
|
@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
|
|
|
|
@@ -0,0 +0,0 @@ public class PerRegistryTest extends AbstractTestingBase {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- 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);
|
|
|
|
}
|
|
|
|
|