Fix attributes not being refreshed client side

This commit is contained in:
themode 2021-01-06 20:45:40 +01:00
parent d6a3b18f04
commit f5d550cda9
3 changed files with 16 additions and 27 deletions

View File

@ -18,7 +18,6 @@ public class AttributeInstance {
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) {
@ -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;
}
}

View File

@ -613,7 +613,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
public List<BlockPosition> getLineOfSight(int maxDistance) {
List<BlockPosition> blocks = new ArrayList<>();
Iterator<BlockPosition> 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<BlockPosition> 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;
}

View File

@ -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