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