actually add useChat

This commit is contained in:
Chayapak 2023-04-08 12:36:15 +07:00
parent aa55137f36
commit c7c34f4c5d
5 changed files with 61 additions and 24 deletions

View file

@ -25,6 +25,7 @@ public class Bot {
private final String _username; private final String _username;
@Getter private final boolean kaboom; @Getter private final boolean kaboom;
@Getter private final String serverName; @Getter private final String serverName;
@Getter private final boolean useChat;
@Getter private final List<Bot> allBots; @Getter private final List<Bot> allBots;
@Getter private final Configuration config; @Getter private final Configuration config;
@ -60,12 +61,13 @@ public class Bot {
@Getter private CloopPlugin cloop; @Getter private CloopPlugin cloop;
@Getter private MazePlugin maze; @Getter private MazePlugin maze;
public Bot (String host, int port, String _username, boolean kaboom, String serverName, List<Bot> allBots, Configuration config) { public Bot (String host, int port, String _username, boolean kaboom, String serverName, boolean useChat, List<Bot> allBots, Configuration config) {
this.host = host; this.host = host;
this.port = port; this.port = port;
this._username = _username; this._username = _username;
this.kaboom = kaboom; this.kaboom = kaboom;
this.serverName = serverName; this.serverName = serverName;
this.useChat = useChat;
this.allBots = allBots; this.allBots = allBots;
this.config = config; this.config = config;

View file

@ -66,5 +66,6 @@ public class Configuration {
@Getter public String username; @Getter public String username;
@Getter public boolean kaboom = false; @Getter public boolean kaboom = false;
@Getter public String serverName; @Getter public String serverName;
@Getter public boolean useChat = false;
} }
} }

View file

@ -70,8 +70,9 @@ public class Main {
final String username = botOption.username(); final String username = botOption.username();
final boolean kaboom = botOption.kaboom(); final boolean kaboom = botOption.kaboom();
final String serverName = botOption.serverName(); final String serverName = botOption.serverName();
final boolean useChat = botOption.useChat();
final Bot bot = new Bot(host, port, username, kaboom, serverName, allBots, config); final Bot bot = new Bot(host, port, username, kaboom, serverName, useChat, allBots, config);
allBots.add(bot); allBots.add(bot);
} }

View file

@ -23,6 +23,7 @@ import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class ChatPlugin extends SessionAdapter { public class ChatPlugin extends SessionAdapter {
private final Bot bot; private final Bot bot;
@ -83,32 +84,63 @@ public class ChatPlugin extends SessionAdapter {
} }
} }
public void send (String message) { public void send (String _message, String prefix) {
if (message.startsWith("/")) { final String message = prefix + _message;
bot.session().send(new ServerboundChatCommandPacket(
message.substring(1), final String[] splitted = message.split("(?<=\\G.{100})|\\n");
Instant.now().toEpochMilli(),
0L, int i = 200;
new ArrayList<>(), for (String splitMessage : splitted) {
false, if (splitMessage.trim().equals("")) continue;
new ArrayList<>(),
null bot.executor().schedule(() -> {
)); if (splitMessage.startsWith("/")) {
} else { bot.session().send(new ServerboundChatCommandPacket(
bot.session().send(new ServerboundChatPacket( splitMessage.substring(1),
message, Instant.now().toEpochMilli(),
Instant.now().toEpochMilli(), 0L,
0L, new ArrayList<>(),
new byte[0], false,
false, new ArrayList<>(),
new ArrayList<>(), null
null ));
)); } else {
bot.session().send(new ServerboundChatPacket(
splitMessage,
Instant.now().toEpochMilli(),
0L,
new byte[0],
false,
new ArrayList<>(),
null
));
}
}, i, TimeUnit.MILLISECONDS);
i = i + 200;
} }
} }
public void send (String message) {
send(message, "");
}
public void whisper (String targets, String message) {
send(message, "/tell " + targets + " ");
}
public void tellraw (Component component, String targets) { public void tellraw (Component component, String targets) {
bot.core().run("minecraft:tellraw " + targets + " " + GsonComponentSerializer.gson().serialize(component)); if (bot.useChat()) {
if (targets.equals("@a")) {
final String stringified = ComponentUtilities.stringifyMotd(component).replace("§", "&");
send(stringified);
} else {
final String stringified = ComponentUtilities.stringify(component);
whisper(targets, stringified);
}
} else {
bot.core().run("minecraft:tellraw " + targets + " " + GsonComponentSerializer.gson().serialize(component));
}
} }
public void tellraw (Component component, UUID uuid) { tellraw(component, UUIDUtilities.selector(uuid)); } public void tellraw (Component component, UUID uuid) { tellraw(component, UUIDUtilities.selector(uuid)); }

View file

@ -60,3 +60,4 @@ bots:
username: 'ChomeNS_Bot' username: 'ChomeNS_Bot'
kaboom: false kaboom: false
serverName: 'Localhost' # name it whatever you like, it will be used as server name in trusted broadcast serverName: 'Localhost' # name it whatever you like, it will be used as server name in trusted broadcast
useChat: false # when the bot sends output (tellraw) it will chat instead of using the core to run tellraw