diff --git a/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java b/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java index 03186a3..47746b5 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java +++ b/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java @@ -15,6 +15,7 @@ import java.util.List; public class ChipmunkBot extends Client { @Getter public final ChatPlugin chat = new ChatPlugin(this); + @Getter public final TabCompletePlugin tabComplete = new TabCompletePlugin(this); @Getter public final QueryPlugin query = new QueryPlugin(this); @Getter public final PlayerListPlugin playerList = new PlayerListPlugin(this); @Getter public final CommandManager commandManager = new CommandManager(this); diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java index c38b3b6..e9c06b1 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java @@ -1,6 +1,6 @@ package land.chipmunk.chipmunkbot.plugins; -import land.chipmunk.chipmunkbot.Client; +import land.chipmunk.chipmunkbot.ChipmunkBot; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerInfoPacket; import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.Session; @@ -16,10 +16,10 @@ import java.util.ArrayList; import java.util.UUID; public class PlayerListPlugin extends SessionAdapter { - private Client client; + private ChipmunkBot client; public List list = new ArrayList<>(); - public PlayerListPlugin (Client client) { + public PlayerListPlugin (ChipmunkBot client) { this.client = client; client.addListener((SessionListener) this); } @@ -106,6 +106,18 @@ public class PlayerListPlugin extends SessionAdapter { final MutablePlayerListEntry target = getEntry(newEntry); if (target == null) return; - list.remove(target); + client.tabComplete().complete("/scoreboard players add ").thenApply(packet -> { + final String[] matches = packet.getMatches(); + final Component[] tooltips = packet.getTooltips(); + final String username = target.profile().getName(); + + for (int i = 0; i < matches.length; i++) { + if (tooltips[i] != null || !matches[i].equals(username)) continue; + return packet; + } + + list.remove(target); + return packet; + }); } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/TabCompletePlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/TabCompletePlugin.java new file mode 100644 index 0000000..a37e8e3 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/TabCompletePlugin.java @@ -0,0 +1,43 @@ +package land.chipmunk.chipmunkbot.plugins; + +import land.chipmunk.chipmunkbot.Client; +import com.nukkitx.math.vector.Vector3i; +import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.packetlib.Session; +import com.github.steveice10.packetlib.event.session.SessionListener; +import com.github.steveice10.packetlib.event.session.SessionAdapter; +import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundCommandSuggestionPacket; +import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundCommandSuggestionsPacket; +import java.util.concurrent.CompletableFuture; +import java.util.Map; +import java.util.HashMap; + +public class TabCompletePlugin extends SessionAdapter { + private Client client; + private int nextTransactionId = 0; + private Map> transactions = new HashMap<>(); + + public TabCompletePlugin (Client client) { + this.client = client; + client.addListener((SessionListener) this); + } + + public CompletableFuture complete (String command) { + final int transactionId = nextTransactionId++; + if (nextTransactionId > Integer.MAX_VALUE) nextTransactionId = 0; // ? Can and should I use negative numbers too? + client.session().send(new ServerboundCommandSuggestionPacket(transactionId, command)); + + final CompletableFuture future = new CompletableFuture(); + transactions.put(transactionId, future); + return future; + } + + @Override + public void packetReceived (Session session, Packet packet) { + if (packet instanceof ClientboundCommandSuggestionsPacket) packetReceived(session, (ClientboundCommandSuggestionsPacket) packet); + } + + public void packetReceived (Session session, ClientboundCommandSuggestionsPacket packet) { + transactions.get(packet.getTransactionId()).complete(packet); + } +}