mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-15 07:05:52 +01:00
Fix attributes not being refreshed client side
This commit is contained in:
parent
d6a3b18f04
commit
f5d550cda9
@ -18,7 +18,6 @@ public class AttributeInstance {
|
|||||||
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 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) {
|
||||||
@ -47,19 +46,6 @@ public class AttributeInstance {
|
|||||||
return baseValue;
|
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 the base value of this instance.
|
* Sets the base value of this instance.
|
||||||
*
|
*
|
||||||
@ -69,7 +55,11 @@ public class AttributeInstance {
|
|||||||
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();
|
refreshCachedValue();
|
||||||
|
|
||||||
|
if (propertyChangeListener != null) {
|
||||||
|
propertyChangeListener.accept(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +70,7 @@ public class AttributeInstance {
|
|||||||
*/
|
*/
|
||||||
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();
|
refreshCachedValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +81,7 @@ public class AttributeInstance {
|
|||||||
*/
|
*/
|
||||||
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();
|
refreshCachedValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,10 +100,6 @@ public class AttributeInstance {
|
|||||||
* @return the attribute value
|
* @return the attribute value
|
||||||
*/
|
*/
|
||||||
public float getValue() {
|
public float getValue() {
|
||||||
if (dirty) {
|
|
||||||
cachedValue = processModifiers();
|
|
||||||
dirty = false;
|
|
||||||
}
|
|
||||||
return cachedValue;
|
return cachedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +108,7 @@ public class AttributeInstance {
|
|||||||
*
|
*
|
||||||
* @return the attribute value
|
* @return the attribute value
|
||||||
*/
|
*/
|
||||||
protected float processModifiers() {
|
protected void refreshCachedValue() {
|
||||||
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)) {
|
||||||
@ -138,6 +124,7 @@ public class AttributeInstance {
|
|||||||
result *= (1.0f + modifier.getAmount());
|
result *= (1.0f + modifier.getAmount());
|
||||||
}
|
}
|
||||||
|
|
||||||
return Math.min(result, getAttribute().getMaxValue());
|
final float finalValue = Math.min(result, getAttribute().getMaxValue());
|
||||||
|
this.cachedValue = finalValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1079,10 +1079,12 @@ public class Player extends LivingEntity implements CommandSender {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onAttributeChanged(@NotNull final AttributeInstance instance) {
|
protected void onAttributeChanged(@NotNull final AttributeInstance attributeInstance) {
|
||||||
if (instance.getAttribute().isShared() && playerConnection != null)
|
if (attributeInstance.getAttribute().isShared() &&
|
||||||
|
playerConnection.getConnectionState() == ConnectionState.PLAY) {
|
||||||
playerConnection.sendPacket(getPropertiesPacket());
|
playerConnection.sendPacket(getPropertiesPacket());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setHealth(float health) {
|
public void setHealth(float health) {
|
||||||
|
Loading…
Reference in New Issue
Block a user