Guest User

GoalRoute.java

a guest
Apr 8th, 2022
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import com.destroystokyo.paper.entity.ai.Goal;
  2. import com.destroystokyo.paper.entity.ai.GoalKey;
  3. import com.destroystokyo.paper.entity.ai.GoalType;
  4. import lombok.NonNull;
  5. import org.bukkit.Location;
  6. import org.bukkit.entity.Mob;
  7.  
  8. import java.util.Arrays;
  9. import java.util.EnumSet;
  10. import java.util.List;
  11.  
  12. // Author: Dymeth.Ru
  13. public class GoalRoute implements Goal<Mob> {
  14.     private static final GoalKey<Mob> KEY = AiHelper.key(GoalRoute.class, Mob.class);
  15.  
  16.     private final Mob mob;
  17.     private final List<Location> route;
  18.     private final double movingSpeed;
  19.     private final StuckPolicy stuckPolicy;
  20.     private int currentPointIndex;
  21.     private ExactNavigator navigator;
  22.     private boolean failed;
  23.  
  24.     public GoalRoute(@NonNull Mob mob, double movingSpeed, @NonNull StuckPolicy stuckPolicy, @NonNull Location... route) {
  25.         this(mob, movingSpeed, stuckPolicy, Arrays.asList(route));
  26.     }
  27.  
  28.     public GoalRoute(@NonNull Mob mob, double movingSpeed, @NonNull StuckPolicy stuckPolicy, @NonNull List<Location> route) {
  29.         this.mob = mob;
  30.         this.route = route;
  31.         this.movingSpeed = movingSpeed;
  32.         this.stuckPolicy = stuckPolicy;
  33.         this.currentPointIndex = 0;
  34.         this.failed = false;
  35.         this.startPointNavigation();
  36.     }
  37.  
  38.     @Override
  39.     public boolean shouldActivate() {
  40.         return !this.failed;
  41.     }
  42.  
  43.     @Override
  44.     public void stop() {
  45.         this.mob.getPathfinder().stopPathfinding();
  46.         this.mob.setTarget(null);
  47.     }
  48.  
  49.     @Override
  50.     public void tick() {
  51.         if (!this.navigator.isPathCompleteOrFailed()) return;
  52.         this.startPointNavigation();
  53.         this.currentPointIndex++;
  54.         if (this.currentPointIndex == this.route.size()) this.currentPointIndex = 0;
  55.     }
  56.  
  57.     @Override
  58.     @NonNull
  59.     public GoalKey<Mob> getKey() {
  60.         return KEY;
  61.     }
  62.  
  63.     @Override
  64.     @NonNull
  65.     public EnumSet<GoalType> getTypes() {
  66.         return EnumSet.of(GoalType.MOVE, GoalType.LOOK, GoalType.JUMP);
  67.     }
  68.  
  69.     private void startPointNavigation() {
  70.         this.navigator = new ExactNavigator(this.mob, this.route.get(this.currentPointIndex), this.movingSpeed, exactNavigator -> {
  71.             switch (this.stuckPolicy) {
  72.                 case SKIP_POINT:
  73.                     return false;
  74.                 case STOP_PATHFINDING:
  75.                     this.failed = true;
  76.                     return false;
  77.                 case WAIT_FOR_PATH:
  78.                     return true;
  79.             }
  80.             throw new IllegalStateException();
  81.         });
  82.     }
  83.  
  84.     public enum StuckPolicy {
  85.         SKIP_POINT,
  86.         STOP_PATHFINDING,
  87.         WAIT_FOR_PATH
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment