Add comments to attribute system

This commit is contained in:
JësFot 2020-11-17 15:52:20 +01:00
parent b9ea934857
commit 3597af34f9
5 changed files with 166 additions and 0 deletions

View File

@ -6,6 +6,9 @@ import java.util.Map;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
/**
* Represent a {@link net.minestom.server.entity.LivingEntity living entity} attribute.
*/
public class Attribute { public class Attribute {
private static final Map<String, Attribute> ATTRIBUTES = new HashMap<>(); private static final Map<String, Attribute> ATTRIBUTES = new HashMap<>();
@ -15,10 +18,28 @@ public class Attribute {
private final float maxValue; private final float maxValue;
private final boolean shareWithClient; private final boolean shareWithClient;
/**
* Create a new attribute with a given key and default.
* <p>
* By default, this attribute will be sent to the client.
* </p>
*
* @param key the attribute registry key
* @param defaultValue the default value
* @param maxValue the maximum allowed value
*/
public Attribute(@NotNull String key, float defaultValue, float maxValue) { public Attribute(@NotNull String key, float defaultValue, float maxValue) {
this(key, true, defaultValue, maxValue); this(key, true, defaultValue, maxValue);
} }
/**
* Create a new attribute with a given key and default.
*
* @param key the attribute registry key
* @param shareWithClient whether to send this attribute to the client
* @param defaultValue the default value
* @param maxValue the maximum allowed value
*/
public Attribute(@NotNull String key, boolean shareWithClient, float defaultValue, float maxValue) { public Attribute(@NotNull String key, boolean shareWithClient, float defaultValue, float maxValue) {
if (defaultValue > maxValue) { if (defaultValue > maxValue) {
throw new IllegalArgumentException("Default value cannot be greater than the maximum allowed"); throw new IllegalArgumentException("Default value cannot be greater than the maximum allowed");
@ -29,39 +50,83 @@ public class Attribute {
this.maxValue = maxValue; this.maxValue = maxValue;
} }
/**
* Gets the attribute unique key.
*
* @return the attribute key
*/
@NotNull @NotNull
public String getKey() { public String getKey() {
return key; return key;
} }
/**
* Gets the attribute default value that should be applied.
*
* @return the attribute default value
*/
public float getDefaultValue() { public float getDefaultValue() {
return defaultValue; return defaultValue;
} }
/**
* Gets the maximum value applicable to an entity for this attribute.
*
* @return the maximum value of this attribute
*/
public float getMaxValue() { public float getMaxValue() {
return maxValue; return maxValue;
} }
/**
* Gets whether this attribute's instances should be sent to clients.
*
* @return if this attribute is to be shared
*/
public boolean isShared() { public boolean isShared() {
return shareWithClient; return shareWithClient;
} }
/**
* Register this attribute.
*
* @see #fromKey(String)
* @see #values()
*
* @return this attribute
*/
@NotNull @NotNull
public Attribute register() { public Attribute register() {
ATTRIBUTES.put(key, this); ATTRIBUTES.put(key, this);
return this; return this;
} }
/**
* Retrieves an attribute by its key.
*
* @param key the key of the attribute
* @return the attribute for the key or null if not any
*/
@Nullable @Nullable
public static Attribute fromKey(@NotNull String key) { public static Attribute fromKey(@NotNull String key) {
return ATTRIBUTES.get(key); return ATTRIBUTES.get(key);
} }
/**
* Retrieves all registered attributes.
*
* @return an array containing all registered attributes
*/
@NotNull @NotNull
public static Attribute[] values() { public static Attribute[] values() {
return ATTRIBUTES.values().toArray(new Attribute[0]); return ATTRIBUTES.values().toArray(new Attribute[0]);
} }
/**
* Retrieves registered attributes that are shared with the client.
*
* @return an array containing registered, sharable attributes
*/
@NotNull @NotNull
public static Attribute[] sharedAttributes() { public static Attribute[] sharedAttributes() {
return ATTRIBUTES.values().stream().filter(Attribute::isShared).toArray(Attribute[]::new); return ATTRIBUTES.values().stream().filter(Attribute::isShared).toArray(Attribute[]::new);

View File

@ -9,6 +9,9 @@ import java.util.function.Consumer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
/**
* Represents an instance of an attribute and its modifiers.
*/
public class AttributeInstance { public class AttributeInstance {
private final Attribute attribute; private final Attribute attribute;
@ -24,15 +27,31 @@ public class AttributeInstance {
this.baseValue = attribute.getDefaultValue(); this.baseValue = attribute.getDefaultValue();
} }
/**
* Gets the attribute associated to this instance.
*
* @return the associated attribute
*/
@NotNull @NotNull
public Attribute getAttribute() { public Attribute getAttribute() {
return attribute; return attribute;
} }
/**
* The base value of this instance without modifiers
*
* @see #setBaseValue(float)
*
* @return the instance base value
*/
public float getBaseValue() { public float getBaseValue() {
return baseValue; return baseValue;
} }
/**
* Sets this instance dirty to trigger calculation of the new value.
* Triggers the {@link #propertyChangeListener}.
*/
private void setDirty() { private void setDirty() {
if (!dirty) { if (!dirty) {
dirty = true; dirty = true;
@ -42,6 +61,13 @@ public class AttributeInstance {
} }
} }
/**
* Sets the base value of this instance.
*
* @see #getBaseValue()
*
* @param baseValue the new base value
*/
public void setBaseValue(float baseValue) { public void setBaseValue(float baseValue) {
if (this.baseValue != baseValue) { if (this.baseValue != baseValue) {
this.baseValue = baseValue; this.baseValue = baseValue;
@ -49,22 +75,42 @@ public class AttributeInstance {
} }
} }
/**
* Add a modifier to this instance.
*
* @param modifier the modifier to add
*/
public void addModifier(@NotNull AttributeModifier modifier) { public void addModifier(@NotNull AttributeModifier modifier) {
if (modifiers.putIfAbsent(modifier.getId(), modifier) == null) { if (modifiers.putIfAbsent(modifier.getId(), modifier) == null) {
setDirty(); setDirty();
} }
} }
/**
* Remove a modifier from this instance.
*
* @param modifier the modifier to remove
*/
public void removeModifier(@NotNull AttributeModifier modifier) { public void removeModifier(@NotNull AttributeModifier modifier) {
if (modifiers.remove(modifier.getId()) != null) { if (modifiers.remove(modifier.getId()) != null) {
setDirty(); setDirty();
} }
} }
/**
* Get the modifiers applied to this instance.
*
* @return the modifiers.
*/
public Collection<AttributeModifier> getModifiers() { public Collection<AttributeModifier> getModifiers() {
return modifiers.values(); return modifiers.values();
} }
/**
* Gets the value of this instance calculated with modifiers applied.
*
* @return the attribute value
*/
public float getValue() { public float getValue() {
if (dirty) { if (dirty) {
cachedValue = processModifiers(); cachedValue = processModifiers();
@ -73,6 +119,11 @@ public class AttributeInstance {
return cachedValue; return cachedValue;
} }
/**
* Recalculate the value of this attribute instance using the modifiers.
*
* @return the attribute value
*/
protected float processModifiers() { protected float processModifiers() {
float base = getBaseValue(); float base = getBaseValue();

View File

@ -6,6 +6,9 @@ import io.netty.util.internal.ThreadLocalRandom;
import net.minestom.server.utils.UniqueIdUtils; import net.minestom.server.utils.UniqueIdUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/**
* Represent an attribute modifier.
*/
public class AttributeModifier { public class AttributeModifier {
private final float amount; private final float amount;
@ -13,10 +16,25 @@ public class AttributeModifier {
private final AttributeOperation operation; private final AttributeOperation operation;
private final UUID id; private final UUID id;
/**
* Creates a new modifier with a random id.
*
* @param name the name of this modifier
* @param amount the value of this modifier
* @param operation the operation to apply this modifier with
*/
public AttributeModifier(@NotNull String name, float amount, @NotNull AttributeOperation operation) { public AttributeModifier(@NotNull String name, float amount, @NotNull AttributeOperation operation) {
this(UniqueIdUtils.createRandomUUID(ThreadLocalRandom.current()), name, amount, operation); this(UniqueIdUtils.createRandomUUID(ThreadLocalRandom.current()), name, amount, operation);
} }
/**
* Creates a new modifier.
*
* @param id the id of this modifier
* @param name the name of this modifier
* @param amount the value of this modifier
* @param operation the operation to apply this modifier with
*/
public AttributeModifier(@NotNull UUID id, @NotNull String name, float amount, @NotNull AttributeOperation operation) { public AttributeModifier(@NotNull UUID id, @NotNull String name, float amount, @NotNull AttributeOperation operation) {
this.id = id; this.id = id;
this.name = name; this.name = name;
@ -24,20 +42,40 @@ public class AttributeModifier {
this.operation = operation; this.operation = operation;
} }
/**
* Gets the id of this modifier.
*
* @return the id of this modifier
*/
@NotNull @NotNull
public UUID getId() { public UUID getId() {
return id; return id;
} }
/**
* Gets the name of this modifier.
*
* @return the name of this modifier
*/
@NotNull @NotNull
public String getName() { public String getName() {
return name; return name;
} }
/**
* Gets the value of this modifier.
*
* @return the value of this modifier
*/
public float getAmount() { public float getAmount() {
return amount; return amount;
} }
/**
* Gets the operation of this modifier.
*
* @return the operation of this modifier
*/
@NotNull @NotNull
public AttributeOperation getOperation() { public AttributeOperation getOperation() {
return operation; return operation;

View File

@ -1,5 +1,8 @@
package net.minestom.server.attribute; package net.minestom.server.attribute;
/**
* The Minecraft, vanilla, standards attributes.
*/
public final class Attributes public final class Attributes
{ {
public static final Attribute MAX_HEALTH = (new Attribute("generic.max_health", true, 20, 1024)).register(); public static final Attribute MAX_HEALTH = (new Attribute("generic.max_health", true, 20, 1024)).register();
@ -15,4 +18,8 @@ public final class Attributes
public static final Attribute LUCK = (new Attribute("generic.luck", true, 0, 1024)).register(); public static final Attribute LUCK = (new Attribute("generic.luck", true, 0, 1024)).register();
public static final Attribute HORSE_JUMP_STRENGTH = (new Attribute("horse.jump_strength", true, 0.7f, 2)).register(); public static final Attribute HORSE_JUMP_STRENGTH = (new Attribute("horse.jump_strength", true, 0.7f, 2)).register();
public static final Attribute ZOMBIE_SPAWN_REINFORCEMENTS = (new Attribute("zombie.spawn_reinforcements", true, 0, 1)).register(); public static final Attribute ZOMBIE_SPAWN_REINFORCEMENTS = (new Attribute("zombie.spawn_reinforcements", true, 0, 1)).register();
private Attributes() throws IllegalAccessException {
throw new IllegalAccessException("Cannot instantiate a static class");
}
} }

View File

@ -405,6 +405,11 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
return attributeModifiers.get(attribute.getKey()); return attributeModifiers.get(attribute.getKey());
} }
/**
* Callback used when an attribute instance has been modified.
*
* @param instance the modified attribute instance
*/
protected void onAttributeChanged(@NotNull AttributeInstance instance) { } protected void onAttributeChanged(@NotNull AttributeInstance instance) { }
/** /**