Solved Trying to spawn entities completely stops code running

Discussion in 'Spigot Plugin Development' started by MCKGaming, Feb 24, 2020.

  1. Hi, I've got this code here to try to spawn a random entity at the player's position:
    Code (Java):
    Integer entID;
                        Random r = new Random();
                        try {
                            entID = r.nextInt(EntityType.values().length);
                            System.out.println("Entity ID found");
                        } catch (Exception e) {
                            entID = 1;
                            System.out.println("Entity ID errored, defaulted to 1");
                        }

                        EntityType toSpawn = EntityType.valueOf(EntityType.values()[entID].toString());
                        System.out.println(String.format("Found entity %s", toSpawn));

                        if (toSpawn.getEntityClass() == null) {
                            p.sendMessage("NULL");
                            break;
                        }
                        p.getWorld().spawn(p.getEyeLocation(), toSpawn.getEntityClass());


                        System.out.println("Spawned entity");
    When this code is run, however, everything runs fine up until
    Code (Java):
    p.getWorld().spawn(p.getEyeLocation(), toSpawn.getEntityClass());
    . At this point, the entity is not spawned and none of the code after this runs. I've debugged and neither the player, the world, the location or the entity class is null. Can someone help me figure out what's wrong? Thanks.
     
  2. Well for sure you should not use toSpawn.getEntityClass() but only toSpawn as it's already a valid EntityType.
    Then the try & catch block on the is out of context and that's not its purpose.
    But the main thing I can see is why the hell are you using valueOf if you already have the EntityType?
    You could straight up do:
    Code (Java):
    EntityType toSpawn = EntityType.values()[random]
    //where random is a random integer number, and a valid array index
     
    Not to mention that you should use the provided logger from the plugin instead of System.out.println
     
  3. toSpawn is a valid EntityType, however the spawn() method takes the class as the argument. spawnEntity() takes an EntityType but that didn't work either. I'm using spawn() rather than spawnEntity() as it has worked elsewhere in the plugin. Thanks for the input about valueOf and System.out.println
     
  4. Nope, the code just stops. The rest of the plugin (commands etc) still work however
     
  5. Oh, yeah. Sorry, completely forgot. Maybe some ignored try-catches?
     
  6. It's not surrounded by a try-catch. It is running in a Runnable to schedule it - is that the issue? I have other similar code elsewhere in my plugin that works fine, not inside a Runnable
     
  7. Are you cancelling the runnable someohow?
    Also, this would be the correct method to spawn an entity:
    Code (Text):
    p.getWorld().spawnEntity(p.getLocation(), EntityType.values()[random]);
     
  8. Weird. Is it async? If not, try to surround spawn line with try-catch for a test, but don't ignore catch and print it.
     
  9. Probably should have used a try-catch before. The code runs now and I actually have an error:
    Code (Java):
    [18:48:09] [pool-6-thread-1/WARN]: java.lang.IllegalStateException: Asynchronous entity add!
    [18:48:09] [pool-6-thread-1/WARN]:     at org.spigotmc.AsyncCatcher.catchOp(AsyncCatcher.java:14)
    [18:48:09] [pool-6-thread-1/WARN]:     at net.minecraft.server.v1_14_R1.WorldServer.addEntity0(WorldServer.java:927)
    [18:48:09] [pool-6-thread-1/WARN]:     at net.minecraft.server.v1_14_R1.WorldServer.addEntity(WorldServer.java:864)
    [18:48:09] [pool-6-thread-1/WARN]:     at org.bukkit.craftbukkit.v1_14_R1.CraftWorld.addEntity(CraftWorld.java:1710)
    [18:48:09] [pool-6-thread-1/WARN]:     at org.bukkit.craftbukkit.v1_14_R1.CraftWorld.spawn(CraftWorld.java:1717)
    [18:48:09] [pool-6-thread-1/WARN]:     at org.bukkit.craftbukkit.v1_14_R1.CraftWorld.spawn(CraftWorld.java:1308)
    [18:48:09] [pool-6-thread-1/WARN]:     at org.bukkit.craftbukkit.v1_14_R1.CraftWorld.spawnEntity(CraftWorld.java:639)
    [18:48:09] [pool-6-thread-1/WARN]:     at me.mck.runs.secondExecutes.lambda$static$4(entityplugin.java:454)
    [18:48:09] [pool-6-thread-1/WARN]:     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    [18:48:09] [pool-6-thread-1/WARN]:     at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
    [18:48:09] [pool-6-thread-1/WARN]:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
    [18:48:09] [pool-6-thread-1/WARN]:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
    [18:48:09] [pool-6-thread-1/WARN]:     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    [18:48:09] [pool-6-thread-1/WARN]:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    [18:48:09] [pool-6-thread-1/WARN]:     at java.lang.Thread.run(Unknown Source)
    Still not too sure what it means, however.
    It's run inside this:
    Code (Java):
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    which could be causing issues.
     
    #12 MCKGaming, Mar 8, 2020
    Last edited: Mar 8, 2020
  10. You're launching async thread(why not bukkit scheduler?). To safely interact with bukkit api you should do everything related to api in main thread. To do it from async you can use, again, bukkit scheduler.
    Bukkit.getScheduler().runTask(pluginInstance, () -> p.getWorld().spawnEntity(p.getLocation(), EntityType.values()[random]));
     
    • Like Like x 1
    • Agree Agree x 1
  11. That fixed it - thanks so much