From e5bd9d98f8c71d988e703056719df8aaf9229b05 Mon Sep 17 00:00:00 2001
From: ChomeNS <95471003+ChomeNS@users.noreply.github.com>
Date: Sun, 30 Apr 2023 14:09:38 +0700
Subject: [PATCH] add packet queue for funni
---
.idea/workspace.xml | 23 +++++++++----------
.../chipmunk/chayapak/chomens_bot/Bot.java | 16 +++++++++++++
.../chomens_bot/plugins/ChatPlugin.java | 4 ++--
.../chomens_bot/plugins/CorePlugin.java | 4 ++--
.../chomens_bot/plugins/PositionPlugin.java | 2 +-
.../chomens_bot/plugins/SelfCarePlugin.java | 2 +-
.../plugins/TabCompletePlugin.java | 2 +-
7 files changed, 34 insertions(+), 19 deletions(-)
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index db364b5..268012a 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,9 +4,8 @@
-
+
-
@@ -99,13 +98,6 @@
1680245437032
-
- 1682131432220
-
-
-
- 1682131432220
-
1682133205362
@@ -442,7 +434,14 @@
1682823229424
-
+
+ 1682828334937
+
+
+
+ 1682828334938
+
+
@@ -457,7 +456,6 @@
-
@@ -482,7 +480,8 @@
-
+
+
diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java
index e76c540..249d700 100644
--- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java
+++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java
@@ -38,6 +38,8 @@ public class Bot {
@Getter private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(100);
+ private final List packetQueue = new ArrayList<>();
+
@Getter @Setter private ConsolePlugin console;
@Getter @Setter private LoggerPlugin logger; // in ConsolePlugin
@Getter @Setter private DiscordPlugin discord; // same for this one too
@@ -82,6 +84,8 @@ public class Bot {
Bot.this.ready();
}
});
+
+ executor.scheduleAtFixedRate(this::tick, 0, 10, TimeUnit.MILLISECONDS);
}
public void ready () {
@@ -111,6 +115,18 @@ public class Bot {
reconnect();
}
+ public void sendPacket (Packet packet) {
+ packetQueue.add(packet);
+ }
+
+ private void tick () {
+ if (!loggedIn || packetQueue.size() == 0) return;
+
+ session.send(packetQueue.get(0));
+
+ packetQueue.remove(0);
+ }
+
private void reconnect () {
for (Listener listener : listeners) {
listener.connecting();
diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java
index e5b7b5b..f6e1347 100644
--- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java
+++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java
@@ -183,7 +183,7 @@ public class ChatPlugin extends Bot.Listener {
if (splitMessage.trim().equals("")) continue;
if (splitMessage.startsWith("/")) {
- bot.session().send(new ServerboundChatCommandPacket(
+ bot.sendPacket(new ServerboundChatCommandPacket(
splitMessage.substring(1),
Instant.now().toEpochMilli(),
0L,
@@ -192,7 +192,7 @@ public class ChatPlugin extends Bot.Listener {
new BitSet()
));
} else {
- bot.session().send(new ServerboundChatPacket(
+ bot.sendPacket(new ServerboundChatPacket(
splitMessage,
Instant.now().toEpochMilli(),
0L,
diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java
index a8df584..67b9526 100644
--- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java
+++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java
@@ -83,7 +83,7 @@ public class CorePlugin extends PositionPlugin.PositionListener {
if (!ready) return;
if (bot.options().useCore()) {
- bot.session().send(new ServerboundSetCommandBlockPacket(
+ bot.sendPacket(new ServerboundSetCommandBlockPacket(
absoluteCorePosition(),
command,
kaboom ? CommandBlockMode.AUTO : CommandBlockMode.REDSTONE,
@@ -109,7 +109,7 @@ public class CorePlugin extends PositionPlugin.PositionListener {
final CompletableFuture future = new CompletableFuture<>();
transactions.put(transactionId, future);
- final Runnable afterTick = () -> bot.session().send(new ServerboundBlockEntityTagQuery(transactionId, position));
+ final Runnable afterTick = () -> bot.sendPacket(new ServerboundBlockEntityTagQuery(transactionId, position));
bot.executor().schedule(afterTick, 50, TimeUnit.MILLISECONDS);
diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PositionPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PositionPlugin.java
index 4031858..88c1a30 100644
--- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PositionPlugin.java
+++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PositionPlugin.java
@@ -49,7 +49,7 @@ public class PositionPlugin extends Bot.Listener {
}
public void packetReceived (ClientboundPlayerPositionPacket packet) {
- bot.session().send(new ServerboundAcceptTeleportationPacket(packet.getTeleportId()));
+ bot.sendPacket(new ServerboundAcceptTeleportationPacket(packet.getTeleportId()));
position = Vector3i.from(packet.getX(), packet.getY(), packet.getZ());
for (PositionListener listener : listeners) { listener.positionChange(position); }
diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java
index a258b6c..9f52a90 100644
--- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java
+++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java
@@ -157,7 +157,7 @@ public class SelfCarePlugin extends Bot.Listener {
final GameEventValue value = packet.getValue();
if (notification == GameEvent.ENTER_CREDITS) {
- bot.session().send(new ServerboundClientCommandPacket(ClientCommand.RESPAWN));
+ bot.sendPacket(new ServerboundClientCommandPacket(ClientCommand.RESPAWN));
return;
}
diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TabCompletePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TabCompletePlugin.java
index d83c439..85543fe 100644
--- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TabCompletePlugin.java
+++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TabCompletePlugin.java
@@ -23,7 +23,7 @@ public class TabCompletePlugin extends Bot.Listener {
public CompletableFuture tabComplete (String command) {
final int transactionId = nextTransactionId++;
- bot.session().send(new ServerboundCommandSuggestionPacket(transactionId, command));
+ bot.sendPacket(new ServerboundCommandSuggestionPacket(transactionId, command));
final CompletableFuture future = new CompletableFuture<>();
transactions.put(transactionId, future);