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;
@Getter private final boolean kaboom;
@Getter private final String serverName;
@Getter private final boolean useChat;
@Getter private final List<Bot> allBots;
@Getter private final Configuration config;
@ -60,12 +61,13 @@ public class Bot {
@Getter private CloopPlugin cloop;
@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.port = port;
this._username = _username;
this.kaboom = kaboom;
this.serverName = serverName;
this.useChat = useChat;
this.allBots = allBots;
this.config = config;

View file

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

View file

@ -70,8 +70,9 @@ public class Main {
final String username = botOption.username();
final boolean kaboom = botOption.kaboom();
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);
}

View file

@ -23,6 +23,7 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class ChatPlugin extends SessionAdapter {
private final Bot bot;
@ -83,32 +84,63 @@ public class ChatPlugin extends SessionAdapter {
}
}
public void send (String message) {
if (message.startsWith("/")) {
bot.session().send(new ServerboundChatCommandPacket(
message.substring(1),
Instant.now().toEpochMilli(),
0L,
new ArrayList<>(),
false,
new ArrayList<>(),
null
));
} else {
bot.session().send(new ServerboundChatPacket(
message,
Instant.now().toEpochMilli(),
0L,
new byte[0],
false,
new ArrayList<>(),
null
));
public void send (String _message, String prefix) {
final String message = prefix + _message;
final String[] splitted = message.split("(?<=\\G.{100})|\\n");
int i = 200;
for (String splitMessage : splitted) {
if (splitMessage.trim().equals("")) continue;
bot.executor().schedule(() -> {
if (splitMessage.startsWith("/")) {
bot.session().send(new ServerboundChatCommandPacket(
splitMessage.substring(1),
Instant.now().toEpochMilli(),
0L,
new ArrayList<>(),
false,
new ArrayList<>(),
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) {
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)); }

View file

@ -60,3 +60,4 @@ bots:
username: 'ChomeNS_Bot'
kaboom: false
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