pathing processes wip

This commit is contained in:
Leijurv 2018-11-03 22:11:52 -07:00
parent 58a2965fea
commit 660efe5e16
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
28 changed files with 760 additions and 439 deletions

View File

@ -17,10 +17,14 @@
package baritone.api;
import baritone.api.behavior.*;
import baritone.api.behavior.ILookBehavior;
import baritone.api.behavior.IMemoryBehavior;
import baritone.api.behavior.IPathingBehavior;
import baritone.api.cache.IWorldProvider;
import baritone.api.cache.IWorldScanner;
import baritone.api.event.listener.IGameEventListener;
import baritone.api.process.IFollowProcess;
import baritone.api.process.IMineProcess;
import baritone.api.utils.SettingsUtil;
import java.util.Iterator;
@ -36,20 +40,20 @@ import java.util.ServiceLoader;
*/
public final class BaritoneAPI {
private static final IBaritoneProvider baritone;
private static final IBaritone baritone;
private static final Settings settings;
static {
ServiceLoader<IBaritoneProvider> baritoneLoader = ServiceLoader.load(IBaritoneProvider.class);
Iterator<IBaritoneProvider> instances = baritoneLoader.iterator();
baritone = instances.next();
baritone = instances.next().getBaritoneForPlayer(null); // PWNAGE
settings = new Settings();
SettingsUtil.readAndApply(settings);
}
public static IFollowBehavior getFollowBehavior() {
return baritone.getFollowBehavior();
public static IFollowProcess getFollowProcess() {
return baritone.getFollowProcess();
}
public static ILookBehavior getLookBehavior() {
@ -60,8 +64,8 @@ public final class BaritoneAPI {
return baritone.getMemoryBehavior();
}
public static IMineBehavior getMineBehavior() {
return baritone.getMineBehavior();
public static IMineProcess getMineProcess() {
return baritone.getMineProcess();
}
public static IPathingBehavior getPathingBehavior() {

View File

@ -0,0 +1,81 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api;
import baritone.api.behavior.*;
import baritone.api.cache.IWorldProvider;
import baritone.api.cache.IWorldScanner;
import baritone.api.event.listener.IGameEventListener;
import baritone.api.process.IFollowProcess;
import baritone.api.process.IMineProcess;
/**
* @author Brady
* @since 9/29/2018
*/
public interface IBaritone {
/**
* @return The {@link IFollowProcess} instance
* @see IFollowProcess
*/
IFollowProcess getFollowProcess();
/**
* @return The {@link ILookBehavior} instance
* @see ILookBehavior
*/
ILookBehavior getLookBehavior();
/**
* @return The {@link IMemoryBehavior} instance
* @see IMemoryBehavior
*/
IMemoryBehavior getMemoryBehavior();
/**
* @return The {@link IMineProcess} instance
* @see IMineProcess
*/
IMineProcess getMineProcess();
/**
* @return The {@link IPathingBehavior} instance
* @see IPathingBehavior
*/
IPathingBehavior getPathingBehavior();
/**
* @return The {@link IWorldProvider} instance
* @see IWorldProvider
*/
IWorldProvider getWorldProvider();
/**
* @return The {@link IWorldScanner} instance
* @see IWorldScanner
*/
IWorldScanner getWorldScanner();
/**
* Registers a {@link IGameEventListener} with Baritone's "event bus".
*
* @param listener The listener
*/
void registerEventListener(IGameEventListener listener);
}

View File

@ -17,70 +17,8 @@
package baritone.api;
import baritone.api.behavior.*;
import baritone.api.cache.IWorldProvider;
import baritone.api.cache.IWorldScanner;
import baritone.api.event.listener.IGameEventListener;
import net.minecraft.client.entity.EntityPlayerSP;
/**
* @author Brady
* @since 9/29/2018
*/
public interface IBaritoneProvider {
/**
* @see IFollowBehavior
*
* @return The {@link IFollowBehavior} instance
*/
IFollowBehavior getFollowBehavior();
/**
* @see ILookBehavior
*
* @return The {@link ILookBehavior} instance
*/
ILookBehavior getLookBehavior();
/**
* @see IMemoryBehavior
*
* @return The {@link IMemoryBehavior} instance
*/
IMemoryBehavior getMemoryBehavior();
/**
* @see IMineBehavior
*
* @return The {@link IMineBehavior} instance
*/
IMineBehavior getMineBehavior();
/**
* @see IPathingBehavior
*
* @return The {@link IPathingBehavior} instance
*/
IPathingBehavior getPathingBehavior();
/**
* @see IWorldProvider
*
* @return The {@link IWorldProvider} instance
*/
IWorldProvider getWorldProvider();
/**
* @see IWorldScanner
*
* @return The {@link IWorldScanner} instance
*/
IWorldScanner getWorldScanner();
/**
* Registers a {@link IGameEventListener} with Baritone's "event bus".
*
* @param listener The listener
*/
void registerEventListener(IGameEventListener listener);
IBaritone getBaritoneForPlayer(EntityPlayerSP player); // tenor be like
}

View File

@ -18,10 +18,9 @@
package baritone.api.behavior;
import baritone.api.event.listener.AbstractGameEventListener;
import baritone.api.utils.interfaces.Toggleable;
/**
* @author Brady
* @since 9/23/2018
*/
public interface IBehavior extends AbstractGameEventListener, Toggleable {}
public interface IBehavior extends AbstractGameEventListener {}

View File

@ -39,35 +39,22 @@ public interface IPathingBehavior extends IBehavior {
*/
Optional<Double> ticksRemainingInSegment();
/**
* Sets the pathing goal.
*
* @param goal The pathing goal
*/
void setGoal(Goal goal);
/**
* @return The current pathing goal
*/
Goal getGoal();
/**
* Begins pathing. Calculation will start in a new thread, and once completed,
* movement will commence. Returns whether or not the operation was successful.
*
* @return Whether or not the operation was successful
*/
boolean path();
/**
* @return Whether or not a path is currently being executed.
*/
boolean isPathing();
/**
* Cancels the pathing behavior or the current path calculation.
* Cancels the pathing behavior or the current path calculation. Also cancels all processes that could be controlling path.
* <p>
* Basically, "MAKE IT STOP".
*/
void cancel();
void cancelEverything();
/**
* Returns the current path, from the current path executor, if there is one.

View File

@ -20,6 +20,7 @@ package baritone.api.pathing.goals;
import net.minecraft.util.math.BlockPos;
import java.util.Arrays;
import java.util.Optional;
/**
* Useful for automated combat (retreating specifically)
@ -32,16 +33,26 @@ public class GoalRunAway implements Goal {
private final double distanceSq;
private final Optional<Integer> maintainY;
public GoalRunAway(double distance, BlockPos... from) {
this(distance, Optional.empty(), from);
}
public GoalRunAway(double distance, Optional<Integer> maintainY, BlockPos... from) {
if (from.length == 0) {
throw new IllegalArgumentException();
}
this.from = from;
this.distanceSq = distance * distance;
this.maintainY = maintainY;
}
@Override
public boolean isInGoal(int x, int y, int z) {
if (maintainY.isPresent() && maintainY.get() != y) {
return false;
}
for (BlockPos p : from) {
int diffX = x - p.getX();
int diffZ = z - p.getZ();
@ -62,11 +73,19 @@ public class GoalRunAway implements Goal {
min = h;
}
}
return -min;
min = -min;
if (maintainY.isPresent()) {
min += GoalYLevel.calculate(maintainY.get(), y);
}
return min;
}
@Override
public String toString() {
return "GoalRunAwayFrom" + Arrays.asList(from);
if (maintainY.isPresent()) {
return "GoalRunAwayFromMaintainY y=" + maintainY.get() + ", " + Arrays.asList(from);
} else {
return "GoalRunAwayFrom" + Arrays.asList(from);
}
}
}

View File

@ -0,0 +1,47 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.process;
import baritone.api.IBaritone;
/**
* A process that can control the PathingBehavior.
* <p>
* Differences between a baritone process and a behavior:
* Only one baritone process can be active at a time
* PathingBehavior can only be controlled by a process
* <p>
* That's it actually
*
* @author leijurv
*/
public interface IBaritoneProcess {
// javadocs small brain, // comment large brain
boolean isActive(); // would you like to be in control?
PathingCommand onTick(); // you're in control, what should baritone do?
boolean isTemporary(); // CombatPauserProcess should return isTemporary true always, and isActive true only when something is in range
void onLostControl(); // called if isActive returned true, but another non-temporary process has control. effectively the same as cancel.
double priority(); // tenor be like
IBaritone associatedWith(); // which bot is this associated with (5000000iq forward thinking)
}

View File

@ -0,0 +1,31 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.process;
import baritone.api.pathing.goals.Goal;
public interface ICustomGoalProcess extends IBaritoneProcess {
void setGoal(Goal goal);
void path();
default void setGoalAndPath(Goal goal) {
setGoal(goal);
path();
}
}

View File

@ -15,15 +15,16 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.behavior;
package baritone.api.process;
import baritone.api.process.IBaritoneProcess;
import net.minecraft.entity.Entity;
/**
* @author Brady
* @since 9/23/2018
*/
public interface IFollowBehavior extends IBehavior {
public interface IFollowProcess extends IBaritoneProcess {
/**
* Set the follow target to the specified entity;
@ -40,5 +41,7 @@ public interface IFollowBehavior extends IBehavior {
/**
* Cancels the follow behavior, this will clear the current follow target.
*/
void cancel();
default void cancel() {
onLostControl();
}
}

View File

@ -0,0 +1,27 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.process;
import net.minecraft.block.Block;
/**
* but it rescans the world every once in a while so it doesn't get fooled by its cache
*/
public interface IGetToBlockProcess extends IBaritoneProcess {
void getToBlock(Block block);
}

View File

@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.behavior;
package baritone.api.process;
import net.minecraft.block.Block;
@ -23,7 +23,7 @@ import net.minecraft.block.Block;
* @author Brady
* @since 9/23/2018
*/
public interface IMineBehavior extends IBehavior {
public interface IMineProcess extends IBaritoneProcess {
/**
* Begin to search for and mine the specified blocks until
@ -31,9 +31,9 @@ public interface IMineBehavior extends IBehavior {
* are mined. This is based on the first target block to mine.
*
* @param quantity The number of items to get from blocks mined
* @param blocks The blocks to mine
* @param blocks The blocks to mine
*/
void mine(int quantity, String... blocks);
void mineByName(int quantity, String... blocks);
/**
* Begin to search for and mine the specified blocks until
@ -41,7 +41,7 @@ public interface IMineBehavior extends IBehavior {
* are mined. This is based on the first target block to mine.
*
* @param quantity The number of items to get from blocks mined
* @param blocks The blocks to mine
* @param blocks The blocks to mine
*/
void mine(int quantity, Block... blocks);
@ -50,8 +50,8 @@ public interface IMineBehavior extends IBehavior {
*
* @param blocks The blocks to mine
*/
default void mine(String... blocks) {
this.mine(0, blocks);
default void mineByName(String... blocks) {
mineByName(0, blocks);
}
/**
@ -60,11 +60,13 @@ public interface IMineBehavior extends IBehavior {
* @param blocks The blocks to mine
*/
default void mine(Block... blocks) {
this.mine(0, blocks);
mine(0, blocks);
}
/**
* Cancels the current mining task
*/
void cancel();
default void cancel() {
onLostControl();
}
}

View File

@ -0,0 +1,30 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.process;
import baritone.api.pathing.goals.Goal;
public class PathingCommand {
public final Goal goal;
public final PathingCommandType commandType;
public PathingCommand(Goal goal, PathingCommandType commandType) {
this.goal = goal;
this.commandType = commandType;
}
}

View File

@ -0,0 +1,27 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.process;
public enum PathingCommandType {
SET_GOAL_AND_PATH, // if you do this one with a null goal it should continue
REQUEST_PAUSE,
// if you do this one with a null goal it should cancel
REVALIDATE_GOAL_AND_PATH, // idkkkkkkk
FORCE_REVALIDATE_GOAL_AND_PATH // idkkkkkkkkkkkkkkkkkkkkkkkk
}

View File

@ -1,54 +0,0 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.interfaces;
/**
* @author Brady
* @since 8/20/2018
*/
public interface Toggleable {
/**
* Toggles the enabled state of this {@link Toggleable}.
*
* @return The new state.
*/
boolean toggle();
/**
* Sets the enabled state of this {@link Toggleable}.
*
* @return The new state.
*/
boolean setEnabled(boolean enabled);
/**
* @return Whether or not this {@link Toggleable} object is enabled
*/
boolean isEnabled();
/**
* Called when the state changes from disabled to enabled
*/
default void onEnable() {}
/**
* Called when the state changes from enabled to disabled
*/
default void onDisable() {}
}

View File

@ -85,6 +85,6 @@ public class MixinEntityPlayerSP {
)
private boolean isAllowFlying(PlayerCapabilities capabilities) {
PathingBehavior pathingBehavior = Baritone.INSTANCE.getPathingBehavior();
return (!pathingBehavior.isEnabled() || !pathingBehavior.isPathing()) && capabilities.allowFlying;
return !pathingBehavior.isPathing() && capabilities.allowFlying;
}
}

View File

@ -142,7 +142,7 @@ public class MixinMinecraft {
)
)
private boolean isAllowUserInput(GuiScreen screen) {
return (Baritone.INSTANCE.getPathingBehavior().getCurrent() != null && Baritone.INSTANCE.getPathingBehavior().isEnabled() && player != null) || screen.allowUserInput;
return (Baritone.INSTANCE.getPathingBehavior().getCurrent() != null && player != null) || screen.allowUserInput;
}
@Inject(

View File

@ -18,16 +18,25 @@
package baritone;
import baritone.api.BaritoneAPI;
import baritone.api.IBaritoneProvider;
import baritone.api.IBaritone;
import baritone.api.Settings;
import baritone.api.event.listener.IGameEventListener;
import baritone.behavior.*;
import baritone.api.process.IBaritoneProcess;
import baritone.behavior.Behavior;
import baritone.behavior.LookBehavior;
import baritone.behavior.MemoryBehavior;
import baritone.behavior.PathingBehavior;
import baritone.cache.WorldProvider;
import baritone.cache.WorldScanner;
import baritone.event.GameEventHandler;
import baritone.process.CustomGoalProcess;
import baritone.process.FollowProcess;
import baritone.process.GetToBlockProcess;
import baritone.process.MineProcess;
import baritone.utils.BaritoneAutoTest;
import baritone.utils.ExampleBaritoneControl;
import baritone.utils.InputOverrideHandler;
import baritone.utils.PathingControlManager;
import net.minecraft.client.Minecraft;
import java.io.File;
@ -44,7 +53,7 @@ import java.util.concurrent.TimeUnit;
* @author Brady
* @since 7/31/2018 10:50 PM
*/
public enum Baritone implements IBaritoneProvider {
public enum Baritone implements IBaritone {
/**
* Singleton instance of this class
@ -66,8 +75,13 @@ public enum Baritone implements IBaritoneProvider {
private PathingBehavior pathingBehavior;
private LookBehavior lookBehavior;
private MemoryBehavior memoryBehavior;
private FollowBehavior followBehavior;
private MineBehavior mineBehavior;
private FollowProcess followProcess;
private MineProcess mineProcess;
private GetToBlockProcess getToBlockProcess;
private CustomGoalProcess customGoalProcess;
private PathingControlManager pathingControlManager;
/**
* Whether or not Baritone is active
@ -89,15 +103,19 @@ public enum Baritone implements IBaritoneProvider {
// We might want to change this...
this.settings = BaritoneAPI.getSettings();
this.pathingControlManager = new PathingControlManager(this);
this.behaviors = new ArrayList<>();
{
// the Behavior constructor calls baritone.registerBehavior(this) so this populates the behaviors arraylist
pathingBehavior = new PathingBehavior(this);
lookBehavior = new LookBehavior(this);
memoryBehavior = new MemoryBehavior(this);
followBehavior = new FollowBehavior(this);
mineBehavior = new MineBehavior(this);
followProcess = new FollowProcess(this);
mineProcess = new MineProcess(this);
new ExampleBaritoneControl(this);
new CustomGoalProcess(this); // very high iq
new GetToBlockProcess(this);
}
if (BaritoneAutoTest.ENABLE_AUTO_TEST) {
registerEventListener(BaritoneAutoTest.INSTANCE);
@ -113,6 +131,10 @@ public enum Baritone implements IBaritoneProvider {
this.initialized = true;
}
public PathingControlManager getPathingControlManager() {
return pathingControlManager;
}
public boolean isInitialized() {
return this.initialized;
}
@ -138,9 +160,13 @@ public enum Baritone implements IBaritoneProvider {
this.registerEventListener(behavior);
}
public void registerProcess(IBaritoneProcess process) {
}
@Override
public FollowBehavior getFollowBehavior() {
return followBehavior;
public FollowProcess getFollowProcess() {
return followProcess;
}
@Override
@ -154,8 +180,8 @@ public enum Baritone implements IBaritoneProvider {
}
@Override
public MineBehavior getMineBehavior() {
return mineBehavior;
public MineProcess getMineProcess() {
return mineProcess;
}
@Override

View File

@ -17,59 +17,17 @@
package baritone;
import baritone.api.IBaritone;
import baritone.api.IBaritoneProvider;
import baritone.api.behavior.*;
import baritone.api.cache.IWorldProvider;
import baritone.api.cache.IWorldScanner;
import baritone.api.event.listener.IGameEventListener;
import baritone.cache.WorldProvider;
import baritone.cache.WorldScanner;
import net.minecraft.client.entity.EntityPlayerSP;
/**
* todo fix this cancer
*
* @author Brady
* @since 9/29/2018
*/
public final class BaritoneProvider implements IBaritoneProvider {
@Override
public IFollowBehavior getFollowBehavior() {
return Baritone.INSTANCE.getFollowBehavior();
}
@Override
public ILookBehavior getLookBehavior() {
return Baritone.INSTANCE.getLookBehavior();
}
@Override
public IMemoryBehavior getMemoryBehavior() {
return Baritone.INSTANCE.getMemoryBehavior();
}
@Override
public IMineBehavior getMineBehavior() {
return Baritone.INSTANCE.getMineBehavior();
}
@Override
public IPathingBehavior getPathingBehavior() {
return Baritone.INSTANCE.getPathingBehavior();
}
@Override
public IWorldProvider getWorldProvider() {
return WorldProvider.INSTANCE;
}
@Override
public IWorldScanner getWorldScanner() {
return WorldScanner.INSTANCE;
}
@Override
public void registerEventListener(IGameEventListener listener) {
Baritone.INSTANCE.registerEventListener(listener);
public IBaritone getBaritoneForPlayer(EntityPlayerSP player) {
return Baritone.INSTANCE; // pwnage
}
}

View File

@ -30,49 +30,8 @@ public class Behavior implements IBehavior {
public final Baritone baritone;
/**
* Whether or not this behavior is enabled
*/
private boolean enabled = true;
protected Behavior(Baritone baritone) {
this.baritone = baritone;
baritone.registerBehavior(this);
}
/**
* Toggles the enabled state of this {@link Behavior}.
*
* @return The new state.
*/
@Override
public final boolean toggle() {
return this.setEnabled(!this.isEnabled());
}
/**
* Sets the enabled state of this {@link Behavior}.
*
* @return The new state.
*/
@Override
public final boolean setEnabled(boolean enabled) {
if (enabled == this.enabled) {
return this.enabled;
}
if (this.enabled = enabled) {
this.onEnable();
} else {
this.onDisable();
}
return this.enabled;
}
/**
* @return Whether or not this {@link Behavior} is active.
*/
@Override
public final boolean isEnabled() {
return this.enabled;
}
}

View File

@ -191,7 +191,6 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
return Optional.of(current.getPath().ticksRemainingFrom(current.getPosition()));
}
@Override
public void setGoal(Goal goal) {
this.goal = goal;
}
@ -227,6 +226,11 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
}
@Override
public void cancelEverything() {
}
// just cancel the current path
public void cancel() {
queuePathEvent(PathEvent.CANCELED);
current = null;
@ -245,7 +249,6 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
*
* @return true if this call started path calculation, false if it was already calculating or executing a path
*/
@Override
public boolean path() {
if (goal == null) {
return false;
@ -435,31 +438,8 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
}
}
public void revalidateGoal() {
if (!Baritone.settings().cancelOnGoalInvalidation.get()) {
return;
}
synchronized (pathPlanLock) {
if (current == null || goal == null) {
return;
}
Goal intended = current.getPath().getGoal();
BlockPos end = current.getPath().getDest();
if (intended.isInGoal(end) && !goal.isInGoal(end)) {
// this path used to end in the goal
// but the goal has changed, so there's no reason to continue...
cancel();
}
}
}
@Override
public void onRenderPass(RenderEvent event) {
PathRenderer.render(event, this);
}
@Override
public void onDisable() {
Baritone.INSTANCE.getInputOverrideHandler().clearAllKeys();
}
}

View File

@ -21,7 +21,6 @@ import baritone.Baritone;
import baritone.api.event.events.*;
import baritone.api.event.events.type.EventState;
import baritone.api.event.listener.IGameEventListener;
import baritone.api.utils.interfaces.Toggleable;
import baritone.cache.WorldProvider;
import baritone.utils.BlockStateInterface;
import baritone.utils.Helper;
@ -42,20 +41,12 @@ public final class GameEventHandler implements IGameEventListener, Helper {
@Override
public final void onTick(TickEvent event) {
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onTick(event);
}
});
listeners.forEach(l -> l.onTick(event));
}
@Override
public final void onPlayerUpdate(PlayerUpdateEvent event) {
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onPlayerUpdate(event);
}
});
listeners.forEach(l -> l.onPlayerUpdate(event));
}
@Override
@ -75,20 +66,12 @@ public final class GameEventHandler implements IGameEventListener, Helper {
}
}
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onProcessKeyBinds();
}
});
listeners.forEach(l -> l.onProcessKeyBinds());
}
@Override
public final void onSendChatMessage(ChatEvent event) {
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onSendChatMessage(event);
}
});
listeners.forEach(l -> l.onSendChatMessage(event));
}
@Override
@ -114,20 +97,12 @@ public final class GameEventHandler implements IGameEventListener, Helper {
}
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onChunkEvent(event);
}
});
listeners.forEach(l -> l.onChunkEvent(event));
}
@Override
public final void onRenderPass(RenderEvent event) {
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onRenderPass(event);
}
});
listeners.forEach(l -> l.onRenderPass(event));
}
@Override
@ -143,72 +118,41 @@ public final class GameEventHandler implements IGameEventListener, Helper {
}
}
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onWorldEvent(event);
}
});
listeners.forEach(l -> l.onWorldEvent(event));
}
@Override
public final void onSendPacket(PacketEvent event) {
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onSendPacket(event);
}
});
listeners.forEach(l -> l.onSendPacket(event));
}
@Override
public final void onReceivePacket(PacketEvent event) {
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onReceivePacket(event);
}
});
listeners.forEach(l -> l.onReceivePacket(event));
}
@Override
public void onPlayerRotationMove(RotationMoveEvent event) {
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onPlayerRotationMove(event);
}
});
listeners.forEach(l -> l.onPlayerRotationMove(event));
}
@Override
public void onBlockInteract(BlockInteractEvent event) {
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onBlockInteract(event);
}
});
listeners.forEach(l -> l.onBlockInteract(event));
}
@Override
public void onPlayerDeath() {
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onPlayerDeath();
}
});
listeners.forEach(l -> l.onPlayerDeath());
}
@Override
public void onPathEvent(PathEvent event) {
listeners.forEach(l -> {
if (canDispatch(l)) {
l.onPathEvent(event);
}
});
listeners.forEach(l -> l.onPathEvent(event));
}
public final void registerEventListener(IGameEventListener listener) {
this.listeners.add(listener);
}
private boolean canDispatch(IGameEventListener listener) {
return !(listener instanceof Toggleable) || ((Toggleable) listener).isEnabled();
}
}

