forked from ChomeNS/chomens-bot-java
make a commandspy plugin instead of making a chat parser that parses commandspy
This commit is contained in:
parent
364203aa80
commit
78eac00461
6 changed files with 115 additions and 78 deletions
|
@ -47,6 +47,7 @@ public class Bot {
|
|||
|
||||
public TickPlugin tick;
|
||||
public ChatPlugin chat;
|
||||
public CommandSpyPlugin commandSpy;
|
||||
public PositionPlugin position;
|
||||
public SelfCarePlugin selfCare;
|
||||
public CorePlugin core;
|
||||
|
@ -92,6 +93,7 @@ public class Bot {
|
|||
public void ready () {
|
||||
this.tick = new TickPlugin(this);
|
||||
this.chat = new ChatPlugin(this);
|
||||
this.commandSpy = new CommandSpyPlugin(this);
|
||||
this.position = new PositionPlugin(this);
|
||||
this.selfCare = new SelfCarePlugin(this);
|
||||
this.core = new CorePlugin(this);
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
package land.chipmunk.chayapak.chomens_bot.chatParsers.commandSpy;
|
||||
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.ChatParser;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerMessage;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CommandSpyParser implements ChatParser {
|
||||
private final Bot bot;
|
||||
|
||||
public CommandSpyParser (Bot bot) {
|
||||
this.bot = bot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerMessage parse (Component message) {
|
||||
if (message instanceof TextComponent) return parse((TextComponent) message);
|
||||
return null;
|
||||
}
|
||||
|
||||
public PlayerMessage parse (TextComponent message) {
|
||||
final List<Component> children = message.children();
|
||||
|
||||
if (
|
||||
(
|
||||
message.color() != NamedTextColor.AQUA ||
|
||||
message.color() != NamedTextColor.YELLOW ||
|
||||
message.style().isEmpty()
|
||||
) &&
|
||||
children.size() < 2
|
||||
) return null;
|
||||
|
||||
final Component username = Component.text(message.content());
|
||||
final Component command = children.get(1);
|
||||
|
||||
final String stringUsername = ComponentUtilities.stringify(username);
|
||||
MutablePlayerListEntry sender = bot.players.getEntry(stringUsername);
|
||||
|
||||
if (sender == null) return null;
|
||||
|
||||
return new PlayerMessage(sender, username, command);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package land.chipmunk.chayapak.chomens_bot.plugins;
|
||||
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerMessage;
|
||||
import land.chipmunk.chayapak.chomens_bot.command.PlayerCommandContext;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
|
||||
|
@ -22,15 +23,17 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.Listener {
|
|||
this.commandSpyPrefixes = bot.config.commandSpyPrefixes;
|
||||
|
||||
bot.chat.addListener(this);
|
||||
|
||||
bot.commandSpy.addListener(new CommandSpyPlugin.Listener() {
|
||||
@Override
|
||||
public void commandReceived(MutablePlayerListEntry sender, String command) {
|
||||
ChatCommandHandlerPlugin.this.commandSpyMessageReceived(sender, command);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playerMessageReceived (PlayerMessage message) { listener(message, false); }
|
||||
|
||||
@Override
|
||||
public void commandSpyMessageReceived (PlayerMessage message) { listener(message, true); }
|
||||
|
||||
private void listener (PlayerMessage message, boolean cspy) {
|
||||
public void playerMessageReceived (PlayerMessage message) {
|
||||
try {
|
||||
if (message.sender.profile.getId().equals(bot.profile.getId())) return;
|
||||
} catch (Exception ignored) {} // kinda sus ngl
|
||||
|
@ -44,7 +47,7 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.Listener {
|
|||
|
||||
String prefix = null;
|
||||
|
||||
for (String eachPrefix : (cspy ? commandSpyPrefixes : prefixes)) {
|
||||
for (String eachPrefix : prefixes) {
|
||||
if (!contents.startsWith(eachPrefix)) continue;
|
||||
prefix = eachPrefix;
|
||||
}
|
||||
|
@ -53,14 +56,42 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.Listener {
|
|||
|
||||
final String commandString = contents.substring(prefix.length());
|
||||
|
||||
final String selector = cspy ? UUIDUtilities.selector(message.sender.profile.getId()) : "@a";
|
||||
|
||||
final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, selector, message.sender, bot.hashing.hash, bot.hashing.ownerHash);
|
||||
final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, "@a", message.sender, bot.hashing.hash, bot.hashing.ownerHash);
|
||||
|
||||
final Component output = bot.commandHandler.executeCommand(commandString, context, true, false, false, null);
|
||||
|
||||
if (output != null) {
|
||||
context.sendOutput(output);
|
||||
if (output != null) context.sendOutput(output);
|
||||
}
|
||||
|
||||
public void commandSpyMessageReceived (MutablePlayerListEntry sender, String command) {
|
||||
try {
|
||||
if (sender.profile.getId().equals(bot.profile.getId())) return;
|
||||
} catch (Exception ignored) {
|
||||
} // kinda sus ngl
|
||||
|
||||
final Component displayNameComponent = Component.text(sender.profile.getName());
|
||||
final Component messageComponent = Component.text(command);
|
||||
|
||||
final String displayName = ComponentUtilities.stringify(displayNameComponent);
|
||||
final String contents = ComponentUtilities.stringify(messageComponent);
|
||||
|
||||
String prefix = null;
|
||||
|
||||
for (String eachPrefix : commandSpyPrefixes) {
|
||||
if (!contents.startsWith(eachPrefix)) continue;
|
||||
prefix = eachPrefix;
|
||||
}
|
||||
|
||||
if (prefix == null) return;
|
||||
|
||||
final String commandString = contents.substring(prefix.length());
|
||||
|
||||
final String selector = UUIDUtilities.selector(sender.profile.getId());
|
||||
|
||||
final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, selector, sender, bot.hashing.hash, bot.hashing.ownerHash);
|
||||
|
||||
final Component output = bot.commandHandler.executeCommand(commandString, context, true, false, false, null);
|
||||
|
||||
if (output != null) context.sendOutput(output);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ import land.chipmunk.chayapak.chomens_bot.chatParsers.CreayunChatParser;
|
|||
import land.chipmunk.chayapak.chomens_bot.chatParsers.KaboomChatParser;
|
||||
import land.chipmunk.chayapak.chomens_bot.chatParsers.MinecraftChatParser;
|
||||
import land.chipmunk.chayapak.chomens_bot.chatParsers.U203aChatParser;
|
||||
import land.chipmunk.chayapak.chomens_bot.chatParsers.commandSpy.CommandSpyParser;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.ChatParser;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerMessage;
|
||||
|
@ -37,8 +36,6 @@ public class ChatPlugin extends Bot.Listener {
|
|||
|
||||
private final List<ChatParser> chatParsers;
|
||||
|
||||
private final CommandSpyParser commandSpyParser;
|
||||
|
||||
private final List<String> queue = new ArrayList<>();
|
||||
|
||||
public final int queueDelay;
|
||||
|
@ -50,8 +47,6 @@ public class ChatPlugin extends Bot.Listener {
|
|||
|
||||
queueDelay = bot.options.chatQueueDelay;
|
||||
|
||||
this.commandSpyParser = new CommandSpyParser(bot);
|
||||
|
||||
bot.addListener(this);
|
||||
|
||||
chatParsers = new ArrayList<>();
|
||||
|
@ -89,12 +84,9 @@ public class ChatPlugin extends Bot.Listener {
|
|||
if (playerMessage != null) break;
|
||||
}
|
||||
|
||||
final PlayerMessage commandSpyMessage = commandSpyParser.parse(component);
|
||||
|
||||
for (Listener listener : listeners) {
|
||||
listener.systemMessageReceived(component);
|
||||
if (playerMessage != null) listener.playerMessageReceived(playerMessage);
|
||||
if (commandSpyMessage != null) listener.commandSpyMessageReceived(commandSpyMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -315,7 +307,6 @@ public class ChatPlugin extends Bot.Listener {
|
|||
|
||||
public static class Listener {
|
||||
public void playerMessageReceived (PlayerMessage message) {}
|
||||
public void commandSpyMessageReceived (PlayerMessage message) {}
|
||||
public void systemMessageReceived (Component component) {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
package land.chipmunk.chayapak.chomens_bot.plugins;
|
||||
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandSpyPlugin extends ChatPlugin.Listener {
|
||||
private final Bot bot;
|
||||
|
||||
private final List<Listener> listeners = new ArrayList<>();
|
||||
|
||||
public CommandSpyPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.chat.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void systemMessageReceived(Component component) {
|
||||
TextComponent textComponent = null;
|
||||
|
||||
try {
|
||||
textComponent = (TextComponent) component;
|
||||
} catch (ClassCastException e) {
|
||||
return;
|
||||
}
|
||||
|
||||
final List<Component> children = textComponent.children();
|
||||
|
||||
if (
|
||||
(
|
||||
textComponent.color() != NamedTextColor.AQUA ||
|
||||
textComponent.color() != NamedTextColor.YELLOW ||
|
||||
textComponent.style().isEmpty()
|
||||
) &&
|
||||
children.size() < 2
|
||||
) return;
|
||||
|
||||
final String username = textComponent.content();
|
||||
final String command = ComponentUtilities.stringify(children.get(1));
|
||||
|
||||
final MutablePlayerListEntry sender = bot.players.getEntry(username);
|
||||
|
||||
if (sender == null) return;
|
||||
|
||||
for (Listener listener : listeners) listener.commandReceived(sender, command);
|
||||
}
|
||||
|
||||
public void addListener (Listener listener) { listeners.add(listener); }
|
||||
|
||||
public static class Listener {
|
||||
public void commandReceived (MutablePlayerListEntry sender, String command) {}
|
||||
}
|
||||
}
|
|
@ -34,17 +34,19 @@ public class FilterPlugin extends PlayersPlugin.Listener {
|
|||
bot.players.addListener(this);
|
||||
|
||||
bot.chat.addListener(new ChatPlugin.Listener() {
|
||||
@Override
|
||||
public void commandSpyMessageReceived(PlayerMessage message) {
|
||||
FilterPlugin.this.commandSpyMessageReceived(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playerMessageReceived(PlayerMessage message) {
|
||||
FilterPlugin.this.playerMessageReceived(message);
|
||||
}
|
||||
});
|
||||
|
||||
bot.commandSpy.addListener(new CommandSpyPlugin.Listener() {
|
||||
@Override
|
||||
public void commandReceived(MutablePlayerListEntry sender, String command) {
|
||||
FilterPlugin.this.commandSpyMessageReceived(sender);
|
||||
}
|
||||
});
|
||||
|
||||
bot.executor.scheduleAtFixedRate(this::kick, 0, 10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
@ -103,12 +105,12 @@ public class FilterPlugin extends PlayersPlugin.Listener {
|
|||
bot.exploits.kick(target.profile.getId());
|
||||
}
|
||||
|
||||
public void commandSpyMessageReceived (PlayerMessage message) {
|
||||
final FilteredPlayer player = getPlayer(message.sender.profile.getName());
|
||||
public void commandSpyMessageReceived (MutablePlayerListEntry sender) {
|
||||
final FilteredPlayer player = getPlayer(sender.profile.getName());
|
||||
|
||||
if (player == null) return;
|
||||
|
||||
deOp(message.sender);
|
||||
deOp(sender);
|
||||
}
|
||||
|
||||
public void playerMessageReceived (PlayerMessage message) {
|
||||
|
|
Loading…
Reference in a new issue