feat: use target game mode based on F3 + F4 instead of hardcoding creative (idea from ZenZoya, thank you!)

refactor: add some braces + change variable names in SelfCare to be less confusing
This commit is contained in:
Chayapak Supasakul 2025-12-26 13:37:41 +07:00
parent 51f181ce69
commit 4142d41d64
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
3 changed files with 42 additions and 22 deletions

View file

@ -28,10 +28,11 @@ public class AutoSkinCommand {
final String username = getString(context, "username");
SelfCare.INSTANCE.skin = username;
SelfCare.INSTANCE.targetSkin = username;
if (username.equals("off")) source.sendFeedback(Text.literal("Successfully disabled auto skin"));
else {
if (username.equals("off")) {
source.sendFeedback(Text.literal("Successfully disabled auto skin"));
} else {
SelfCare.INSTANCE.hasSkin = false;
source.sendFeedback(Text.literal("Set your auto skin username to: " + username));
}

View file

@ -46,15 +46,15 @@ public class SelfCareCommand {
switch (type) {
case "op" -> {
SelfCare.INSTANCE.opEnabled = bool;
SelfCare.INSTANCE.configOpEnabled = bool;
source.sendFeedback(Text.literal("The op self care is now " + (bool ? "enabled" : "disabled")));
}
case "gamemode" -> {
SelfCare.INSTANCE.gamemodeEnabled = bool;
SelfCare.INSTANCE.configGameModeEnabled = bool;
source.sendFeedback(Text.literal("The gamemode self care is now " + (bool ? "enabled" : "disabled")));
}
case "cspy" -> {
SelfCare.INSTANCE.cspyEnabled = bool;
SelfCare.INSTANCE.configCommandSpyEnabled = bool;
source.sendFeedback(Text.literal("The CommandSpy self care is now " + (bool ? "enabled" : "disabled")));
}
}

View file

@ -7,6 +7,7 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.command.DefaultPermissions;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.play.ChangeGameModeC2SPacket;
import net.minecraft.text.Text;
import net.minecraft.world.GameMode;
@ -15,18 +16,24 @@ import static land.chipmunk.chipmunkmod.util.ServerUtilities.serverHasCommand;
public class SelfCare implements Listener {
public static final SelfCare INSTANCE = new SelfCare(MinecraftClient.getInstance());
private final MinecraftClient client;
public boolean opEnabled = ChipmunkMod.CONFIG.selfCare.op;
public boolean gamemodeEnabled = ChipmunkMod.CONFIG.selfCare.gameMode;
public boolean cspyEnabled = ChipmunkMod.CONFIG.selfCare.cspy;
public String skin;
public boolean configOpEnabled = ChipmunkMod.CONFIG.selfCare.op;
public boolean configGameModeEnabled = ChipmunkMod.CONFIG.selfCare.gameMode;
public boolean configCommandSpyEnabled = ChipmunkMod.CONFIG.selfCare.cspy;
public GameMode targetGameMode = GameMode.CREATIVE;
public String targetSkin;
public boolean hasSkin = false;
private boolean cspy = false;
private boolean isCommandSpyEnabled = false;
public SelfCare (final MinecraftClient client) {
this.client = client;
this.skin = ChipmunkMod.CONFIG.autoSkinUsername == null ? "off" : ChipmunkMod.CONFIG.autoSkinUsername;
this.targetSkin = ChipmunkMod.CONFIG.autoSkinUsername == null ? "off" : ChipmunkMod.CONFIG.autoSkinUsername;
ListenerManager.addListener(this);
}
@ -36,17 +43,17 @@ public class SelfCare implements Listener {
public void cleanup () {
hasSkin = false;
cspy = false;
isCommandSpyEnabled = false;
}
@Override
public void chatMessageReceived (final Text message) {
final String stringMessage = message.getString();
if (stringMessage.equals("Successfully enabled CommandSpy")) cspy = true;
else if (stringMessage.equals("Successfully disabled CommandSpy")) cspy = false;
if (stringMessage.equals("Successfully enabled CommandSpy")) isCommandSpyEnabled = true;
else if (stringMessage.equals("Successfully disabled CommandSpy")) isCommandSpyEnabled = false;
else if (stringMessage.equals("Successfully set your skin to " + skin + "'s")) hasSkin = true;
else if (stringMessage.equals("Successfully set your skin to " + targetSkin + "'s")) hasSkin = true;
else if (
stringMessage.equals("Successfully removed your skin") ||
stringMessage.startsWith("Successfully set your skin to ")
@ -60,12 +67,16 @@ public class SelfCare implements Listener {
if (networkHandler == null || player == null) return;
if (!player.getPermissions().hasPermission(DefaultPermissions.OWNERS) && opEnabled && serverHasCommand("op"))
if (
!player.getPermissions().hasPermission(DefaultPermissions.OWNERS)
&& configOpEnabled
&& serverHasCommand("op")
) {
networkHandler.sendChatCommand("op @s[type=player]");
else if (!player.isInCreativeMode() && gamemodeEnabled)
} else if (player.getGameMode() != targetGameMode && configGameModeEnabled) {
// ViaVersion will automatically convert this to `/gamemode creative`
networkHandler.sendPacket(new ChangeGameModeC2SPacket(GameMode.CREATIVE));
else if (!cspy && cspyEnabled && serverHasCommand("c"))
networkHandler.sendPacket(new ChangeGameModeC2SPacket(targetGameMode));
} else if (!isCommandSpyEnabled && configCommandSpyEnabled && serverHasCommand("c")) {
if (
!CommandCore.INSTANCE.ready
|| !CommandCore.INSTANCE.runFillCommand
@ -75,7 +86,15 @@ public class SelfCare implements Listener {
} else {
CommandCore.INSTANCE.run("c " + player.getUuidAsString() + " on");
}
else if (!hasSkin && !skin.equalsIgnoreCase("off"))
networkHandler.sendChatCommand("skin " + skin);
} else if (!hasSkin && !targetSkin.equalsIgnoreCase("off")) {
networkHandler.sendChatCommand("skin " + targetSkin);
}
}
@Override
public void packetSent (final Packet<?> packet) {
if (packet instanceof ChangeGameModeC2SPacket (final GameMode mode)) {
targetGameMode = mode;
}
}
}