#1498: Make Attribute an interface and align names with the new minecraft ones
By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
@@ -12,19 +12,15 @@ import org.bukkit.attribute.Attribute;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
import org.bukkit.craftbukkit.legacy.FieldRename;
|
||||
import org.bukkit.craftbukkit.util.ApiVersion;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.craftbukkit.util.Handleable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class CraftAttribute {
|
||||
public class CraftAttribute implements Attribute, Handleable<AttributeBase> {
|
||||
|
||||
private static int count = 0;
|
||||
|
||||
public static Attribute minecraftToBukkit(AttributeBase minecraft) {
|
||||
Preconditions.checkArgument(minecraft != null);
|
||||
|
||||
IRegistry<AttributeBase> registry = CraftRegistry.getMinecraftRegistry(Registries.ATTRIBUTE);
|
||||
Attribute bukkit = Registry.ATTRIBUTE.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
|
||||
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return bukkit;
|
||||
return CraftRegistry.minecraftToBukkit(minecraft, Registries.ATTRIBUTE, Registry.ATTRIBUTE);
|
||||
}
|
||||
|
||||
public static Attribute minecraftHolderToBukkit(Holder<AttributeBase> minecraft) {
|
||||
@@ -45,10 +41,7 @@ public class CraftAttribute {
|
||||
}
|
||||
|
||||
public static AttributeBase bukkitToMinecraft(Attribute bukkit) {
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return CraftRegistry.getMinecraftRegistry(Registries.ATTRIBUTE)
|
||||
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
|
||||
return CraftRegistry.bukkitToMinecraft(bukkit);
|
||||
}
|
||||
|
||||
public static Holder<AttributeBase> bukkitToMinecraftHolder(Attribute bukkit) {
|
||||
@@ -69,4 +62,81 @@ public class CraftAttribute {
|
||||
|
||||
return bukkit.getKey().toString();
|
||||
}
|
||||
|
||||
private final NamespacedKey key;
|
||||
private final AttributeBase attributeBase;
|
||||
private final String name;
|
||||
private final int ordinal;
|
||||
|
||||
public CraftAttribute(NamespacedKey key, AttributeBase attributeBase) {
|
||||
this.key = key;
|
||||
this.attributeBase = attributeBase;
|
||||
// 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 attribute specific values.
|
||||
// Custom attributes will return the key with namespace. For a plugin this should look than like a new attribute
|
||||
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
|
||||
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
|
||||
this.name = key.getKey().toUpperCase(Locale.ROOT);
|
||||
} else {
|
||||
this.name = key.toString();
|
||||
}
|
||||
this.ordinal = count++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AttributeBase getHandle() {
|
||||
return attributeBase;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public NamespacedKey getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getTranslationKey() {
|
||||
return attributeBase.getDescriptionId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(@NotNull Attribute attribute) {
|
||||
return ordinal - attribute.ordinal();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int ordinal() {
|
||||
return ordinal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// For backwards compatibility
|
||||
return name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(other instanceof CraftAttribute otherAttribute)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return getKey().equals(otherAttribute.getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getKey().hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user