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
|
|
|
|
|
|
|
|
|
2024-12-08 07:08:31 +01:00
|
|
|
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<PaintingVariant> {
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2023-12-23 22:16:27 +01:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
|
2024-12-08 21:50:55 +01:00
|
|
|
index 7df1ce34c60ce14beb6acf8135a587e7b4e959b1..2df2f02d1c02f8b4297a60e468e6cd653b2bf271 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-12-08 21:50:55 +01:00
|
|
|
@@ -184,6 +184,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;
|
2024-12-08 21:50:55 +01:00
|
|
|
private final io.papermc.paper.registry.entry.RegistryTypeMapper<M, B> minecraftToBukkit; // Paper - switch to Holder
|
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-12-08 21:50:55 +01:00
|
|
|
@@ -237,6 +238,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-12-08 21:50:55 +01:00
|
|
|
@@ -279,4 +281,11 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
|
|
|
return this.minecraftToBukkit.convertDirectHolder(holder);
|
2023-12-23 22:16:27 +01:00
|
|
|
}
|
2024-12-08 21:50:55 +01:00
|
|
|
// Paper end - support Direct Holders
|
2023-12-23 22:16:27 +01:00
|
|
|
+
|
|
|
|
+ // Paper start - improve Registry
|
|
|
|
+ @Override
|
|
|
|
+ public NamespacedKey getKey(final B value) {
|
|
|
|
+ return this.byValue.get(value);
|
|
|
|
+ }
|
|
|
|
+ // Paper end - improve Registry
|
|
|
|
}
|
2024-12-08 07:08:31 +01:00
|
|
|
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<SoundEvent> {
|
|
|
|
@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<BannerPattern>
|
|
|
|
|
|
|
|
@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<net.minecraf
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NamespacedKey getKey() {
|
|
|
|
+ if (true) return java.util.Objects.requireNonNull(org.bukkit.Registry.STRUCTURE.getKey(this), () -> this + " doesn't have a key"); // Paper
|
|
|
|
return this.key;
|
|
|
|
}
|
|
|
|
}
|
2023-12-23 22:16:27 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|