1
0
Fork 0

FINALLY improve chat queue thingy

which fixes the nullpointer too
and might make the bot use less memory too because it doesn't run that tick shit every 1ms
This commit is contained in:
Chayapak 2023-06-04 19:24:11 +07:00
parent e305cec555
commit ad8fa2ba03
3 changed files with 47 additions and 59 deletions
src/main/java/land/chipmunk/chayapak/chomens_bot

View file

@ -36,8 +36,7 @@ public class ClearChatQueueCommand implements Command {
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
final Bot bot = context.bot();
bot.chat()._queue().clear();
bot.chat().queue().clear();
bot.chat().clearQueue();
return null;
}

View file

@ -38,37 +38,35 @@ public class NetMessageCommand implements Command {
}
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
try {
final Bot bot = context.bot();
final List<Bot> bots = bot.bots();
final Bot bot = context.bot();
final List<Bot> bots = bot.bots();
final String hostAndPort = bot.host() + ":" + bot.port();
final String hostAndPort = bot.host() + ":" + bot.port();
final Component component = Component.translatable(
"[%s]%s%s%s %s",
Component
.text(hostAndPort)
.color(NamedTextColor.GRAY)
.clickEvent(ClickEvent.copyToClipboard(hostAndPort))
.hoverEvent(
HoverEvent.showText(
Component.empty()
.append(Component.text(hostAndPort).color(NamedTextColor.GRAY))
.append(Component.newline())
.append(Component.text("Click here to copy the server host and port to your clipboard").color(NamedTextColor.GREEN))
)
),
Component.text(" "),
context.sender().displayName().color(NamedTextColor.GRAY),
Component.text(" "),
Component.text(String.join(" ", args)).color(NamedTextColor.GRAY)
).color(NamedTextColor.DARK_GRAY);
final Component component = Component.translatable(
"[%s]%s%s%s %s",
Component
.text(hostAndPort)
.color(NamedTextColor.GRAY)
.clickEvent(ClickEvent.copyToClipboard(hostAndPort))
.hoverEvent(
HoverEvent.showText(
Component.empty()
.append(Component.text(hostAndPort).color(NamedTextColor.GRAY))
.append(Component.newline())
.append(Component.text("Click here to copy the server host and port to your clipboard").color(NamedTextColor.GREEN))
)
),
Component.text(" "),
context.sender().displayName().color(NamedTextColor.GRAY),
Component.text(" "),
Component.text(String.join(" ", args)).color(NamedTextColor.GRAY)
).color(NamedTextColor.DARK_GRAY);
for (Bot eachBot : bots) {
if (!eachBot.loggedIn()) continue;
eachBot.chat().tellraw(component);
}
} catch (Exception ignored) {} // lazy fix for the bot spamming nullpointers in console (mabe)
for (Bot eachBot : bots) {
if (!eachBot.loggedIn()) continue;
eachBot.chat().tellraw(component);
}
return null;
}

View file

@ -36,8 +36,7 @@ public class ChatPlugin extends Bot.Listener {
private final CommandSpyParser commandSpyParser;
@Getter private final List<String> queue = new ArrayList<>();
@Getter private final List<String> _queue = new ArrayList<>();
private final List<String> queue = new ArrayList<>();
@Getter @Setter private int queueDelay;
@ -58,7 +57,6 @@ public class ChatPlugin extends Bot.Listener {
chatParsers.add(new ChomeNSCustomChatParser(bot));
chatParsers.add(new CreayunChatParser(bot));
bot.executor().scheduleAtFixedRate(this::_sendChatTick, 0, 1, TimeUnit.MILLISECONDS);
bot.executor().scheduleAtFixedRate(this::sendChatTick, 0, queueDelay, TimeUnit.MILLISECONDS);
}
@ -211,34 +209,16 @@ public class ChatPlugin extends Bot.Listener {
}
}
// TODO: Probably improve this code
private void _sendChatTick () {
try {
if (queue.size() == 0) return;
final String message = queue.get(0);
final String[] splitted = message.split("(?<=\\G.{255})|\\n");
for (String subMessage : splitted) {
if (
subMessage.trim().equals("") ||
IllegalCharactersUtilities.containsIllegalCharacters(subMessage)
) continue;
_queue.add(subMessage);
}
queue.remove(0);
} catch (Exception e) {
e.printStackTrace();
}
public void fard () {
new Thread(() -> {
for (int i = 0; i < 69420; i++) queue.add("fard " + i);
}).start();
}
private void sendChatTick () {
if (_queue.size() == 0) return;
if (queue.size() == 0) return;
final String message = _queue.get(0);
final String message = queue.get(0);
if (message.startsWith("/")) {
String removedMessage = message.substring(1);
@ -273,11 +253,22 @@ public class ChatPlugin extends Bot.Listener {
));
}
_queue.remove(0);
queue.remove(0);
}
public void clearQueue () { queue.clear(); }
public void send (String message) {
queue.add(message);
final String[] splitted = message.split("(?<=\\G.{255})|\\n");
for (String subMessage : splitted) {
if (
subMessage.trim().equals("") ||
IllegalCharactersUtilities.containsIllegalCharacters(subMessage)
) continue;
queue.add(subMessage);
}
}
public void tellraw (Component component, String targets) {