Modifying Zombie Speed/behavior

Discussion in 'Plugin Development' started by Major_Derp, Dec 22, 2012.

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

    Major_Derp

    I spent about the last 30 minutes looking through google, and bukkit forums for a way to modify zombie behavior/speed. I couldn't find any clear tutorials, or good ways to do so. I found some, but i got many errors in the code, and even when i fixed alot of the errors, there was some parts that i didn't know how to fix. I am fairly new to Java, and Making plugins, any help would be nice.
     
  2. Offline

    youngbawss22

    If you posted your code, i bet you could find some people to help. It would be much easier for them so they can locate the problems.
     
  3. Offline

    ShadowDog007

    Not sure if this would work.

    But this should slow zombies down.

    Code:
    @EventHandler(priority = EventPriority.NORMAL)
    public void onCreatureSpawn(final CreatureSpawnEvent event)
    {
            if (event.getEntityType() != EntityType.ZOMBIE)
                    return;
            final Zombie z = (Zombie) event.getEntity();
     
            getServer().getScheduler().runTaskTimer(this, new BukkitRunnable()
                    {
                            public void run()
                            {
                                    if (!z.isValid())
                                    {
                                            this.cancel();
                                            return;
                                    }
     
                                    Vector v = z.getVelocity();
     
                                    v.setX(v.getX() / 2.0D);
                                    v.setZ(v.getZ() / 2.0D);
                            }
                    }, 1L, 1L);
    }
    

    Edit: Nope. :(
     
  4. Offline

    Major_Derp

    Here is some of the code i saw from a tutorial, that i am trying to use, but it shows many errors.

    Code:
    public class CustomZombie extends net.minecraft.server.EntityZombie {
       
        public CustomZombie(World world) {
            super(world);
        }
     
        @Override
        public void s_(){
            Zombie zombie = (Zombie) this.Entity.getType();
     
            Location from = new Location(zombie.getWorld(), this.lastX, this.lastY, this.lastZ, this.lastYaw, this.lastPitch);
            Location to = new Location(zombie.getWorld(), this.locX, this.locY, this.locZ, this.yaw, this.pitch);
     
            ZombieMoveEvent event = new ZombieMoveEvent(zombie, from, to);
     
            this.world.getServer().getPluginManager().callEvent(event);
     
            if (event.isCancelled() && zombie.isDead() == false){
                return;
            }
     
            super.s_();
        }
     
    }
    This next bit is inside the onEnable
    Code:
     @SuppressWarnings("rawtypes")
                Class[] args = new Class[3];
                args[0] = Class.class;
                args[1] = String.class;
                args[2] = int.class;
     
                Method a = net.minecraft.server.EntityTypes.class.getDeclaredMethod("a", args);
                a.setAccessible(true);
     
                a.invoke(a, CustomZombie.class, "Zombie", 54);
            }catch (Exception e){
                e.printStackTrace();
                this.setEnabled(false);
            }
    And here is the last bit That is in the main class, but not in the onEnable/Disable
    Code:
    @EventHandler
        public void onCreatureSpawn(CreatureSpawnEvent event){
            if (event.isCancelled()) return;
     
            Location location = event.getLocation();
            Entity entity = event.getEntity();
            EntityType creatureType = event.getEntityType();
            World world = location.getWorld();
     
            net.minecraft.server.World mcWorld = ((CraftWorld) world).getHandle();
            net.minecraft.server.Entity mcEntity = (((CraftEntity) entity).getHandle());
     
            if (creatureType == EntityType.ZOMBIE && mcEntity instanceof CustomZombie == false){
                CustomZombie bloodMoonEntityZombie = new CustomZombie(mcWorld);
     
                bloodMoonEntityZombie.setPosition(location.getX(), location.getY(), location.getZ());
     
                mcWorld.removeEntity((net.minecraft.server.EntityZombie) mcEntity);
                mcWorld.addEntity(bloodMoonEntityZombie, SpawnReason.CUSTOM);
     
                return;
            }
        }
    
     
  5. Offline

    ShadowDog007

    Can you compile it?

    If not you probably need to import CraftBukkit as one of your libraries.
     
  6. Offline

    Major_Derp


    I tried importing craftbukkit, and now it works, but only somewhat.
    Once i imported craftbukkit, alot of the other errors were easily fixed, and now it runs fine, but the zombie modification only works on Zombies spawned from eggs. Here is the edited source code:

    The super zombie class:
    Code:
    import java.lang.reflect.Field;
     
    import net.minecraft.server.v1_4_5.EntityHuman;
    import net.minecraft.server.v1_4_5.EntityVillager;
    import net.minecraft.server.v1_4_5.PathfinderGoalBreakDoor;
    import net.minecraft.server.v1_4_5.PathfinderGoalFloat;
    import net.minecraft.server.v1_4_5.PathfinderGoalHurtByTarget;
    import net.minecraft.server.v1_4_5.PathfinderGoalLookAtPlayer;
    import net.minecraft.server.v1_4_5.PathfinderGoalMeleeAttack;
    import net.minecraft.server.v1_4_5.PathfinderGoalMoveThroughVillage;
    import net.minecraft.server.v1_4_5.PathfinderGoalMoveTowardsRestriction;
    import net.minecraft.server.v1_4_5.PathfinderGoalNearestAttackableTarget;
    import net.minecraft.server.v1_4_5.PathfinderGoalRandomLookaround;
    import net.minecraft.server.v1_4_5.PathfinderGoalRandomStroll;
     
    //import org.bukkit.World;
    import org.bukkit.craftbukkit.v1_4_5.util.UnsafeList;
     
    public class SuperZombie extends net.minecraft.server.v1_4_5.EntityZombie {
        public int damage;
     
        @SuppressWarnings("rawtypes")
        public SuperZombie(net.minecraft.server.v1_4_5.World world) {
            super(world);
            this.bw = .40F;  //Change this to your liking. This is were you set the speed
            this.damage = 15; // set the damage
    //There's also a ton of options of you do this.  play around with it
         
     
            try {
                Field gsa = net.minecraft.server.v1_4_5.PathfinderGoalSelector.class.getDeclaredField("a");
                gsa.setAccessible(true);
     
                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();
            }
     
            this.goalSelector.a(0, new PathfinderGoalFloat(this));
            this.goalSelector.a(1, new PathfinderGoalBreakDoor(this));
            this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, (float) (this.bw * 1.9f), false)); // this one to attack human
            this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityVillager.class, (float) this.bw, true));
            this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, (float) this.bw));
            this.goalSelector.a(5, new PathfinderGoalMoveThroughVillage(this, (float) this.bw, false));
            this.goalSelector.a(6, new PathfinderGoalRandomStroll(this, (float) this.bw));
            this.goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F)); // this one to look at human
            this.goalSelector.a(7, new PathfinderGoalRandomLookaround(this));
            this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, false));
            this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 8.0F, 0, true)); // this one to target human
            this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityVillager.class, 16.0F, 0, false));
        }
    }
    Here is the event handler:
    Code:
        @EventHandler
        public void onCreatureSpawn(CreatureSpawnEvent event){
            if (event.isCancelled()) return;
     
            Location location = event.getLocation();
            Entity entity = event.getEntity();
            EntityType entityType = event.getEntityType();
            World world = location.getWorld();
     
            net.minecraft.server.v1_4_5.World mcWorld = ((CraftWorld) world).getHandle();
            net.minecraft.server.v1_4_5.Entity mcEntity = (((CraftEntity) entity).getHandle());
     
            if (entityType == EntityType.ZOMBIE && mcEntity instanceof SuperZombie == false){
                SuperZombie bloodMoonEntityZombie = new SuperZombie(mcWorld);
     
                bloodMoonEntityZombie.setPosition(location.getX(), location.getY(), location.getZ());
     
                mcWorld.removeEntity((net.minecraft.server.v1_4_5.EntityZombie) mcEntity);
                mcWorld.addEntity(bloodMoonEntityZombie, SpawnReason.CUSTOM);
     
                return;
            }
        }
    And Here is the onEnable Part:
    Code:
    try{
                @SuppressWarnings("rawtypes")
                Class[] args = new Class[3];
                args[0] = Class.class;
                args[1] = String.class;
                args[2] = int.class;
     
                Method a = net.minecraft.server.v1_4_5.EntityTypes.class.getDeclaredMethod("a", args);
                a.setAccessible(true);
     
                a.invoke(a, SuperZombie.class, "Zombie", 54);
            }catch (Exception e){
                e.printStackTrace();
                this.setEnabled(false);
            }
     
  7. Offline

    ShadowDog007

    If all you want to do is change the zombies speed you could try just adding potion effects to the mob? And repeat the potion effects when it expires?

    Damage can be changed with EntityDamageByEntityEvent
     
Thread Status:
Not open for further replies.

Share This Page