Merge pull request #2177 from Lucwousin/chat-eventbus
client: Refactor chat input listeners to just use the eventbus
This commit is contained in:
@@ -35,26 +35,26 @@ import net.runelite.api.Client;
|
|||||||
import net.runelite.api.GameState;
|
import net.runelite.api.GameState;
|
||||||
import net.runelite.api.events.ChatMessage;
|
import net.runelite.api.events.ChatMessage;
|
||||||
import net.runelite.client.eventbus.EventBus;
|
import net.runelite.client.eventbus.EventBus;
|
||||||
|
import net.runelite.client.eventbus.Subscribe;
|
||||||
import net.runelite.client.events.ChatInput;
|
import net.runelite.client.events.ChatInput;
|
||||||
import net.runelite.client.events.ChatboxInput;
|
import net.runelite.client.events.ChatboxInput;
|
||||||
import net.runelite.client.events.PrivateMessageInput;
|
import net.runelite.client.events.PrivateMessageInput;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
public class ChatCommandManager implements ChatboxInputListener
|
public class ChatCommandManager
|
||||||
{
|
{
|
||||||
private final Map<String, ChatCommand> commands = new ConcurrentHashMap<>();
|
private final Map<String, ChatCommand> commands = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private final Client client;
|
private final Client client;
|
||||||
private final ScheduledExecutorService scheduledExecutorService;
|
private final ScheduledExecutorService scheduledExecutorService;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ChatCommandManager(EventBus eventBus, CommandManager commandManager, Client client, ScheduledExecutorService scheduledExecutorService)
|
private ChatCommandManager(EventBus eventBus, Client client, ScheduledExecutorService scheduledExecutorService)
|
||||||
{
|
{
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.scheduledExecutorService = scheduledExecutorService;
|
this.scheduledExecutorService = scheduledExecutorService;
|
||||||
// eventBus.register(this);
|
|
||||||
commandManager.register(this);
|
|
||||||
|
|
||||||
|
eventBus.subscribe(ChatboxInput.class, this, this::onChatboxInput);
|
||||||
|
eventBus.subscribe(PrivateMessageInput.class, this, this::onPrivateMessageInput);
|
||||||
eventBus.subscribe(ChatMessage.class, this, this::onChatMessage);
|
eventBus.subscribe(ChatMessage.class, this, this::onChatMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,11 +106,6 @@ public class ChatCommandManager implements ChatboxInputListener
|
|||||||
String message = chatMessage.getMessage();
|
String message = chatMessage.getMessage();
|
||||||
|
|
||||||
String command = extractCommand(message);
|
String command = extractCommand(message);
|
||||||
if (command == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChatCommand chatCommand = commands.get(command.toLowerCase());
|
ChatCommand chatCommand = commands.get(command.toLowerCase());
|
||||||
if (chatCommand == null)
|
if (chatCommand == null)
|
||||||
{
|
{
|
||||||
@@ -127,8 +122,8 @@ public class ChatCommandManager implements ChatboxInputListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Subscribe // just for show
|
||||||
public boolean onChatboxInput(ChatboxInput chatboxInput)
|
private void onChatboxInput(ChatboxInput chatboxInput)
|
||||||
{
|
{
|
||||||
String message = chatboxInput.getValue();
|
String message = chatboxInput.getValue();
|
||||||
if (message.startsWith("/"))
|
if (message.startsWith("/"))
|
||||||
@@ -136,51 +131,30 @@ public class ChatCommandManager implements ChatboxInputListener
|
|||||||
message = message.substring(1); // clan chat input
|
message = message.substring(1); // clan chat input
|
||||||
}
|
}
|
||||||
|
|
||||||
String command = extractCommand(message);
|
onInput(chatboxInput, message);
|
||||||
if (command == null)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChatCommand chatCommand = commands.get(command.toLowerCase());
|
|
||||||
if (chatCommand == null)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
BiPredicate<ChatInput, String> input = chatCommand.getInput();
|
|
||||||
if (input == null)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return input.test(chatboxInput, message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Subscribe // just for show
|
||||||
public boolean onPrivateMessageInput(PrivateMessageInput privateMessageInput)
|
private void onPrivateMessageInput(PrivateMessageInput input)
|
||||||
{
|
{
|
||||||
final String message = privateMessageInput.getMessage();
|
onInput(input, input.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onInput(ChatInput chatInput, String message)
|
||||||
|
{
|
||||||
String command = extractCommand(message);
|
String command = extractCommand(message);
|
||||||
if (command == null)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChatCommand chatCommand = commands.get(command.toLowerCase());
|
ChatCommand chatCommand = commands.get(command.toLowerCase());
|
||||||
if (chatCommand == null)
|
if (chatCommand == null)
|
||||||
{
|
{
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
BiPredicate<ChatInput, String> input = chatCommand.getInput();
|
BiPredicate<ChatInput, String> input = chatCommand.getInput();
|
||||||
if (input == null)
|
if (input != null && input.test(chatInput, message))
|
||||||
{
|
{
|
||||||
return false;
|
chatInput.setStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
return input.test(privateMessageInput, message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String extractCommand(String message)
|
private static String extractCommand(String message)
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2018, Adam <Adam@sigterm.info>
|
|
||||||
* 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;
|
|
||||||
import net.runelite.client.events.PrivateMessageInput;
|
|
||||||
|
|
||||||
public interface ChatboxInputListener
|
|
||||||
{
|
|
||||||
boolean onChatboxInput(ChatboxInput chatboxInput);
|
|
||||||
|
|
||||||
boolean onPrivateMessageInput(PrivateMessageInput privateMessageInput);
|
|
||||||
}
|
|
||||||
@@ -26,8 +26,6 @@
|
|||||||
package net.runelite.client.chat;
|
package net.runelite.client.chat;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -54,8 +52,6 @@ public class CommandManager
|
|||||||
private final ClientThread clientThread;
|
private final ClientThread clientThread;
|
||||||
private boolean sending;
|
private boolean sending;
|
||||||
|
|
||||||
private final List<ChatboxInputListener> chatboxInputListenerList = new CopyOnWriteArrayList<>();
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private CommandManager(
|
private CommandManager(
|
||||||
final Client client,
|
final Client client,
|
||||||
@@ -70,16 +66,6 @@ public class CommandManager
|
|||||||
eventBus.subscribe(ScriptCallbackEvent.class, this, this::onScriptCallbackEvent);
|
eventBus.subscribe(ScriptCallbackEvent.class, this, this::onScriptCallbackEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void register(ChatboxInputListener chatboxInputListener)
|
|
||||||
{
|
|
||||||
chatboxInputListenerList.add(chatboxInputListener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void unregister(ChatboxInputListener chatboxInputListener)
|
|
||||||
{
|
|
||||||
chatboxInputListenerList.remove(chatboxInputListener);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onScriptCallbackEvent(ScriptCallbackEvent event)
|
private void onScriptCallbackEvent(ScriptCallbackEvent event)
|
||||||
{
|
{
|
||||||
if (sending)
|
if (sending)
|
||||||
@@ -148,13 +134,10 @@ public class CommandManager
|
|||||||
clientThread.invoke(() -> sendChatboxInput(chatType, typedText));
|
clientThread.invoke(() -> sendChatboxInput(chatType, typedText));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
boolean stop = false;
|
|
||||||
for (ChatboxInputListener chatboxInputListener : chatboxInputListenerList)
|
|
||||||
{
|
|
||||||
stop |= chatboxInputListener.onChatboxInput(chatboxInput);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stop)
|
eventBus.post(ChatboxInput.class, chatboxInput);
|
||||||
|
|
||||||
|
if (chatboxInput.isStop())
|
||||||
{
|
{
|
||||||
// input was blocked.
|
// input was blocked.
|
||||||
stringStack[stringStackCount - 1] = ""; // prevent script from sending
|
stringStack[stringStackCount - 1] = ""; // prevent script from sending
|
||||||
@@ -188,13 +171,9 @@ public class CommandManager
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
boolean stop = false;
|
eventBus.post(PrivateMessageInput.class, privateMessageInput);
|
||||||
for (ChatboxInputListener chatboxInputListener : chatboxInputListenerList)
|
|
||||||
{
|
|
||||||
stop |= chatboxInputListener.onPrivateMessageInput(privateMessageInput);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stop)
|
if (privateMessageInput.isStop())
|
||||||
{
|
{
|
||||||
intStack[intStackCount - 1] = 1;
|
intStack[intStackCount - 1] = 1;
|
||||||
client.setStringStackSize(stringStackCount - 2); // remove both target and message
|
client.setStringStackSize(stringStackCount - 2); // remove both target and message
|
||||||
|
|||||||
@@ -24,7 +24,18 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.client.events;
|
package net.runelite.client.events;
|
||||||
|
|
||||||
public abstract class ChatInput
|
import lombok.Getter;
|
||||||
|
import net.runelite.api.events.Event;
|
||||||
|
|
||||||
|
public abstract class ChatInput implements Event
|
||||||
{
|
{
|
||||||
|
@Getter
|
||||||
|
private boolean stop = false;
|
||||||
|
|
||||||
|
public void setStop()
|
||||||
|
{
|
||||||
|
this.stop = true;
|
||||||
|
}
|
||||||
|
|
||||||
public abstract void resume();
|
public abstract void resume();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,14 +37,12 @@ import net.runelite.api.GameState;
|
|||||||
import net.runelite.client.chat.ChatColorType;
|
import net.runelite.client.chat.ChatColorType;
|
||||||
import net.runelite.client.chat.ChatMessageBuilder;
|
import net.runelite.client.chat.ChatMessageBuilder;
|
||||||
import net.runelite.client.chat.ChatMessageManager;
|
import net.runelite.client.chat.ChatMessageManager;
|
||||||
import net.runelite.client.chat.ChatboxInputListener;
|
|
||||||
import net.runelite.client.chat.CommandManager;
|
import net.runelite.client.chat.CommandManager;
|
||||||
import net.runelite.client.chat.QueuedMessage;
|
import net.runelite.client.chat.QueuedMessage;
|
||||||
import net.runelite.client.config.ConfigManager;
|
import net.runelite.client.config.ConfigManager;
|
||||||
import net.runelite.client.eventbus.Subscribe;
|
import net.runelite.client.eventbus.Subscribe;
|
||||||
import net.runelite.client.events.ChatboxInput;
|
import net.runelite.client.events.ChatboxInput;
|
||||||
import net.runelite.client.events.ConfigChanged;
|
import net.runelite.client.events.ConfigChanged;
|
||||||
import net.runelite.client.events.PrivateMessageInput;
|
|
||||||
import net.runelite.client.plugins.Plugin;
|
import net.runelite.client.plugins.Plugin;
|
||||||
import net.runelite.client.plugins.PluginDescriptor;
|
import net.runelite.client.plugins.PluginDescriptor;
|
||||||
import net.runelite.client.plugins.twitch.irc.TwitchIRCClient;
|
import net.runelite.client.plugins.twitch.irc.TwitchIRCClient;
|
||||||
@@ -58,7 +56,7 @@ import net.runelite.client.task.Schedule;
|
|||||||
)
|
)
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Singleton
|
@Singleton
|
||||||
public class TwitchPlugin extends Plugin implements TwitchListener, ChatboxInputListener
|
public class TwitchPlugin extends Plugin implements TwitchListener
|
||||||
{
|
{
|
||||||
@Inject
|
@Inject
|
||||||
private TwitchConfig twitchConfig;
|
private TwitchConfig twitchConfig;
|
||||||
@@ -78,7 +76,6 @@ public class TwitchPlugin extends Plugin implements TwitchListener, ChatboxInput
|
|||||||
protected void startUp()
|
protected void startUp()
|
||||||
{
|
{
|
||||||
connect();
|
connect();
|
||||||
commandManager.register(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -89,8 +86,6 @@ public class TwitchPlugin extends Plugin implements TwitchListener, ChatboxInput
|
|||||||
twitchIRCClient.close();
|
twitchIRCClient.close();
|
||||||
twitchIRCClient = null;
|
twitchIRCClient = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
commandManager.unregister(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@@ -208,29 +203,22 @@ public class TwitchPlugin extends Plugin implements TwitchListener, ChatboxInput
|
|||||||
addChatMessage("[System]", sysmsg);
|
addChatMessage("[System]", sysmsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Subscribe
|
||||||
public boolean onChatboxInput(ChatboxInput chatboxInput)
|
private void onChatboxInput(ChatboxInput chatboxInput)
|
||||||
{
|
{
|
||||||
String message = chatboxInput.getValue();
|
String message = chatboxInput.getValue();
|
||||||
if (message.startsWith("//"))
|
if (!message.startsWith("//"))
|
||||||
{
|
{
|
||||||
message = message.substring(2);
|
return;
|
||||||
if (message.isEmpty() || twitchIRCClient == null)
|
}
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
message = message.substring(2);
|
||||||
|
if (!message.isEmpty() && twitchIRCClient != null)
|
||||||
|
{
|
||||||
twitchIRCClient.privmsg(message);
|
twitchIRCClient.privmsg(message);
|
||||||
addChatMessage(twitchConfig.username(), message);
|
addChatMessage(twitchConfig.username(), message);
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
chatboxInput.setStop();
|
||||||
public boolean onPrivateMessageInput(PrivateMessageInput privateMessageInput)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user