Merge pull request #2177 from Lucwousin/chat-eventbus

client: Refactor chat input listeners to just use the eventbus
This commit is contained in:
ST0NEWALL
2020-01-08 19:25:43 -05:00
committed by GitHub
5 changed files with 44 additions and 127 deletions

View File

@@ -35,26 +35,26 @@ import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.events.ChatMessage;
import net.runelite.client.eventbus.EventBus;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ChatInput;
import net.runelite.client.events.ChatboxInput;
import net.runelite.client.events.PrivateMessageInput;
@Singleton
public class ChatCommandManager implements ChatboxInputListener
public class ChatCommandManager
{
private final Map<String, ChatCommand> commands = new ConcurrentHashMap<>();
private final Client client;
private final ScheduledExecutorService scheduledExecutorService;
@Inject
private ChatCommandManager(EventBus eventBus, CommandManager commandManager, Client client, ScheduledExecutorService scheduledExecutorService)
private ChatCommandManager(EventBus eventBus, Client client, ScheduledExecutorService scheduledExecutorService)
{
this.client = client;
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);
}
@@ -106,11 +106,6 @@ public class ChatCommandManager implements ChatboxInputListener
String message = chatMessage.getMessage();
String command = extractCommand(message);
if (command == null)
{
return;
}
ChatCommand chatCommand = commands.get(command.toLowerCase());
if (chatCommand == null)
{
@@ -127,8 +122,8 @@ public class ChatCommandManager implements ChatboxInputListener
}
}
@Override
public boolean onChatboxInput(ChatboxInput chatboxInput)
@Subscribe // just for show
private void onChatboxInput(ChatboxInput chatboxInput)
{
String message = chatboxInput.getValue();
if (message.startsWith("/"))
@@ -136,51 +131,30 @@ public class ChatCommandManager implements ChatboxInputListener
message = message.substring(1); // clan chat input
}
String command = extractCommand(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);
onInput(chatboxInput, message);
}
@Override
public boolean onPrivateMessageInput(PrivateMessageInput privateMessageInput)
@Subscribe // just for show
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);
if (command == null)
{
return false;
}
ChatCommand chatCommand = commands.get(command.toLowerCase());
if (chatCommand == null)
{
return false;
return;
}
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)

View File

@@ -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);
}

View File

@@ -26,8 +26,6 @@
package net.runelite.client.chat;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
@@ -54,8 +52,6 @@ public class CommandManager
private final ClientThread clientThread;
private boolean sending;
private final List<ChatboxInputListener> chatboxInputListenerList = new CopyOnWriteArrayList<>();
@Inject
private CommandManager(
final Client client,
@@ -70,16 +66,6 @@ public class CommandManager
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)
{
if (sending)
@@ -148,13 +134,10 @@ public class CommandManager
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.
stringStack[stringStackCount - 1] = ""; // prevent script from sending
@@ -188,13 +171,9 @@ public class CommandManager
}
};
boolean stop = false;
for (ChatboxInputListener chatboxInputListener : chatboxInputListenerList)
{
stop |= chatboxInputListener.onPrivateMessageInput(privateMessageInput);
}
eventBus.post(PrivateMessageInput.class, privateMessageInput);
if (stop)
if (privateMessageInput.isStop())
{
intStack[intStackCount - 1] = 1;
client.setStringStackSize(stringStackCount - 2); // remove both target and message

View File

@@ -24,7 +24,18 @@
*/
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();
}

View File

@@ -37,14 +37,12 @@ import net.runelite.api.GameState;
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.chat.QueuedMessage;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ChatboxInput;
import net.runelite.client.events.ConfigChanged;
import net.runelite.client.events.PrivateMessageInput;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.twitch.irc.TwitchIRCClient;
@@ -58,7 +56,7 @@ import net.runelite.client.task.Schedule;
)
@Slf4j
@Singleton
public class TwitchPlugin extends Plugin implements TwitchListener, ChatboxInputListener
public class TwitchPlugin extends Plugin implements TwitchListener
{
@Inject
private TwitchConfig twitchConfig;
@@ -78,7 +76,6 @@ public class TwitchPlugin extends Plugin implements TwitchListener, ChatboxInput
protected void startUp()
{
connect();
commandManager.register(this);
}
@Override
@@ -89,8 +86,6 @@ public class TwitchPlugin extends Plugin implements TwitchListener, ChatboxInput
twitchIRCClient.close();
twitchIRCClient = null;
}
commandManager.unregister(this);
}
@Provides
@@ -208,29 +203,22 @@ public class TwitchPlugin extends Plugin implements TwitchListener, ChatboxInput
addChatMessage("[System]", sysmsg);
}
@Override
public boolean onChatboxInput(ChatboxInput chatboxInput)
@Subscribe
private void onChatboxInput(ChatboxInput chatboxInput)
{
String message = chatboxInput.getValue();
if (message.startsWith("//"))
if (!message.startsWith("//"))
{
message = message.substring(2);
if (message.isEmpty() || twitchIRCClient == null)
{
return true;
}
return;
}
message = message.substring(2);
if (!message.isEmpty() && twitchIRCClient != null)
{
twitchIRCClient.privmsg(message);
addChatMessage(twitchConfig.username(), message);
return true;
}
return false;
}
@Override
public boolean onPrivateMessageInput(PrivateMessageInput privateMessageInput)
{
return false;
chatboxInput.setStop();
}
}