Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IExecutionControl API class #4125

Open
wants to merge 1 commit into
base: 1.19.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/api/java/baritone/api/IBaritone.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;

Expand Down Expand Up @@ -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
*/
Expand Down
47 changes: 47 additions & 0 deletions src/api/java/baritone/api/utils/IExecutionControl.java
Original file line number Diff line number Diff line change
@@ -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();
}
11 changes: 7 additions & 4 deletions src/main/java/baritone/Baritone.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@
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;
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;

Expand Down Expand Up @@ -235,6 +233,11 @@ public CommandManager getCommandManager() {
return this.commandManager;
}

@Override
public IExecutionControl createExecutionControl(String name, double priority) {
return new ExecutionControl(name, priority, this);
}

@Override
public IElytraProcess getElytraProcess() {
return this.elytraProcess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
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;
Expand All @@ -33,9 +33,8 @@
/**
* 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 {

Expand All @@ -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");
}

Expand Down Expand Up @@ -118,10 +83,10 @@ public List<String> getLongDesc() {
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");
}

Expand Down Expand Up @@ -149,7 +114,7 @@ public List<String> getLongDesc() {
@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
Expand All @@ -176,10 +141,7 @@ public List<String> getLongDesc() {
@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");
}

Expand Down
85 changes: 85 additions & 0 deletions src/main/java/baritone/utils/ExecutionControl.java
Original file line number Diff line number Diff line change
@@ -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();
}
}