ShopChest
ShopChest copied to clipboard
Error in task
See pastebin: https://pastebin.com/nFjGSaYX
It is generated if a shop is removed by player (/shop remove) at the same time when updateNearestShops() is called async. The next() call of an iterator over all shopLocations does fail because the collection has been modified and in this case next() generate an exception.
There are two solutions:
- Lock the collection when iterating over it so that no other thread can modify it. This would need a lot of rewriting your code (every time you modify / read that collection).
- The simple but not professional solution: Catch this exception and break the updateNearestShops() for this time (that's the same behavior it actually does because of that exception).
BTW: You could also iterate over a copy of that collection but in this case it is possible that items / holograms of removed shops are created and never got removed again (until server restart).
Found another solution: When using a copy of that collection. you can check, if the original still contains an equal shop before re-showing holograms and items that are already removed and killed.
But remember: creating a copy of that collection can also throw a CME. This can occure if a shop is removed concurrent to the new ArrayList<>(...) call. For this, you should consider to catch this and return an empty collection.
See the complete suggestion here: https://github.com/sigmaroot/ShopChest/commit/9cd7e0650eb776aff2e39681c981674412779d2e
If a merge request is wanted, just tell me here.