Controling Zombie Speed

Discussion in 'Plugin Development' started by BadReuben, Nov 7, 2012.

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

    BadReuben

    I found the following code for controlling Zombie speed, however it seems to be buggy and causes entities (including non-zombies) to teleport around like Enderman. This is for a server running the 1.4 Beta Build #2455.


    Code:
    package me.blakkdock.zombies;
     
    import java.lang.reflect.Field;
     
     
    import org.bukkit.craftbukkit.util.UnsafeList;
     
     
    import net.minecraft.server.EntityHuman;
    import net.minecraft.server.EntityVillager;
    import net.minecraft.server.PathfinderGoalBreakDoor;
    import net.minecraft.server.PathfinderGoalFloat;
    import net.minecraft.server.PathfinderGoalHurtByTarget;
    import net.minecraft.server.PathfinderGoalLookAtPlayer;
    import net.minecraft.server.PathfinderGoalMeleeAttack;
    import net.minecraft.server.PathfinderGoalMoveThroughVillage;
    import net.minecraft.server.PathfinderGoalMoveTowardsRestriction;
    import net.minecraft.server.PathfinderGoalNearestAttackableTarget;
    import net.minecraft.server.PathfinderGoalRandomLookaround;
    import net.minecraft.server.PathfinderGoalRandomStroll;
    import net.minecraft.server.World;
     
     
    public class BlakkEntityZombie extends net.minecraft.server.EntityZombie {
     
        public BlakkEntityZombie(World world) {
            super(world);
            this.bw = 0.28F; //Zombie Speed 
         
     
            try {
                Field gsa = net.minecraft.server.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));
        }
    }
    
    Code:
    package me.blakkdock.zombies;
     
    import java.lang.reflect.Method;
    import java.util.logging.Logger;
     
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.craftbukkit.CraftWorld;
    import org.bukkit.craftbukkit.entity.CraftEntity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Entity;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import org.bukkit.plugin.PluginDescriptionFile;
     
    public class Zombies extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Zombies plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Disabled!!");
        }
       
        @Override
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + "V." + pdfFile.getVersion() + " Enabled!!");
           
            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.EntityTypes.class.getDeclaredMethod("a", args);
                a.setAccessible(true);
     
                a.invoke(a, BlakkEntityZombie.class, "Zombie", 54);
            }catch (Exception e){
                e.printStackTrace();
                this.setEnabled(false);
            }
        }
       
        @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 BlakkEntityZombie == false){
                BlakkEntityZombie blakkEntityZombie = new BlakkEntityZombie(mcWorld);
     
                blakkEntityZombie.setPosition(location.getX(), location.getY(), location.getZ());
     
                mcWorld.removeEntity((net.minecraft.server.EntityZombie) mcEntity);
                mcWorld.addEntity(blakkEntityZombie, SpawnReason.CUSTOM);
     
                return;
            }
        }
    }
    
     
  2. Offline

    Woobie

  3. Offline

    kabbage

    In 1.4, they renamed
    Code:
    this.bw
    to
    Code:
    this.bI
     
    BlakkDock likes this.
  4. Offline

    Deathmarine

    ^
    |
    He's right. Take a look at
    https://github.com/Bukkit/CraftBukk...n/java/net/minecraft/server/EntityZombie.java

    and

    https://github.com/Bukkit/CraftBukk...n/java/net/minecraft/server/EntityLiving.java

    OnEnable will overwrite every zombie with your custom Zombie.

    Honestly the methods I use now is to just edit the Monster directly and edit freely without having to extend the class. (Also cool for making baby, villager, or Villager Baby Zombies.)
    Here is a method Example of something I was working on with all the new 1.4 additions.
    Code:
    private void superZombie(Zombie z){
            Location loc = z.getLocation();
            EntityZombie zombie = ((CraftZombie) z).getHandle();
    try {
          int chance = plugin.gen.nextInt(22);
          if(chance==0){
          if(plugin.gen.nextBoolean())zombie.setBaby(true);
          if(!zombie.isBaby()){
    float speed = 0.42F;
    //42 and they are REALLY FAST
    Field fGoalSelector = EntityLiving.class.getDeclaredField("goalSelector");
          fGoalSelector.setAccessible(true);
          PathfinderGoalSelector gs = new PathfinderGoalSelector(((CraftWorld) loc.getWorld()).getHandle() != null && ((CraftWorld) loc.getWorld()).getHandle().methodProfiler != null ? ((CraftWorld) loc.getWorld()).getHandle().methodProfiler : null);
          gs.a(0, new PathfinderGoalFloat(zombie));
          gs.a(1, new PathfinderGoalBreakDoor(zombie));
          gs.a(2, new PathfinderGoalMeleeAttack(zombie, EntityHuman.class, speed, false));
          gs.a(3, new PathfinderGoalMeleeAttack(zombie, EntityVillager.class, speed, true));
          gs.a(4, new PathfinderGoalMoveTowardsRestriction(zombie, speed));
          gs.a(5, new PathfinderGoalMoveThroughVillage(zombie, speed, false));
          gs.a(6, new PathfinderGoalRandomStroll(zombie, speed));
          gs.a(7, new PathfinderGoalLookAtPlayer(zombie, EntityHuman.class, 15.0F));
          gs.a(7, new PathfinderGoalRandomLookaround(zombie));
          fGoalSelector.set(zombie,gs);
          }
          Material weapon = drop.weaponPicker();
          if(weapon!=null){
          CraftItemStack ci = new CraftItemStack(weapon,1);
          int once=plugin.gen.nextInt(22)+1;
          for(int i=0;i<once;i++){
          Enchantment en = drop.enchant();
          if(en!=null)ci.addUnsafeEnchantment(en, plugin.gen.nextInt(10)+1);
          }
          zombie.setEquipment(0,ci.getHandle());
          }
          if(plugin.gen.nextBoolean()){
          Material helmet = drop.getHelmet();
          if(helmet!=null)zombie.setEquipment(1,new CraftItemStack(helmet,1).getHandle());
          Material chest = drop.getChestPlate();
          if(chest!=null)zombie.setEquipment(2,new CraftItemStack(chest,1).getHandle());
          Material leg = drop.getLeggings();
          if(leg!=null)zombie.setEquipment(3,new CraftItemStack(leg,1).getHandle());
          Material boots = drop.getBoots();
          if(boots!=null)zombie.setEquipment(4,new CraftItemStack(boots,1).getHandle());
          }else{
          Material[] mats = drop.armorPicker();
          if(mats!=null){
          for(int i=0;i<mats.length;i++){
          zombie.setEquipment(i+1, new CraftItemStack(mats[i],1).getHandle());
          }
          }
          }
     
          }
          //0=ItemInHand
    } catch (Exception e) {
    e.printStackTrace();
    }
        }
    
    I won't fill in the variables considering sudo code should point you in the right direction, but that should give you some ideas.
    [​IMG]
    Badass random super zombies.

    Sup Man.
     
    BlakkDock and Woobie like this.
  5. Offline

    Jnorr44

    I just used navigation, because it can be changed on the go. The one problem I see with that is that they move along the path at that speed even if there is a block in front of them for some reason. An example is if there is a block in front of them and it breaks, they zoom forward really fast. I am not really sure about this method though, because it will probably break a lot.
     
Thread Status:
Not open for further replies.

Share This Page