From cb2fd9401e19f4be2955a6f9ecd9947e089d11f3 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 5 May 2023 16:02:17 +0700 Subject: [PATCH] fix concurrent fard exception i guess finally !!!! --- .../chipmunk/chipmunkmod/modules/Players.java | 46 ++++++++++++++++--- .../chipmunkmod/modules/TabComplete.java | 2 + 2 files changed, 42 insertions(+), 6 deletions(-) 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();