Why does this throw a CuncurrentModificationException? Code (Text): for (PlayerShopInfo psi : main.signmanager.playershop){ }
Code (Text): Block attached = psi.getChestLocation().getBlock(); Location chestLoc = attached.getLocation(); if (loc.equals(chestLoc)) { // TODO CHECK FOR NPE getItemByUniqueID(psi.getItemUniqueID(), loc.getWorld()).remove(); main.signManager.playershop.remove(psi); }
ConcurrentModificationExceptions are called when (most) collections are being modified/read from by multiple sources at the same time. If you loop over the shops, and remove an item from it in the loop, there are 2 modifications going on at the same time. You can use an Iterator and Iterator#remove to avoid this
You're removing the item while iterating over the collection "main.signManager.playershop.remove(psi);" use an Iterator, or I believe looping over the collection in reverse would solve your issue as well.
@BakedPotato_HD Iterators are actually what Java turns your for-loops into when your code is compiled. Using a for-loop with an Iterator in it is something I like (this format): Code (Java): for (preLoop; boolean that keeps the loop going; run after each value) The Iterator is a class that an Iterable can give you by calling .iterator(). The Iterator class has a hasNext() method to check if it has any more elements, and a next() method to get the next element. If we put that in a simple for-loop: Code (Java): for (Iterator<PlayerShopInfo> it = main.signmanager.playershop.iterator(); it.hasNext();) { PlayerShopInfo info = it.next(); } Notice how I left the last part in the for line empty. Each part of the for loops can be left empty if you don't want to do anything there. Now that you have the PlayerShopInfo, you can do yiur stuff with it, and instead of calling main.signmanager.playershop.remove(info), you call it.remove()