Skip to content

Commit

Permalink
Fix attribute copy
Browse files Browse the repository at this point in the history
  • Loading branch information
wode490390 committed Jul 23, 2024
1 parent 7fb89a3 commit 82f8965
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/main/java/cn/nukkit/entity/attribute/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import cn.nukkit.entity.attribute.AttributeModifier.Operation;
import cn.nukkit.math.Mth;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import lombok.ToString;

import javax.annotation.Nullable;
import java.util.*;
import java.util.Map.Entry;

/**
* Attribute
Expand Down Expand Up @@ -162,8 +162,15 @@ public void setDirty() {
}

public Attribute copy() {
EnumMap<Operation, Set<AttributeModifier>> modifiersByOperation = new EnumMap<>(Operation.class);
for (Entry<Operation, Set<AttributeModifier>> entry : this.modifiersByOperation.entrySet()) {
Set<AttributeModifier> modifiers = entry.getValue();
if (!modifiers.isEmpty()) {
modifiersByOperation.put(entry.getKey(), new HashSet<>(modifiers));
}
}
return new Attribute(id, name, minValue, maxValue, defaultValue, currentValue, redefinitionMode, shouldSend,
modifiersByOperation.clone(), modifiersById.clone(), calculatedValues.clone(), dirty);
modifiersByOperation, modifiersById.clone(), calculatedValues.clone(), dirty);
}

@Nullable
Expand All @@ -172,7 +179,7 @@ public AttributeModifier getModifier(UUID modifierId) {
}

public void addModifier(AttributeModifier modifier) {
if (!modifiersByOperation.computeIfAbsent(modifier.getOperation(), op -> new ObjectOpenHashSet<>()).add(modifier)) {
if (!modifiersByOperation.computeIfAbsent(modifier.getOperation(), op -> new HashSet<>()).add(modifier)) {
return;
}
modifiersById.put(modifier.getId(), modifier);
Expand All @@ -193,7 +200,7 @@ public void replaceModifier(AttributeModifier modifier) {
}
}

modifiersByOperation.computeIfAbsent(modifier.getOperation(), op -> new ObjectOpenHashSet<>()).add(modifier);
modifiersByOperation.computeIfAbsent(modifier.getOperation(), op -> new HashSet<>()).add(modifier);

setDirty();
}
Expand Down

0 comments on commit 82f8965

Please sign in to comment.