View File

@ -0,0 +1,65 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.process;
import baritone.Baritone;
import baritone.api.pathing.goals.Goal;
import baritone.api.process.ICustomGoalProcess;
import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType;
import baritone.utils.BaritoneProcessHelper;
/**
* As set by ExampleBaritoneControl or something idk
*
* @author leijurv
*/
public class CustomGoalProcess extends BaritoneProcessHelper implements ICustomGoalProcess {
private Goal goal;
private boolean active;
public CustomGoalProcess(Baritone baritone) {
super(baritone);
}
@Override
public void setGoal(Goal goal) {
this.goal = goal;
}
@Override
public void path() {
active = true;
}
@Override
public boolean isActive() {
return active;
}
@Override
public PathingCommand onTick() {
active = false; // only do this once
return new PathingCommand(goal, PathingCommandType.SET_GOAL_AND_PATH);
}
@Override
public void onLostControl() {
active = false;
}
}

View File

@ -15,14 +15,15 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.behavior;
package baritone.process;
import baritone.Baritone;
import baritone.api.behavior.IFollowBehavior;
import baritone.api.event.events.TickEvent;
import baritone.api.process.IFollowProcess;
import baritone.api.pathing.goals.GoalNear;
import baritone.api.pathing.goals.GoalXZ;
import baritone.utils.Helper;
import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType;
import baritone.utils.BaritoneProcessHelper;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
@ -31,23 +32,16 @@ import net.minecraft.util.math.BlockPos;
*
* @author leijurv
*/
public final class FollowBehavior extends Behavior implements IFollowBehavior, Helper {
public final class FollowProcess extends BaritoneProcessHelper implements IFollowProcess {
private Entity following;
public FollowBehavior(Baritone baritone) {
public FollowProcess(Baritone baritone) {
super(baritone);
}
@Override
public void onTick(TickEvent event) {
if (event.getType() == TickEvent.Type.OUT) {
following = null;
return;
}
if (following == null) {
return;
}
public PathingCommand onTick() {
// lol this is trashy but it works
BlockPos pos;
if (Baritone.settings().followOffsetDistance.get() == 0) {
@ -56,9 +50,17 @@ public final class FollowBehavior extends Behavior implements IFollowBehavior, H
GoalXZ g = GoalXZ.fromDirection(following.getPositionVector(), Baritone.settings().followOffsetDirection.get(), Baritone.settings().followOffsetDistance.get());
pos = new BlockPos(g.getX(), following.posY, g.getZ());
}
baritone.getPathingBehavior().setGoal(new GoalNear(pos, Baritone.settings().followRadius.get()));
((PathingBehavior) baritone.getPathingBehavior()).revalidateGoal();
baritone.getPathingBehavior().path();
return new PathingCommand(new GoalNear(pos, Baritone.settings().followRadius.get()), PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH);
}
@Override
public boolean isActive() {
return following != null;
}
@Override
public void onLostControl() {
following = null;
}
@Override
@ -70,10 +72,4 @@ public final class FollowBehavior extends Behavior implements IFollowBehavior, H
public Entity following() {
return this.following;
}
@Override
public void cancel() {
baritone.getPathingBehavior().cancel();
follow(null);
}
}

View File

@ -0,0 +1,82 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.process;
import baritone.Baritone;
import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalComposite;
import baritone.api.pathing.goals.GoalGetToBlock;
import baritone.api.process.IGetToBlockProcess;
import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType;
import baritone.utils.BaritoneProcessHelper;
import net.minecraft.block.Block;
import net.minecraft.util.math.BlockPos;
import java.util.Collections;
import java.util.List;
public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBlockProcess {
Block gettingTo;
List<BlockPos> knownLocations;
int tickCount = 0;
public GetToBlockProcess(Baritone baritone) {
super(baritone);
}
@Override
public void getToBlock(Block block) {
gettingTo = block;
rescan();
}
@Override
public boolean isActive() {
return gettingTo != null;
}
@Override
public PathingCommand onTick() {
if (knownLocations == null) {
rescan();
}
if (knownLocations.isEmpty()) {
logDirect("No known locations of " + gettingTo);
onLostControl();
return null;
}
int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get();
if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain
Baritone.INSTANCE.getExecutor().execute(this::rescan);
}
Goal goal = new GoalComposite(knownLocations.stream().map(GoalGetToBlock::new).toArray(Goal[]::new));
return new PathingCommand(goal, PathingCommandType.SET_GOAL_AND_PATH);
}
@Override
public void onLostControl() {
gettingTo = null;
knownLocations = null;
}
private void rescan() {
knownLocations = MineProcess.searchWorld(Collections.singletonList(gettingTo), 64);
}
}

View File

@ -15,18 +15,20 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.behavior;
package baritone.process;
import baritone.Baritone;
import baritone.api.behavior.IMineBehavior;
import baritone.api.event.events.TickEvent;
import baritone.api.pathing.goals.*;
import baritone.api.process.IMineProcess;
import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType;
import baritone.api.utils.RotationUtils;
import baritone.cache.CachedChunk;
import baritone.cache.ChunkPacker;
import baritone.cache.WorldProvider;
import baritone.cache.WorldScanner;
import baritone.pathing.movement.MovementHelper;
import baritone.utils.BaritoneProcessHelper;
import baritone.utils.BlockStateInterface;
import baritone.utils.Helper;
import net.minecraft.block.Block;
@ -44,26 +46,27 @@ import java.util.stream.Collectors;
*
* @author leijurv
*/
public final class MineBehavior extends Behavior implements IMineBehavior, Helper {
public final class MineProcess extends BaritoneProcessHelper implements IMineProcess {
private static final int ORE_LOCATIONS_COUNT = 64;
private List<Block> mining;
private List<BlockPos> knownOreLocations;
private BlockPos branchPoint;
private int desiredQuantity;
private int tickCount;
public MineBehavior(Baritone baritone) {
public MineProcess(Baritone baritone) {
super(baritone);
}
@Override
public void onTick(TickEvent event) {
if (event.getType() == TickEvent.Type.OUT) {
cancel();
return;
}
if (mining == null) {
return;
}
public boolean isActive() {
return mining != null;
}
@Override
public PathingCommand onTick() {
if (desiredQuantity > 0) {
Item item = mining.get(0).getItemDropped(mining.get(0).getDefaultState(), new Random(), 0);
int curr = player().inventory.mainInventory.stream().filter(stack -> item.equals(stack.getItem())).mapToInt(ItemStack::getCount).sum();
@ -71,45 +74,52 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
if (curr >= desiredQuantity) {
logDirect("Have " + curr + " " + item.getItemStackDisplayName(new ItemStack(item, 1)));
cancel();
return;
return null;
}
}
int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get();
if (mineGoalUpdateInterval != 0 && event.getCount() % mineGoalUpdateInterval == 0) {
if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain
Baritone.INSTANCE.getExecutor().execute(this::rescan);
}
if (Baritone.settings().legitMine.get()) {
addNearby();
}
updateGoal();
baritone.getPathingBehavior().revalidateGoal();
Goal goal = updateGoal();
if (goal == null) {
// none in range
// maybe say something in chat? (ahem impact)
cancel();
return null;
}
return new PathingCommand(goal, PathingCommandType.REVALIDATE_GOAL_AND_PATH);
}
private void updateGoal() {
if (mining == null) {
return;
}
@Override
public void onLostControl() {
mine(0, (Block[]) null);
}
private Goal updateGoal() {
List<BlockPos> locs = knownOreLocations;
if (!locs.isEmpty()) {
List<BlockPos> locs2 = prune(new ArrayList<>(locs), mining, 64);
List<BlockPos> locs2 = prune(new ArrayList<>(locs), mining, ORE_LOCATIONS_COUNT);
// can't reassign locs, gotta make a new var locs2, because we use it in a lambda right here, and variables you use in a lambda must be effectively final
baritone.getPathingBehavior().setGoalAndPath(new GoalComposite(locs2.stream().map(loc -> coalesce(loc, locs2)).toArray(Goal[]::new)));
Goal goal = new GoalComposite(locs2.stream().map(loc -> coalesce(loc, locs2)).toArray(Goal[]::new));
knownOreLocations = locs2;
return;
return goal;
}
// we don't know any ore locations at the moment
if (!Baritone.settings().legitMine.get()) {
return;
return null;
}
// only in non-Xray mode (aka legit mode) do we do this
if (branchPoint == null) {
int y = Baritone.settings().legitMineYLevel.get();
if (!baritone.getPathingBehavior().isPathing() && playerFeet().y == y) {
if (!associatedWith().getPathingBehavior().isPathing() && playerFeet().y == y) {
// cool, path is over and we are at desired y
branchPoint = playerFeet();
} else {
baritone.getPathingBehavior().setGoalAndPath(new GoalYLevel(y));
return;
return new GoalYLevel(y);
}
}
@ -117,7 +127,7 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
// TODO mine 1x1 shafts to either side
branchPoint = branchPoint.north(10);
}
baritone.getPathingBehavior().setGoalAndPath(new GoalBlock(branchPoint));
return new GoalBlock(branchPoint);
}
private void rescan() {
@ -127,10 +137,10 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
if (Baritone.settings().legitMine.get()) {
return;
}
List<BlockPos> locs = searchWorld(mining, 64);
List<BlockPos> locs = searchWorld(mining, ORE_LOCATIONS_COUNT);
if (locs.isEmpty()) {
logDebug("No locations for " + mining + " known, cancelling");
mine(0, (String[]) null);
cancel();
return;
}
knownOreLocations = locs;
@ -158,7 +168,7 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
}
}
public List<BlockPos> searchWorld(List<Block> mining, int max) {
public static List<BlockPos> searchWorld(List<Block> mining, int max) {
List<BlockPos> locs = new ArrayList<>();
List<Block> uninteresting = new ArrayList<>();
//long b = System.currentTimeMillis();
@ -194,18 +204,18 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
}
}
}
knownOreLocations = prune(knownOreLocations, mining, 64);
knownOreLocations = prune(knownOreLocations, mining, ORE_LOCATIONS_COUNT);
}
public List<BlockPos> prune(List<BlockPos> locs2, List<Block> mining, int max) {
public static List<BlockPos> prune(List<BlockPos> locs2, List<Block> mining, int max) {
List<BlockPos> locs = locs2
.stream()
// remove any that are within loaded chunks that aren't actually what we want
.filter(pos -> world().getChunk(pos) instanceof EmptyChunk || mining.contains(BlockStateInterface.get(pos).getBlock()))
.filter(pos -> Helper.HELPER.world().getChunk(pos) instanceof EmptyChunk || mining.contains(BlockStateInterface.get(pos).getBlock()))
// remove any that are implausible to mine (encased in bedrock, or touching lava)
.filter(MineBehavior::plausibleToBreak)
.filter(MineProcess::plausibleToBreak)
.sorted(Comparator.comparingDouble(Helper.HELPER.playerFeet()::distanceSq))
.collect(Collectors.toList());
@ -225,13 +235,8 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
}
@Override
public void mine(int quantity, String... blocks) {
this.mining = blocks == null || blocks.length == 0 ? null : Arrays.stream(blocks).map(ChunkPacker::stringToBlock).collect(Collectors.toList());
this.desiredQuantity = quantity;
this.knownOreLocations = new ArrayList<>();
this.branchPoint = null;
rescan();
updateGoal();
public void mineByName(int quantity, String... blocks) {
mine(quantity, blocks == null || blocks.length == 0 ? null : Arrays.stream(blocks).map(ChunkPacker::stringToBlock).toArray(Block[]::new));
}
@Override
@ -241,12 +246,5 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
this.knownOreLocations = new ArrayList<>();
this.branchPoint = null;
rescan();
updateGoal();
}
@Override
public void cancel() {
mine(0, (String[]) null);
baritone.getPathingBehavior().cancel();
}
}

View File

@ -0,0 +1,53 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils;
import baritone.Baritone;
import baritone.api.process.IBaritoneProcess;
public abstract class BaritoneProcessHelper implements IBaritoneProcess, Helper {
public static final double DEFAULT_PRIORITY = 0;
private final Baritone baritone;
private final double priority;
public BaritoneProcessHelper(Baritone baritone) {
this(baritone, DEFAULT_PRIORITY);
}
public BaritoneProcessHelper(Baritone baritone, double priority) {
this.baritone = baritone;
this.priority = priority;
baritone.getPathingControlManager().registerProcess(this);
}
@Override
public Baritone associatedWith() {
return baritone;
}
@Override
public boolean isTemporary() {
return false;
}
@Override
public double priority() {
return priority;
}
}

View File

@ -73,7 +73,6 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
"sethome - Sets \"home\"\n" +
"home - Paths towards \"home\" \n" +
"costs - (debug) all movement costs from current location\n" +
"pause - Toggle pause\n" +
"damn - Daniel ";
public ExampleBaritoneControl(Baritone baritone) {
@ -228,15 +227,15 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
return true;
}
if (msg.equals("cancel") || msg.equals("stop")) {
baritone.getMineBehavior().cancel();
baritone.getFollowBehavior().cancel();
baritone.getMineProcess().cancel();
baritone.getFollowProcess().cancel();
pathingBehavior.cancel();
logDirect("ok canceled");
return true;
}
if (msg.equals("forcecancel")) {
baritone.getMineBehavior().cancel();
baritone.getFollowBehavior().cancel();
baritone.getMineProcess().cancel();
baritone.getFollowProcess().cancel();
pathingBehavior.cancel();
AbstractNodeCostSearch.forceCancel();
pathingBehavior.forceCancel();
@ -288,7 +287,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
logDirect("Not found");
return true;
}
baritone.getFollowBehavior().follow(toFollow.get());
baritone.getFollowProcess().follow(toFollow.get());
logDirect("Following " + toFollow.get());
return true;
}
@ -320,7 +319,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
int quantity = Integer.parseInt(blockTypes[1]);
Block block = ChunkPacker.stringToBlock(blockTypes[0]);
Objects.requireNonNull(block);
baritone.getMineBehavior().mine(quantity, block);
baritone.getMineProcess().mine(quantity, block);
logDirect("Will mine " + quantity + " " + blockTypes[0]);
return true;
} catch (NumberFormatException | ArrayIndexOutOfBoundsException | NullPointerException ex) {}
@ -331,7 +330,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
}
}
baritone.getMineBehavior().mine(0, blockTypes);
baritone.getMineProcess().mineByName(0, blockTypes);
logDirect("Started mining blocks of type " + Arrays.toString(blockTypes));
return true;
}
@ -407,7 +406,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
return true;
}
} else {
List<BlockPos> locs = baritone.getMineBehavior().searchWorld(Collections.singletonList(block), 64);
List<BlockPos> locs = baritone.getMineProcess().searchWorld(Collections.singletonList(block), 64);
if (locs.isEmpty()) {
logDirect("No locations for " + mining + " known, cancelling");
return true;
@ -479,11 +478,6 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
}
return true;
}
if (msg.equals("pause")) {
boolean enabled = pathingBehavior.toggle();
logDirect("Pathing Behavior has " + (enabled ? "resumed" : "paused") + ".");
return true;
}
if (msg.equals("damn")) {
logDirect("daniel");
}

