mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-15 14:13:56 +01:00
Improve Registry
This commit is contained in:
parent
a264c42314
commit
5b1ab02f39
9 changed files with 24 additions and 5 deletions
|
@ -83,6 +83,7 @@ public class CraftArt implements Art, Handleable<PaintingVariant> {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public NamespacedKey getKey() {
|
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;
|
return this.key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,6 +65,7 @@ public class CraftMusicInstrument extends MusicInstrument implements Handleable<
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public NamespacedKey getKey() {
|
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;
|
return this.key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -184,6 +184,7 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
||||||
|
|
||||||
private final Class<?> bukkitClass; // Paper - relax preload class
|
private final Class<?> bukkitClass; // Paper - relax preload class
|
||||||
private final Map<NamespacedKey, B> cache = new HashMap<>();
|
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 net.minecraft.core.Registry<M> minecraftRegistry;
|
||||||
private final io.papermc.paper.registry.entry.RegistryTypeMapper<M, B> minecraftToBukkit; // Paper - switch to Holder
|
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
|
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.cache.put(namespacedKey, bukkit);
|
||||||
|
this.byValue.put(bukkit, namespacedKey); // Paper - improve Registry
|
||||||
|
|
||||||
return bukkit;
|
return bukkit;
|
||||||
}
|
}
|
||||||
|
@ -279,4 +281,11 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
||||||
return this.minecraftToBukkit.convertDirectHolder(holder);
|
return this.minecraftToBukkit.convertDirectHolder(holder);
|
||||||
}
|
}
|
||||||
// Paper end - support Direct Holders
|
// 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,6 +64,7 @@ public class CraftSound implements Sound, Handleable<SoundEvent> {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public NamespacedKey getKey() {
|
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;
|
return this.key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,6 +67,7 @@ public class CraftPatternType implements PatternType, Handleable<BannerPattern>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NamespacedKey getKey() {
|
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;
|
return this.key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,7 @@ public class CraftStructure extends Structure implements Handleable<net.minecraf
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NamespacedKey getKey() {
|
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;
|
return this.key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,6 +54,7 @@ public class CraftTrimMaterial implements TrimMaterial, Handleable<net.minecraft
|
||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public NamespacedKey getKey() {
|
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;
|
return this.key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,6 +54,7 @@ public class CraftTrimPattern implements TrimPattern, Handleable<net.minecraft.w
|
||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public NamespacedKey getKey() {
|
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;
|
return this.key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,19 +49,22 @@ public class PerRegistryTest {
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@MethodSource("data")
|
@MethodSource("data")
|
||||||
public void testGet(Registry<?> registry) {
|
public <T extends Keyed> void testGet(Registry<T> registry) { // Paper - improve Registry
|
||||||
registry.forEach(element -> {
|
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()
|
// 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
|
// 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
|
@ParameterizedTest
|
||||||
@MethodSource("data")
|
@MethodSource("data")
|
||||||
public void testMatch(Registry<?> registry) {
|
public <T extends Keyed> void testMatch(Registry<T> registry) { // Paper - improve Registry
|
||||||
registry.forEach(element -> {
|
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.toString()); // namespace:key
|
||||||
this.assertSameMatchWithKeyMessage(registry, element, key.getKey()); // 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);
|
assertSame(element, registry.match(key), key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue