0

I have no idea why I'm getting this error: error message

This is line 107 (for loop):

        for (Pair<UUID, UUID> pair : timeStopList) {

           if (pair.getA() == playerUUID) {

              cancelTimeStop(pair.getB());

           }

        }

I hate to ask what might seem like such a simple solution but I really have no idea whats going on! So I would appreciate it that if you know the answer you could tell me. Thanks!

Code for cancelTimeStop:

    public static void cancelTimeStop(UUID entityUUID) {

    Entity unknownEntity = Bukkit.getEntity(entityUUID);

    if (unknownEntity == null || unknownEntity.isDead()) return;

    LivingEntity entity = (LivingEntity) unknownEntity;

    entity.removePotionEffect(PotionEffectType.SLOW);
    entity.removePotionEffect(PotionEffectType.BLINDNESS);

    entity.setGravity(true);
    entity.setAI(true);
    entity.setCanPickupItems(true);
    entity.setSilent(false);

    removeEntityFromTimeStop(entityUUID);

}

removeEntityFromTimeStop() is:

    private static void removeEntityFromTimeStop(UUID entityUUID) {

    if (timeStopList == null) return;

    timeStopList.removeIf(pair -> pair.getB() == entityUUID);

}
  • 2
    A `ConcurrentModificationException` means you're altering a data structure (here - a `List`) while iterating over it. Can you share the code for `cancelTimeStop`? – Mureinik Sep 12 '21 at 18:33
  • @Mureinik ` Entity unknownEntity = Bukkit.getEntity(entityUUID); if (unknownEntity == null || unknownEntity.isDead()) return; LivingEntity entity = (LivingEntity) unknownEntity; entity.removePotionEffect(PotionEffectType.SLOW); entity.removePotionEffect(PotionEffectType.BLINDNESS); entity.setGravity(true); entity.setAI(true); entity.setCanPickupItems(true); entity.setSilent(false); removeEntityFromTimeStop(entityUUID);` – UberSuperBoss Sep 12 '21 at 18:42
  • It's really hard to understand code posted in comments like this. Please [edit] your post and add this as formatted text – Mureinik Sep 12 '21 at 18:43
  • Does this answer your question? [Why is a ConcurrentModificationException thrown and how to debug it](https://stackoverflow.com/questions/602636/why-is-a-concurrentmodificationexception-thrown-and-how-to-debug-it) – 9ilsdx 9rvj 0lo Sep 13 '21 at 11:57

2 Answers2

0

You can change slightly your for loop to avoid this exception:

Pair<UUID, UUID> pairToRemove = null;
for (Pair<UUID, UUID> pair : timeStopList) {
    if (pair.getA() == playerUUID) {
        pairToRemove = pair; // assign the found pair to pairToRemove variable
        break;
    }
}
if (pairToRemove != null) {
    cancelTimeStop(pairToRemove.getB());
}

UPDATE

P.S. I made the assumption that once a pair.getA() == playerUUID is found there is no need to continue the loop. If there are multiple chances that this if statement evaluates to true then you can adjust the code to:

List<Pair<UUID, UUID>> pairToRemoveList = new ArrayList();
for (Pair<UUID, UUID> pair : timeStopList) {
    if (pair.getA() == playerUUID) {
        pairToRemoveList.add(pair);
    }
}
if (!pairToRemoveList.isEmpty()) {
    for (Pair<UUID, UUID> pair : pairToRemoveList) {
        cancelTimeStop(pair.getB());
    }
}
pleft
  • 7,567
  • 2
  • 21
  • 45
0

Concurrent Modification Exception is thrown when iterating a collection and an item is removed from it. In this case a Pair<UUID, UUID> is removed from the timeStopList

timeStopList.removeIf(pair -> pair.getB() == entityUUID);

The solution is to use Iterator when you have to remove the list while iterating and no other modification is needed to your logic:

Iterator<Pair<UUID, UUID>> iter = cancelTimeStop.listIterator();
while(iter.hasNext()){
    if(...){
        iter.remove();
    }
}
gkatiforis
  • 1,588
  • 9
  • 19