diff --git a/paper-api/src/main/java/org/bukkit/Registry.java b/paper-api/src/main/java/org/bukkit/Registry.java index 30ba3daba..eb03e39e9 100644 --- a/paper-api/src/main/java/org/bukkit/Registry.java +++ b/paper-api/src/main/java/org/bukkit/Registry.java @@ -93,7 +93,7 @@ public interface Registry extends Iterable { * * @see Attribute */ - Registry ATTRIBUTE = new SimpleRegistry<>(Attribute.class); + Registry ATTRIBUTE = Objects.requireNonNull(Bukkit.getRegistry(Attribute.class), "No registry present for Attribute. This is a bug."); /** * Server banner patterns. * diff --git a/paper-api/src/main/java/org/bukkit/UnsafeValues.java b/paper-api/src/main/java/org/bukkit/UnsafeValues.java index 0bb3637d7..dd049e9a4 100644 --- a/paper-api/src/main/java/org/bukkit/UnsafeValues.java +++ b/paper-api/src/main/java/org/bukkit/UnsafeValues.java @@ -98,6 +98,7 @@ public interface UnsafeValues { String getTranslationKey(ItemStack itemStack); + @Deprecated(since = "1.21.3", forRemoval = true) String getTranslationKey(Attribute attribute); @Nullable diff --git a/paper-api/src/main/java/org/bukkit/attribute/Attribute.java b/paper-api/src/main/java/org/bukkit/attribute/Attribute.java index e5a9d1692..951c23491 100644 --- a/paper-api/src/main/java/org/bukkit/attribute/Attribute.java +++ b/paper-api/src/main/java/org/bukkit/attribute/Attribute.java @@ -1,160 +1,175 @@ package org.bukkit.attribute; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import java.util.Locale; import org.bukkit.Bukkit; import org.bukkit.Keyed; import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.Translatable; +import org.bukkit.util.OldEnum; import org.jetbrains.annotations.NotNull; /** * Types of attributes which may be present on an {@link Attributable}. */ -public enum Attribute implements Keyed, Translatable { +public interface Attribute extends OldEnum, Keyed, Translatable { /** * Maximum health of an Entity. */ - GENERIC_MAX_HEALTH("max_health"), + Attribute MAX_HEALTH = getAttribute("max_health"); /** * Range at which an Entity will follow others. */ - GENERIC_FOLLOW_RANGE("follow_range"), + Attribute FOLLOW_RANGE = getAttribute("follow_range"); /** * Resistance of an Entity to knockback. */ - GENERIC_KNOCKBACK_RESISTANCE("knockback_resistance"), + Attribute KNOCKBACK_RESISTANCE = getAttribute("knockback_resistance"); /** * Movement speed of an Entity. */ - GENERIC_MOVEMENT_SPEED("movement_speed"), + Attribute MOVEMENT_SPEED = getAttribute("movement_speed"); /** * Flying speed of an Entity. */ - GENERIC_FLYING_SPEED("flying_speed"), + Attribute FLYING_SPEED = getAttribute("flying_speed"); /** * Attack damage of an Entity. */ - GENERIC_ATTACK_DAMAGE("attack_damage"), + Attribute ATTACK_DAMAGE = getAttribute("attack_damage"); /** * Attack knockback of an Entity. */ - GENERIC_ATTACK_KNOCKBACK("attack_knockback"), + Attribute ATTACK_KNOCKBACK = getAttribute("attack_knockback"); /** * Attack speed of an Entity. */ - GENERIC_ATTACK_SPEED("attack_speed"), + Attribute ATTACK_SPEED = getAttribute("attack_speed"); /** * Armor bonus of an Entity. */ - GENERIC_ARMOR("armor"), + Attribute ARMOR = getAttribute("armor"); /** * Armor durability bonus of an Entity. */ - GENERIC_ARMOR_TOUGHNESS("armor_toughness"), + Attribute ARMOR_TOUGHNESS = getAttribute("armor_toughness"); /** * The fall damage multiplier of an Entity. */ - GENERIC_FALL_DAMAGE_MULTIPLIER("fall_damage_multiplier"), + Attribute FALL_DAMAGE_MULTIPLIER = getAttribute("fall_damage_multiplier"); /** * Luck bonus of an Entity. */ - GENERIC_LUCK("luck"), + Attribute LUCK = getAttribute("luck"); /** * Maximum absorption of an Entity. */ - GENERIC_MAX_ABSORPTION("max_absorption"), + Attribute MAX_ABSORPTION = getAttribute("max_absorption"); /** * The distance which an Entity can fall without damage. */ - GENERIC_SAFE_FALL_DISTANCE("safe_fall_distance"), + Attribute SAFE_FALL_DISTANCE = getAttribute("safe_fall_distance"); /** * The relative scale of an Entity. */ - GENERIC_SCALE("scale"), + Attribute SCALE = getAttribute("scale"); /** * The height which an Entity can walk over. */ - GENERIC_STEP_HEIGHT("step_height"), + Attribute STEP_HEIGHT = getAttribute("step_height"); /** * The gravity applied to an Entity. */ - GENERIC_GRAVITY("gravity"), + Attribute GRAVITY = getAttribute("gravity"); /** * Strength with which an Entity will jump. */ - GENERIC_JUMP_STRENGTH("jump_strength"), + Attribute JUMP_STRENGTH = getAttribute("jump_strength"); /** * How long an entity remains burning after ingition. */ - GENERIC_BURNING_TIME("burning_time"), + Attribute BURNING_TIME = getAttribute("burning_time"); /** * Resistance to knockback from explosions. */ - GENERIC_EXPLOSION_KNOCKBACK_RESISTANCE("explosion_knockback_resistance"), + Attribute EXPLOSION_KNOCKBACK_RESISTANCE = getAttribute("explosion_knockback_resistance"); /** * Movement speed through difficult terrain. */ - GENERIC_MOVEMENT_EFFICIENCY("movement_efficiency"), + Attribute MOVEMENT_EFFICIENCY = getAttribute("movement_efficiency"); /** * Oxygen use underwater. */ - GENERIC_OXYGEN_BONUS("oxygen_bonus"), + Attribute OXYGEN_BONUS = getAttribute("oxygen_bonus"); /** * Movement speed through water. */ - GENERIC_WATER_MOVEMENT_EFFICIENCY("water_movement_efficiency"), + Attribute WATER_MOVEMENT_EFFICIENCY = getAttribute("water_movement_efficiency"); /** * Range at which mobs will be tempted by items. */ - GENERIC_TEMPT_RANGE("tempt_range"), + Attribute TEMPT_RANGE = getAttribute("tempt_range"); /** * The block reach distance of a Player. */ - PLAYER_BLOCK_INTERACTION_RANGE("block_interaction_range"), + Attribute BLOCK_INTERACTION_RANGE = getAttribute("block_interaction_range"); /** * The entity reach distance of a Player. */ - PLAYER_ENTITY_INTERACTION_RANGE("entity_interaction_range"), + Attribute ENTITY_INTERACTION_RANGE = getAttribute("entity_interaction_range"); /** * Block break speed of a Player. */ - PLAYER_BLOCK_BREAK_SPEED("block_break_speed"), + Attribute BLOCK_BREAK_SPEED = getAttribute("block_break_speed"); /** * Mining speed for correct tools. */ - PLAYER_MINING_EFFICIENCY("mining_efficiency"), + Attribute MINING_EFFICIENCY = getAttribute("mining_efficiency"); /** * Sneaking speed. */ - PLAYER_SNEAKING_SPEED("sneaking_speed"), + Attribute SNEAKING_SPEED = getAttribute("sneaking_speed"); /** * Underwater mining speed. */ - PLAYER_SUBMERGED_MINING_SPEED("submerged_mining_speed"), + Attribute SUBMERGED_MINING_SPEED = getAttribute("submerged_mining_speed"); /** * Sweeping damage. */ - PLAYER_SWEEPING_DAMAGE_RATIO("sweeping_damage_ratio"), + Attribute SWEEPING_DAMAGE_RATIO = getAttribute("sweeping_damage_ratio"); /** * Chance of a zombie to spawn reinforcements. */ - ZOMBIE_SPAWN_REINFORCEMENTS("spawn_reinforcements"); - - private final NamespacedKey key; - - private Attribute(String key) { - this.key = NamespacedKey.minecraft(key); - } + Attribute SPAWN_REINFORCEMENTS = getAttribute("spawn_reinforcements"); @NotNull - @Override - public NamespacedKey getKey() { - return key; + private static Attribute getAttribute(@NotNull String key) { + return Registry.ATTRIBUTE.getOrThrow(NamespacedKey.minecraft(key)); } + /** + * @param name of the attribute. + * @return the attribute with the given name. + * @deprecated only for backwards compatibility, use {@link Registry#get(NamespacedKey)} instead. + */ @NotNull - @Override - public String getTranslationKey() { - return Bukkit.getUnsafe().getTranslationKey(this); + @Deprecated(since = "1.21.3") + static Attribute valueOf(@NotNull String name) { + Attribute attribute = Bukkit.getUnsafe().get(Registry.ATTRIBUTE, NamespacedKey.fromString(name.toLowerCase(Locale.ROOT))); + Preconditions.checkArgument(attribute != null, "No attribute found with the name %s", name); + return attribute; + } + + /** + * @return an array of all known attributes. + * @deprecated use {@link Registry#iterator()}. + */ + @NotNull + @Deprecated(since = "1.21.3") + static Attribute[] values() { + return Lists.newArrayList(Registry.ATTRIBUTE).toArray(new Attribute[0]); } }