From 42fd04e3299d7bd123f33cf3d4a92c9380a19540 Mon Sep 17 00:00:00 2001 From: Brady Date: Wed, 1 Aug 2018 20:10:19 -0700 Subject: [PATCH] Initial Behavior system commit. Still needs some work, just getting this in the repo! --- build.gradle | 1 + src/main/java/baritone/bot/Baritone.java | 18 +++++ .../java/baritone/bot/GameEventHandler.java | 17 +++- .../java/baritone/bot/behavior/Behavior.java | 78 +++++++++++++++++++ .../baritone/bot/behavior/PathFinding.java | 8 ++ 5 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 src/main/java/baritone/bot/behavior/Behavior.java create mode 100644 src/main/java/baritone/bot/behavior/PathFinding.java diff --git a/build.gradle b/build.gradle index e50c77c4a..8c115dd24 100755 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,7 @@ repositories { } dependencies { + implementation 'org.reflections:reflections:0.9.11' implementation ('org.spongepowered:mixin:0.7.8-SNAPSHOT') { // Mixin includes a lot of dependencies that are too up-to-date exclude module: 'launchwrapper' diff --git a/src/main/java/baritone/bot/Baritone.java b/src/main/java/baritone/bot/Baritone.java index 7dbd65a77..e0c6fe5ff 100755 --- a/src/main/java/baritone/bot/Baritone.java +++ b/src/main/java/baritone/bot/Baritone.java @@ -1,5 +1,10 @@ package baritone.bot; +import baritone.bot.behavior.Behavior; +import org.reflections.Reflections; + +import java.util.List; + /** * @author Brady * @since 7/31/2018 10:50 PM @@ -21,6 +26,7 @@ public enum Baritone { private GameActionHandler actionHandler; private GameEventHandler gameEventHandler; private InputOverrideHandler inputOverrideHandler; + private List behaviors; /** * Whether or not Baritone is active @@ -36,6 +42,14 @@ public enum Baritone { this.active = true; this.initialized = true; + + new Reflections("baritone.bot.behavior.impl").getSubTypesOf(Behavior.class).forEach(c -> { + try { + behaviors.add(c.newInstance()); + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + }); } public final boolean isInitialized() { @@ -62,6 +76,10 @@ public enum Baritone { return this.inputOverrideHandler; } + public final List getBehaviors() { + return this.behaviors; + } + public final boolean isActive() { return this.active; } diff --git a/src/main/java/baritone/bot/GameEventHandler.java b/src/main/java/baritone/bot/GameEventHandler.java index de79272af..24ebc4332 100755 --- a/src/main/java/baritone/bot/GameEventHandler.java +++ b/src/main/java/baritone/bot/GameEventHandler.java @@ -1,10 +1,13 @@ package baritone.bot; +import baritone.bot.behavior.Behavior; import baritone.bot.event.IGameEventListener; import baritone.bot.event.events.ChatEvent; import net.minecraft.client.settings.KeyBinding; import org.lwjgl.input.Keyboard; +import java.util.function.Consumer; + /** * @author Brady * @since 7/31/2018 11:04 PM @@ -14,7 +17,9 @@ public final class GameEventHandler implements IGameEventListener { GameEventHandler() {} @Override - public final void onTick() {} + public final void onTick() { + dispatchEventToBehaviors(behavior -> onTick()); + } @Override public void onProcessKeyBinds() { @@ -31,8 +36,16 @@ public final class GameEventHandler implements IGameEventListener { KeyBinding.onTick(keyCode < 0 ? keyCode + 100 : keyCode); } } + + dispatchEventToBehaviors(behavior -> onProcessKeyBinds()); } @Override - public void onSendChatMessage(ChatEvent event) {} + public void onSendChatMessage(ChatEvent event) { + dispatchEventToBehaviors(behavior -> onSendChatMessage(event)); + } + + private void dispatchEventToBehaviors(Consumer dispatchFunction) { + Baritone.INSTANCE.getBehaviors().stream().filter(Behavior::isEnabled).forEach(dispatchFunction); + } } \ No newline at end of file diff --git a/src/main/java/baritone/bot/behavior/Behavior.java b/src/main/java/baritone/bot/behavior/Behavior.java new file mode 100644 index 000000000..c28253841 --- /dev/null +++ b/src/main/java/baritone/bot/behavior/Behavior.java @@ -0,0 +1,78 @@ +package baritone.bot.behavior; + +import baritone.bot.event.AbstractGameEventListener; +import baritone.bot.utils.Helper; + +/** + * A generic bot behavior. + * + * @author Brady + * @since 8/1/2018 6:29 PM + */ +public class Behavior implements AbstractGameEventListener, Helper { + + /** + * Whether or not this behavior is enabled + */ + private boolean enabled; + + /** + * Toggles the enabled state of this {@link Behavior}. + * + * @return The new state. + */ + public final boolean toggle() { + return this.setEnabled(!this.enabled); + } + + /** + * Sets the enabled state of this {@link Behavior}. + * + * @return The new state. + */ + public final boolean setEnabled(boolean enabled) { + boolean newState = getNewState(this.enabled, enabled); + if (newState == this.enabled) + return this.enabled; + + if (this.enabled = newState) { + onStart(); + } else { + onCancel(); + } + + return this.enabled; + } + + /** + * Function to determine what the new enabled state of this + * {@link Behavior} should be given the old state, and the + * proposed state. Intended to be overriden by behaviors + * that should always be active, given that the bot itself is + * active. + * + * @param oldState The old state + * @param proposedState The proposed state + * @return The new state + */ + public boolean getNewState(boolean oldState, boolean proposedState) { + return proposedState; + } + + /** + * @return Whether or not this {@link Behavior} is active. + */ + public final boolean isEnabled() { + return this.enabled; + } + + /** + * Called when the state changes from disabled to enabled + */ + public void onStart() {} + + /** + * Called when the state changes from enabled to disabled + */ + public void onCancel() {} +} diff --git a/src/main/java/baritone/bot/behavior/PathFinding.java b/src/main/java/baritone/bot/behavior/PathFinding.java new file mode 100644 index 000000000..131712c7d --- /dev/null +++ b/src/main/java/baritone/bot/behavior/PathFinding.java @@ -0,0 +1,8 @@ +package baritone.bot.behavior; + +/** + * @author Brady + * @since 8/1/2018 5:38 PM + */ +public class PathFinding { +}