Fix race condition when concurrently Stealing

This commit is contained in:
P0nk
2023-03-15 22:38:54 +01:00
parent 703ae30a27
commit 7ed7b25268
2 changed files with 9 additions and 17 deletions

View File

@@ -60,6 +60,7 @@ import java.util.List;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
@@ -86,7 +87,7 @@ public class Monster extends AbstractLoadedLife {
private final Set<Integer> usedAttacks = new HashSet<>();
private Set<Integer> calledMobOids = null;
private WeakReference<Monster> callerMob = new WeakReference<>(null);
private final List<Integer> stolenItems = new ArrayList<>(5);
private final AtomicBoolean isStolen = new AtomicBoolean(false);
private int team;
private int parentMobOid = 0;
private int spawnEffect = 0;
@@ -1644,12 +1645,9 @@ public class Monster extends AbstractLoadedLife {
return stats.getName();
}
public void addStolen(int itemId) {
stolenItems.add(itemId);
}
public List<Integer> getStolen() {
return stolenItems;
public boolean trySteal() {
boolean wasAlreadyStolen = this.isStolen.getAndSet(true);
return !wasAlreadyStolen;
}
public void setTempEffectiveness(Element e, ElementalEffectiveness ee, long milli) {
@@ -2177,4 +2175,4 @@ public class Monster extends AbstractLoadedLife {
this.getMap().dismissRemoveAfter(this);
}
}
}