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

This commit is contained in:
DerFrZocker 2024-10-29 06:43:08 +11:00 committed by md_5
parent 63472efba0
commit ed2cdfc3d4
No known key found for this signature in database
GPG key ID: E8E901AC7C617C11
3 changed files with 62 additions and 46 deletions

View file

@ -93,7 +93,7 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
* *
* @see Attribute * @see Attribute
*/ */
Registry<Attribute> ATTRIBUTE = new SimpleRegistry<>(Attribute.class); Registry<Attribute> ATTRIBUTE = Objects.requireNonNull(Bukkit.getRegistry(Attribute.class), "No registry present for Attribute. This is a bug.");
/** /**
* Server banner patterns. * Server banner patterns.
* *

View file

@ -98,6 +98,7 @@ public interface UnsafeValues {
String getTranslationKey(ItemStack itemStack); String getTranslationKey(ItemStack itemStack);
@Deprecated(since = "1.21.3", forRemoval = true)
String getTranslationKey(Attribute attribute); String getTranslationKey(Attribute attribute);
@Nullable @Nullable

View file

@ -1,160 +1,175 @@
package org.bukkit.attribute; 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.Bukkit;
import org.bukkit.Keyed; import org.bukkit.Keyed;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Translatable; import org.bukkit.Translatable;
import org.bukkit.util.OldEnum;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Types of attributes which may be present on an {@link Attributable}. * Types of attributes which may be present on an {@link Attributable}.
*/ */
public enum Attribute implements Keyed, Translatable { public interface Attribute extends OldEnum<Attribute>, Keyed, Translatable {
/** /**
* Maximum health of an Entity. * Maximum health of an Entity.
*/ */
GENERIC_MAX_HEALTH("max_health"), Attribute MAX_HEALTH = getAttribute("max_health");
/** /**
* Range at which an Entity will follow others. * 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. * Resistance of an Entity to knockback.
*/ */
GENERIC_KNOCKBACK_RESISTANCE("knockback_resistance"), Attribute KNOCKBACK_RESISTANCE = getAttribute("knockback_resistance");
/** /**
* Movement speed of an Entity. * Movement speed of an Entity.
*/ */
GENERIC_MOVEMENT_SPEED("movement_speed"), Attribute MOVEMENT_SPEED = getAttribute("movement_speed");
/** /**
* Flying speed of an Entity. * Flying speed of an Entity.
*/ */
GENERIC_FLYING_SPEED("flying_speed"), Attribute FLYING_SPEED = getAttribute("flying_speed");
/** /**
* Attack damage of an Entity. * Attack damage of an Entity.
*/ */
GENERIC_ATTACK_DAMAGE("attack_damage"), Attribute ATTACK_DAMAGE = getAttribute("attack_damage");
/** /**
* Attack knockback of an Entity. * Attack knockback of an Entity.
*/ */
GENERIC_ATTACK_KNOCKBACK("attack_knockback"), Attribute ATTACK_KNOCKBACK = getAttribute("attack_knockback");
/** /**
* Attack speed of an Entity. * Attack speed of an Entity.
*/ */
GENERIC_ATTACK_SPEED("attack_speed"), Attribute ATTACK_SPEED = getAttribute("attack_speed");
/** /**
* Armor bonus of an Entity. * Armor bonus of an Entity.
*/ */
GENERIC_ARMOR("armor"), Attribute ARMOR = getAttribute("armor");
/** /**
* Armor durability bonus of an Entity. * Armor durability bonus of an Entity.
*/ */
GENERIC_ARMOR_TOUGHNESS("armor_toughness"), Attribute ARMOR_TOUGHNESS = getAttribute("armor_toughness");
/** /**
* The fall damage multiplier of an Entity. * 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. * Luck bonus of an Entity.
*/ */
GENERIC_LUCK("luck"), Attribute LUCK = getAttribute("luck");
/** /**
* Maximum absorption of an Entity. * 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. * 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. * The relative scale of an Entity.
*/ */
GENERIC_SCALE("scale"), Attribute SCALE = getAttribute("scale");
/** /**
* The height which an Entity can walk over. * 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. * The gravity applied to an Entity.
*/ */
GENERIC_GRAVITY("gravity"), Attribute GRAVITY = getAttribute("gravity");
/** /**
* Strength with which an Entity will jump. * 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. * How long an entity remains burning after ingition.
*/ */
GENERIC_BURNING_TIME("burning_time"), Attribute BURNING_TIME = getAttribute("burning_time");
/** /**
* Resistance to knockback from explosions. * 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. * Movement speed through difficult terrain.
*/ */
GENERIC_MOVEMENT_EFFICIENCY("movement_efficiency"), Attribute MOVEMENT_EFFICIENCY = getAttribute("movement_efficiency");
/** /**
* Oxygen use underwater. * Oxygen use underwater.
*/ */
GENERIC_OXYGEN_BONUS("oxygen_bonus"), Attribute OXYGEN_BONUS = getAttribute("oxygen_bonus");
/** /**
* Movement speed through water. * 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. * 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. * 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. * 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. * 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. * Mining speed for correct tools.
*/ */
PLAYER_MINING_EFFICIENCY("mining_efficiency"), Attribute MINING_EFFICIENCY = getAttribute("mining_efficiency");
/** /**
* Sneaking speed. * Sneaking speed.
*/ */
PLAYER_SNEAKING_SPEED("sneaking_speed"), Attribute SNEAKING_SPEED = getAttribute("sneaking_speed");
/** /**
* Underwater mining speed. * Underwater mining speed.
*/ */
PLAYER_SUBMERGED_MINING_SPEED("submerged_mining_speed"), Attribute SUBMERGED_MINING_SPEED = getAttribute("submerged_mining_speed");
/** /**
* Sweeping damage. * Sweeping damage.
*/ */
PLAYER_SWEEPING_DAMAGE_RATIO("sweeping_damage_ratio"), Attribute SWEEPING_DAMAGE_RATIO = getAttribute("sweeping_damage_ratio");
/** /**
* Chance of a zombie to spawn reinforcements. * Chance of a zombie to spawn reinforcements.
*/ */
ZOMBIE_SPAWN_REINFORCEMENTS("spawn_reinforcements"); Attribute SPAWN_REINFORCEMENTS = getAttribute("spawn_reinforcements");
private final NamespacedKey key;
private Attribute(String key) {
this.key = NamespacedKey.minecraft(key);
}
@NotNull @NotNull
@Override private static Attribute getAttribute(@NotNull String key) {
public NamespacedKey getKey() { return Registry.ATTRIBUTE.getOrThrow(NamespacedKey.minecraft(key));
return 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 @NotNull
@Override @Deprecated(since = "1.21.3")
public String getTranslationKey() { static Attribute valueOf(@NotNull String name) {
return Bukkit.getUnsafe().getTranslationKey(this); 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]);
} }
} }