aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0899-Improve-Registry.patch
blob: 4e6b1187701f0636df24f8f93e3518327dd5598f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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/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;
     }
 
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<B extends Keyed, M> implements Registry<B> {
 
     private final Class<?> bukkitClass; // Paper - relax preload class
     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 io.papermc.paper.registry.entry.RegistryTypeMapper<M, B> minecraftToBukkit; // Paper - switch to Holder
     private final BiFunction<NamespacedKey, ApiVersion, NamespacedKey> serializationUpdater; // Paper - rename to make it *clear* what it is *only* for
@@ -237,6 +238,7 @@ 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;
     }
@@ -279,4 +281,11 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
         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<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;
     }
 }
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<net.minecraft
     @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;
     }
 
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<net.minecraft.w
     @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;
     }
 
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 <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
@@ -72,7 +75,7 @@ public class PerRegistryTest {
         });
     }
 
-    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);
     }