Solved Changing an entity's speed [Code is in comments]

Discussion in 'Plugin Development' started by krazyswaggaO, Oct 21, 2012.

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

    krazyswaggaO

  2. Offline

    ienze

    Can I look at source..?
     
  3. Offline

    hice3000

    I think you cant do that by the server!
     
  4. Offline

    skipperguy12

    Hice You can, look at the link he provided

    I too would like to know how to modify zombie speed. Preferably also entity damage :)
     
  5. Offline

    Codex Arcanum

    Will applying a speed potion effect work? I haven't tried it, but it seems likely to.
     
  6. Offline

    krazyswaggaO

    owo
    I found out how to. If you wanna know, here's how
    Create a new class:
    Code:
    public class SuperZombie extends net.minecraft.server.EntityZombie {
        public SuperZombie(World world) {
            super(world);
            this.bw = 0.28F;   //Change this to your liking. This is were you set the speed 
            this.damage = 5; // set the damage
    //Theres also a ton of options of you do this.   play around with it
           
     
            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, this.bw * 1.9f, false)); // this one to attack human
            this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityVillager.class, this.bw, true));
            this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, this.bw));
            this.goalSelector.a(5, new PathfinderGoalMoveThroughVillage(this, this.bw, false));
            this.goalSelector.a(6, new PathfinderGoalRandomStroll(this, 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));
        }

    Put this in the onEnable
    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.EntityTypes.class.getDeclaredMethod("a", args);
                a.setAccessible(true);
     
                a.invoke(a, SuperZombie.class, "Zombie", 54);
            }catch (Exception e){
                e.printStackTrace();
                this.setEnabled(false);
            }



    If you want the zombie to spawn in the world
    Code:
    @EventHandler
        public void onCreatureSpawn(CreatureSpawnEvent event){
            if (event.isCancelled()) return;
     
            Location location = event.getLocation();
            Entity entity = event.getEntity();
            CreatureType creatureType = event.getCreatureType();
            World world = location.getWorld();
     
            net.minecraft.server.World mcWorld = ((CraftWorld) world).getHandle();
            net.minecraft.server.Entity mcEntity = (((CraftEntity) entity).getHandle());
     
            if (creatureType == CreatureType.ZOMBIE && mcEntity instanceof SuperZombie == false){
                SuperZombie bloodMoonEntityZombie = new SuperZombie(mcWorld);
     
                bloodMoonEntityZombie.setPosition(location.getX(), location.getY(), location.getZ());
     
                mcWorld.removeEntity((net.minecraft.server.EntityZombie) mcEntity);
                mcWorld.addEntity(bloodMoonEntityZombie, SpawnReason.CUSTOM);
     
                return;
            }
        }
     
  7. Offline

    Major_Derp

    I recently found this article and i tried some of this code, and alot of it brings up errors. I know some of the things are deapreciated, and i replaced some of the deapreciated things, but any errors still popped up, also net.minecraft always comes up as an error too. I am a beginner, and Im new to this, any help would be nice.
     
  8. Offline

    krazyswaggaO

    Because this is from 1.3.2. I haven't had time to find the most recent variables or changing things around to fit the new build. You'll have to do some research on your own
     
  9. Offline

    fireblast709

    krazyswaggaO Major_Derp why not use this method...
     
  10. Offline

    krazyswaggaO

    Because it's been proven that it doesn't work on all mobs. It's also not going to help you out in the long run.
     
  11. Offline

    fireblast709

    Can you post the proof?
     
  12. Offline

    Major_Derp

    fireblast709
    I actually looked through the code a bit and changed some things, and i got it to work, but here are the small problems with it.
    1. The this.damage does not work
    2. Only zombies spawned form eggs are modified, the naturally spawned zombies arn't modified
    Here is the updated code:
    The SuperZombie Class:
    Code:
    import java.lang.reflect.Field;
     
    import net.minecraft.server.v1_4_6.EntityHuman;
    import net.minecraft.server.v1_4_6.EntityVillager;
    import net.minecraft.server.v1_4_6.PathfinderGoalBreakDoor;
    import net.minecraft.server.v1_4_6.PathfinderGoalFloat;
    import net.minecraft.server.v1_4_6.PathfinderGoalHurtByTarget;
    import net.minecraft.server.v1_4_6.PathfinderGoalLookAtPlayer;
    import net.minecraft.server.v1_4_6.PathfinderGoalMeleeAttack;
    import net.minecraft.server.v1_4_6.PathfinderGoalMoveThroughVillage;
    import net.minecraft.server.v1_4_6.PathfinderGoalMoveTowardsRestriction;
    import net.minecraft.server.v1_4_6.PathfinderGoalNearestAttackableTarget;
    import net.minecraft.server.v1_4_6.PathfinderGoalRandomLookaround;
    import net.minecraft.server.v1_4_6.PathfinderGoalRandomStroll;
     
    //import org.bukkit.World;
    import org.bukkit.craftbukkit.v1_4_6.util.UnsafeList;
     
     
    public class SuperZombie extends net.minecraft.server.v1_4_6.EntityZombie {
     
     
        @SuppressWarnings("rawtypes")
        public SuperZombie(net.minecraft.server.v1_4_6.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_6.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));
        }
    }
    The @EventHandler part:
    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_6.World mcWorld = ((CraftWorld) world).getHandle();
            net.minecraft.server.v1_4_6.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_6.EntityZombie) mcEntity);
                mcWorld.addEntity(bloodMoonEntityZombie, SpawnReason.CUSTOM);
     
                return;
            }
        }
    And the part inside the onEnable:
    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_6.EntityTypes.class.getDeclaredMethod("a", args);
                a.setAccessible(true);
     
                a.invoke(a, SuperZombie.class, "Zombie", 54);
            }catch (Exception e){
                e.printStackTrace();
                this.setEnabled(false);
            }
    Hopefully someone can help fix some of the problems, But for the most part it works, But only on zombies spawned through eggs.
     
  13. Offline

    krazyswaggaO

    Hmmm... interesting. It seems that the zombie isn't being replaced on the event.
    CorrieKay You were the person I figured out how to work with changing entities. Any ideas?
     
  14. Offline

    fireblast709

    Besides changing the egg, create a CreatureSpawnEvent listener and replace them if the SpawnReason != spawn egg
     
  15. Offline

    CorrieKay

    xD It was just a bit of derping around on my part, honestly, i didnt do much of any figuring out. Everyone else did, i just pieced it all together. I dislike touching the obfuscated code for NMS, so i dropped the idea i had at that point, and havnt played around with it since :\

    Sorry!
     
  16. Offline

    Major_Derp

    I will try this, and i will get back on how it works. it sounds like it would work, but who knows! And ive somewhat abandoned the project for now, im working on 2 other projects to make my java skills better because im just beginning, but ill get back to the project in a bit.
     
Thread Status:
Not open for further replies.

Share This Page