dispatch path events from main thread instead of calculation thread

This commit is contained in:
Leijurv 2018-10-18 15:04:40 -07:00
parent 313a5fddbe
commit 1a809fa7a3
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 38 additions and 26 deletions

View File

@ -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<PathEvent> 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<PathEvent> 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");

View File

@ -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();