commands!

This commit is contained in:
Parker2991
2026-04-11 09:49:51 -05:00
parent f39660471d
commit 619f423574
22 changed files with 593 additions and 13 deletions
+2 -2
View File
@@ -26,7 +26,7 @@ repositories {
maven("https://maven.maxhenkel.de/repository/public/")
// maven("https://libraries.minecraft.net")
maven("https://libraries.minecraft.net")
/* maven("https://code.optmstc.dev/api/packages/kso/maven") {
content {
@@ -50,7 +50,7 @@ dependencies {
implementation("net.kyori:adventure-text-serializer-plain:4.26.1")
implementation("net.kyori:adventure-text-serializer-legacy:4.26.1")
implementation("net.kyori:adventure-text-serializer-gson:4.26.1")
// implementation("com.mojang:brigadier:1.3.10")
implementation("com.mojang:brigadier:1.3.10")
//implementation("land.chipmunk.code.kaboomstandardsorganization.messaginglib:mcprotocollib:3.1.1-SNAPSHOT")
// implementation("land.chipmunk.code.kaboomstandardsorganization.messaginglib:3.1.1-SNAPSHOT")
}
@@ -55,10 +55,13 @@ public class Bot extends SessionAdapter {
public CommandCoreModule core;
public PlayerListModule player;
public PlayerListModule players;
public RegistryModule registry;
public CommandManagerModule commandManager;
//private ChatCommandHandlerModule ChatCommandHandler;
public void loadModules () {
this.chat = new ChatModule(this);
this.console = new ConsoleModule(this);
@@ -66,7 +69,9 @@ public class Bot extends SessionAdapter {
this.position = new PositionModule(this);
this.core = new CommandCoreModule(this);
this.registry = new RegistryModule(this);
this.player = new PlayerListModule(this);
this.players = new PlayerListModule(this);
new ChatCommandHandlerModule(this);
this.commandManager = new CommandManagerModule(this);
}
public Bot (Config.Options options, List<Bot> bots, Config config) {
@@ -107,7 +112,7 @@ public class Bot extends SessionAdapter {
@Override
public void packetError (PacketErrorEvent error) {
System.out.println(error.getCause());
}
@Override
@@ -122,7 +127,7 @@ public class Bot extends SessionAdapter {
public void getProfile (ClientboundLoginFinishedPacket packet) {
profile = packet.getProfile();
//profile = packet.getProfile();
loggedIn = true;
}
@@ -133,6 +138,10 @@ public class Bot extends SessionAdapter {
@Override
public void disconnected (DisconnectedEvent event) {
ListenerManager.clearListener();
//final Throwable cause = disconnectedEvent.getCause();
Throwable cause = event.getCause();
System.out.println(cause);
loggedIn = false;
Component component = event.getReason();
String reason = ComponentUtil.componentToAnsi(component);
@@ -1,6 +1,10 @@
package land.chipmunk.parker2991.nitoribot;
import java.util.List;
public class Config {
public List<String> prefixes;
public static class Core {
public Area area = new Area();
public String coreName;
@@ -0,0 +1,53 @@
package land.chipmunk.parker2991.nitoribot.chatparsers;
import java.util.List;
import land.chipmunk.parker2991.nitoribot.data.chat.*;
import land.chipmunk.parker2991.nitoribot.Bot;
import land.chipmunk.parker2991.nitoribot.data.PlayerProfileData;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
public class KaboomChatParser implements ParseChatData {
private Bot bot;
private static Component SEPERATOR_COLON = Component.text(":");
private static Component SEPERATOR_COLON_RACCOON = Component.text("\u00a7f:"); // https://github.com/raccoonserver/extras/commit/315f704075db2f00e3e1bfbf55f858469c22880f
private static Component SEPERATOR_SPACE = Component.space();
public KaboomChatParser (Bot bot) {
this.bot = bot;
}
@Override
public PlayerMessageData parse (Component message) {
if (message instanceof TextComponent) return parse((TextComponent) message);
return null;
}
public PlayerMessageData parse (TextComponent message) {
List<Component> children = message.children();
if (!message.content().equals("") || !message.style().isEmpty() || children == null || children.size() < 3) return null;
final Component prefix = children.get(0);
Component displayName = Component.empty();
Component contents = Component.empty();
if (isSeperatorAt(children, 1)) { // Missing/blank display name
if (children.size() > 3) contents = children.get(3);
} else if (isSeperatorAt(children, 2)) {
displayName = children.get(1);
if (children.size() > 4) contents = children.get(4);
} else {
return null;
}
PlayerProfileData sender = bot.players.getDisplayName(Component.empty().append(prefix).append(displayName));//getSender());
return new PlayerMessageData(sender, contents, "minecraft:chat", displayName);
}
private boolean isSeperatorAt (List<Component> children, int start) {
return (children.get(start).equals(SEPERATOR_COLON) || children.get(start).equals(SEPERATOR_COLON_RACCOON)) && children.get(start + 1).equals(SEPERATOR_SPACE);
}
}
@@ -0,0 +1,20 @@
package land.chipmunk.parker2991.nitoribot.command;
import land.chipmunk.parker2991.nitoribot.Bot;
public class CommandContext {
public Bot bot;
public String[] args;
public CommandSource source;
public CommandInfo commandData;
public CommandContext (Bot bot, String[] args, CommandSource source, CommandInfo commandData) {
this.bot = bot;
this.args = args;
this.source = source;
this.commandData = commandData;
}
}
@@ -0,0 +1,16 @@
package land.chipmunk.parker2991.nitoribot.command;
import net.kyori.adventure.text.Component;
public class CommandError extends Exception {
public String name = "CommandError";
public Component message;
public CommandError (Component message) {
this.message = message;
}
public Component message () {
return message;
}
}
@@ -0,0 +1,19 @@
package land.chipmunk.parker2991.nitoribot.command;
public abstract class CommandInfo {
public String name;
public CommandTrustLevels trustlevel;
public String[] aliases;
public String description;
public CommandInfo (String name, CommandTrustLevels trustlevel, String[] aliases, String description) {
this.name = name;
this.trustlevel = trustlevel;
this.aliases = aliases;
this.description = description;
}
public abstract CommandContext execute (CommandContext context);
}
@@ -0,0 +1,30 @@
package land.chipmunk.parker2991.nitoribot.command;
import land.chipmunk.parker2991.nitoribot.data.PlayerProfileData;
import land.chipmunk.parker2991.nitoribot.Bot;
import net.kyori.adventure.text.Component;
public class CommandSource {
private Bot bot;
public PlayerProfileData sender;
public boolean inGame;
public CommandSource (Bot bot, PlayerProfileData sender) {
this.bot = bot;
this.sender = sender;
}
public void sendFeedback (Component message) {
String selector = String.format(
"@p[nbt={UUID:%s}]",
sender.entityUUID
);
bot.chat.tellraw(selector, message);
}
}
@@ -0,0 +1,10 @@
package land.chipmunk.parker2991.nitoribot.command;
public enum CommandTrustLevels {
PUBLIC,
TRUSTED,
ADMIN,
OWNER,
CONSOLE,
DISABLED
}
@@ -0,0 +1,33 @@
package land.chipmunk.parker2991.nitoribot.commands.Public;
import land.chipmunk.parker2991.nitoribot.Bot;
import land.chipmunk.parker2991.nitoribot.command.CommandContext;
import land.chipmunk.parker2991.nitoribot.command.CommandInfo;
import land.chipmunk.parker2991.nitoribot.command.CommandTrustLevels;
public class EchoCommand extends CommandInfo {
public EchoCommand () {
super(
"echo",
CommandTrustLevels.PUBLIC,
new String[] { "say" },
"make the bot say a message"
);
}
@Override
public CommandContext execute(CommandContext context) {
Bot bot = context.bot;
String args = String.join(" ", context.args);//context.args.join(' ');
if (args.startsWith("/")) {
bot.chat.command(args.substring("/".length()));
// return;
} else {
bot.chat.message(args);
}
//CommandSource source = context.source;
return null;
}
}
@@ -0,0 +1,79 @@
package land.chipmunk.parker2991.nitoribot.commands.Public;
import land.chipmunk.parker2991.nitoribot.Bot;
import land.chipmunk.parker2991.nitoribot.command.*;
import land.chipmunk.parker2991.nitoribot.util.ComponentUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.JoinConfiguration;
import java.util.ArrayList;
import java.util.List;
public class HelpCommand extends CommandInfo {
public HelpCommand () {
super(
"help",
CommandTrustLevels.PUBLIC,
new String[] { "heko", "hell", "?", "cmds"},
"see the list of commands"
);
}
public Component getCommands (Bot bot) {
List<Component> commandlist = new ArrayList<>();
List<Component> Public = new ArrayList<>();
List<Component> Trusted = new ArrayList<>();
List<Component> Admin = new ArrayList<>();
List<Component> Owner = new ArrayList<>();
for (CommandInfo commands : bot.commandManager.commands) {
switch (commands.trustlevel) {
case CommandTrustLevels.PUBLIC:
Public.add(
Component.text(commands.name + " ").color(NamedTextColor.BLUE)
);
break;
case CommandTrustLevels.TRUSTED:
Trusted.add(
Component.text(commands.name + " ").color(NamedTextColor.DARK_AQUA)
);
break;
}
}
Component component = Component.empty()
.append(Public)
.append(Component.text("\n"));
return component;
}
@Override
public CommandContext execute (CommandContext context) {
Bot bot = context.bot;
String[] args = context.args;
CommandSource source = context.source;
Component HelpLayout = Component.translatable(
"%s: (%s) (%s | %s | %s | %s) \u203a\n",
Component.text("Commands").color(NamedTextColor.BLUE),
Component.text(bot.commandManager.commands.size()).color(NamedTextColor.GOLD),
Component.text("Public").color(NamedTextColor.BLUE),
Component.text("Trusted").color(NamedTextColor.DARK_AQUA),
Component.text("Admin").color(NamedTextColor.AQUA),
Component.text("Owner").color(NamedTextColor.LIGHT_PURPLE)
).color(NamedTextColor.GRAY);
Component component = Component.empty()
.append(HelpLayout)
.append(Component.join(JoinConfiguration.separator(Component.space()), getCommands(bot)));
// System.out.println(ComponentUtil.componentToAnsi(component));
source.sendFeedback(component);
return null;
}
}
@@ -1,5 +1,7 @@
package land.chipmunk.parker2991.nitoribot.data;
import land.chipmunk.parker2991.nitoribot.util.UUIDUtil;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
import org.geysermc.mcprotocollib.auth.GameProfile;
@@ -9,7 +11,8 @@ import java.util.UUID;
public class PlayerProfileData {
public UUID uuid;
public GameProfile profile;
public String entityUUID;
public final GameProfile profile;
public boolean listed;
public int latency;
public GameMode gameMode;
@@ -26,6 +29,7 @@ public class PlayerProfileData {
Component displayName
) {
this.uuid = uuid;
this.entityUUID = UUIDUtil.snbt(uuid);
this.profile = profile;
this.listed = listed;
this.latency = latency;
@@ -0,0 +1,22 @@
package land.chipmunk.parker2991.nitoribot.data.chat;
//import land.chipmunk.parker2991.nitoribot.data.PlayerProfileData;
import net.kyori.adventure.text.Component;
//import org.geysermc.mcprotocollib.protocol.data.game.PlayerListEntry;
public interface ParseChatData {
PlayerMessageData parse (Component message);
// PlayerProfileData parse (PlayerMessageData data);
}
/*
package land.chipmunk.chipmunkbot.data.chat;
import net.kyori.adventure.text.Component;
public interface SystemChatParser {
PlayerMessage parse (Component message);
}
*/
@@ -0,0 +1,38 @@
package land.chipmunk.parker2991.nitoribot.data.chat;
import net.kyori.adventure.text.Component;
import land.chipmunk.parker2991.nitoribot.data.PlayerProfileData;
public class PlayerMessageData {
public PlayerProfileData sender;
public Component contents;
public String chatType;
public Component senderName;
public PlayerMessageData(PlayerProfileData sender, Component contents, String chatType, Component senderName) {
this.sender = sender;
this.contents = contents;
this.chatType = chatType;
this.senderName = senderName;
// return this;
}
}
/*
package land.chipmunk.chipmunkbot.data.chat;
import land.chipmunk.chipmunkbot.data.MutablePlayerListEntry;
import lombok.Data;
import lombok.AllArgsConstructor;
import net.kyori.adventure.text.Component;
@Data
@AllArgsConstructor
public class PlayerMessage {
private MutablePlayerListEntry sender;
private Component contents;
private String chatType;
private Component senderName;
}
*/
@@ -3,6 +3,7 @@ package land.chipmunk.parker2991.nitoribot.listeners;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.packet.Packet;
import land.chipmunk.parker2991.nitoribot.data.chat.PlayerMessageData;
import net.kyori.adventure.text.Component;
public class Listener {
@@ -16,5 +17,7 @@ public class Listener {
public void systemChatReceived (Component message) {}
public void parsedMessage (Component message, PlayerMessageData data) {}
public void botMoved () {}
}
@@ -0,0 +1,48 @@
package land.chipmunk.parker2991.nitoribot.modules;
import land.chipmunk.parker2991.nitoribot.Bot;
import land.chipmunk.parker2991.nitoribot.Config;
import land.chipmunk.parker2991.nitoribot.command.CommandSource;
import land.chipmunk.parker2991.nitoribot.data.chat.PlayerMessageData;
import land.chipmunk.parker2991.nitoribot.listeners.*;
import land.chipmunk.parker2991.nitoribot.util.ComponentUtil;
import net.kyori.adventure.text.Component;
import java.util.List;
public class ChatCommandHandlerModule extends Listener {
private Bot bot;
public void sendFeedback (Component message, boolean broadcast) {
bot.core.run("say meow");
}
@Override
public void parsedMessage (Component message, PlayerMessageData data) {
if (data.chatType != "minecraft:chat") return;
List<String> prefixes = bot.config.prefixes;
for (String prefix : prefixes) {
String plainMessage = ComponentUtil.componentToString(data.contents);
if (!plainMessage.startsWith(prefix)) return;
String command = plainMessage.substring(prefix.length());
CommandSource source = new CommandSource(bot, data.sender);
Component testMessage = Component.text("hello world!");
bot.commandManager.executeString(source, command);
}
}
public ChatCommandHandlerModule (Bot bot) {
this.bot = bot;
bot.ListenerManager.addListener(this);
}
}
@@ -2,6 +2,9 @@ package land.chipmunk.parker2991.nitoribot.modules;
import land.chipmunk.parker2991.nitoribot.Bot;
import land.chipmunk.parker2991.nitoribot.listeners.*;
import land.chipmunk.parker2991.nitoribot.chatparsers.*;
import land.chipmunk.parker2991.nitoribot.data.chat.*;
import land.chipmunk.parker2991.nitoribot.data.PlayerProfileData;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.packet.Packet;
@@ -11,14 +14,20 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.Clientbound
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatPacket;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import java.util.ArrayList;
import java.util.List;
import java.time.Instant;
import java.util.BitSet;
public class ChatModule extends Listener {
private Bot bot;
public final List<ParseChatData> chatParsers = new ArrayList<>();
@Override
public void packetReceived (Session session, Packet packet) {
if (packet instanceof ClientboundSystemChatPacket) systemChat((ClientboundSystemChatPacket) packet);
@@ -48,10 +57,7 @@ public class ChatModule extends Listener {
final String parseChatTypes = parseChatTypes(packet.getChatType().id());
if (packet.getChatType().id() == 4) {
message = Component.translatable(
parseChatTypes,
getMessage
);
message = getMessage;
} else {
message = Component.translatable(
parseChatTypes,
@@ -59,6 +65,23 @@ public class ChatModule extends Listener {
getMessage
);
}
/*
parseMessage(message, {
players: bot.players,
getMessageAsPrismarine: bot.getMessageAsPrismarine,
chatType: "minecraft:system_chat",
}
public PlayerProfileData sender;
public Component contents;
public String chatType;
public Component senderName;
*/
// parseMessage(
// message,
//new PlayerMessageData(
//)
//);
for (Listener listener : bot.ListenerManager.listeners) {
listener.disguisedChatReceived(message);
@@ -67,11 +90,44 @@ public class ChatModule extends Listener {
public void playerChat (ClientboundPlayerChatPacket packet) {
final Component unsignedContent = packet.getUnsignedContent();
final Component content = Component.text(packet.getContent());
//final PlayerProfileData sender = bot.players.getPlayerUUID(packet.getSender());
//System.out.println(packet.getSender());
for (Listener listener : bot.ListenerManager.listeners) {
listener.playerChatReceived(unsignedContent);
}
}
parseMessage(
unsignedContent,
new PlayerMessageData(
bot.players.getPlayerUUID(packet.getSender()),
content,
"minecraft:chat",
bot.players.getPlayerUUID(packet.getSender()).displayName
)
);
//final PlayerMessage playerMessage = new PlayerMessage(sender, parsedFromMessage.contents(), "minecraft:chat", packet.getName());
//parseMessage(unsignedContent, packet);
}
public void parseMessage (Component message, PlayerMessageData data) {
try {
PlayerMessageData parsed = null;
for (ParseChatData parser : chatParsers) {
parsed = parser.parse(message);
if (parsed != null) break;
};
if (parsed == null) return;
for (Listener listener : bot.ListenerManager.listeners) {
listener.parsedMessage(message, data);
}
} catch (Exception ignore) {}
}
public void message (String message) {
bot.session.send(
@@ -95,8 +151,14 @@ public class ChatModule extends Listener {
);
}
public void tellraw (String selector, Component message) {
bot.core.run("minecraft:tellraw " + selector + " " + GsonComponentSerializer.gson().serialize(message));
}
public ChatModule (Bot bot) {
this.bot = bot;
chatParsers.add(new KaboomChatParser(bot));
bot.ListenerManager.addListener(this);
};
@@ -31,7 +31,6 @@ public class CommandCoreModule extends Listener {
);
refill();
run("say hello world!");
};
public void refill () {
@@ -0,0 +1,79 @@
package land.chipmunk.parker2991.nitoribot.modules;
import land.chipmunk.parker2991.nitoribot.Bot;
import land.chipmunk.parker2991.nitoribot.command.CommandError;
import land.chipmunk.parker2991.nitoribot.command.CommandContext;
import land.chipmunk.parker2991.nitoribot.command.CommandInfo;
import land.chipmunk.parker2991.nitoribot.command.CommandSource;
import land.chipmunk.parker2991.nitoribot.commands.Public.*;
import land.chipmunk.parker2991.nitoribot.logger.LoggerManager;
import land.chipmunk.parker2991.nitoribot.util.ErrorToString;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class CommandManagerModule {
private Bot bot;
public List<CommandInfo> commands = new ArrayList<>();
public void registerCommand (CommandInfo command) {
commands.add(command);
}
public CommandInfo getCommand (String getCommand) {
for (CommandInfo command : commands) {
if (getCommand.equals(command.name)) return command;
}
return null;
}
public void execute (CommandSource source, String commandName, String[] args) {
try {
CommandInfo command = getCommand(commandName);
if (command == null) throw new CommandError(
Component.translatable(
"%s%s%s %s",
Component.translatable("command.unknown.command"),
Component.text("\n"),
Component.text(commandName).color(NamedTextColor.GRAY),
Component.translatable("command.context.here")
).color(NamedTextColor.RED)
);
CommandContext context = new CommandContext(bot, args, source, command);
command.execute(context);
} catch (CommandError error) {
source.sendFeedback(error.message());
} catch (Exception error) {
source.sendFeedback(
Component.translatable("command.failed").color(NamedTextColor.DARK_RED)
);
String Error = ErrorToString.errorToString(error);
Component host = Component.text(bot.options.host + ":" + bot.options.port).color(NamedTextColor.BLUE);
LoggerManager.ERROR(host, Error);
}
}
public void executeString (CommandSource source, String command) {
String[] splitArguments = command.split(" ");
String commandName = splitArguments[0];
final String[] args = Arrays.copyOfRange(splitArguments, 1, splitArguments.length);
this.execute(source, commandName, args);
}
public CommandManagerModule (Bot bot) {
this.bot = bot;
registerCommand(new EchoCommand());
registerCommand(new HelpCommand());
}
}
@@ -3,6 +3,7 @@ package land.chipmunk.parker2991.nitoribot.modules;
import land.chipmunk.parker2991.nitoribot.Bot;
import land.chipmunk.parker2991.nitoribot.data.PlayerProfileData;
import land.chipmunk.parker2991.nitoribot.listeners.*;
import net.kyori.adventure.text.Component;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundPlayerInfoRemovePacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundPlayerInfoUpdatePacket;
@@ -55,6 +56,16 @@ public class PlayerListModule extends Listener {
return null;
}
public final PlayerProfileData getDisplayName (Component displayName) {
for (PlayerProfileData candidate : list) {
if (candidate.displayName != null && candidate.displayName.equals(displayName)) {
return candidate;
}
}
return null;
}
public void updateListed (PlayerListEntry player) {
PlayerProfileData getPlayer = getPlayerUUID(player.getProfileId());
if (getPlayer == null) return;
@@ -0,0 +1,38 @@
package land.chipmunk.parker2991.nitoribot.util;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;
import java.util.UUID;
import java.nio.ByteBuffer;
public class UUIDUtil {
private UUIDUtil () {
}
public static int[] intArray (UUID uuid) {
final ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
buffer.putLong(0, uuid.getMostSignificantBits());
buffer.putLong(8, uuid.getLeastSignificantBits());
final int[] intArray = new int[4];
for (int i = 0; i < intArray.length; i++) intArray[i] = buffer.getInt();
return intArray;
}
public static NbtMap tag (UUID uuid) {
final NbtMapBuilder builder = NbtMap.builder();
builder.putIntArray("", intArray(uuid));
return builder.build();
}
public static String snbt (UUID uuid) {
int[] array = intArray(uuid);
return "[I;" + array[0] + "," + array[1] + "," + array[2] + "," + array[3] + "]";
}
public static String selector (UUID uuid) { return "@p[nbt={UUID:" + snbt(uuid) + "}]"; }
public static String exclusiveSelector (UUID uuid) { return "@a[nbt=!{UUID:" + snbt(uuid) + "}]"; }
}
+3
View File
@@ -0,0 +1,3 @@
{
}