From abb432848f71b0bb512ad9d01ee265c064bad59b Mon Sep 17 00:00:00 2001 From: RacoonDog <32882447+RacoonDog@users.noreply.github.com> Date: Wed, 30 Aug 2023 18:22:18 -0400 Subject: [PATCH] IExecutionControl API class --- src/api/java/baritone/api/IBaritone.java | 10 +++ .../baritone/api/utils/IExecutionControl.java | 47 ++++++++++ src/main/java/baritone/Baritone.java | 11 ++- .../defaults/ExecutionControlCommands.java | 58 +++---------- .../java/baritone/utils/ExecutionControl.java | 85 +++++++++++++++++++ 5 files changed, 159 insertions(+), 52 deletions(-) create mode 100644 src/api/java/baritone/api/utils/IExecutionControl.java create mode 100644 src/main/java/baritone/utils/ExecutionControl.java diff --git a/src/api/java/baritone/api/IBaritone.java b/src/api/java/baritone/api/IBaritone.java index 3c9681532..28d906b1b 100644 --- a/src/api/java/baritone/api/IBaritone.java +++ b/src/api/java/baritone/api/IBaritone.java @@ -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 */ diff --git a/src/api/java/baritone/api/utils/IExecutionControl.java b/src/api/java/baritone/api/utils/IExecutionControl.java new file mode 100644 index 000000000..22374caf1 --- /dev/null +++ b/src/api/java/baritone/api/utils/IExecutionControl.java @@ -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 . + */ + +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(); +} diff --git a/src/main/java/baritone/Baritone.java b/src/main/java/baritone/Baritone.java index c1176a3d4..40bc4b7f3 100755 --- a/src/main/java/baritone/Baritone.java +++ b/src/main/java/baritone/Baritone.java @@ -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; diff --git a/src/main/java/baritone/command/defaults/ExecutionControlCommands.java b/src/main/java/baritone/command/defaults/ExecutionControlCommands.java index 6f6293ccd..12a876629 100644 --- a/src/main/java/baritone/command/defaults/ExecutionControlCommands.java +++ b/src/main/java/baritone/command/defaults/ExecutionControlCommands.java @@ -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. *

- * 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"); } diff --git a/src/main/java/baritone/utils/ExecutionControl.java b/src/main/java/baritone/utils/ExecutionControl.java new file mode 100644 index 000000000..d13022eb5 --- /dev/null +++ b/src/main/java/baritone/utils/ExecutionControl.java @@ -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 . + */ + +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(); + } +}