13
0
geforkt von Mirrors/Paper

Return null in getRegistry(Class) for unknown type (#11422)

The Bukkit#getRegistry(Class) method contract specifies that it returns
null for unknown registry types. The current implementation however
requires the passed class to be mappable to a known registry key.

For types like Material, which have a SimpleRegistry in bukkit's
Registry interface, no server side registry exists and such the type
cannot be mapped to a registry key.

The commit correctly returns null for types that are not mappable to a
registry key instead of throwing a NullPointerException.
Dieser Commit ist enthalten in:
Bjarne Koll 2024-09-21 19:57:08 +02:00
Ursprung f65939c6bd
Commit 1b174804c7

Datei anzeigen

@ -213,10 +213,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ @Deprecated(forRemoval = true) + @Deprecated(forRemoval = true)
+ @Override + @Override
+ public <T extends Keyed> @Nullable Registry<T> getRegistry(final Class<T> type) { + public <T extends Keyed> @Nullable Registry<T> getRegistry(final Class<T> type) {
+ final RegistryKey<T> registryKey; + final @Nullable RegistryKey<T> registryKey = byType(type);
+ final @Nullable RegistryEntry<?, T> entry; + // If our mapping from Class -> RegistryKey did not contain the passed type it was either a completely invalid type or a registry
+ registryKey = requireNonNull(byType(type), () -> type + " is not a valid registry type"); + // that merely exists as a SimpleRegistry in the org.bukkit.Registry type. We cannot return a registry for these, return null
+ entry = PaperRegistries.getEntry(registryKey); + // as per method contract in Bukkit#getRegistry.
+ if (registryKey == null) return null;
+
+ final @Nullable RegistryEntry<?, T> entry = PaperRegistries.getEntry(registryKey);
+ final @Nullable RegistryHolder<T> registry = (RegistryHolder<T>) this.registries.get(registryKey); + final @Nullable RegistryHolder<T> registry = (RegistryHolder<T>) this.registries.get(registryKey);
+ if (registry != null) { + if (registry != null) {
+ // if the registry exists, return right away. Since this is the "legacy" method, we return DelayedRegistry + // if the registry exists, return right away. Since this is the "legacy" method, we return DelayedRegistry