Skip to content

Commit

Permalink
fix action deactivation sync to client
Browse files Browse the repository at this point in the history
- do not use iterator remove
  • Loading branch information
Cheaterpaul committed Dec 14, 2023
1 parent 32b0230 commit 06bc582
Showing 1 changed file with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,21 @@ public void readUpdateFromServer(@NotNull CompoundTag nbt) {
*/
if (nbt.contains("actions_active")) {
CompoundTag active = nbt.getCompound("actions_active");
for (ObjectIterator<Object2IntMap.Entry<ResourceLocation>> it = activeTimers.object2IntEntrySet().iterator(); it.hasNext(); ) {
Object2IntMap.Entry<ResourceLocation> client_active = it.next();
List<ResourceLocation> toRemove = new ArrayList<>();
for (Object2IntMap.Entry<ResourceLocation> client_active : activeTimers.object2IntEntrySet()) {
String key = client_active.getKey().toString();
if (active.contains(key)) {
client_active.setValue(active.getInt(key));
nbt.remove(key);
} else {
@SuppressWarnings("unchecked")
ILastingAction<T> action = (ILastingAction<T>) RegUtil.getAction(client_active.getKey());
it.remove(); //Remove here so that deactivate action does not break the iterator
deactivateAction(action);
toRemove.add(client_active.getKey());
}
}
toRemove.forEach(id -> {
@SuppressWarnings("unchecked")
ILastingAction<T> action = (ILastingAction<T>) RegUtil.getAction(id);
deactivateAction(action);
});
for (String key : active.getAllKeys()) {
ResourceLocation id = new ResourceLocation(key);
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -412,19 +414,16 @@ public boolean updateActions() {
}
}

for (Iterator<Object2IntMap.Entry<ResourceLocation>> it = activeTimers.object2IntEntrySet().iterator(); it.hasNext(); ) {
Object2IntMap.Entry<ResourceLocation> entry = it.next();
List<ResourceLocation> toRemove = new ArrayList<>();
for (Object2IntMap.Entry<ResourceLocation> entry : activeTimers.object2IntEntrySet()) {
int newtimer = entry.getIntValue() - 1;
ResourceLocation id = entry.getKey();
@SuppressWarnings("unchecked")
ILastingAction<T> action = (ILastingAction<T>) RegUtil.getAction(id);
if (newtimer == 0) {
it.remove();//Remove here so that deactivate action does not break the iterator
deactivateAction(action, true);
cooldownTimers.put(id, expectedCooldownTimes.getInt(id));

dirty = true;
if (newtimer == 0) {
toRemove.add(id);
} else {
@SuppressWarnings("unchecked")
ILastingAction<T> action = (ILastingAction<T>) RegUtil.getAction(id);
/*
* If the event result is DENY, the lasting action will always be deactivated next tick and won't call {@link de.teamlapen.vampirism.api.entity.player.actions.ILastingAction#onUpdate(de.teamlapen.vampirism.api.entity.player.IFactionPlayer)}.
* If the event result is ALLOW, the lasting action will call onUpdate, but the return value will be ignored.
Expand All @@ -444,6 +443,13 @@ public boolean updateActions() {
}
}
}
toRemove.forEach(id -> {
@SuppressWarnings("unchecked")
ILastingAction<T> action = (ILastingAction<T>) RegUtil.getAction(id);
deactivateAction(action, true);
cooldownTimers.put(id, expectedCooldownTimes.getInt(id));
dirty = true;
});
if (dirty) {
dirty = false;
return true;
Expand Down

0 comments on commit 06bc582

Please sign in to comment.