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) { public Component execute(CommandContext context, String[] args, String[] fullArgs) {
final Bot bot = context.bot(); final Bot bot = context.bot();
bot.chat()._queue().clear(); bot.chat().clearQueue();
bot.chat().queue().clear();
return null; return null;
} }

View file

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

View file

@ -36,8 +36,7 @@ public class ChatPlugin extends Bot.Listener {
private final CommandSpyParser commandSpyParser; private final CommandSpyParser commandSpyParser;
@Getter private final List<String> queue = new ArrayList<>(); private final List<String> queue = new ArrayList<>();
@Getter private final List<String> _queue = new ArrayList<>();
@Getter @Setter private int queueDelay; @Getter @Setter private int queueDelay;
@ -58,7 +57,6 @@ public class ChatPlugin extends Bot.Listener {
chatParsers.add(new ChomeNSCustomChatParser(bot)); chatParsers.add(new ChomeNSCustomChatParser(bot));
chatParsers.add(new CreayunChatParser(bot)); chatParsers.add(new CreayunChatParser(bot));
bot.executor().scheduleAtFixedRate(this::_sendChatTick, 0, 1, TimeUnit.MILLISECONDS);
bot.executor().scheduleAtFixedRate(this::sendChatTick, 0, queueDelay, 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 public void fard () {
private void _sendChatTick () { new Thread(() -> {
try { for (int i = 0; i < 69420; i++) queue.add("fard " + i);
if (queue.size() == 0) return; }).start();
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();
}
} }
private void sendChatTick () { 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("/")) { if (message.startsWith("/")) {
String removedMessage = message.substring(1); 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) { 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) { public void tellraw (Component component, String targets) {