From cdef4b98ff6ad3e5b04735f50bdd62c12d45b76a Mon Sep 17 00:00:00 2001 From: Esophose Date: Sat, 1 Jun 2019 10:35:29 -0600 Subject: [PATCH] Fix concurrentmodificationexception --- .../java/com/songoda/epichoppers/tasks/HopTask.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/songoda/epichoppers/tasks/HopTask.java b/src/main/java/com/songoda/epichoppers/tasks/HopTask.java index 80aa34b..1ce1988 100644 --- a/src/main/java/com/songoda/epichoppers/tasks/HopTask.java +++ b/src/main/java/com/songoda/epichoppers/tasks/HopTask.java @@ -2,6 +2,7 @@ package com.songoda.epichoppers.tasks; import com.songoda.epichoppers.EpicHoppers; import com.songoda.epichoppers.boost.BoostData; +import com.songoda.epichoppers.hopper.HopperManager; import com.songoda.epichoppers.hopper.levels.modules.Module; import com.songoda.epichoppers.utils.HopperDirection; import com.songoda.epichoppers.utils.Methods; @@ -11,7 +12,6 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; -import org.bukkit.Particle; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; @@ -31,9 +31,11 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -61,6 +63,8 @@ public class HopTask extends BukkitRunnable { Collection hoppers = plugin.getHopperManager().getHoppers().values(); Iterator itr = hoppers.iterator(); + Set toRemove = new HashSet<>(); + main: while (itr.hasNext()) { com.songoda.epichoppers.hopper.Hopper hopper = itr.next(); @@ -78,7 +82,7 @@ public class HopTask extends BukkitRunnable { // If block is not a hopper remove and continue. if (block.getType() != Material.HOPPER) { - plugin.getHopperManager().removeHopper(location); + toRemove.add(location); continue; } @@ -282,6 +286,10 @@ public class HopTask extends BukkitRunnable { } } + // Clear out invalid hoppers + HopperManager hopperManager = plugin.getHopperManager(); + toRemove.forEach(hopperManager::removeHopper); + // Empty blacklist in preparation for next cycle. this.blacklist.clear(); }