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
- * 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