Fix Registry#getKey implementation
This commit is contained in:
@@ -7,7 +7,7 @@ import com.mojang.serialization.JsonOps;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.resources.RegistryOps;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
import org.bukkit.craftbukkit.util.Handleable;
|
||||
@@ -25,7 +25,7 @@ public interface Holderable<M> extends Handleable<M> {
|
||||
return this.getHolder().value();
|
||||
}
|
||||
|
||||
static <T extends Keyed, M> @Nullable T fromBukkitSerializationObject(final Object deserialized, final Codec<? extends Holder<M>> codec, final Registry<T> registry) { // TODO remove Keyed
|
||||
static <T extends org.bukkit.Keyed, M> @Nullable T fromBukkitSerializationObject(final Object deserialized, final Codec<? extends Holder<M>> codec, final Registry<T> registry) { // TODO remove Keyed
|
||||
return switch (deserialized) {
|
||||
case @Subst("key:value") final String string -> {
|
||||
if (!(Key.parseable(string))) {
|
||||
@@ -75,4 +75,12 @@ public interface Holderable<M> extends Handleable<M> {
|
||||
default String implToString() {
|
||||
return "%s{holder=%s}".formatted(this.getClass().getSimpleName(), this.getHolder().toString());
|
||||
}
|
||||
|
||||
default @Nullable NamespacedKey getKeyOrNull() {
|
||||
return this.getHolder().unwrapKey().map(MCUtil::fromResourceKey).orElse(null);
|
||||
}
|
||||
|
||||
default NamespacedKey getKey() {
|
||||
return MCUtil.fromResourceKey(this.getHolder().unwrapKey().orElseThrow(() -> new IllegalStateException("Cannot get key for this registry item, because it is not registered.")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package io.papermc.paper.util;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import java.util.Locale;
|
||||
import net.minecraft.core.Holder;
|
||||
import org.bukkit.Keyed;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.util.OldEnum;
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated
|
||||
@NullMarked
|
||||
public abstract class OldEnumHolderable<A extends OldEnum<A>, M> implements Holderable<M>, OldEnum<A>, Keyed {
|
||||
|
||||
private final Holder<M> holder;
|
||||
private final int ordinal;
|
||||
private final @Nullable String name;
|
||||
|
||||
protected OldEnumHolderable(final Holder<M> holder, final int ordinal) {
|
||||
this.holder = holder;
|
||||
this.ordinal = ordinal;
|
||||
if (holder instanceof final Holder.Reference<M> reference) {
|
||||
// For backwards compatibility, minecraft values will stile return the uppercase name without the namespace,
|
||||
// in case plugins use for example the name as key in a config file to receive registry item specific values.
|
||||
// Custom registry items will return the key with namespace. For a plugin this should look than like a new registry item
|
||||
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
|
||||
if (NamespacedKey.MINECRAFT.equals(reference.key().location().getNamespace())) {
|
||||
this.name = reference.key().location().getPath().toUpperCase(Locale.ROOT);
|
||||
} else {
|
||||
this.name = reference.key().location().toString();
|
||||
}
|
||||
} else {
|
||||
this.name = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Holder<M> getHolder() {
|
||||
return this.holder;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public int compareTo(A other) {
|
||||
this.checkIsReference();
|
||||
return this.ordinal - other.ordinal();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public String name() {
|
||||
this.checkIsReference();
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public int ordinal() {
|
||||
this.checkIsReference();
|
||||
return this.ordinal;
|
||||
}
|
||||
|
||||
private void checkIsReference() {
|
||||
Preconditions.checkState(this.holder.kind() == Holder.Kind.REFERENCE, "Cannot call method for this registry item, because it is not registered.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamespacedKey getKey() {
|
||||
return MCUtil.fromResourceKey(this.holder.unwrapKey().orElseThrow(() -> new IllegalStateException("Cannot get key for this registry item, because it is not registered.")));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
return this.implEquals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.implHashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.implToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user