rework minebehavior threading model, fixes #217

This commit is contained in:
Leijurv 2018-10-10 16:40:33 -07:00
parent 5676acbba6
commit 7e78ed2139
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A

View File

@ -19,7 +19,6 @@ package baritone.behavior;
import baritone.Baritone; import baritone.Baritone;
import baritone.api.behavior.IMineBehavior; import baritone.api.behavior.IMineBehavior;
import baritone.api.event.events.PathEvent;
import baritone.api.event.events.TickEvent; import baritone.api.event.events.TickEvent;
import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalBlock; import baritone.api.pathing.goals.GoalBlock;
@ -78,35 +77,37 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get(); int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get();
if (mineGoalUpdateInterval != 0) { if (mineGoalUpdateInterval != 0) {
if (event.getCount() % mineGoalUpdateInterval == 0) { if (event.getCount() % mineGoalUpdateInterval == 0) {
Baritone.INSTANCE.getExecutor().execute(this::updateGoal); Baritone.INSTANCE.getExecutor().execute(this::rescan);
} }
} }
PathingBehavior.INSTANCE.revalidateGoal();
}
@Override
public void onPathEvent(PathEvent event) {
updateGoal(); updateGoal();
PathingBehavior.INSTANCE.revalidateGoal();
} }
private void updateGoal() { private void updateGoal() {
if (mining == null) { if (mining == null) {
return; return;
} }
if (!locationsCache.isEmpty()) { List<BlockPos> locs = locationsCache;
locationsCache = prune(new ArrayList<>(locationsCache), mining, 64); if (!locs.isEmpty()) {
PathingBehavior.INSTANCE.setGoal(coalesce(locationsCache)); locs = prune(new ArrayList<>(locs), mining, 64);
PathingBehavior.INSTANCE.setGoal(coalesce(locs));
PathingBehavior.INSTANCE.path(); PathingBehavior.INSTANCE.path();
locationsCache = locs;
}
}
private void rescan() {
if (mining == null) {
return;
} }
List<BlockPos> locs = scanFor(mining, 64); List<BlockPos> locs = scanFor(mining, 64);
if (locs.isEmpty()) { if (locs.isEmpty()) {
logDebug("No locations for " + mining + " known, cancelling"); logDebug("No locations for " + mining + " known, cancelling");
cancel(); mine(0, (String[]) null);
return; return;
} }
locationsCache = locs; locationsCache = locs;
PathingBehavior.INSTANCE.setGoal(coalesce(locs));
PathingBehavior.INSTANCE.path();
} }
public GoalComposite coalesce(List<BlockPos> locs) { public GoalComposite coalesce(List<BlockPos> locs) {
@ -176,6 +177,7 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
this.mining = blocks == null || blocks.length == 0 ? null : Arrays.stream(blocks).map(ChunkPacker::stringToBlock).collect(Collectors.toList()); this.mining = blocks == null || blocks.length == 0 ? null : Arrays.stream(blocks).map(ChunkPacker::stringToBlock).collect(Collectors.toList());
this.quantity = quantity; this.quantity = quantity;
this.locationsCache = new ArrayList<>(); this.locationsCache = new ArrayList<>();
rescan();
updateGoal(); updateGoal();
} }
@ -184,6 +186,7 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
this.mining = blocks == null || blocks.length == 0 ? null : Arrays.asList(blocks); this.mining = blocks == null || blocks.length == 0 ? null : Arrays.asList(blocks);
this.quantity = quantity; this.quantity = quantity;
this.locationsCache = new ArrayList<>(); this.locationsCache = new ArrayList<>();
rescan();
updateGoal(); updateGoal();
} }