From 90307520072ad1a9481bafb897f225f2b1f3f422 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Tue, 6 May 2025 19:02:28 +0700 Subject: [PATCH] refactor: improve TabCompletePlugin - `nextTransactionId` is now an AtomicInteger - `tabComplete` function has been renamed to only `complete` so instead of calling like `bot.tabComplete.tabComplete()` now it's `bot.tabComplete.complete()` - also clear the transactions and reset `nextTransactionId` in case the server didn't respond the bot won't leak memory --- build-number.txt | 2 +- .../chomens_bot/plugins/TabCompletePlugin.java | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/build-number.txt b/build-number.txt index acb644cf..bf074e85 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -3146 \ No newline at end of file +3147 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/TabCompletePlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/TabCompletePlugin.java index fa5d75cd..1706a734 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/TabCompletePlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/TabCompletePlugin.java @@ -4,16 +4,18 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.data.listener.Listener; import org.geysermc.mcprotocollib.network.Session; +import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent; import org.geysermc.mcprotocollib.network.packet.Packet; import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundCommandSuggestionsPacket; import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundCommandSuggestionPacket; import java.util.Map; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicInteger; public class TabCompletePlugin implements Listener { private final Bot bot; - private int nextTransactionId = 0; + private final AtomicInteger nextTransactionId = new AtomicInteger(); private final Map> transactions = new Object2ObjectOpenHashMap<>(); public TabCompletePlugin (final Bot bot) { @@ -22,10 +24,10 @@ public class TabCompletePlugin implements Listener { bot.listener.addListener(this); } - public CompletableFuture tabComplete (final String command) { + public CompletableFuture complete (final String command) { if (!bot.loggedIn) return null; - final int transactionId = nextTransactionId++; + final int transactionId = nextTransactionId.getAndIncrement(); bot.session.send(new ServerboundCommandSuggestionPacket(transactionId, command)); @@ -43,8 +45,13 @@ public class TabCompletePlugin implements Listener { private void packetReceived (final ClientboundCommandSuggestionsPacket packet) { final int id = packet.getTransactionId(); - if (!transactions.containsKey(id)) return; + final CompletableFuture future = transactions.remove(id); + if (future != null) future.complete(packet); + } - transactions.remove(id).complete(packet); + @Override + public void disconnected (final DisconnectedEvent event) { + nextTransactionId.set(0); + transactions.clear(); } }