[Tutorial] Increasing pathfinding range of an entity

Discussion in 'Resources' started by TomTheDeveloper, Sep 26, 2013.

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

    TomTheDeveloper

    Hello,

    I am going to show you how to increase the pathfinding range of an entity. I know you can change it in another way, but this is another easy way when you have made a custom entity. (I think this is the easiest way when you have already created your own custom zombie)

    First you need to make a custom entity. In this case, I made a custom zombie.

    Code:java
    1. public class CustomZombie extends net.minecraft.server.v1_6_R2.EntityZombie {
    2. public int damage;
    3. private float bw;
    4.  
    5.  
    6. @SuppressWarnings("rawtypes")
    7. public CustomZombie(net.minecraft.server.v1_6_R2.World world) {
    8. super(world);
    9. this.bw = 1.0F; //Change this to your liking. This is were you set the speed
    10. this.damage = 15; // set the damage
    11. //There's also a ton of options of you do this. play around with it
    12.  
    13.  
    14. try {
    15. Field gsa = net.minecraft.server.v1_6_R2.PathfinderGoalSelector.class.getDeclaredField("a");
    16. gsa.setAccessible(true);
    17.  
    18. gsa.set(this.goalSelector, new UnsafeList());
    19. gsa.set(this.targetSelector, new UnsafeList());
    20. } catch (SecurityException e) {
    21. e.printStackTrace();
    22. } catch (NoSuchFieldException e) {
    23. e.printStackTrace();
    24. e.printStackTrace();
    25. e.printStackTrace();
    26. }
    27.  
    28. this.goalSelector.a(0, new PathfinderGoalFloat(this));
    29. this.goalSelector.a(1, new PathfinderGoalBreakDoorFaster(this));
    30. this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, (float) (this.bw) , false)); // this one to attack human
    31. this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityIronGolem.class, (float) this.bw, true));
    32. this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityVillager.class, (float) this.bw, true));
    33. this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, (float) this.bw));
    34. this.goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F)); // this one to look at human
    35. this.goalSelector.a(7, new PathfinderGoalRandomLookaround(this));
    36. this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, false));
    37. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 0, true)); // this one to target human
    38. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityVillager.class, 0, false));
    39. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityIronGolem.class, 0, false));
    40.  
    41.  
    42. }
    43.  
    44.  
    45. }


    Now you have made you custom zombie, you just have to do one thing more. Add this:

    Code:java
    1. @Override
    2. protected void ay() {
    3. super.ay();
    4. this.getAttributeInstance(GenericAttributes.b).setValue(70.0D);
    5. }


    The value 70.0 stands for the pathfinding range. The range of a normal zombie is 40.0D. To increase the range, you just have to increase that number.

    I know there are other ways to increase the pathfinding range of custom entity's, but this is the easiest way when you already have made a custom zombie

    Whole the code:

    Whole the code: (open)
    Code:java
    1. public class CustomZombie extends net.minecraft.server.v1_6_R2.EntityZombie {
    2. public int damage;
    3. private float bw;
    4.  
    5. @Override
    6. protected void ay() {
    7. super.ay();
    8. this.getAttributeInstance(GenericAttributes.b).setValue(70.0D);
    9. }
    10.  
    11. @SuppressWarnings("rawtypes")
    12. public CustomZombie(net.minecraft.server.v1_6_R2.World world) {
    13. super(world);
    14. this.bw = 1.5F; //Change this to your liking. This is were you set the speed
    15. this.damage = 15; // set the damage
    16. //There's also a ton of options of you do this. play around with it
    17.  
    18.  
    19. try {
    20. Field gsa = net.minecraft.server.v1_6_R2.PathfinderGoalSelector.class.getDeclaredField("a");
    21. gsa.setAccessible(true);
    22.  
    23. gsa.set(this.goalSelector, new UnsafeList());
    24. gsa.set(this.targetSelector, new UnsafeList());
    25. } catch (SecurityException e) {
    26. e.printStackTrace();
    27. } catch (NoSuchFieldException e) {
    28. e.printStackTrace();
    29. e.printStackTrace();
    30. e.printStackTrace();
    31. }
    32.  
    33. this.goalSelector.a(0, new PathfinderGoalFloat(this));
    34. this.goalSelector.a(1, new PathfinderGoalBreakDoorFaster(this));
    35. this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, (float) (this.bw) , false)); // this one to attack human
    36. this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityIronGolem.class, (float) this.bw, true));
    37. this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityVillager.class, (float) this.bw, true));
    38. this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, (float) this.bw));
    39. this.goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F)); // this one to look at human
    40. this.goalSelector.a(7, new PathfinderGoalRandomLookaround(this));
    41. this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, false));
    42. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 0, true)); // this one to target human
    43. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityVillager.class, 0, false));
    44. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityIronGolem.class, 0, false));
    45.  
    46.  
    47. }
    48.  
    49.  
    50. }


    For 1.7:
    Show Spoiler

    Code:java
    1. public class CustomZombie extends net.minecraft.server.v1_7_R1.EntityZombie {
    2. public int damage;
    3. private float bw;
    4.  
    5. @Override
    6. protected void aD() {
    7. super.aD();
    8. this.getAttributeInstance(GenericAttributes.b).setValue(70.0D);
    9. }
    10.  
    11. @SuppressWarnings("rawtypes")
    12. public CustomZombie(net.minecraft.server.v1_6_R2.World world) {
    13. super(world);
    14. this.bw = 1.5F; //Change this to your liking. This is were you set the speed
    15. this.damage = 15; // set the damage
    16. //There's also a ton of options of you do this. play around with it
    17.  
    18.  
    19. try {
    20. Field gsa = net.minecraft.server.v1_6_R2.PathfinderGoalSelector.class.getDeclaredField("a");
    21. gsa.setAccessible(true);
    22.  
    23. gsa.set(this.goalSelector, new UnsafeList());
    24. gsa.set(this.targetSelector, new UnsafeList());
    25. } catch (SecurityException e) {
    26. e.printStackTrace();
    27. } catch (NoSuchFieldException e) {
    28. e.printStackTrace();
    29. e.printStackTrace();
    30. e.printStackTrace();
    31. }
    32.  
    33. this.goalSelector.a(0, new PathfinderGoalFloat(this));
    34. this.goalSelector.a(1, new PathfinderGoalBreakDoorFaster(this));
    35. this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, (float) (this.bw) , false)); // this one to attack human
    36. this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityIronGolem.class, (float) this.bw, true));
    37. this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityVillager.class, (float) this.bw, true));
    38. this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, (float) this.bw));
    39. this.goalSelector.a(7, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F)); // this one to look at human
    40. this.goalSelector.a(7, new PathfinderGoalRandomLookaround(this));
    41. this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, false));
    42. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 0, true)); // this one to target human
    43. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityVillager.class, 0, false));
    44. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityIronGolem.class, 0, false));
    45.  
    46.  
    47. }
    48.  
    49.  
    50. }
     
    RingOfStorms and DSH105 like this.
  2. Offline

    Ultimate_n00b

    I find it easier to use something like RemoteEntities. Good job on the tutorial though.
     
  3. Offline

    waicool20

    TomTheDeveloper
    How do you spawn an individual zombie of this class at a specific location?
    Is the normal way or do we have to add it to the entity map first?
     
  4. Offline

    TomTheDeveloper

  5. Offline

    waicool20

  6. Offline

    TomTheDeveloper

    waicool20 You can just spawn one individual custom zombie. This will not affect the other normal ones. But you can also spawn custom zombies instead of the normal zombies if you want.
     
  7. TomTheDeveloper How would I go about updating this to work with 1.7 versions?
     
  8. Offline

    TomTheDeveloper

    dajako I'll try to update this to 1.7 this evening.
     
    dajako likes this.
  9. TomTheDeveloper I feel like I'm being a bit of a derp but how can I link a class with this code in to my main class?
     
  10. Offline

    TomTheDeveloper

    dajako likes this.
  11. Offline

    bars96

    TomTheDeveloper, how I can change zombie see players range to 70 blocks? Minecraft 1.5.2.
     
  12. Offline

    TomTheDeveloper

    bars96 Hmmm. I forgot how zombies work in 1.5.2 but I think it's the same as in 1.6. Though a few methods change every update. (Example: "ay" is no "aD" I think).
     
  13. in 1.7.6+ (i think) it has changed from aD to aC, though for some reason it doesnt seem to work as in whent he value changes nothing happens.
     
  14. Offline

    Löwenpower

    Cool Tutorial.
    Does the goal and targetselector get overriden or can i change something there.
    And if, can you tell me/us, what these affects?
    (My English is not the best i think)

    //edit
    And how to change walk speed?
     
Thread Status:
Not open for further replies.

Share This Page