forked from chipmunkmc/chipmunkbot
netmsg
This commit is contained in:
parent
5a7280ba3a
commit
152fad3144
5 changed files with 65 additions and 4 deletions
|
@ -19,7 +19,7 @@ public class Client {
|
|||
@Getter private final List<SessionListener> 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<Client> allClients () { return options.allClients(); }
|
||||
}
|
||||
|
|
|
@ -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<Client> allClients = new ArrayList<>();
|
||||
|
||||
/* public ClientOptions profile (GameProfile profile) {
|
||||
protocol(new MinecraftProtocol(profile));
|
||||
|
|
|
@ -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<Client> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<CommandSource> 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;
|
||||
}
|
||||
}
|
|
@ -29,7 +29,8 @@ public class CommandManager {
|
|||
new RunCommand(),
|
||||
new EchoCommand(),
|
||||
new InfoCommand(),
|
||||
new ReconnectCommand()
|
||||
new ReconnectCommand(),
|
||||
new NetMsgCommand()
|
||||
};
|
||||
|
||||
static {
|
||||
|
|
Loading…
Reference in a new issue