[Error] Caused by: java.lang.IllegalStateException: Asynchronous entity add!

Discussion in 'Spigot Help' started by Yekllurt, May 22, 2015.

  1. Hi Guys,
    while I have tryed spawning my custom Zombie this error came:
    [22:16:34] [Async Chat Thread - #2/ERROR]: Could not pass event AsyncPlayerChatEvent to EvilDead v0.0.0.01
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.PlayerConnection.chat(PlayerConnection.java:1061) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:999) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_31]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_31]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.IllegalStateException: Asynchronous entity add!
    at org.spigotmc.AsyncCatcher.catchOp(AsyncCatcher.java:14) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.World.addEntity(World.java:991) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.World.addEntity(World.java:987) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at me.Yekllurt.EvilDead.CustomEntitys.EntityTypes.spawnCustomStandardZombie(EntityTypes.java:26) ~[?:?]
    at me.Yekllurt.EvilDead.Listeners.onChat(Listeners.java:32) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_31]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    ... 11 more

    Spawning the custom Zombie (The error comes in the line addEntity in the spawn Method):
    import java.lang.reflect.Field;
    import java.util.Map;

    import net.minecraft.server.v1_8_R2.Entity;

    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_8_R2.CraftWorld;

    public enum EntityTypes {

    CustomStandardZombie("Zombie", 54, CustomStandardZombie.class);


    EntityTypes(String name, int id, Class<? extends Entity> custom) {
    addToMaps(custom, name, id);
    }

    public static void spawnCustomStandardZombie(Location loc){
    /*entity.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
    ((CraftWorld)loc.getWorld()).getHandle().addEntity(entity); */
    CraftWorld craftWorld = (CraftWorld) loc.getWorld();
    CustomStandardZombie customStandardZombie = new CustomStandardZombie(craftWorld);
    customStandardZombie.getBukkitEntity().teleport(loc);
    craftWorld.getHandle().addEntity(customStandardZombie);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private static void addToMaps(Class clazz, String name, int id){

    ((Map)getPrivateField("c", net.minecraft.server.v1_8_R2.EntityTypes.class, null)).put(name, clazz);
    ((Map)getPrivateField("d", net.minecraft.server.v1_8_R2.EntityTypes.class, null)).put(clazz, name);
    ((Map)getPrivateField("e", net.minecraft.server.v1_8_R2.EntityTypes.class, null)).put(Integer.valueOf(id), clazz);
    ((Map)getPrivateField("f", net.minecraft.server.v1_8_R2.EntityTypes.class, null)).put(clazz, Integer.valueOf(id));
    ((Map)getPrivateField("g", net.minecraft.server.v1_8_R2.EntityTypes.class, null)).put(name, Integer.valueOf(id));
    }

    public static Object getPrivateField(String fieldName, Class clazz, Object object) {
    Field field;
    Object o = null;

    try {
    field = clazz.getDeclaredField(fieldName);

    field.setAccessible(true);

    o = field.get(object);
    } catch (NoSuchFieldException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    }

    return o;
    }

    }

    Custom Zombie Class:
    import net.minecraft.server.v1_8_R2.*;
    import org.bukkit.craftbukkit.v1_8_R2.CraftWorld;

    import java.lang.reflect.Field;
    import java.util.List;

    public class CustomStandardZombie extends EntityChicken {


    public CustomStandardZombie(org.bukkit.World world) {

    super(((CraftWorld) world).getHandle());

    List goalB = (List)getPrivateField("b", PathfinderGoalSelector.class, goalSelector); goalB.clear();
    List goalC = (List)getPrivateField("c", PathfinderGoalSelector.class, goalSelector); goalC.clear();
    List targetB = (List)getPrivateField("b", PathfinderGoalSelector.class, targetSelector); targetB.clear();
    List targetC = (List)getPrivateField("c", PathfinderGoalSelector.class, targetSelector); targetC.clear();

    }

    public static Object getPrivateField(String fieldName, Class clazz, Object object) {
    Field field;
    Object o = null;

    try {
    field = clazz.getDeclaredField(fieldName);

    field.setAccessible(true);

    o = field.get(object);
    } catch (NoSuchFieldException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    }

    return o;
    }

    }
    Thx for any productive help!
     
  2. Never call API methods from another than the main thread.