How do I properly spawn an Entity on a secondary Thread?

Discussion in 'Plugin Development' started by Soul_Eater301, Oct 4, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    Soul_Eater301

    I am developing a plugin that uses Zombies to create a Minigame.
    These Zombies will spawn in timed waves (that's why I need the secondary Thread), and there needs to be a lot of them. My problem is that when I do spawn a lot of Zombies (or any other Entites if that helps) after a while I will inevitably get the Error java.util.ConcurrentModificationException.
    BTW these Zombies are not custom, I do, however, change their Names and Items.

    I know that this Error occures e.g. when an Entry is removed (or added?) out of an ArrayList while it is being iterated over, I assume this Entry might be the Zombie I'm trying to spawn and the ArrayList might be the EntityList of the current World, so my Questions are as follows:
    - Is there a way I can add these Zombies to a Queue so Bukkit handles the spawning of them at the correct time in the Game Tick?
    - Can I maybe call a function that sends this information to the Bukkit Server instead of world.spawnEntity()?
    - Can I somehow move the spawning of the Zombies into the Main Thread?

    Thanks in advance!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Soul_Eater301 Make a BukkitRunnable, you can run it with runTask, that will make it sync again.
     
  3. Offline

    Soul_Eater301

    Can I call this Runnable out of my second Thread or do I replace the Thread all along?
     
  4. Offline

    timtower Administrator Administrator Moderator

    You can call it from the second thread.
    I don't use other things than BukkitRunnable to go from async to sync.
    But you might be better off using BukkitRunnable.runTaskTimer, then you don't need an async thread all together.
     
  5. Offline

    Soul_Eater301

    I'll try that out, thanks for the quick help!
     
  6. Offline

    timtower Administrator Administrator Moderator

    Let me know when you got it working ;)
     
Thread Status:
Not open for further replies.

Share This Page