From a77010e0f4557959cbc47e301cd6cbcbd243e3e4 Mon Sep 17 00:00:00 2001 From: Chip <65827213+ChipmunkMC@users.noreply.github.com> Date: Wed, 8 Feb 2023 21:30:14 -0500 Subject: [PATCH] Add basic self-care --- .../mixin/ClientConnectionMixin.java | 2 + .../mixin/ClientPlayNetworkHandlerMixin.java | 16 +++++ .../chipmunkmod/modules/CommandCore.java | 5 +- .../chipmunkmod/modules/SelfCare.java | 72 +++++++++++++++++++ src/main/resources/chipmunkmod.mixins.json | 3 +- 5 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java create mode 100644 src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionMixin.java index 2065cb5..e2bee5b 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionMixin.java @@ -6,6 +6,8 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.text.Text; +import land.chipmunk.chipmunkmod.modules.CommandCore; +import land.chipmunk.chipmunkmod.modules.SelfCare; @Mixin(net.minecraft.network.ClientConnection.class) public class ClientConnectionMixin { diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java new file mode 100644 index 0000000..c422825 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java @@ -0,0 +1,16 @@ +package land.chipmunk.chipmunkmod.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket; +import land.chipmunk.chipmunkmod.modules.SelfCare; + +@Mixin(net.minecraft.client.network.ClientPlayNetworkHandler.class) +public class ClientPlayNetworkHandlerMixin { + @Inject(method = "onGameJoin", at = @At("TAIL")) + private void onGameJoin (GameJoinS2CPacket packet, CallbackInfo ci) { + SelfCare.INSTANCE.init(); + } +} diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java b/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java index 9610596..404bf42 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java @@ -139,10 +139,9 @@ public class CommandCore { return future; } - /* @Override - public void disconnected (DisconnectedEvent event) { + public void cleanup () { origin = null; currentBlockRelative = null; ready = false; - } */ + } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java b/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java new file mode 100644 index 0000000..7319aaf --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java @@ -0,0 +1,72 @@ +package land.chipmunk.chipmunkmod.modules; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.client.network.ClientPlayerEntity; +import com.mojang.brigadier.tree.CommandNode; +import com.mojang.brigadier.tree.LiteralCommandNode; +import lombok.Getter; +import java.util.Timer; +import java.util.TimerTask; + +public class SelfCare { + private final MinecraftClient client; + @Getter private long interval; + + private Timer timer = null; + + public static final SelfCare INSTANCE = new SelfCare(MinecraftClient.getInstance(), 70L); + + public SelfCare (MinecraftClient client, long interval) { + this.client = client; + this.interval = interval; + } + + public void init () { + final TimerTask task = new TimerTask() { + public void run () { + tick(); + } + }; + + if (timer != null) cleanup(); + + timer = new Timer(); + timer.schedule(task, interval, interval); + } + + public void cleanup () { + if (timer == null) return; + + timer.cancel(); + timer.purge(); + timer = null; + } + + public void tick () { + final ClientPlayerEntity player = client.player; + final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); + + if (networkHandler == null) { + cleanup(); + return; + } + + if (!player.hasPermissionLevel(2)) { if (serverHasCommand("op")) networkHandler.sendChatCommand("op @s[type=player]"); } + else if (!client.player.isCreative()) networkHandler.sendChatCommand("gamemode creative"); + } + + // TODO: Move this into a separate class related to server info gathering (and yes, I plan on making this d y n a m i c and require little to no configuration for most servers) + private boolean serverHasCommand (String name) { + final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); + + for (CommandNode node : networkHandler.getCommandDispatcher().getRoot().getChildren()) { + if (!(node instanceof LiteralCommandNode)) continue; + final LiteralCommandNode literal = (LiteralCommandNode) node; + + if (literal.getLiteral().equals(name)) return true; + } + + return false; + } +} diff --git a/src/main/resources/chipmunkmod.mixins.json b/src/main/resources/chipmunkmod.mixins.json index 4972de4..d3c7cf9 100644 --- a/src/main/resources/chipmunkmod.mixins.json +++ b/src/main/resources/chipmunkmod.mixins.json @@ -10,7 +10,8 @@ "ChatInputSuggestorMixin", "ClientPlayNetworkHandlerAccessor", "ClientConnectionMixin", - "ClientPlayerEntityMixin" + "ClientPlayerEntityMixin", + "ClientPlayNetworkHandlerMixin" ], "injectors": { "defaultRequire": 1