split out into baritone api

This commit is contained in:
leijurv 2018-08-13 20:57:29 -07:00
parent c9a6a36f67
commit 578e08a893
5 changed files with 136 additions and 60 deletions

View File

@ -44,7 +44,7 @@ public enum Baritone {
private GameEventHandler gameEventHandler;
private InputOverrideHandler inputOverrideHandler;
private Settings settings = new Settings();
private Settings settings;
private List<Behavior> behaviors;
/**
@ -52,9 +52,13 @@ public enum Baritone {
*/
private boolean active;
public void init() {
public synchronized void init() {
if (initialized) {
return;
}
this.gameEventHandler = new GameEventHandler();
this.inputOverrideHandler = new InputOverrideHandler();
this.settings = new Settings();
this.behaviors = new ArrayList<>();
behaviors.add(PathingBehavior.INSTANCE);
behaviors.add(LookBehavior.INSTANCE);
@ -80,6 +84,10 @@ public enum Baritone {
return this.behaviors;
}
public void registerBehavior(Behavior behavior) {
this.behaviors.add(behavior);
}
public final boolean isActive() {
return this.active;
}

View File

@ -31,6 +31,7 @@ public class Settings {
public int planningTickLookAhead = 150;
public boolean renderPath = true;
public boolean chatDebug = true;
public boolean chatControl = true;
Settings() {

View File

@ -135,63 +135,8 @@ public class PathingBehavior extends Behavior {
}
}
@Override
public void onSendChatMessage(ChatEvent event) {
String msg = event.getMessage();
if (msg.toLowerCase().startsWith("goal")) {
event.cancel();
String[] params = msg.toLowerCase().substring(4).trim().split(" ");
if (params[0].equals("")) {
params = new String[]{};
}
try {
switch (params.length) {
case 0:
goal = new GoalBlock(playerFeet());
break;
case 1:
goal = new GoalYLevel(Integer.parseInt(params[0]));
break;
case 2:
goal = new GoalXZ(Integer.parseInt(params[0]), Integer.parseInt(params[1]));
break;
case 3:
goal = new GoalBlock(new BlockPos(Integer.parseInt(params[0]), Integer.parseInt(params[1]), Integer.parseInt(params[2])));
break;
default:
displayChatMessageRaw("unable to understand lol");
return;
}
} catch (NumberFormatException ex) {
displayChatMessageRaw("unable to parse integer " + ex);
return;
}
displayChatMessageRaw("Goal: " + goal);
return;
}
if (msg.equals("path")) {
findPathInNewThread(playerFeet(), true);
event.cancel();
return;
}
if (msg.toLowerCase().equals("slowpath")) {
AStarPathFinder.slowPath ^= true;
event.cancel();
return;
}
if (msg.toLowerCase().equals("cancel")) {
current = null;
Baritone.INSTANCE.getInputOverrideHandler().clearAllKeys();
event.cancel();
displayChatMessageRaw("ok canceled");
return;
}
if (msg.toLowerCase().startsWith("thisway")) {
goal = GoalXZ.fromDirection(playerFeetAsVec(), player().rotationYaw, Double.parseDouble(msg.substring(7).trim()));
displayChatMessageRaw("Goal: " + goal);
event.cancel();
return;
}
public void setGoal(Goal goal) {
this.goal = goal;
}
public PathExecutor getExecutor() {
@ -202,13 +147,28 @@ public class PathingBehavior extends Behavior {
return Optional.ofNullable(current).map(PathExecutor::getPath);
}
public void cancel() {
current = null;
next = null;
Baritone.INSTANCE.getInputOverrideHandler().clearAllKeys();
}
public void path() {
synchronized (pathCalcLock) {
if (isPathCalcInProgress) {
return;
}
findPathInNewThread(playerFeet(), true);
}
}
/**
* In a new thread, pathfind to target blockpos
*
* @param start
* @param talkAboutIt
*/
public void findPathInNewThread(final BlockPos start, final boolean talkAboutIt) {
private void findPathInNewThread(final BlockPos start, final boolean talkAboutIt) {
synchronized (pathCalcLock) {
if (isPathCalcInProgress) {
throw new IllegalStateException("Already doing it");

View File

@ -0,0 +1,105 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.bot.utils;
import baritone.bot.Baritone;
import baritone.bot.behavior.Behavior;
import baritone.bot.behavior.impl.PathingBehavior;
import baritone.bot.event.events.ChatEvent;
import baritone.bot.pathing.calc.AStarPathFinder;
import baritone.bot.pathing.goals.Goal;
import baritone.bot.pathing.goals.GoalBlock;
import baritone.bot.pathing.goals.GoalXZ;
import baritone.bot.pathing.goals.GoalYLevel;
import net.minecraft.util.math.BlockPos;
public class ExampleBaritoneControl extends Behavior {
public static ExampleBaritoneControl INSTANCE = new ExampleBaritoneControl();
private ExampleBaritoneControl() {
}
public void initAndRegister() {
Baritone.INSTANCE.registerBehavior(this);
}
@Override
public void onSendChatMessage(ChatEvent event) {
if (!Baritone.settings().chatControl) {
return;
}
String msg = event.getMessage();
if (msg.toLowerCase().startsWith("goal")) {
event.cancel();
String[] params = msg.toLowerCase().substring(4).trim().split(" ");
if (params[0].equals("")) {
params = new String[]{};
}
Goal goal;
try {
switch (params.length) {
case 0:
goal = new GoalBlock(playerFeet());
break;
case 1:
goal = new GoalYLevel(Integer.parseInt(params[0]));
break;
case 2:
goal = new GoalXZ(Integer.parseInt(params[0]), Integer.parseInt(params[1]));
break;
case 3:
goal = new GoalBlock(new BlockPos(Integer.parseInt(params[0]), Integer.parseInt(params[1]), Integer.parseInt(params[2])));
break;
default:
displayChatMessageRaw("unable to understand lol");
return;
}
} catch (NumberFormatException ex) {
displayChatMessageRaw("unable to parse integer " + ex);
return;
}
PathingBehavior.INSTANCE.setGoal(goal);
displayChatMessageRaw("Goal: " + goal);
return;
}
if (msg.equals("path")) {
PathingBehavior.INSTANCE.path();
event.cancel();
return;
}
if (msg.toLowerCase().equals("slowpath")) {
AStarPathFinder.slowPath ^= true;
event.cancel();
return;
}
if (msg.toLowerCase().equals("cancel")) {
PathingBehavior.INSTANCE.cancel();
event.cancel();
displayChatMessageRaw("ok canceled");
return;
}
if (msg.toLowerCase().startsWith("thisway")) {
Goal goal = GoalXZ.fromDirection(playerFeetAsVec(), player().rotationYaw, Double.parseDouble(msg.substring(7).trim()));
PathingBehavior.INSTANCE.setGoal(goal);
displayChatMessageRaw("Goal: " + goal);
event.cancel();
return;
}
}
}

View File

@ -21,6 +21,7 @@ import baritone.bot.Baritone;
import baritone.bot.event.events.TickEvent;
import baritone.bot.event.events.WorldEvent;
import baritone.bot.event.events.type.EventState;
import baritone.bot.utils.ExampleBaritoneControl;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.WorldClient;
import org.spongepowered.asm.lib.Opcodes;
@ -47,6 +48,7 @@ public class MixinMinecraft {
)
private void init(CallbackInfo ci) {
Baritone.INSTANCE.init();
ExampleBaritoneControl.INSTANCE.initAndRegister();
}
@Inject(