mirror of
https://github.com/Minestom/Minestom.git
synced 2024-06-28 15:35:01 +02:00
Reformat code
This commit is contained in:
parent
36986f6aa2
commit
ccaf9b5c47
|
@ -1,134 +1,133 @@
|
|||
package net.minestom.server.attribute;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Represent a {@link net.minestom.server.entity.LivingEntity living entity} attribute.
|
||||
* Represents a {@link net.minestom.server.entity.LivingEntity living entity} attribute.
|
||||
*/
|
||||
public class Attribute {
|
||||
|
||||
private static final Map<String, Attribute> ATTRIBUTES = new HashMap<>();
|
||||
private static final Map<String, Attribute> ATTRIBUTES = new HashMap<>();
|
||||
|
||||
private final String key;
|
||||
private final float defaultValue;
|
||||
private final float maxValue;
|
||||
private final boolean shareWithClient;
|
||||
private final String key;
|
||||
private final float defaultValue;
|
||||
private final float maxValue;
|
||||
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) {
|
||||
this(key, true, defaultValue, maxValue);
|
||||
}
|
||||
/**
|
||||
* 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) {
|
||||
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) {
|
||||
if (defaultValue > maxValue) {
|
||||
throw new IllegalArgumentException("Default value cannot be greater than the maximum allowed");
|
||||
}
|
||||
this.key = key;
|
||||
this.shareWithClient = shareWithClient;
|
||||
this.defaultValue = defaultValue;
|
||||
this.maxValue = 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) {
|
||||
if (defaultValue > maxValue) {
|
||||
throw new IllegalArgumentException("Default value cannot be greater than the maximum allowed");
|
||||
}
|
||||
this.key = key;
|
||||
this.shareWithClient = shareWithClient;
|
||||
this.defaultValue = defaultValue;
|
||||
this.maxValue = maxValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the attribute unique key.
|
||||
*
|
||||
* @return the attribute key
|
||||
*/
|
||||
@NotNull
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
/**
|
||||
* Gets the attribute unique key.
|
||||
*
|
||||
* @return the attribute key
|
||||
*/
|
||||
@NotNull
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the attribute default value that should be applied.
|
||||
*
|
||||
* @return the attribute default value
|
||||
*/
|
||||
public float getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
/**
|
||||
* Gets the attribute default value that should be applied.
|
||||
*
|
||||
* @return the attribute default value
|
||||
*/
|
||||
public float getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the maximum value applicable to an entity for this attribute.
|
||||
*
|
||||
* @return the maximum value of this attribute
|
||||
*/
|
||||
public float getMaxValue() {
|
||||
return maxValue;
|
||||
}
|
||||
/**
|
||||
* Gets the maximum value applicable to an entity for this attribute.
|
||||
*
|
||||
* @return the maximum value of this attribute
|
||||
*/
|
||||
public float getMaxValue() {
|
||||
return maxValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether this attribute's instances should be sent to clients.
|
||||
*
|
||||
* @return if this attribute is to be shared
|
||||
*/
|
||||
public boolean isShared() {
|
||||
return shareWithClient;
|
||||
}
|
||||
/**
|
||||
* Gets whether this attribute's instances should be sent to clients.
|
||||
*
|
||||
* @return if this attribute is to be shared
|
||||
*/
|
||||
public boolean isShared() {
|
||||
return shareWithClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register this attribute.
|
||||
*
|
||||
* @see #fromKey(String)
|
||||
* @see #values()
|
||||
*
|
||||
* @return this attribute
|
||||
*/
|
||||
@NotNull
|
||||
public Attribute register() {
|
||||
ATTRIBUTES.put(key, this);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Register this attribute.
|
||||
*
|
||||
* @return this attribute
|
||||
* @see #fromKey(String)
|
||||
* @see #values()
|
||||
*/
|
||||
@NotNull
|
||||
public Attribute register() {
|
||||
ATTRIBUTES.put(key, 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
|
||||
public static Attribute fromKey(@NotNull String key) {
|
||||
return ATTRIBUTES.get(key);
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
public static Attribute fromKey(@NotNull String key) {
|
||||
return ATTRIBUTES.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all registered attributes.
|
||||
*
|
||||
* @return an array containing all registered attributes
|
||||
*/
|
||||
@NotNull
|
||||
public static Attribute[] values() {
|
||||
return ATTRIBUTES.values().toArray(new Attribute[0]);
|
||||
}
|
||||
/**
|
||||
* Retrieves all registered attributes.
|
||||
*
|
||||
* @return an array containing all registered attributes
|
||||
*/
|
||||
@NotNull
|
||||
public static Attribute[] values() {
|
||||
return ATTRIBUTES.values().toArray(new Attribute[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves registered attributes that are shared with the client.
|
||||
*
|
||||
* @return an array containing registered, sharable attributes
|
||||
*/
|
||||
@NotNull
|
||||
public static Attribute[] sharedAttributes() {
|
||||
return ATTRIBUTES.values().stream().filter(Attribute::isShared).toArray(Attribute[]::new);
|
||||
}
|
||||
/**
|
||||
* Retrieves registered attributes that are shared with the client.
|
||||
*
|
||||
* @return an array containing registered, sharable attributes
|
||||
*/
|
||||
@NotNull
|
||||
public static Attribute[] sharedAttributes() {
|
||||
return ATTRIBUTES.values().stream().filter(Attribute::isShared).toArray(Attribute[]::new);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,145 +1,143 @@
|
|||
package net.minestom.server.attribute;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Represents an instance of an attribute and its modifiers.
|
||||
*/
|
||||
public class AttributeInstance {
|
||||
|
||||
private final Attribute attribute;
|
||||
private final Map<UUID, AttributeModifier> modifiers = new HashMap<>();
|
||||
private final Consumer<AttributeInstance> propertyChangeListener;
|
||||
private float baseValue;
|
||||
private boolean dirty = true;
|
||||
private float cachedValue = 0.0f;
|
||||
private final Attribute attribute;
|
||||
private final Map<UUID, AttributeModifier> modifiers = new HashMap<>();
|
||||
private final Consumer<AttributeInstance> propertyChangeListener;
|
||||
private float baseValue;
|
||||
private boolean dirty = true;
|
||||
private float cachedValue = 0.0f;
|
||||
|
||||
public AttributeInstance(@NotNull Attribute attribute, @Nullable Consumer<AttributeInstance> listener) {
|
||||
this.attribute = attribute;
|
||||
this.propertyChangeListener = listener;
|
||||
this.baseValue = attribute.getDefaultValue();
|
||||
}
|
||||
public AttributeInstance(@NotNull Attribute attribute, @Nullable Consumer<AttributeInstance> listener) {
|
||||
this.attribute = attribute;
|
||||
this.propertyChangeListener = listener;
|
||||
this.baseValue = attribute.getDefaultValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the attribute associated to this instance.
|
||||
*
|
||||
* @return the associated attribute
|
||||
*/
|
||||
@NotNull
|
||||
public Attribute getAttribute() {
|
||||
return attribute;
|
||||
}
|
||||
/**
|
||||
* Gets the attribute associated to this instance.
|
||||
*
|
||||
* @return the associated attribute
|
||||
*/
|
||||
@NotNull
|
||||
public Attribute getAttribute() {
|
||||
return attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* The base value of this instance without modifiers
|
||||
*
|
||||
* @see #setBaseValue(float)
|
||||
*
|
||||
* @return the instance base value
|
||||
*/
|
||||
public float getBaseValue() {
|
||||
return baseValue;
|
||||
}
|
||||
/**
|
||||
* The base value of this instance without modifiers
|
||||
*
|
||||
* @return the instance base value
|
||||
* @see #setBaseValue(float)
|
||||
*/
|
||||
public float getBaseValue() {
|
||||
return baseValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this instance dirty to trigger calculation of the new value.
|
||||
* Triggers the {@link #propertyChangeListener}.
|
||||
*/
|
||||
private void setDirty() {
|
||||
if (!dirty) {
|
||||
dirty = true;
|
||||
if (propertyChangeListener != null) {
|
||||
propertyChangeListener.accept(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Sets this instance dirty to trigger calculation of the new value.
|
||||
* Triggers the {@link #propertyChangeListener}.
|
||||
*/
|
||||
private void setDirty() {
|
||||
if (!dirty) {
|
||||
dirty = true;
|
||||
if (propertyChangeListener != null) {
|
||||
propertyChangeListener.accept(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the base value of this instance.
|
||||
*
|
||||
* @see #getBaseValue()
|
||||
*
|
||||
* @param baseValue the new base value
|
||||
*/
|
||||
public void setBaseValue(float baseValue) {
|
||||
if (this.baseValue != baseValue) {
|
||||
this.baseValue = baseValue;
|
||||
setDirty();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Sets the base value of this instance.
|
||||
*
|
||||
* @param baseValue the new base value
|
||||
* @see #getBaseValue()
|
||||
*/
|
||||
public void setBaseValue(float baseValue) {
|
||||
if (this.baseValue != baseValue) {
|
||||
this.baseValue = baseValue;
|
||||
setDirty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a modifier to this instance.
|
||||
*
|
||||
* @param modifier the modifier to add
|
||||
*/
|
||||
public void addModifier(@NotNull AttributeModifier modifier) {
|
||||
if (modifiers.putIfAbsent(modifier.getId(), modifier) == null) {
|
||||
setDirty();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Add a modifier to this instance.
|
||||
*
|
||||
* @param modifier the modifier to add
|
||||
*/
|
||||
public void addModifier(@NotNull AttributeModifier modifier) {
|
||||
if (modifiers.putIfAbsent(modifier.getId(), modifier) == null) {
|
||||
setDirty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a modifier from this instance.
|
||||
*
|
||||
* @param modifier the modifier to remove
|
||||
*/
|
||||
public void removeModifier(@NotNull AttributeModifier modifier) {
|
||||
if (modifiers.remove(modifier.getId()) != null) {
|
||||
setDirty();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Remove a modifier from this instance.
|
||||
*
|
||||
* @param modifier the modifier to remove
|
||||
*/
|
||||
public void removeModifier(@NotNull AttributeModifier modifier) {
|
||||
if (modifiers.remove(modifier.getId()) != null) {
|
||||
setDirty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the modifiers applied to this instance.
|
||||
*
|
||||
* @return the modifiers.
|
||||
*/
|
||||
public Collection<AttributeModifier> getModifiers() {
|
||||
return modifiers.values();
|
||||
}
|
||||
/**
|
||||
* Get the modifiers applied to this instance.
|
||||
*
|
||||
* @return the modifiers.
|
||||
*/
|
||||
public Collection<AttributeModifier> getModifiers() {
|
||||
return modifiers.values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of this instance calculated with modifiers applied.
|
||||
*
|
||||
* @return the attribute value
|
||||
*/
|
||||
public float getValue() {
|
||||
if (dirty) {
|
||||
cachedValue = processModifiers();
|
||||
dirty = false;
|
||||
}
|
||||
return cachedValue;
|
||||
}
|
||||
/**
|
||||
* Gets the value of this instance calculated with modifiers applied.
|
||||
*
|
||||
* @return the attribute value
|
||||
*/
|
||||
public float getValue() {
|
||||
if (dirty) {
|
||||
cachedValue = processModifiers();
|
||||
dirty = false;
|
||||
}
|
||||
return cachedValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate the value of this attribute instance using the modifiers.
|
||||
*
|
||||
* @return the attribute value
|
||||
*/
|
||||
protected float processModifiers() {
|
||||
float base = getBaseValue();
|
||||
/**
|
||||
* Recalculate the value of this attribute instance using the modifiers.
|
||||
*
|
||||
* @return the attribute value
|
||||
*/
|
||||
protected float processModifiers() {
|
||||
float base = getBaseValue();
|
||||
|
||||
for (var modifier : modifiers.values().stream().filter(mod -> mod.getOperation() == AttributeOperation.ADDITION).toArray(AttributeModifier[]::new)) {
|
||||
base += modifier.getAmount();
|
||||
}
|
||||
for (var modifier : modifiers.values().stream().filter(mod -> mod.getOperation() == AttributeOperation.ADDITION).toArray(AttributeModifier[]::new)) {
|
||||
base += modifier.getAmount();
|
||||
}
|
||||
|
||||
float result = base;
|
||||
float result = base;
|
||||
|
||||
for (var modifier : modifiers.values().stream().filter(mod -> mod.getOperation() == AttributeOperation.MULTIPLY_BASE).toArray(AttributeModifier[]::new)) {
|
||||
result += (base * modifier.getAmount());
|
||||
}
|
||||
for (var modifier : modifiers.values().stream().filter(mod -> mod.getOperation() == AttributeOperation.MULTIPLY_TOTAL).toArray(AttributeModifier[]::new)) {
|
||||
result *= (1.0f + modifier.getAmount());
|
||||
}
|
||||
for (var modifier : modifiers.values().stream().filter(mod -> mod.getOperation() == AttributeOperation.MULTIPLY_BASE).toArray(AttributeModifier[]::new)) {
|
||||
result += (base * modifier.getAmount());
|
||||
}
|
||||
for (var modifier : modifiers.values().stream().filter(mod -> mod.getOperation() == AttributeOperation.MULTIPLY_TOTAL).toArray(AttributeModifier[]::new)) {
|
||||
result *= (1.0f + modifier.getAmount());
|
||||
}
|
||||
|
||||
return Math.min(result, getAttribute().getMaxValue());
|
||||
}
|
||||
return Math.min(result, getAttribute().getMaxValue());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,83 +1,83 @@
|
|||
package net.minestom.server.attribute;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import io.netty.util.internal.ThreadLocalRandom;
|
||||
import net.minestom.server.utils.UniqueIdUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Represent an attribute modifier.
|
||||
*/
|
||||
public class AttributeModifier {
|
||||
|
||||
private final float amount;
|
||||
private final String name;
|
||||
private final AttributeOperation operation;
|
||||
private final UUID id;
|
||||
private final float amount;
|
||||
private final String name;
|
||||
private final AttributeOperation operation;
|
||||
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) {
|
||||
this(UniqueIdUtils.createRandomUUID(ThreadLocalRandom.current()), name, amount, operation);
|
||||
}
|
||||
/**
|
||||
* 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) {
|
||||
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) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.amount = amount;
|
||||
this.operation = 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) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.amount = amount;
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the id of this modifier.
|
||||
*
|
||||
* @return the id of this modifier
|
||||
*/
|
||||
@NotNull
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
/**
|
||||
* Gets the id of this modifier.
|
||||
*
|
||||
* @return the id of this modifier
|
||||
*/
|
||||
@NotNull
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of this modifier.
|
||||
*
|
||||
* @return the name of this modifier
|
||||
*/
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
/**
|
||||
* Gets the name of this modifier.
|
||||
*
|
||||
* @return the name of this modifier
|
||||
*/
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of this modifier.
|
||||
*
|
||||
* @return the value of this modifier
|
||||
*/
|
||||
public float getAmount() {
|
||||
return amount;
|
||||
}
|
||||
/**
|
||||
* Gets the value of this modifier.
|
||||
*
|
||||
* @return the value of this modifier
|
||||
*/
|
||||
public float getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the operation of this modifier.
|
||||
*
|
||||
* @return the operation of this modifier
|
||||
*/
|
||||
@NotNull
|
||||
public AttributeOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
/**
|
||||
* Gets the operation of this modifier.
|
||||
*
|
||||
* @return the operation of this modifier
|
||||
*/
|
||||
@NotNull
|
||||
public AttributeOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,7 @@ 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 FOLLOW_RANGE = (new Attribute("generic.follow_range", true, 32, 2048)).register();
|
||||
public static final Attribute KNOCKBACK_RESISTANCE = (new Attribute("generic.knockback_resistance", true, 0, 1)).register();
|
||||
|
|
Loading…
Reference in New Issue
Block a user