From a36b223aabcf402713dc3755b01fe37334424e80 Mon Sep 17 00:00:00 2001 From: Brady Date: Wed, 1 Aug 2018 18:46:51 -0700 Subject: [PATCH] Add an onSendChatMessage event --- .../java/baritone/bot/GameEventHandler.java | 10 ++++--- .../bot/event/AbstractGameEventListener.java | 28 +++++++++++++++++++ .../bot/event/IGameEventListener.java | 7 +++++ .../baritone/bot/event/events/ChatEvent.java | 26 +++++++++++++++++ .../bot/event/events/type/Cancellable.java | 27 ++++++++++++++++++ .../launch/mixins/MixinEntityPlayerSP.java | 15 ++++------ 6 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 src/main/java/baritone/bot/event/AbstractGameEventListener.java create mode 100644 src/main/java/baritone/bot/event/events/ChatEvent.java create mode 100644 src/main/java/baritone/bot/event/events/type/Cancellable.java diff --git a/src/main/java/baritone/bot/GameEventHandler.java b/src/main/java/baritone/bot/GameEventHandler.java index a705e9d9..de79272a 100755 --- a/src/main/java/baritone/bot/GameEventHandler.java +++ b/src/main/java/baritone/bot/GameEventHandler.java @@ -1,6 +1,7 @@ package baritone.bot; import baritone.bot.event.IGameEventListener; +import baritone.bot.event.events.ChatEvent; import net.minecraft.client.settings.KeyBinding; import org.lwjgl.input.Keyboard; @@ -13,9 +14,7 @@ public final class GameEventHandler implements IGameEventListener { GameEventHandler() {} @Override - public final void onTick() { - baritone.Baritone.onTick(); - } + public final void onTick() {} @Override public void onProcessKeyBinds() { @@ -33,4 +32,7 @@ public final class GameEventHandler implements IGameEventListener { } } } -} + + @Override + public void onSendChatMessage(ChatEvent event) {} +} \ No newline at end of file diff --git a/src/main/java/baritone/bot/event/AbstractGameEventListener.java b/src/main/java/baritone/bot/event/AbstractGameEventListener.java new file mode 100644 index 00000000..6e7953bd --- /dev/null +++ b/src/main/java/baritone/bot/event/AbstractGameEventListener.java @@ -0,0 +1,28 @@ +package baritone.bot.event; + +import baritone.bot.event.events.ChatEvent; + +/** + * An implementation of {@link IGameEventListener} that has all methods + * overriden with empty bodies, allowing inheritors of this class to choose + * which events they would like to listen in on. + * + * Has no implementors at the moment, but will likely be used with the + * manager/behavior system is ported over to the new system. + * + * @see IGameEventListener + * + * @author Brady + * @since 8/1/2018 6:29 PM + */ +public interface AbstractGameEventListener extends IGameEventListener { + + @Override + default void onTick() {} + + @Override + default void onProcessKeyBinds() {} + + @Override + default void onSendChatMessage(ChatEvent event) {} +} diff --git a/src/main/java/baritone/bot/event/IGameEventListener.java b/src/main/java/baritone/bot/event/IGameEventListener.java index 3b1e7680..c3e31a56 100755 --- a/src/main/java/baritone/bot/event/IGameEventListener.java +++ b/src/main/java/baritone/bot/event/IGameEventListener.java @@ -1,6 +1,8 @@ package baritone.bot.event; +import baritone.bot.event.events.ChatEvent; import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.EntityPlayerSP; /** * @author Brady @@ -17,4 +19,9 @@ public interface IGameEventListener { * Run once per game tick from {@link Minecraft#processKeyBinds()} */ void onProcessKeyBinds(); + + /** + * Runs whenever the client player sends a message to the server {@link EntityPlayerSP#sendChatMessage(String)} + */ + void onSendChatMessage(ChatEvent event); } diff --git a/src/main/java/baritone/bot/event/events/ChatEvent.java b/src/main/java/baritone/bot/event/events/ChatEvent.java new file mode 100644 index 00000000..e928834e --- /dev/null +++ b/src/main/java/baritone/bot/event/events/ChatEvent.java @@ -0,0 +1,26 @@ +package baritone.bot.event.events; + +import baritone.bot.event.events.type.Cancellable; + +/** + * @author Brady + * @since 8/1/2018 6:39 PM + */ +public final class ChatEvent extends Cancellable { + + /** + * The message being sent + */ + private final String message; + + public ChatEvent(String message) { + this.message = message; + } + + /** + * @return The message being sent + */ + public final String getMessage() { + return this.message; + } +} diff --git a/src/main/java/baritone/bot/event/events/type/Cancellable.java b/src/main/java/baritone/bot/event/events/type/Cancellable.java new file mode 100644 index 00000000..d590ad9a --- /dev/null +++ b/src/main/java/baritone/bot/event/events/type/Cancellable.java @@ -0,0 +1,27 @@ +package baritone.bot.event.events.type; + +/** + * @author Brady + * @since 8/1/2018 6:41 PM + */ +public class Cancellable { + + /** + * Whether or not this event has been cancelled + */ + private boolean cancelled; + + /** + * Cancels this event + */ + public final void cancel() { + this.cancelled = true; + } + + /** + * @return Whether or not this event has been cancelled + */ + public final boolean isCancelled() { + return this.cancelled; + } +} diff --git a/src/main/java/baritone/launch/mixins/MixinEntityPlayerSP.java b/src/main/java/baritone/launch/mixins/MixinEntityPlayerSP.java index bae0cc5c..2a4ebca3 100644 --- a/src/main/java/baritone/launch/mixins/MixinEntityPlayerSP.java +++ b/src/main/java/baritone/launch/mixins/MixinEntityPlayerSP.java @@ -1,14 +1,13 @@ package baritone.launch.mixins; -import baritone.util.ChatCommand; +import baritone.bot.Baritone; +import baritone.bot.event.events.ChatEvent; import net.minecraft.client.entity.EntityPlayerSP; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import java.lang.reflect.InvocationTargetException; - /** * @author Brady * @since 8/1/2018 5:06 PM @@ -22,11 +21,9 @@ public class MixinEntityPlayerSP { cancellable = true ) private void sendChatMessage(String msg, CallbackInfo ci) { - try { - if (ChatCommand.message(msg)) - ci.cancel(); - } catch (IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - } + ChatEvent event = new ChatEvent(msg); + Baritone.INSTANCE.getGameEventHandler().onSendChatMessage(event); + if (event.isCancelled()) + ci.cancel(); } }