#1498: Make Attribute an interface and align names with the new minecraft ones

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2024-10-29 06:43:14 +11:00
parent 228b3effa4
commit 08c83835f3
9 changed files with 134 additions and 57 deletions

View File

@@ -22,6 +22,7 @@ import org.bukkit.attribute.Attribute;
import org.bukkit.block.Biome;
import org.bukkit.block.BlockType;
import org.bukkit.block.banner.PatternType;
import org.bukkit.craftbukkit.attribute.CraftAttribute;
import org.bukkit.craftbukkit.block.CraftBlockType;
import org.bukkit.craftbukkit.block.banner.CraftPatternType;
import org.bukkit.craftbukkit.damage.CraftDamageType;
@@ -131,6 +132,9 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
* @return the bukkit registry of the provided class
*/
public static <B extends Keyed> Registry<?> createRegistry(Class<? super B> bukkitClass, IRegistryCustom registryHolder) {
if (bukkitClass == Attribute.class) {
return new CraftRegistry<>(Attribute.class, registryHolder.lookupOrThrow(Registries.ATTRIBUTE), CraftAttribute::new, FieldRename.ATTRIBUTE_RENAME);
}
if (bukkitClass == Enchantment.class) {
return new CraftRegistry<>(Enchantment.class, registryHolder.lookupOrThrow(Registries.ENCHANTMENT), CraftEnchantment::new, FieldRename.ENCHANTMENT_RENAME);
}
@@ -214,10 +218,6 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
if (bClass == Particle.class) {
return bukkit.get(FieldRename.PARTICLE_TYPE_RENAME.apply(namespacedKey, apiVersion));
}
if (bClass == Attribute.class) {
return bukkit.get(FieldRename.ATTRIBUTE_RENAME.apply(namespacedKey, apiVersion));
}
}
return bukkit.get(namespacedKey);

View File

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

View File

@@ -354,7 +354,39 @@ public class FieldRename {
private static final FieldRenameData ATTRIBUTE_DATA = FieldRenameData.Builder.newBuilder()
.forAllVersions()
.withKeyRename()
.change("HORSE.JUMP_STRENGTH", "GENERIC.JUMP_STRENGTH")
.change("HORSE.JUMP_STRENGTH", "JUMP_STRENGTH")
.change("GENERIC.MAX_HEALTH", "MAX_HEALTH")
.change("GENERIC.FOLLOW_RANGE", "FOLLOW_RANGE")
.change("GENERIC.KNOCKBACK_RESISTANCE", "KNOCKBACK_RESISTANCE")
.change("GENERIC.MOVEMENT_SPEED", "MOVEMENT_SPEED")
.change("GENERIC.FLYING_SPEED", "FLYING_SPEED")
.change("GENERIC.ATTACK_DAMAGE", "ATTACK_DAMAGE")
.change("GENERIC.ATTACK_KNOCKBACK", "ATTACK_KNOCKBACK")
.change("GENERIC.ATTACK_SPEED", "ATTACK_SPEED")
.change("GENERIC.ARMOR", "ARMOR")
.change("GENERIC.ARMOR_TOUGHNESS", "ARMOR_TOUGHNESS")
.change("GENERIC.FALL_DAMAGE_MULTIPLIER", "FALL_DAMAGE_MULTIPLIER")
.change("GENERIC.LUCK", "LUCK")
.change("GENERIC.MAX_ABSORPTION", "MAX_ABSORPTION")
.change("GENERIC.SAFE_FALL_DISTANCE", "SAFE_FALL_DISTANCE")
.change("GENERIC.SCALE", "SCALE")
.change("GENERIC.STEP_HEIGHT", "STEP_HEIGHT")
.change("GENERIC.GRAVITY", "GRAVITY")
.change("GENERIC.JUMP_STRENGTH", "JUMP_STRENGTH")
.change("GENERIC.BURNING_TIME", "BURNING_TIME")
.change("GENERIC.EXPLOSION_KNOCKBACK_RESISTANCE", "EXPLOSION_KNOCKBACK_RESISTANCE")
.change("GENERIC.MOVEMENT_EFFICIENCY", "MOVEMENT_EFFICIENCY")
.change("GENERIC.OXYGEN_BONUS", "OXYGEN_BONUS")
.change("GENERIC.WATER_MOVEMENT_EFFICIENCY", "WATER_MOVEMENT_EFFICIENCY")
.change("GENERIC.TEMPT_RANGE", "TEMPT_RANGE")
.change("PLAYER.BLOCK_INTERACTION_RANGE", "BLOCK_INTERACTION_RANGE")
.change("PLAYER.ENTITY_INTERACTION_RANGE", "ENTITY_INTERACTION_RANGE")
.change("PLAYER.BLOCK_BREAK_SPEED", "BLOCK_BREAK_SPEED")
.change("PLAYER.MINING_EFFICIENCY", "MINING_EFFICIENCY")
.change("PLAYER.SNEAKING_SPEED", "SNEAKING_SPEED")
.change("PLAYER.SUBMERGED_MINING_SPEED", "SUBMERGED_MINING_SPEED")
.change("PLAYER.SWEEPING_DAMAGE_RATIO", "SWEEPING_DAMAGE_RATIO")
.change("ZOMBIE.SPAWN_REINFORCEMENTS", "SPAWN_REINFORCEMENTS")
.build();
public static final BiFunction<NamespacedKey, ApiVersion, NamespacedKey> ATTRIBUTE_RENAME = ATTRIBUTE_DATA::getReplacement;

View File

@@ -17,6 +17,7 @@ import java.util.stream.Collector;
import java.util.stream.Collectors;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.banner.PatternType;
import org.bukkit.craftbukkit.legacy.FieldRename;
import org.bukkit.craftbukkit.legacy.reroute.DoNotReroute;
@@ -44,6 +45,7 @@ public class EnumEvil {
static {
// Add Classes which got changed here
REGISTRIES.put(Attribute.class, new LegacyRegistryData(Registry.ATTRIBUTE, Attribute::valueOf));
REGISTRIES.put(Villager.Type.class, new LegacyRegistryData(Registry.VILLAGER_TYPE, Villager.Type::valueOf));
REGISTRIES.put(Villager.Profession.class, new LegacyRegistryData(Registry.VILLAGER_PROFESSION, Villager.Profession::valueOf));
REGISTRIES.put(Frog.Variant.class, new LegacyRegistryData(Registry.FROG_VARIANT, Frog.Variant::valueOf));

View File

@@ -87,7 +87,8 @@ public class Commodore {
"org/bukkit/entity/Frog$Variant", "NOP",
"org/bukkit/entity/Cat$Type", "NOP",
"org/bukkit/map/MapCursor$Type", "NOP",
"org/bukkit/block/banner/PatternType", "NOP"
"org/bukkit/block/banner/PatternType", "NOP",
"org/bukkit/attribute/Attribute", "NOP"
);
private final List<Reroute> reroutes = new ArrayList<>(); // only for testing

View File

@@ -51,7 +51,6 @@ import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.CraftFeatureFlag;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.attribute.CraftAttribute;
import org.bukkit.craftbukkit.block.data.CraftBlockData;
import org.bukkit.craftbukkit.damage.CraftDamageEffect;
import org.bukkit.craftbukkit.damage.CraftDamageSourceBuilder;
@@ -368,7 +367,7 @@ public final class CraftMagicNumbers implements UnsafeValues {
@Override
public String getTranslationKey(final Attribute attribute) {
return CraftAttribute.bukkitToMinecraft(attribute).getDescriptionId();
return attribute.getTranslationKey();
}
@Override