IExecutionControl API class

This commit is contained in:
RacoonDog 2023-08-30 18:22:18 -04:00
parent 2cd5c6b0af
commit abb432848f
5 changed files with 159 additions and 52 deletions

View File

@ -25,6 +25,7 @@ import baritone.api.event.listener.IEventBus;
import baritone.api.pathing.calc.IPathingControlManager;
import baritone.api.process.*;
import baritone.api.selection.ISelectionManager;
import baritone.api.utils.IExecutionControl;
import baritone.api.utils.IInputOverrideHandler;
import baritone.api.utils.IPlayerContext;
@ -139,6 +140,15 @@ public interface IBaritone {
*/
ICommandManager getCommandManager();
/**
* @param name a user-friendly name for the underlying {@link IBaritoneProcess}
* @param priority the priority. Default for {@link IBaritoneProcess} instance is {@link IBaritoneProcess#DEFAULT_PRIORITY}.
* Any Baritone process with a higher priority will not be paused by this {@link IExecutionControl}.
* @return A newly created {@link IExecutionControl} instance
* @see IExecutionControl
*/
IExecutionControl createExecutionControl(String name, double priority);
/**
* Open click
*/

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.utils;
/**
* Wrapper around a {@link baritone.api.process.IBaritoneProcess} used exclusively for pausing the Baritone.
*
* @author Crosby
*/
public interface IExecutionControl {
/**
* Tells Baritone to temporarily stop whatever it's doing until you resume.
*/
void pause();
/**
* Resumes Baritone, resuming what it was doing when it was paused.
*/
void resume();
/**
* Whether this instance of {@link IExecutionControl} is currently blocking Baritone from executing lower priority processes.
*
* @return whether Baritone is currently paused by this {@link IExecutionControl}.
*/
boolean paused();
/**
* Cancels whatever Baritone is currently doing.
*/
void stop();
}

View File

@ -24,6 +24,7 @@ import baritone.api.behavior.IBehavior;
import baritone.api.event.listener.IEventBus;
import baritone.api.process.IBaritoneProcess;
import baritone.api.process.IElytraProcess;
import baritone.api.utils.IExecutionControl;
import baritone.api.utils.IPlayerContext;
import baritone.behavior.*;
import baritone.cache.WorldProvider;
@ -31,10 +32,7 @@ import baritone.command.manager.CommandManager;
import baritone.event.GameEventHandler;
import baritone.process.*;
import baritone.selection.SelectionManager;
import baritone.utils.BlockStateInterface;
import baritone.utils.GuiClick;
import baritone.utils.InputOverrideHandler;
import baritone.utils.PathingControlManager;
import baritone.utils.*;
import baritone.utils.player.BaritonePlayerContext;
import net.minecraft.client.Minecraft;
@ -235,6 +233,11 @@ public class Baritone implements IBaritone {
return this.commandManager;
}
@Override
public IExecutionControl createExecutionControl(String name, double priority) {
return new ExecutionControl(name, priority, this);
}
@Override
public IElytraProcess getElytraProcess() {
return this.elytraProcess;

View File

@ -23,8 +23,8 @@ import baritone.api.command.argument.IArgConsumer;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.process.IBaritoneProcess;
import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType;
import baritone.api.utils.IExecutionControl;
import java.util.Arrays;
import java.util.List;
@ -33,9 +33,8 @@ import java.util.stream.Stream;
/**
* Contains the pause, resume, and paused commands.
* <p>
* This thing is scoped to hell, private so far you can't even access it using reflection, because you AREN'T SUPPOSED
* TO USE THIS to pause and resume Baritone. Make your own process that returns {@link PathingCommandType#REQUEST_PAUSE
* REQUEST_PAUSE} as needed.
* Instead of trying to access this in order to pause and resume Baritone, you should instead create your own using
* {@link IBaritone#createExecutionControl(String, double)}.
*/
public class ExecutionControlCommands {
@ -45,49 +44,15 @@ public class ExecutionControlCommands {
Command cancelCommand;
public ExecutionControlCommands(IBaritone baritone) {
// array for mutability, non-field so reflection can't touch it
final boolean[] paused = {false};
baritone.getPathingControlManager().registerProcess(
new IBaritoneProcess() {
@Override
public boolean isActive() {
return paused[0];
}
@Override
public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
baritone.getInputOverrideHandler().clearAllKeys();
return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE);
}
@Override
public boolean isTemporary() {
return true;
}
@Override
public void onLostControl() {
}
@Override
public double priority() {
return DEFAULT_PRIORITY + 1;
}
@Override
public String displayName0() {
return "Pause/Resume Commands";
}
}
);
IExecutionControl executionControl = baritone.createExecutionControl("Pause/Resume Commands", IBaritoneProcess.DEFAULT_PRIORITY + 1);
pauseCommand = new Command(baritone, "pause", "p", "paws") {
@Override
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
if (paused[0]) {
if (executionControl.paused()) {
throw new CommandInvalidStateException("Already paused");
}
paused[0] = true;
executionControl.pause();
logDirect("Paused");
}
@ -118,10 +83,10 @@ public class ExecutionControlCommands {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
baritone.getBuilderProcess().resume();
if (!paused[0]) {
if (!executionControl.paused()) {
throw new CommandInvalidStateException("Not paused");
}
paused[0] = false;
executionControl.resume();
logDirect("Resumed");
}
@ -149,7 +114,7 @@ public class ExecutionControlCommands {
@Override
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
logDirect(String.format("Baritone is %spaused", paused[0] ? "" : "not "));
logDirect(String.format("Baritone is %spaused", executionControl.paused() ? "" : "not "));
}
@Override
@ -176,10 +141,7 @@ public class ExecutionControlCommands {
@Override
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
if (paused[0]) {
paused[0] = false;
}
baritone.getPathingBehavior().cancelEverything();
executionControl.stop();
logDirect("ok canceled");
}

View File

@ -0,0 +1,85 @@
/*
* 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.api.IBaritone;
import baritone.api.process.IBaritoneProcess;
import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType;
import baritone.api.utils.IExecutionControl;
public class ExecutionControl implements IExecutionControl {
private final IBaritone baritone;
private boolean active;
public ExecutionControl(String name, double priority, IBaritone baritone) {
this.baritone = baritone;
baritone.getPathingControlManager().registerProcess(new IBaritoneProcess() {
@Override
public boolean isActive() {
return active;
}
@Override
public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
baritone.getInputOverrideHandler().clearAllKeys();
return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE);
}
@Override
public boolean isTemporary() {
return true;
}
@Override
public void onLostControl() {
}
@Override
public double priority() {
return priority;
}
@Override
public String displayName0() {
return name;
}
});
}
@Override
public void pause() {
active = true;
}
@Override
public void resume() {
active = false;
}
@Override
public boolean paused() {
return active;
}
@Override
public void stop() {
active = false;
baritone.getPathingBehavior().cancelEverything();
}
}