Improve Registry

This commit is contained in:
Jake Potrebic 2023-12-20 02:03:05 -08:00
parent a264c42314
commit 5b1ab02f39
9 changed files with 24 additions and 5 deletions

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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);
}