From 2452f555ef576206898d812cfa7da7b05b4486d4 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Thu, 6 Jul 2023 19:05:48 +0700 Subject: [PATCH] fix self care + some improvements --- .../chipmunk/chipmunkmod/ChipmunkMod.java | 2 + .../mixin/ClientPlayNetworkHandlerMixin.java | 4 +- .../chipmunkmod/modules/SelfCare.java | 41 ++++++++++++++----- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java b/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java index 7438ebe..478002f 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java +++ b/src/main/java/land/chipmunk/chipmunkmod/ChipmunkMod.java @@ -3,6 +3,7 @@ package land.chipmunk.chipmunkmod; import com.google.gson.GsonBuilder; import land.chipmunk.chipmunkmod.modules.KaboomCheck; import land.chipmunk.chipmunkmod.modules.Players; +import land.chipmunk.chipmunkmod.modules.SelfCare; import land.chipmunk.chipmunkmod.util.gson.BlockPosTypeAdapter; import net.fabricmc.api.ModInitializer; import java.io.InputStream; @@ -46,6 +47,7 @@ public class ChipmunkMod implements ModInitializer { Players.INSTANCE.init(); KaboomCheck.INSTANCE.init(); + SelfCare.INSTANCE.init(); LOGGER.info("Loaded ChipmunkMod (chayapak's fork)"); } diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java index 7debdfd..0839a98 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java @@ -7,13 +7,11 @@ import land.chipmunk.chipmunkmod.modules.*; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.network.ClientDynamicRegistryType; -import net.minecraft.client.network.PlayerListEntry; import net.minecraft.client.network.ServerInfo; import net.minecraft.client.util.telemetry.WorldSession; import net.minecraft.command.CommandRegistryAccess; import net.minecraft.network.ClientConnection; import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket; -import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket; import net.minecraft.network.packet.s2c.play.PlayerRemoveS2CPacket; import net.minecraft.registry.CombinedDynamicRegistries; import net.minecraft.resource.featuretoggle.FeatureSet; @@ -39,7 +37,7 @@ public class ClientPlayNetworkHandlerMixin { KaboomCheck.INSTANCE.onJoin(); CommandManager.INSTANCE = new CommandManager(ChipmunkMod.CONFIG.commands.prefix, commandRegistryAccess); - SelfCare.INSTANCE.init(); + SelfCare.INSTANCE.onJoin(); LoopCrouch.INSTANCE.init(); SongPlayer.INSTANCE.coreReady(); RainbowName.INSTANCE.init(); diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java b/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java index 256a458..3fc5aff 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java @@ -3,11 +3,12 @@ package land.chipmunk.chipmunkmod.modules; import land.chipmunk.chipmunkmod.ChipmunkMod; 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.client.network.ClientPlayerEntity; - +import net.minecraft.network.packet.Packet; +import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket; +import net.minecraft.network.packet.s2c.play.GameStateChangeS2CPacket; import net.minecraft.text.Text; import java.util.Timer; @@ -24,14 +25,16 @@ public class SelfCare extends Listener { public boolean gamemodeEnabled = true; public boolean cspyEnabled = true; - public String skin; + private int gameMode; - private boolean cspy = false; - public boolean hasSkin = false; + public String skin; private Timer timer = null; private Timer chatTimer = null; + private boolean cspy = false; + public boolean hasSkin = false; + public static final SelfCare INSTANCE = new SelfCare(MinecraftClient.getInstance(), 70L, 500L); // make the intervals in config? public SelfCare (MinecraftClient client, long interval, long chatInterval) { @@ -44,7 +47,9 @@ public class SelfCare extends Listener { ListenerManager.addListener(this); } - public void init () { + public void init () {} + + public void onJoin () { final TimerTask task = new TimerTask() { public void run () { tick(); @@ -57,8 +62,6 @@ public class SelfCare extends Listener { } }; - if (timer != null || chatTimer != null) cleanup(); - timer = new Timer(); chatTimer = new Timer(); @@ -71,11 +74,11 @@ public class SelfCare extends Listener { timer.cancel(); timer.purge(); - timer = null; chatTimer.cancel(); chatTimer.purge(); - chatTimer = null; + + gameMode = -1; hasSkin = false; // cspy too mabe? @@ -105,7 +108,7 @@ public class SelfCare extends Listener { } if (player != null && !player.hasPermissionLevel(2) && opEnabled) { if (serverHasCommand("op")) networkHandler.sendChatCommand("op @s[type=player]"); } - else if (client.player != null && !client.player.isCreative() && gamemodeEnabled) networkHandler.sendChatCommand("gamemode creative"); + else if (gameMode != 1 && gamemodeEnabled) networkHandler.sendChatCommand("gamemode creative"); } public void chatTick () { @@ -114,4 +117,20 @@ public class SelfCare extends Listener { if (!cspy && cspyEnabled) { if (serverHasCommand("c")) networkHandler.sendChatCommand("c on"); } else if (!hasSkin && !skin.equals("off")) { if (serverHasCommand("skin")) networkHandler.sendChatCommand("skin " + skin); } } + + @Override + public void packetReceived(Packet packet) { + if (packet instanceof GameJoinS2CPacket) packetReceived((GameJoinS2CPacket) packet); + else if (packet instanceof GameStateChangeS2CPacket) packetReceived((GameStateChangeS2CPacket) packet); + } + + public void packetReceived(GameJoinS2CPacket packet) { + gameMode = packet.gameMode().getId(); + } + + public void packetReceived(GameStateChangeS2CPacket packet) { + if (packet.getReason() != GameStateChangeS2CPacket.GAME_MODE_CHANGED) return; + + gameMode = (int) packet.getValue(); + } }