Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.destroystokyo.paper.entity.ai.Goal;
- import com.destroystokyo.paper.entity.ai.GoalKey;
- import com.destroystokyo.paper.entity.ai.GoalType;
- import lombok.NonNull;
- import org.bukkit.Location;
- import org.bukkit.entity.Mob;
- import java.util.Arrays;
- import java.util.EnumSet;
- import java.util.List;
- // Author: Dymeth.Ru
- public class GoalRoute implements Goal<Mob> {
- private static final GoalKey<Mob> KEY = AiHelper.key(GoalRoute.class, Mob.class);
- private final Mob mob;
- private final List<Location> route;
- private final double movingSpeed;
- private final StuckPolicy stuckPolicy;
- private int currentPointIndex;
- private ExactNavigator navigator;
- private boolean failed;
- public GoalRoute(@NonNull Mob mob, double movingSpeed, @NonNull StuckPolicy stuckPolicy, @NonNull Location... route) {
- this(mob, movingSpeed, stuckPolicy, Arrays.asList(route));
- }
- public GoalRoute(@NonNull Mob mob, double movingSpeed, @NonNull StuckPolicy stuckPolicy, @NonNull List<Location> route) {
- this.mob = mob;
- this.route = route;
- this.movingSpeed = movingSpeed;
- this.stuckPolicy = stuckPolicy;
- this.currentPointIndex = 0;
- this.failed = false;
- this.startPointNavigation();
- }
- @Override
- public boolean shouldActivate() {
- return !this.failed;
- }
- @Override
- public void stop() {
- this.mob.getPathfinder().stopPathfinding();
- this.mob.setTarget(null);
- }
- @Override
- public void tick() {
- if (!this.navigator.isPathCompleteOrFailed()) return;
- this.startPointNavigation();
- this.currentPointIndex++;
- if (this.currentPointIndex == this.route.size()) this.currentPointIndex = 0;
- }
- @Override
- @NonNull
- public GoalKey<Mob> getKey() {
- return KEY;
- }
- @Override
- @NonNull
- public EnumSet<GoalType> getTypes() {
- return EnumSet.of(GoalType.MOVE, GoalType.LOOK, GoalType.JUMP);
- }
- private void startPointNavigation() {
- this.navigator = new ExactNavigator(this.mob, this.route.get(this.currentPointIndex), this.movingSpeed, exactNavigator -> {
- switch (this.stuckPolicy) {
- case SKIP_POINT:
- return false;
- case STOP_PATHFINDING:
- this.failed = true;
- return false;
- case WAIT_FOR_PATH:
- return true;
- }
- throw new IllegalStateException();
- });
- }
- public enum StuckPolicy {
- SKIP_POINT,
- STOP_PATHFINDING,
- WAIT_FOR_PATH
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment