From 578e08a8938d838ca93a20767fa4dea301811bc9 Mon Sep 17 00:00:00 2001 From: leijurv Date: Mon, 13 Aug 2018 20:57:29 -0700 Subject: [PATCH] split out into baritone api --- src/main/java/baritone/bot/Baritone.java | 12 +- src/main/java/baritone/bot/Settings.java | 1 + .../bot/behavior/impl/PathingBehavior.java | 76 +++---------- .../bot/utils/ExampleBaritoneControl.java | 105 ++++++++++++++++++ .../launch/mixins/MixinMinecraft.java | 2 + 5 files changed, 136 insertions(+), 60 deletions(-) create mode 100644 src/main/java/baritone/bot/utils/ExampleBaritoneControl.java diff --git a/src/main/java/baritone/bot/Baritone.java b/src/main/java/baritone/bot/Baritone.java index fc5ccd58..3bc352b9 100755 --- a/src/main/java/baritone/bot/Baritone.java +++ b/src/main/java/baritone/bot/Baritone.java @@ -44,7 +44,7 @@ public enum Baritone { private GameEventHandler gameEventHandler; private InputOverrideHandler inputOverrideHandler; - private Settings settings = new Settings(); + private Settings settings; private List 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; } diff --git a/src/main/java/baritone/bot/Settings.java b/src/main/java/baritone/bot/Settings.java index 48ad9679..da8aff5b 100644 --- a/src/main/java/baritone/bot/Settings.java +++ b/src/main/java/baritone/bot/Settings.java @@ -31,6 +31,7 @@ public class Settings { public int planningTickLookAhead = 150; public boolean renderPath = true; public boolean chatDebug = true; + public boolean chatControl = true; Settings() { diff --git a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java index 074d2b04..6941d17f 100644 --- a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java +++ b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java @@ -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"); diff --git a/src/main/java/baritone/bot/utils/ExampleBaritoneControl.java b/src/main/java/baritone/bot/utils/ExampleBaritoneControl.java new file mode 100644 index 00000000..fd441000 --- /dev/null +++ b/src/main/java/baritone/bot/utils/ExampleBaritoneControl.java @@ -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 . + */ + +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; + } + } +} diff --git a/src/main/java/baritone/launch/mixins/MixinMinecraft.java b/src/main/java/baritone/launch/mixins/MixinMinecraft.java index a466d0f0..52cbf163 100755 --- a/src/main/java/baritone/launch/mixins/MixinMinecraft.java +++ b/src/main/java/baritone/launch/mixins/MixinMinecraft.java @@ -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(