77import net .minecraft .world .entity .Entity ;
88import net .minecraft .world .entity .ai .goal .Goal ;
99import net .minecraft .world .entity .ai .navigation .PathNavigation ;
10- import net .minecraft .world .level .Level ;
10+ import net .minecraft .world .level .block .LeavesBlock ;
11+ import net .minecraft .world .level .block .state .BlockState ;
1112import net .minecraft .world .level .pathfinder .PathType ;
1213import net .minecraft .world .level .pathfinder .WalkNodeEvaluator ;
1314
@@ -39,10 +40,6 @@ protected PathNavigation getNavigator() {
3940 return theRobit .getNavigation ();
4041 }
4142
42- protected Level getWorld () {
43- return theRobit .level ();
44- }
45-
4643 @ Override
4744 public void start () {
4845 timeToRecalcPath = 0 ;
@@ -61,40 +58,57 @@ protected void updateTask(Entity target) {
6158 if (--timeToRecalcPath <= 0 ) {
6259 timeToRecalcPath = MekanismUtils .TICKS_PER_HALF_SECOND ;
6360 if (!theRobit .isPassenger ()) {
61+ //Math from TamableAnimal#shouldTryTeleportToOwner and tryToTeleportToOwner
6462 if (theRobit .distanceToSqr (target ) >= 144.0 ) {
65- BlockPos targetPos = target .blockPosition ();
66- for (int i = 0 ; i < 10 ; i ++) {
67- if (tryPathTo (target , targetPos .getX () + randomize (-3 , 3 ), targetPos .getY () + randomize (-1 , 1 ), targetPos .getZ () + randomize (-3 , 3 ))) {
68- return ;
69- }
70- }
63+ teleportToAroundBlockPos (target .blockPosition ());
7164 } else {
7265 getNavigator ().moveTo (target , moveSpeed );
7366 }
7467 }
7568 }
7669 }
7770
78- private int randomize (int min , int max ) {
79- return theRobit .getRandom ().nextInt (max - min + 1 ) + min ;
71+ /**
72+ * Copy of {@link net.minecraft.world.entity.TamableAnimal#teleportToAroundBlockPos(BlockPos)}
73+ */
74+ private void teleportToAroundBlockPos (BlockPos pos ) {
75+ for (int i = 0 ; i < 10 ; i ++) {
76+ int j = theRobit .getRandom ().nextIntBetweenInclusive (-3 , 3 );
77+ int k = theRobit .getRandom ().nextIntBetweenInclusive (-3 , 3 );
78+ if (Math .abs (j ) >= 2 || Math .abs (k ) >= 2 ) {
79+ int l = theRobit .getRandom ().nextIntBetweenInclusive (-1 , 1 );
80+ if (maybeTeleportTo (pos .getX () + j , pos .getY () + l , pos .getZ () + k )) {
81+ return ;
82+ }
83+ }
84+ }
8085 }
8186
82- private boolean tryPathTo (Entity target , int x , int y , int z ) {
83- if (Math .abs (x - target .getX ()) < 2 && Math .abs (z - target .getZ ()) < 2 || !canNavigate (new BlockPos (x , y , z ))) {
84- return false ;
87+ /**
88+ * Copy of {@link net.minecraft.world.entity.TamableAnimal#maybeTeleportTo(int, int, int)}
89+ */
90+ private boolean maybeTeleportTo (int x , int y , int z ) {
91+ if (canTeleportTo (new BlockPos (x , y , z ))) {
92+ theRobit .moveTo (x + 0.5 , y , z + 0.5 , theRobit .getYRot (), theRobit .getXRot ());
93+ getNavigator ().stop ();
94+ return true ;
8595 }
86- theRobit .moveTo (x + 0.5 , y , z + 0.5 , theRobit .getYRot (), theRobit .getXRot ());
87- getNavigator ().stop ();
88- return true ;
96+ return false ;
8997 }
9098
91- private boolean canNavigate (BlockPos pos ) {
92- Level world = getWorld ();
93- PathType pathnodetype = WalkNodeEvaluator .getPathTypeStatic (theRobit , pos );
94- if (pathnodetype == PathType .WALKABLE ) {
95- BlockPos blockpos = pos .subtract (theRobit .blockPosition ());
96- return world .noCollision (theRobit , theRobit .getBoundingBox ().move (blockpos ));
99+ /**
100+ * Copy of {@link net.minecraft.world.entity.TamableAnimal#canTeleportTo(BlockPos)}
101+ */
102+ private boolean canTeleportTo (BlockPos pos ) {
103+ PathType pathtype = WalkNodeEvaluator .getPathTypeStatic (theRobit , pos );
104+ if (pathtype != PathType .WALKABLE ) {
105+ return false ;
97106 }
98- return false ;
107+ BlockState blockstate = theRobit .level ().getBlockState (pos .below ());
108+ if (blockstate .getBlock () instanceof LeavesBlock ) {
109+ return false ;
110+ }
111+ BlockPos blockpos = pos .subtract (theRobit .blockPosition ());
112+ return theRobit .level ().noCollision (theRobit , theRobit .getBoundingBox ().move (blockpos ));
99113 }
100114}
0 commit comments