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