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
This commit is contained in:
ChomeNS
2025-05-06 19:02:28 +07:00
parent c952ba5a6f
commit 9030752007
2 changed files with 13 additions and 6 deletions

View File

@@ -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<Integer, CompletableFuture<ClientboundCommandSuggestionsPacket>> transactions = new Object2ObjectOpenHashMap<>();
public TabCompletePlugin (final Bot bot) {
@@ -22,10 +24,10 @@ public class TabCompletePlugin implements Listener {
bot.listener.addListener(this);
}
public CompletableFuture<ClientboundCommandSuggestionsPacket> tabComplete (final String command) {
public CompletableFuture<ClientboundCommandSuggestionsPacket> 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<ClientboundCommandSuggestionsPacket> 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();
}
}