From c0b4fb1a7ec682c390b3021a186a8d84f5007801 Mon Sep 17 00:00:00 2001 From: Chipmunk <65827213+ChipmunkMC@users.noreply.github.com> Date: Tue, 28 Mar 2023 10:13:20 -0400 Subject: [PATCH] Optimize stuff --- src/main/java/land/chipmunk/chipmunkbot/Client.java | 5 ++++- .../java/land/chipmunk/chipmunkbot/plugins/CommandCore.java | 5 ++++- .../java/land/chipmunk/chipmunkbot/plugins/QueryPlugin.java | 5 ++++- .../land/chipmunk/chipmunkbot/plugins/TabCompletePlugin.java | 5 ++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkbot/Client.java b/src/main/java/land/chipmunk/chipmunkbot/Client.java index 79647d1..409de02 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Client.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Client.java @@ -61,14 +61,17 @@ public class Client { if (reconnectDelay < 0) return; + Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run () { reconnect(); + + timer.purge(); } }; - new Timer().schedule(task, reconnectDelay); + timer.schedule(task, reconnectDelay); } }); diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java index 27f0b36..55a61ed 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java @@ -157,14 +157,17 @@ public class CommandCore extends SessionAdapter { CompletableFuture future = new CompletableFuture(); + final Timer timer = new Timer(); final TimerTask queryTask = new TimerTask() { public void run () { client.query().block(currentBlock) .thenApply(tag -> { future.complete(tag); return tag; }); + + timer.purge(); } }; - new Timer().schedule(queryTask, 50); + timer.schedule(queryTask, 50); return future; } diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/QueryPlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/QueryPlugin.java index 3dbf7de..c4a94e1 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/QueryPlugin.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/QueryPlugin.java @@ -50,6 +50,9 @@ public class QueryPlugin extends SessionAdapter { } public void packetReceived (Session session, ClientboundTagQueryPacket packet) { - transactions.get(packet.getTransactionId()).complete(packet.getNbt()); + final CompletableFuture future = transactions.get(packet.getTransactionId()); + if (future == null) return; + future.complete(packet.getNbt()); + transactions.remove(future); } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/TabCompletePlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/TabCompletePlugin.java index a37e8e3..c6be83c 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/TabCompletePlugin.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/TabCompletePlugin.java @@ -38,6 +38,9 @@ public class TabCompletePlugin extends SessionAdapter { } public void packetReceived (Session session, ClientboundCommandSuggestionsPacket packet) { - transactions.get(packet.getTransactionId()).complete(packet); + final CompletableFuture future = transactions.get(packet.getTransactionId()); + if (future == null) return; + future.complete(packet); + transactions.remove(future); } }