Domanda

I am writing a simple zombie-spawning plugin. The spawning is done by another thread. When I run the plugin, I get the following error after some time of playing:

[Server thread/ERROR]: Encountered an unexpected exception
 java.util.ConcurrentModificationException
 at java.util.HashMap$HashIterator.nextEntry(Unknown Source) ~[?:1.7.0_25]
 at java.util.HashMap$KeyIterator.next(Unknown Source) ~[?:1.7.0_25]
 at net.minecraft.server.v1_7_R1.EntityTracker.updatePlayers(EntityTracker.java:152) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
 at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:646) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
 at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
 at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
 at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
 at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]

This error continues to occur, but it occurs rarely and it seems random. My thoughts to this problem are that the spawning-thread is asynchroune to the EntityTracker, which leads to a exception because my thread modifies the entity-list while another part of bukkit tries to tell the players the positions of the monsters.
I don't have any idea what the exact error-source is, but I think I have to synchronize my thread with some part of bukkit. Does anyone have a solution for that? (And is there any way to set the items which a zombie will drop? The whole rotten flesh is starting to give me nightmares).

Thanks in advance.

È stato utile?

Soluzione

Your stacktrace is saying that you are trying to modify your collection from another thread. You could either try to synchronize your methods or use something like a ConcurrentHashMap.

If you don't really need the modification to be on another thread, you could also use Bukkit's schedule facility to do the job :

private final Map<T> map = new HashMap<T>();
// ... stuff happens

final T value;
// work with your value

server.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    public void run() {
        map.add(value);
    }
}, 0L);
// runs the run() method after 0 ticks on the main thread
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top