Skip to content

Commit

Permalink
Optimize looping over attribute modifier properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Axionize committed Oct 5, 2024
1 parent 0ddf0b5 commit c4848ef
Showing 1 changed file with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,29 @@ public void recalculate() {
}

public double with(WrapperPlayServerUpdateAttributes.Property property) {
double d0 = property.getValue();
double baseValue = property.getValue();
double additionSum = 0;
double multiplyBaseSum = 0;
double multiplyTotalProduct = 1.0;

List<WrapperPlayServerUpdateAttributes.PropertyModifier> modifiers = property.getModifiers();
modifiers.removeIf(modifier -> modifier.getUUID().equals(SPRINTING_MODIFIER_UUID) || modifier.getName().getKey().equals("sprinting"));

for (WrapperPlayServerUpdateAttributes.PropertyModifier attributemodifier : modifiers) {
if (attributemodifier.getOperation() == WrapperPlayServerUpdateAttributes.PropertyModifier.Operation.ADDITION)
d0 += attributemodifier.getAmount();
}

double d1 = d0;

for (WrapperPlayServerUpdateAttributes.PropertyModifier attributemodifier : modifiers) {
if (attributemodifier.getOperation() == WrapperPlayServerUpdateAttributes.PropertyModifier.Operation.MULTIPLY_BASE)
d1 += d0 * attributemodifier.getAmount();
}

for (WrapperPlayServerUpdateAttributes.PropertyModifier attributemodifier : modifiers) {
if (attributemodifier.getOperation() == WrapperPlayServerUpdateAttributes.PropertyModifier.Operation.MULTIPLY_TOTAL)
d1 *= 1.0D + attributemodifier.getAmount();
for (WrapperPlayServerUpdateAttributes.PropertyModifier modifier : modifiers) {
switch (modifier.getOperation()) {
case ADDITION:
additionSum += modifier.getAmount();
break;
case MULTIPLY_BASE:
multiplyBaseSum += modifier.getAmount();
break;
case MULTIPLY_TOTAL:
multiplyTotalProduct *= (1.0 + modifier.getAmount());
break;
}
}

double newValue = GrimMath.clamp(d1, min, max);
double newValue = GrimMath.clamp((baseValue + additionSum) * (1 + multiplyBaseSum) * multiplyTotalProduct, min, max);
if (setRewriter != null) {
newValue = setRewriter.apply(this.value, newValue);
}
Expand Down

0 comments on commit c4848ef

Please sign in to comment.