diff --git a/src/main/java/land/chipmunk/chipmunkbot/Client.java b/src/main/java/land/chipmunk/chipmunkbot/Client.java index 9946ef1..fcff9e6 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Client.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Client.java @@ -19,7 +19,7 @@ public class Client { @Getter private final List listeners = new ArrayList<>(); public Client (ClientOptions options) { - this.options = new ClientOptions(options.host(), options.port(), options.protocol(), options.proxy(), options.reconnectDelay()); + this.options = new ClientOptions(options.host(), options.port(), options.protocol(), options.proxy(), options.reconnectDelay(), options.allClients()); reconnect(); } @@ -56,4 +56,7 @@ public class Client { } }); } + + public long reconnectDelay () { return options.reconnectDelay(); } + public List allClients () { return options.allClients(); } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java b/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java index 8455a82..74426df 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java +++ b/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java @@ -8,6 +8,8 @@ import com.github.steveice10.packetlib.tcp.TcpClientSession; import lombok.Data; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; +import java.util.List; +import java.util.ArrayList; @Data @AllArgsConstructor @@ -17,7 +19,8 @@ public class ClientOptions { private int port; private MinecraftProtocol protocol; private ProxyInfo proxy; - private long reconnectDelay; + private long reconnectDelay = 1000l; + private List allClients = new ArrayList<>(); /* public ClientOptions profile (GameProfile profile) { protocol(new MinecraftProtocol(profile)); diff --git a/src/main/java/land/chipmunk/chipmunkbot/Main.java b/src/main/java/land/chipmunk/chipmunkbot/Main.java index 7b2b57a..c6ce3d6 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Main.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Main.java @@ -15,6 +15,9 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.util.List; +import java.util.ArrayList; + import com.google.gson.JsonObject; import com.google.gson.JsonElement; import com.google.gson.JsonParser; @@ -70,9 +73,14 @@ public class Main { System.exit(1); } + List allClients = new ArrayList<>(); + for (JsonElement element : config.get("bots").getAsJsonArray()) { ClientOptions options = parseClientOptions(element.getAsJsonObject()); - Client client = new ChipmunkBot(options); // TODO: Maybe create a list of some sort + options.allClients(allClients); + + final Client client = new ChipmunkBot(options); // TODO: Maybe create a list of some sort + allClients.add(client); } } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/NetMsgCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/NetMsgCommand.java new file mode 100644 index 0000000..ee561cf --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/NetMsgCommand.java @@ -0,0 +1,46 @@ +package land.chipmunk.chipmunkbot.commands; + +import land.chipmunk.chipmunkbot.Client; +import land.chipmunk.chipmunkbot.ChipmunkBot; +import land.chipmunk.chipmunkbot.command.*; +import static land.chipmunk.chipmunkbot.plugins.CommandManager.literal; +import static land.chipmunk.chipmunkbot.plugins.CommandManager.argument; +import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; +import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import com.mojang.brigadier.context.CommandContext; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +public class NetMsgCommand extends Command { + public NetMsgCommand () { + super(); + + this.node( + literal("netmsg") + .then( + argument("message", greedyString()) + .executes(this::netmsg) + ) + ); + } + + public int netmsg (CommandContext context) { + final CommandSource source = context.getSource(); + final ChipmunkBot client = source.client(); + final String input = getString(context, "message"); + + final Component message = Component.translatable( + "[%s] %s › %s", + Component.text(client.session().getHost() + ":" + client.session().getPort(), NamedTextColor.GRAY), + Component.empty().color(NamedTextColor.DARK_GREEN).append(source.displayName()), + Component.text(input, NamedTextColor.GRAY) + ).color(NamedTextColor.DARK_GRAY); + + for (Client remote : source.client().allClients()) { + if (!(remote instanceof ChipmunkBot)) continue; // ? Is this optimal? + ((ChipmunkBot) remote).chat().tellraw(message); + } + + return 1; + } +} \ No newline at end of file diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java index af416a9..186d590 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java @@ -29,7 +29,8 @@ public class CommandManager { new RunCommand(), new EchoCommand(), new InfoCommand(), - new ReconnectCommand() + new ReconnectCommand(), + new NetMsgCommand() }; static {