Add CommandSpy parssing and remove file

i should prob just use branches correctly instead lol
it is a little messy but if it works it works™️
This commit is contained in:
Chipmunk 2023-05-22 16:03:57 -04:00
parent 489d65bf76
commit fc065d4e57
7 changed files with 110 additions and 65 deletions

View file

@ -18,8 +18,9 @@ public class ChipmunkBot extends Client {
@Getter private final TabCompletePlugin tabComplete; @Getter private final TabCompletePlugin tabComplete;
@Getter private final QueryPlugin query; @Getter private final QueryPlugin query;
@Getter private final PlayerListPlugin playerList; @Getter private final PlayerListPlugin playerList;
@Getter private final CommandSpyPlugin commandSpy;
@Getter private final CommandManager commandManager; @Getter private final CommandManager commandManager;
@Getter private final ChatCommandHandler chatCommandHandler; @Getter private final PlayerCommandHandler playerCommandHandler;
@Getter private final PositionManager position; @Getter private final PositionManager position;
@Getter private final CommandCore core; @Getter private final CommandCore core;
@Getter private final SelfCarePlugin selfCare; @Getter private final SelfCarePlugin selfCare;
@ -33,8 +34,9 @@ public class ChipmunkBot extends Client {
this.tabComplete = new TabCompletePlugin(this); this.tabComplete = new TabCompletePlugin(this);
this.query = new QueryPlugin(this); this.query = new QueryPlugin(this);
this.playerList = new PlayerListPlugin(this); this.playerList = new PlayerListPlugin(this);
this.commandSpy = new CommandSpyPlugin(this);
this.commandManager = new CommandManager(this); this.commandManager = new CommandManager(this);
this.chatCommandHandler = new ChatCommandHandler(this, options); this.playerCommandHandler = new PlayerCommandHandler(this, options);
this.position = new PositionManager(this); this.position = new PositionManager(this);
this.core = new CommandCore(this, options); this.core = new CommandCore(this, options);
this.selfCare = new SelfCarePlugin(this); this.selfCare = new SelfCarePlugin(this);

View file

@ -13,6 +13,7 @@ public class Options {
public class Commands { public class Commands {
public String prefix = "default."; public String prefix = "default.";
public String cspyPrefix = "default.";
} }
public class Core { public class Core {

View file

@ -139,7 +139,7 @@ public class MusicCommand {
public int list (CommandContext<CommandSource> context, Path path) throws CommandSyntaxException { public int list (CommandContext<CommandSource> context, Path path) throws CommandSyntaxException {
final CommandSource source = context.getSource(); final CommandSource source = context.getSource();
final ChipmunkBot client = source.client(); final ChipmunkBot client = source.client();
final String prefix = client.chatCommandHandler().prefix(); final String prefix = client.playerCommandHandler().prefix();
final File directory = path.toFile(); final File directory = path.toFile();
final String[] filenames = directory.list(); final String[] filenames = directory.list();

View file

@ -1,55 +0,0 @@
package land.chipmunk.chipmunkbot.plugins;
import land.chipmunk.chipmunkbot.ChipmunkBot;
import land.chipmunk.chipmunkbot.Options;
import com.github.steveice10.packetlib.event.session.SessionListener;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.mc.protocol.data.ProtocolState;
import lombok.Getter;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class TickLoop extends SessionAdapter {
private Client client;
private Timer timer;
@Getter private List<Listener> listeners = new ArrayList<>();
public TickLoop (ChipmunkBot client, Options options) {
this.client = client;
client.session().addListener((SessionListener) this);
final TimerTask task = new TimerTask() {
public void run () {
if (((MinecraftProtocol) client.session().getPacketProtocol()).getState() != ProtocolState.GAME) return;
int cancel = false;
for (Listener listener : listeners) {
if (!listener.onTick()) cancel = true;
}
if (!c)
}
};
timer = new Timer();
timer.schedule(task, options.chat.queue.interval, options.chat.queue.interval);
}
public void disconnected (DisconnectedEvent event) {
if (client.reconnectDelay() < 0 && timer != null) {
timer.cancel();
timer.purge();
}
}
public static class Listener {
public void onTick () {}
}
public void addListener (Listener listener) { listeners.add(listener); }
public void removeListener (Listener listener) { listeners.remove(listener); }
}

View file

@ -0,0 +1,80 @@
package land.chipmunk.chipmunkbot.plugins;
import land.chipmunk.chipmunkbot.ChipmunkBot;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.NamedTextColor;
import land.chipmunk.chipmunkbot.data.MutablePlayerListEntry;
import lombok.Getter;
import java.util.List;
import java.util.ArrayList;
public class CommandSpyPlugin extends ChatPlugin.Listener {
private final ChipmunkBot client;
@Getter private List<Listener> listeners = new ArrayList<>();
private static final Style ENABLED_STYLE = Style.style(NamedTextColor.YELLOW);
private static final Style DISABLED_STYLE = Style.style(NamedTextColor.AQUA);
private static final Component COMMAND_SEPARATOR_COMPONENT = Component.text(": ");
private static final Component SIGN_CREATED_TEXT_COMPONENT = Component.text(" created a sign with contents:");
private static final Component SIGN_LINE_SEPARATOR_COMPONENT = Component.text("\n ");
public CommandSpyPlugin (ChipmunkBot client) {
this.client = client;
client.chat().addListener((ChatPlugin.Listener) this);
}
@Override
public void systemMessageReceived (Component component, boolean overlay) {
if (overlay || !(component instanceof final TextComponent t_component)) return;
final boolean enabled = component.style().equals(ENABLED_STYLE);
if (!enabled && !component.style().equals(DISABLED_STYLE)) return;
final String username = t_component.content();
final MutablePlayerListEntry sender = client.playerList().getEntry(username);
if (sender == null) return;
final List<Component> children = component.children();
if (children.size() == 2) {
// Command
final Component separator = children.get(0);
final Component prefixedCommand = children.get(1);
if (
!(separator instanceof final TextComponent t_separator) ||
!(prefixedCommand instanceof final TextComponent t_prefixedCommand) ||
!separator.equals(COMMAND_SEPARATOR_COMPONENT) ||
!prefixedCommand.style().isEmpty()
) return;
final String command = t_prefixedCommand.content();
if (command.length() < 1 || command.charAt(0) != '/') return;
final String rawCommand = command.substring(1);
for (Listener listener : listeners) listener.commandReceived(sender, rawCommand, enabled);
} else if (children.size() == 9) {
// Sign created
final Component[] lines = new Component[4];
if (!children.get(0).equals(SIGN_CREATED_TEXT_COMPONENT)) return;
for (int i = 0; i < lines.length; i++) {
int separatorChildIndex = (i * 2) + 1;
if (!children.get(separatorChildIndex).equals(SIGN_LINE_SEPARATOR_COMPONENT)) return;
lines[i] = children.get(separatorChildIndex + 1);
}
for (Listener listener : listeners) listener.signCreated(sender, lines, enabled);
}
}
public static class Listener {
public void commandReceived (MutablePlayerListEntry sender, String command, boolean senderHasCommandSpy) {}
public void signCreated (MutablePlayerListEntry sender, Component[] lines, boolean senderHasCommandSpy) {}
}
public void addListener (Listener listener) { listeners.add(listener); }
public void removeListener (Listener listener) { listeners.remove(listener); }
}

View file

@ -9,6 +9,7 @@ import land.chipmunk.chipmunkbot.plugins.ChatPlugin;
import land.chipmunk.chipmunkbot.data.chat.PlayerMessage; import land.chipmunk.chipmunkbot.data.chat.PlayerMessage;
import land.chipmunk.chipmunkbot.util.ComponentUtilities; import land.chipmunk.chipmunkbot.util.ComponentUtilities;
import land.chipmunk.chipmunkbot.plugins.CommandManager; import land.chipmunk.chipmunkbot.plugins.CommandManager;
import land.chipmunk.chipmunkbot.data.MutablePlayerListEntry;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
@ -17,18 +18,22 @@ import com.mojang.brigadier.Message;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
public class ChatCommandHandler extends ChatPlugin.Listener { public class PlayerCommandHandler {
private ChipmunkBot client; private ChipmunkBot client;
@Getter @Setter private String prefix; @Getter @Setter private String prefix;
@Getter @Setter private String cspyPrefix;
public ChatCommandHandler (ChipmunkBot client, Options options) { public PlayerCommandHandler (ChipmunkBot client, Options options) {
this.client = client; this.client = client;
this.prefix = options.commands.prefix; this.prefix = options.commands.prefix;
client.chat().addListener((ChatPlugin.Listener) this); this.cspyPrefix = options.commands.cspyPrefix != null ? options.commands.cspyPrefix : this.prefix;
// TODO: Make this less messy (I might just rewrite how I do events eventually)
client.chat().addListener(new ChatPlugin.Listener() { @Override public void playerMessageReceived (PlayerMessage message) { handleMessage(message); } });
client.commandSpy().addListener(new CommandSpyPlugin.Listener() { @Override public void commandReceived (MutablePlayerListEntry sender, String command, boolean senderHasCommandSpy) { handleCommand(sender, command); } });
} }
@Override public void handleMessage (PlayerMessage message) {
public void playerMessageReceived (PlayerMessage message) {
final Component contents = message.contents(); final Component contents = message.contents();
if (contents == null) return; if (contents == null) return;
final String contentsString = ComponentUtilities.stringify(contents); final String contentsString = ComponentUtilities.stringify(contents);
@ -36,10 +41,21 @@ public class ChatCommandHandler extends ChatPlugin.Listener {
final String commandString = contentsString.substring(prefix.length()); final String commandString = contentsString.substring(prefix.length());
final PlayerCommandSource source = new PlayerCommandSource(client, message.sender()); final PlayerCommandSource source = new PlayerCommandSource(client, message.sender());
this.tryExecuteCommand(source, commandString);
}
public void handleCommand (MutablePlayerListEntry sender, String command) {
if (!command.startsWith(cspyPrefix)) return;
final String commandString = command.substring(cspyPrefix.length());
final PlayerCommandSource source = new PlayerCommandSource(client, sender);
this.tryExecuteCommand(source, commandString);
}
private void tryExecuteCommand (CommandSource source, String command) {
try { try {
CommandDispatcher dispatcher = client.commandManager().dispatcher(); CommandDispatcher dispatcher = client.commandManager().dispatcher();
dispatcher.execute(commandString, source); dispatcher.execute(command, source);
} catch (CommandSyntaxException exception) { } catch (CommandSyntaxException exception) {
CommandManager.sendException(source, exception); CommandManager.sendException(source, exception);
} catch (Exception exception) { } catch (Exception exception) {

View file

@ -7,7 +7,8 @@
"reconnectDelay": 1000, "reconnectDelay": 1000,
"commands": { "commands": {
"prefix": "default." "prefix": "default.",
"cspyPrefix": "default."
}, },
"core": { "core": {