NMS Custom Pathfinder exemple nms v1_12_R1 (1.12)

Discussion in 'Resources' started by flo077, Aug 9, 2017.

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

    flo077

    Hello, i just want to share my work, this is a custom Pathfinder that move the mob to a new location, in minecraft 1.12:

    Code:java
    1. package fr.MB.flo077.CustomEntities;
    2.  
    3. import java.util.Random;
    4. import javax.annotation.Nullable;
    5.  
    6. import org.bukkit.Bukkit;
    7.  
    8. import net.minecraft.server.v1_12_R1.EntityCreature;
    9. import net.minecraft.server.v1_12_R1.PathfinderGoal;
    10. import net.minecraft.server.v1_12_R1.RandomPositionGenerator;
    11. import net.minecraft.server.v1_12_R1.Vec3D;
    12.  
    13. public class PathfinderGoalWalkToLoc
    14. extends PathfinderGoal
    15. {
    16. protected final EntityCreature a;
    17. protected double b;
    18. protected double c;
    19. protected double d;
    20. protected final double e;
    21. protected int f;
    22. protected boolean g;
    23.  
    24. public PathfinderGoalWalkToLoc(EntityCreature paramEntityCreature,double x,double y, double z, double speed)
    25. {
    26. this(paramEntityCreature, x, y, z,speed, 5);
    27. }
    28.  
    29. public PathfinderGoalWalkToLoc(EntityCreature paramEntityCreature,double x, double y, double z, double speed, int timeBetweenMovement)
    30. {
    31. a = paramEntityCreature;
    32. e = speed;
    33. b = x;
    34. c = y;
    35. d = z;
    36. f = timeBetweenMovement;
    37. a(1); // non obligatory, ?something relative to priority?
    38. }
    39.  
    40. public boolean a()
    41. {
    42. if (!g) {
    43. // some condition relative to distanceFromPlayer
    44. if (a.bW() >= 100) {
    45. return false;
    46. }
    47. // Random from 0 to f (for the PathfinderGoalRandomScroll, basic value is 120)
    48. if (a.getRandom().nextInt(f) != 0) {
    49. return false;
    50. }
    51. }
    52.  
    53. g = false; // to skip the conditions, maybe used by the game sometimes ???
    54.  
    55. return true; // execute c()
    56. }
    57.  
    58.  
    59. public boolean b()//executed when the mob isn't following an other pathFinder, when the mob isn't moving with this path, return false each tick to something that execute a() each tick,else return true, a() is not executed.
    60. {
    61. return !a.getNavigation().o();
    62. }
    63.  
    64. public void c()// start navigation,doesnt work for some reason sometimes (mainly if the mob is too far, it depends of the generic.followRange attribute). relative to AbstractNavigation.b().the navigation stop if it is too long, and b() come back to false.
    65. {
    66. a.getNavigation().a(b,c,d,e);
    67. }
    68.  
    69. public void i() {// you can use this to skip the conditions; maybe used by the game sometimes ??
    70. g = true;
    71. }
    72.  
    73. public void setTimeBetweenMovement(int paramInt) { // to change the random condition (0 to paramInt)
    74. f = paramInt;
    75. }
    76. }


    and the methods in my CustomZombie:
    Code:java
    1. @Override
    2. public void r()// on spawn
    3. {
    4. goalSelector.a(0, new PathfinderGoalFloat(this));
    5. goalSelector.a(2, new PathfinderGoalZombieAttack(this, 1.0D, false));
    6. goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 1.0D));
    7. goalSelector.a(7, new PathfinderGoalWalkToLoc(this,-33.0D,71.0D,71.0D, 1.0D));// you can replace the fix values with variables, put a new method in your Zombie entity for exemple.
    8. goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
    9. goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
    10. do_();
    11. }
    12. @Override
    13. protected void do_() {
    14. goalSelector.a(6, new PathfinderGoalMoveThroughVillage(this, 1.0D, false));
    15.  
    16. targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true, new Class[] { EntityPigZombie.class }));
    17. targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, true));
    18. targetSelector.a(3, new PathfinderGoalNearestAttackableTarget(this, EntityVillager.class, false));
    19. targetSelector.a(3, new PathfinderGoalNearestAttackableTarget(this, EntityIronGolem.class, true));
    20. }


    [EDIT]
    if you want to increase maximum distance between your mob and the location, you can change the generic.followRange attribute during the PathFinder calculation.But take care; a big generic.followRange value causes server lags.

    This is my toolClass with the Method that change the attributes of an Entity:
    Code:java
    1.  
    2. import net.minecraft.server.v1_12_R1.Entity;
    3. import net.minecraft.server.v1_12_R1.EntityLiving;
    4. import net.minecraft.server.v1_12_R1.NBTTagCompound;
    5. import net.minecraft.server.v1_12_R1.NBTTagDouble;
    6. import net.minecraft.server.v1_12_R1.NBTTagList;
    7. import net.minecraft.server.v1_12_R1.NBTTagString;
    8.  
    9.  
    10. public class CustomizingEntityTools {
    11. public static void setFollowRange(Entity entity, double followRange)
    12. {
    13. setOrAdAttribute(entity,"generic.followRange",followRange);
    14. }
    15. public static void setOrAdAttribute(Entity entity, String name, double base)
    16. {
    17. NBTTagCompound compound = new NBTTagCompound();
    18. entity.c(compound);
    19. NBTTagList attributeList;
    20. NBTTagCompound attribute = new NBTTagCompound();
    21. attribute.set("Name", new NBTTagString(name));
    22. attribute.set("Base", new NBTTagDouble(base));
    23. if( compound.hasKey("Attributes"))
    24. {
    25. attributeList = compound.getList("Attributes",10);
    26. for(int compt = 0;compt < attributeList.size();compt ++)
    27. {
    28. if(attributeList.get(compt).get("Name").equals(attribute.get("Name")))
    29. {
    30. attributeList.remove(compt);
    31. }
    32. }
    33. attributeList.add(attribute);
    34. }
    35. else
    36. {
    37. attributeList = new NBTTagList();
    38. attributeList.add(attribute);
    39. }
    40. compound.set("Attributes", attributeList);
    41. ((EntityLiving)entity).a(compound);
    42. }
    43. }
    44.  

    And the modified c() method of the PathfinderGoalWalkToLoc
    Code:java
    1.  
    2. public void c()
    3. {
    4. CustomizingEntityTools.setFollowRange(a,60);//the value that will be efficient for this navigation
    5. a.getNavigation().a(b,c,d,e);
    6. CustomizingEntityTools.setFollowRange(a,35);//the base value you want your zombie to have the rest of the time (default 35 for a Zombie)
    7. }
    8.  

    It is not realy clean, but it's the only way i know to do it.
    I hope it was usefull.
     
    Last edited: Aug 10, 2017
  2. Offline

    MrGriefer_

    Thanks man this is really useful, I was looking for something like this in awhile. One question tho! I haven't tested it but does the location have to be not more than 20 blocks?
     
  3. Offline

    flo077

    A part of the answer is in the code:
    if the mob is too far from the location, a.getNavigation().a(b,c,d,e); doesn work and return false.
    I dont know the exact maximum distance, but i think it is around 20 blocks like you said, tell me if you find or if you find how to increase it.
    EDIT:
    the maximum range (for my customZombie) is around 35blocks.
    EDIT:
    if found a way to increase this value.
    [​IMG]
     
    Last edited: Aug 10, 2017
Thread Status:
Not open for further replies.

Share This Page