From eb5cb5deba4a2f8f1cd181fef073c0ae88d8009b Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 12 Jul 2018 14:40:36 -0400 Subject: [PATCH] Add chatbox input listener and use for chatbox input event The immediate eventbus does not pass classloader validation: java.lang.SecurityException: class "com.google.common.eventbus.ImmediateEventBus"'s signer information does not match signer information of other classes in the same package --- .../client/chat/ChatboxInputListener.java | 32 +++++++++++++++++++ .../runelite/client/chat/CommandManager.java | 22 +++++++++++-- .../runelite/client/events/ChatboxInput.java | 1 - .../chatcommands/ChatCommandsPlugin.java | 16 ++++++---- 4 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/chat/ChatboxInputListener.java diff --git a/runelite-client/src/main/java/net/runelite/client/chat/ChatboxInputListener.java b/runelite-client/src/main/java/net/runelite/client/chat/ChatboxInputListener.java new file mode 100644 index 0000000000..41e1ba9301 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/chat/ChatboxInputListener.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2018, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.chat; + +import net.runelite.client.events.ChatboxInput; + +public interface ChatboxInputListener +{ + boolean onChatboxInput(ChatboxInput chatboxInput); +} diff --git a/runelite-client/src/main/java/net/runelite/client/chat/CommandManager.java b/runelite-client/src/main/java/net/runelite/client/chat/CommandManager.java index a0600c651c..0c6f5734aa 100644 --- a/runelite-client/src/main/java/net/runelite/client/chat/CommandManager.java +++ b/runelite-client/src/main/java/net/runelite/client/chat/CommandManager.java @@ -27,7 +27,9 @@ package net.runelite.client.chat; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import javax.inject.Inject; import javax.inject.Provider; import javax.inject.Singleton; @@ -52,6 +54,8 @@ public class CommandManager private final Provider clientThreadProvider; private boolean sending; + private final List chatboxInputListenerList = new ArrayList<>(); + @Inject public CommandManager(Provider clientProvider, EventBus eventBus, Provider clientThreadProvider) { @@ -60,6 +64,16 @@ public class CommandManager this.clientThreadProvider = clientThreadProvider; } + public void register(ChatboxInputListener chatboxInputListener) + { + chatboxInputListenerList.add(chatboxInputListener); + } + + public void unregister(ChatboxInputListener chatboxInputListener) + { + chatboxInputListenerList.remove(chatboxInputListener); + } + @Subscribe private void scriptEvent(ScriptCallbackEvent event) { @@ -129,9 +143,13 @@ public class CommandManager clientThread.invokeLater(() -> sendChatboxInput(chatType, typedText)); } }; - eventBus.post(chatboxInput); + boolean stop = false; + for (ChatboxInputListener chatboxInputListener : chatboxInputListenerList) + { + stop |= chatboxInputListener.onChatboxInput(chatboxInput); + } - if (chatboxInput.isStop()) + if (stop) { // input was blocked. stringStack[stringStackCount - 1] = ""; // prevent script from sending diff --git a/runelite-client/src/main/java/net/runelite/client/events/ChatboxInput.java b/runelite-client/src/main/java/net/runelite/client/events/ChatboxInput.java index 5b00cf0065..77f11b6024 100644 --- a/runelite-client/src/main/java/net/runelite/client/events/ChatboxInput.java +++ b/runelite-client/src/main/java/net/runelite/client/events/ChatboxInput.java @@ -31,7 +31,6 @@ public abstract class ChatboxInput { private final String value; private final int chatType; - private boolean stop; public abstract void resume(); } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/chatcommands/ChatCommandsPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/chatcommands/ChatCommandsPlugin.java index f423c40957..a77cb8ddd3 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/chatcommands/ChatCommandsPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/chatcommands/ChatCommandsPlugin.java @@ -47,6 +47,7 @@ import net.runelite.api.vars.AccountType; import net.runelite.client.chat.ChatColorType; import net.runelite.client.chat.ChatMessageBuilder; import net.runelite.client.chat.ChatMessageManager; +import net.runelite.client.chat.ChatboxInputListener; import net.runelite.client.chat.CommandManager; import net.runelite.client.config.ConfigManager; import net.runelite.client.events.ChatboxInput; @@ -72,7 +73,7 @@ import net.runelite.http.api.kc.KillCountClient; tags = {"grand", "exchange", "level", "prices"} ) @Slf4j -public class ChatCommandsPlugin extends Plugin +public class ChatCommandsPlugin extends Plugin implements ChatboxInputListener { private static final float HIGH_ALCHEMY_CONSTANT = 0.6f; private static final Pattern KILLCOUNT_PATERN = Pattern.compile("Your ([a-zA-Z ]+) kill count is: (\\d+)."); @@ -112,12 +113,14 @@ public class ChatCommandsPlugin extends Plugin public void startUp() { keyManager.registerKeyListener(chatKeyboardListener); + commandManager.register(this); } @Override public void shutDown() { keyManager.unregisterKeyListener(chatKeyboardListener); + commandManager.unregister(this); } @Provides @@ -237,13 +240,13 @@ public class ChatCommandsPlugin extends Plugin } } - @Subscribe - public void onChatboxInput(ChatboxInput chatboxInput) + @Override + public boolean onChatboxInput(ChatboxInput chatboxInput) { final String value = chatboxInput.getValue(); if (!value.startsWith("!kc ") && !value.startsWith("/!kc ")) { - return; + return false; } int idx = value.indexOf(' '); @@ -252,10 +255,9 @@ public class ChatCommandsPlugin extends Plugin final int kc = getKc(boss); if (kc <= 0) { - return; + return false; } - chatboxInput.setStop(true); final String playerName = client.getLocalPlayer().getName(); executor.execute(() -> @@ -273,6 +275,8 @@ public class ChatCommandsPlugin extends Plugin chatboxInput.resume(); } }); + + return true; } private void killCountLookup(ChatMessageType type, SetMessage setMessage, String search)