diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index f9916611e..869aac1f9 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -41,6 +41,7 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.chunk.EmptyChunk; import java.util.*; +import java.util.concurrent.LinkedBlockingQueue; import java.util.stream.Collectors; public final class PathingBehavior extends Behavior implements IPathingBehavior, Helper { @@ -59,29 +60,45 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, private boolean lastAutoJump; + private final LinkedBlockingQueue toDispatch = new LinkedBlockingQueue<>(); + private PathingBehavior() {} - private void dispatchPathEvent(PathEvent event) { - Baritone.INSTANCE.getExecutor().execute(() -> Baritone.INSTANCE.getGameEventHandler().onPathEvent(event)); + private void queuePathEvent(PathEvent event) { + toDispatch.add(event); + } + + private void dispatchEvents() { + ArrayList curr = new ArrayList<>(); + toDispatch.drainTo(curr); + for (PathEvent event : curr) { + Baritone.INSTANCE.getGameEventHandler().onPathEvent(event); + } } @Override public void onTick(TickEvent event) { + dispatchEvents(); if (event.getType() == TickEvent.Type.OUT) { - this.cancel(); + cancel(); return; } mc.playerController.setPlayerCapabilities(mc.player); + tickPath(); + dispatchEvents(); + } + + private void tickPath() { if (current == null) { return; } - boolean safe = current.onTick(event); + boolean safe = current.onTick(); synchronized (pathPlanLock) { if (current.failed() || current.finished()) { current = null; if (goal == null || goal.isInGoal(playerFeet())) { logDebug("All done. At " + goal); - dispatchPathEvent(PathEvent.AT_GOAL); + queuePathEvent(PathEvent.AT_GOAL); next = null; return; } @@ -93,25 +110,25 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, // but if we fail in the middle of current // we're nowhere close to our planned ahead path // so need to discard it sadly. - dispatchPathEvent(PathEvent.DISCARD_NEXT); + queuePathEvent(PathEvent.DISCARD_NEXT); next = null; } if (next != null) { logDebug("Continuing on to planned next path"); - dispatchPathEvent(PathEvent.CONTINUING_ONTO_PLANNED_NEXT); + queuePathEvent(PathEvent.CONTINUING_ONTO_PLANNED_NEXT); current = next; next = null; - current.onTick(event); + current.onTick(); return; } // at this point, current just ended, but we aren't in the goal and have no plan for the future synchronized (pathCalcLock) { if (isPathCalcInProgress) { - dispatchPathEvent(PathEvent.PATH_FINISHED_NEXT_STILL_CALCULATING); + queuePathEvent(PathEvent.PATH_FINISHED_NEXT_STILL_CALCULATING); // if we aren't calculating right now return; } - dispatchPathEvent(PathEvent.CALC_STARTED); + queuePathEvent(PathEvent.CALC_STARTED); findPathInNewThread(pathStart(), true, Optional.empty()); } return; @@ -123,10 +140,10 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, if (next.snipsnapifpossible()) { // jump directly onto the next path logDebug("Splicing into planned next path early..."); - dispatchPathEvent(PathEvent.SPLICING_ONTO_NEXT_EARLY); + queuePathEvent(PathEvent.SPLICING_ONTO_NEXT_EARLY); current = next; next = null; - current.onTick(event); + current.onTick(); return; } } @@ -147,7 +164,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, if (ticksRemainingInSegment().get() < Baritone.settings().planningTickLookAhead.get()) { // and this path has 5 seconds or less left logDebug("Path almost over. Planning ahead..."); - dispatchPathEvent(PathEvent.NEXT_SEGMENT_CALC_STARTED); + queuePathEvent(PathEvent.NEXT_SEGMENT_CALC_STARTED); findPathInNewThread(current.getPath().getDest(), false, Optional.of(current.getPath())); } } @@ -211,7 +228,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, @Override public void cancel() { - dispatchPathEvent(PathEvent.CANCELED); + queuePathEvent(PathEvent.CANCELED); current = null; next = null; Baritone.INSTANCE.getInputOverrideHandler().clearAllKeys(); @@ -244,7 +261,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, if (isPathCalcInProgress) { return false; } - dispatchPathEvent(PathEvent.CALC_STARTED); + queuePathEvent(PathEvent.CALC_STARTED); findPathInNewThread(pathStart(), true, Optional.empty()); return true; } @@ -343,18 +360,18 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, synchronized (pathPlanLock) { if (current == null) { if (executor.isPresent()) { - dispatchPathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING); + queuePathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING); current = executor.get(); } else { - dispatchPathEvent(PathEvent.CALC_FAILED); + queuePathEvent(PathEvent.CALC_FAILED); } } else { if (next == null) { if (executor.isPresent()) { - dispatchPathEvent(PathEvent.NEXT_SEGMENT_CALC_FINISHED); + queuePathEvent(PathEvent.NEXT_SEGMENT_CALC_FINISHED); next = executor.get(); } else { - dispatchPathEvent(PathEvent.NEXT_CALC_FAILED); + queuePathEvent(PathEvent.NEXT_CALC_FAILED); } } else { throw new IllegalStateException("I have no idea what to do with this path"); diff --git a/src/main/java/baritone/pathing/path/PathExecutor.java b/src/main/java/baritone/pathing/path/PathExecutor.java index 61251882c..045cdab9c 100644 --- a/src/main/java/baritone/pathing/path/PathExecutor.java +++ b/src/main/java/baritone/pathing/path/PathExecutor.java @@ -18,7 +18,6 @@ package baritone.pathing.path; import baritone.Baritone; -import baritone.api.event.events.TickEvent; import baritone.api.pathing.calc.IPath; import baritone.api.pathing.movement.ActionCosts; import baritone.api.pathing.movement.IMovement; @@ -81,14 +80,10 @@ public class PathExecutor implements IPathExecutor, Helper { /** * Tick this executor * - * @param event * @return True if a movement just finished (and the player is therefore in a "stable" state, like, * not sneaking out over lava), false otherwise */ - public boolean onTick(TickEvent event) { - if (event.getType() == TickEvent.Type.OUT) { - throw new IllegalStateException(); - } + public boolean onTick() { if (pathPosition == path.length() - 1) { pathPosition++; } @@ -261,7 +256,7 @@ public class PathExecutor implements IPathExecutor, Helper { //System.out.println("Movement done, next path"); pathPosition++; onChangeInPathPosition(); - onTick(event); + onTick(); return true; } else { sprintIfRequested();