Item-NBT-API icon indicating copy to clipboard operation
Item-NBT-API copied to clipboard

clearNBT() function giving ConcurrentModificationException

Open blubbarbs opened this issue 3 years ago • 1 comments

I was trying to clear an NBT compound for my plugin and I kept getting ConcurrentModificationExceptions, so I decided to make a quick command to make sure it wasn't me. The command does this:

NBTContainer test = new NBTContainer();

test.setString("test", "t");
test.setInteger("test2", 1);
test.addCompound("test3");
test.clearNBT();

However, when the command is run, I get a ConcurrentModificationException at the clearNBT() line. Attached is the log for that. I am using this API for the commands, which includes this NBT API as a shaded jar. However, my plugin does not use this and has this version of the API as a maven dependency.

Some additional info is that replacing the clearNBT() function call with a loop going through all of the keys and deleting them gives the same result. However, copying the keys into a new collection and deleting them after iterating through that collection does not give the error, so I feel like this is probably an easy fix.

EDIT: I tested the above code after removing the command API as a dependency, and the error above still happens.

blubbarbs avatar Aug 04 '22 03:08 blubbarbs

Ahhhhhh craaaap I think I know what's going on. https://github.com/tr7zw/Item-NBT-API/blob/0a033f1fc919351907a827611cec296747c46776/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBTCompound.java#L848 That list seems to be linked deep down to the nbt internals, instead of being a copy. On items this was fine, but for NBTContainers it causes the ConcurrentModificationExceptions. Will be fixed in the 2.11.x release.

tr7zw avatar Aug 04 '22 16:08 tr7zw