View File

@ -0,0 +1,125 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils;
import baritone.Baritone;
import baritone.api.pathing.goals.Goal;
import baritone.api.process.IBaritoneProcess;
import baritone.api.process.PathingCommand;
import baritone.pathing.path.PathExecutor;
import net.minecraft.util.math.BlockPos;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
public class PathingControlManager {
private final Baritone baritone;
private final HashSet<IBaritoneProcess> processes; // unGh
public PathingControlManager(Baritone baritone) {
this.baritone = baritone;
this.processes = new HashSet<>();
}
public void registerProcess(IBaritoneProcess process) {
processes.add(process);
}
public void doTheThingWithTheStuff() {
PathingCommand cmd = doTheStuff();
if (cmd == null) {
baritone.getPathingBehavior().cancel();
return;
}
switch (cmd.commandType) {
case REQUEST_PAUSE:
// idk
// ask pathingbehavior if its safe
case FORCE_REVALIDATE_GOAL_AND_PATH:
if (cmd.goal == null) {
baritone.getPathingBehavior().cancel(); // todo only if its safe
return;
}
// pwnage
baritone.getPathingBehavior().setGoal(cmd.goal);
if (revalidateGoal(cmd.goal)) {
baritone.getPathingBehavior().cancel(); // todo only if its safe
}
case REVALIDATE_GOAL_AND_PATH:
if (cmd.goal == null) {
baritone.getPathingBehavior().cancel(); // todo only if its safe
return;
}
baritone.getPathingBehavior().setGoal(cmd.goal);
if (Baritone.settings().cancelOnGoalInvalidation.get() && revalidateGoal(cmd.goal)) {
baritone.getPathingBehavior().cancel(); // todo only if its safe
}
case SET_GOAL_AND_PATH:
// now this i can do
if (cmd.goal != null) {
baritone.getPathingBehavior().setGoalAndPath(cmd.goal);
}
// breaks are for wusses!!!!
}
}
public boolean revalidateGoal(Goal newGoal) {
PathExecutor current = baritone.getPathingBehavior().getCurrent();
if (current != null) {
Goal intended = current.getPath().getGoal();
BlockPos end = current.getPath().getDest();
if (intended.isInGoal(end) && !newGoal.isInGoal(end)) {
// this path used to end in the goal
// but the goal has changed, so there's no reason to continue...
return true;
}
}
return false;
}
public PathingCommand doTheStuff() {
List<IBaritoneProcess> inContention = processes.stream().filter(IBaritoneProcess::isActive).sorted(Comparator.comparingDouble(IBaritoneProcess::priority)).collect(Collectors.toList());
boolean found = false;
boolean cancelOthers = false;
PathingCommand exec = null;
for (int i = inContention.size() - 1; i >= 0; i--) { // truly a gamer moment
IBaritoneProcess proc = inContention.get(i);
if (found) {
if (cancelOthers) {
proc.onLostControl();
}
} else {
exec = proc.onTick();
if (exec == null) {
if (proc.isActive()) {
throw new IllegalStateException(proc + "");
}
proc.onLostControl();
continue;
}
found = true;
cancelOthers = !proc.isTemporary();
}
}
return exec;
}
}