Trouble when spawning a custom entity

Discussion in 'Plugin Development' started by Rprrr, Apr 25, 2013.

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

    Rprrr

    Hi,

    I'm trying to spawn a custom entity with my plugin - this, however, leads to tons of errors and a console being flooded with this same error message:

    Code:
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_5_R2.event.CraftEventFactory.callCreatureSp
    awnEvent(CraftEventFactory.java:230)
            at net.minecraft.server.v1_5_R2.World.addEntity(World.java:925)
            at net.manticore.gamemechanics.mobs.SpawnListener.onEntitySpawn(SpawnLis
    tener.java:68)
            at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_5_R2.event.CraftEventFactory.callCreatureSp
    awnEvent(CraftEventFactory.java:230)
            at net.minecraft.server.v1_5_R2.World.addEntity(World.java:925)
            at net.manticore.gamemechanics.mobs.SpawnListener.onEntitySpawn(SpawnLis
    tener.java:68)
            at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_5_R2.event.CraftEventFactory.callCreatureSp
    awnEvent(CraftEventFactory.java:230)
    I honestly have no idea how to fix this. I've done some research, and found that one should always load the plugin before the server loads the worlds and that the custom entity should be added to the server list.

    I've done those things, but I'm still getting this error.

    I will show you my custom zombie class:

    Code:
    public class HostileSpeedZombie extends net.minecraft.server.v1_5_R2.EntityZombie {
     
        @SuppressWarnings("rawtypes")
        public HostileSpeedZombie(World world) {
            super(world);
            this.bw = 0.46F;  // new speed  0.46 is double normal zombie
     
            try {
           
                //enable PathfinderGoalSelector's "a" feild to be editable
                Field gsa = net.minecraft.server.v1_5_R2.PathfinderGoalSelector.class.getDeclaredField("a");
                gsa.setAccessible(true);
     
                // ok now take this instances goals and targets and blank them
                gsa.set(this.goalSelector, new UnsafeList());
                gsa.set(this.targetSelector, new UnsafeList());
           
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
     
            // now re-add goals and targets  with new speed
            // you could remove or edit these (or add line from other entities) to change behaviours.
     
            this.goalSelector.a(0, new PathfinderGoalFloat(this));
            this.goalSelector.a(1, new PathfinderGoalBreakDoor(this));
            this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, (float) 0.5F, false));
            this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityVillager.class, (float) 0.5F, true));
            this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, (float) 0.28F));
            this.goalSelector.a(5, new PathfinderGoalMoveThroughVillage(this, (float) 0.28F, false));
            this.goalSelector.a(6, new PathfinderGoalRandomStroll(this, (float) 0.20F));
            this.goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
            this.goalSelector.a(7, new PathfinderGoalRandomLookaround(this));
            this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, false));
            this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 16.0F, 0, true));
            this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityVillager.class, 16.0F, 0, false));
        }
    }
    
    So, I'm getting the error in this SpawnListener class all the time (it replaces normal zombies by HostileSpeedZombies) (the error is at 'world.addEntity(zomb, SpawnReason.CUSTOM)'):

    Code:
        @EventHandler
        public void onEntitySpawn(CreatureSpawnEvent e){
       
            if (e.getEntity() instanceof Zombie){
           
                World world = ((CraftWorld)e.getLocation().getWorld()).getHandle();
           
                HostileSpeedZombie zomb = new HostileSpeedZombie(world);
                zomb.setLocation(e.getLocation().getX(), e.getLocation().getY(), e.getLocation().getZ(), e.getLocation().getPitch(), e.getLocation().getYaw());
           
                world.addEntity(zomb, SpawnReason.CUSTOM);
           
                e.setCancelled(true);
     
    //...
    I honestly have no idea why this isn't working - if anyone's experienced with this, it would be nice to receive help.

    Thanks in advance!
     
  2. Offline

    Tzeentchful

    Rprrr
    You don't want to cancel the event you want to simply remove the zombie that spawn and spawn your one.
    Code:java
    1.  
    2. public void onCreatureSpawn(CreatureSpawnEvent event){
    3. if (event.isCancelled()) {
    4. return;
    5. }
    6. Location location = event.getLocation();
    7. Entity entity = event.getEntity();
    8. CreatureType creatureType = event.getCreatureType();
    9. World world = location.getWorld();
    10.  
    11. net.minecraft.server.World mcWorld = ((CraftWorld) world).getHandle();
    12. net.minecraft.server.Entity mcEntity = (((CraftEntity) entity).getHandle());
    13.  
    14. if (creatureType == CreatureType.ZOMBIE && !(mcEntity instanceof HostileSpeedZombie)){
    15. HostileSpeedZombie zomb = new HostileSpeedZombie(mcWorld);
    16.  
    17. zomb.setPosition(location.getX(), location.getY(), location.getZ());
    18.  
    19. mcWorld.removeEntity((net.minecraft.server.EntityZombie) mcEntity);
    20. mcWorld.addEntity(zomb, SpawnReason.CUSTOM);
    21. }
    22. }
    23.  
     
    MoeMix likes this.
Thread Status:
Not open for further replies.

Share This Page