From f5d550cda9f8050c3bdd9f5cc838d7e7a2fc1414 Mon Sep 17 00:00:00 2001 From: themode Date: Wed, 6 Jan 2021 20:45:40 +0100 Subject: [PATCH] Fix attributes not being refreshed client side --- .../server/attribute/AttributeInstance.java | 33 ++++++------------- .../minestom/server/entity/LivingEntity.java | 4 +-- .../net/minestom/server/entity/Player.java | 6 ++-- 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/minestom/server/attribute/AttributeInstance.java b/src/main/java/net/minestom/server/attribute/AttributeInstance.java index 4f499a259..d528697b7 100644 --- a/src/main/java/net/minestom/server/attribute/AttributeInstance.java +++ b/src/main/java/net/minestom/server/attribute/AttributeInstance.java @@ -18,7 +18,6 @@ public class AttributeInstance { private final Map modifiers = new HashMap<>(); private final Consumer propertyChangeListener; private float baseValue; - private boolean dirty = true; private float cachedValue = 0.0f; public AttributeInstance(@NotNull Attribute attribute, @Nullable Consumer listener) { @@ -47,19 +46,6 @@ public class AttributeInstance { 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. * @@ -69,7 +55,11 @@ public class AttributeInstance { public void setBaseValue(float baseValue) { if (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) { if (modifiers.putIfAbsent(modifier.getId(), modifier) == null) { - setDirty(); + refreshCachedValue(); } } @@ -91,7 +81,7 @@ public class AttributeInstance { */ public void removeModifier(@NotNull AttributeModifier modifier) { if (modifiers.remove(modifier.getId()) != null) { - setDirty(); + refreshCachedValue(); } } @@ -110,10 +100,6 @@ public class AttributeInstance { * @return the attribute value */ public float getValue() { - if (dirty) { - cachedValue = processModifiers(); - dirty = false; - } return cachedValue; } @@ -122,7 +108,7 @@ public class AttributeInstance { * * @return the attribute value */ - protected float processModifiers() { + protected void refreshCachedValue() { float base = getBaseValue(); 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()); } - return Math.min(result, getAttribute().getMaxValue()); + final float finalValue = Math.min(result, getAttribute().getMaxValue()); + this.cachedValue = finalValue; } } diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 3951939c2..f9c33a475 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -613,7 +613,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { public List getLineOfSight(int maxDistance) { List blocks = new ArrayList<>(); Iterator it = new BlockIterator(this, maxDistance); - while(it.hasNext()) { + while (it.hasNext()) { BlockPosition position = it.next(); if (Block.fromStateId(getInstance().getBlockStateId(position)) != Block.AIR) blocks.add(position); } @@ -628,7 +628,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { */ public BlockPosition getTargetBlockPosition(int maxDistance) { Iterator it = new BlockIterator(this, maxDistance); - while(it.hasNext()) { + while (it.hasNext()) { BlockPosition position = it.next(); if (Block.fromStateId(getInstance().getBlockStateId(position)) != Block.AIR) return position; } diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 73d2b224f..4264821d8 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1079,9 +1079,11 @@ public class Player extends LivingEntity implements CommandSender { } @Override - protected void onAttributeChanged(@NotNull final AttributeInstance instance) { - if (instance.getAttribute().isShared() && playerConnection != null) + protected void onAttributeChanged(@NotNull final AttributeInstance attributeInstance) { + if (attributeInstance.getAttribute().isShared() && + playerConnection.getConnectionState() == ConnectionState.PLAY) { playerConnection.sendPacket(getPropertiesPacket()); + } } @Override