diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/Players.java b/src/main/java/land/chipmunk/chipmunkmod/modules/Players.java index c9fdf4d..8ef94b1 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/Players.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/Players.java @@ -6,27 +6,61 @@ import com.mojang.brigadier.suggestion.Suggestions; import land.chipmunk.chipmunkmod.data.MutablePlayerListEntry; import land.chipmunk.chipmunkmod.listeners.Listener; import land.chipmunk.chipmunkmod.listeners.ListenerManager; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.s2c.play.CommandSuggestionsS2CPacket; import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket; import net.minecraft.network.packet.s2c.play.PlayerRemoveS2CPacket; import net.minecraft.text.Text; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; +import java.util.*; import java.util.concurrent.CompletableFuture; public class Players extends Listener { public List list = new ArrayList<>(); - public static Players INSTANCE = new Players(); + public static Players INSTANCE = new Players(MinecraftClient.getInstance()); - public Players () { + private Timer timer; + + private final MinecraftClient client; + + public Players (MinecraftClient client) { + this.client = client; ListenerManager.addListener(this); + + TabComplete.INSTANCE.init(); } - public void init () {} + public void init () { + final TimerTask task = new TimerTask() { + public void run () { + tick(); + } + }; + + if (timer != null) cleanup(); + + timer = new Timer(); + timer.schedule(task, 0, 50); + } + + public void cleanup () { + list.clear(); + + if (timer == null) return; + + timer.cancel(); + timer.purge(); + timer = null; + } + + private void tick () { + final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); + + if (networkHandler == null) cleanup(); + } @Override public void packetReceived (Packet packet) { diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/TabComplete.java b/src/main/java/land/chipmunk/chipmunkmod/modules/TabComplete.java index aad937b..f3f28a1 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/TabComplete.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/TabComplete.java @@ -29,6 +29,8 @@ public class TabComplete extends Listener { ListenerManager.addListener(this); } + public void init () {} + public CompletableFuture complete (String command) { final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();