Add basic self-care

This commit is contained in:
chipmunk 2023-02-08 21:30:14 -05:00
parent 26fd290b92
commit a77010e0f4
5 changed files with 94 additions and 4 deletions

View file

@ -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 {

View file

@ -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();
}
}

View file

@ -139,10 +139,9 @@ public class CommandCore {
return future;
}
/* @Override
public void disconnected (DisconnectedEvent event) {
public void cleanup () {
origin = null;
currentBlockRelative = null;
ready = false;
} */
}
}

View file

@ -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;
}
}

View file

@ -10,7 +10,8 @@
"ChatInputSuggestorMixin",
"ClientPlayNetworkHandlerAccessor",
"ClientConnectionMixin",
"ClientPlayerEntityMixin"
"ClientPlayerEntityMixin",
"ClientPlayNetworkHandlerMixin"
],
"injectors": {
"defaultRequire": 1