From 9e5a53f55abd3d04b9044548451867eae1e933b0 Mon Sep 17 00:00:00 2001 From: Frederik van der Els <49305700+xpple@users.noreply.github.com> Date: Mon, 19 Feb 2024 01:18:08 +0100 Subject: [PATCH] Add the cgamemode command (#601) * Add the cgamemode command * player -> query * Fix capitalisation --- .../clientcommands/ClientCommands.java | 1 + .../command/CGameModeCommand.java | 62 +++++++++++++++++++ .../assets/clientcommands/lang/en_us.json | 5 ++ 3 files changed, 68 insertions(+) create mode 100644 src/main/java/net/earthcomputer/clientcommands/command/CGameModeCommand.java diff --git a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java index 80dd9b063..26efe2134 100644 --- a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java +++ b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java @@ -160,6 +160,7 @@ public static void registerCommands(CommandDispatcher CrackRNGCommand.register(dispatcher); WeatherCommand.register(dispatcher); PluginsCommand.register(dispatcher); + CGameModeCommand.register(dispatcher); Calendar calendar = Calendar.getInstance(); boolean registerChatCommand = calendar.get(Calendar.MONTH) == Calendar.APRIL && calendar.get(Calendar.DAY_OF_MONTH) == 1; diff --git a/src/main/java/net/earthcomputer/clientcommands/command/CGameModeCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/CGameModeCommand.java new file mode 100644 index 000000000..8ec1e78b7 --- /dev/null +++ b/src/main/java/net/earthcomputer/clientcommands/command/CGameModeCommand.java @@ -0,0 +1,62 @@ +package net.earthcomputer.clientcommands.command; + +import com.mojang.authlib.GameProfile; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.multiplayer.PlayerInfo; +import net.minecraft.network.chat.Component; +import net.minecraft.world.level.GameType; + +import java.util.Collection; +import java.util.Set; +import java.util.stream.Collectors; + +import static dev.xpple.clientarguments.arguments.CEnumArgumentType.*; +import static dev.xpple.clientarguments.arguments.CGameProfileArgumentType.*; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*; + +public class CGameModeCommand { + + private static final SimpleCommandExceptionType PLAYER_NOT_FOUND_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("commands.cgamemode.playerNotFound")); + + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(literal("cgamemode") + .then(literal("query") + .then(argument("player", gameProfile()) + .executes(ctx -> getPlayerGameMode(ctx.getSource(), getCProfileArgument(ctx, "player"))))) + .then(literal("list") + .then(argument("gameMode", enumArg(GameType.class)) + .executes(ctx -> listWithGameMode(ctx.getSource(), getEnum(ctx, "gameMode")))))); + } + + private static int getPlayerGameMode(FabricClientCommandSource source, Collection profiles) throws CommandSyntaxException { + if (profiles.size() != 1) { + throw PLAYER_NOT_FOUND_EXCEPTION.create(); + } + PlayerInfo player = source.getClient().getConnection().getOnlinePlayers().stream() + .filter(p -> p.getProfile().getName().equalsIgnoreCase(profiles.iterator().next().getName())) + .findAny() + .orElseThrow(PLAYER_NOT_FOUND_EXCEPTION::create); + + source.sendFeedback(Component.translatable("commands.cgamemode.playerGameMode", player.getProfile().getName(), player.getGameMode().getShortDisplayName())); + return Command.SINGLE_SUCCESS; + } + + private static int listWithGameMode(FabricClientCommandSource source, GameType gameMode) { + Set playersWithGameMode = source.getClient().getConnection().getOnlinePlayers().stream() + .filter(p -> p.getGameMode() == gameMode) + .collect(Collectors.toSet()); + + if (playersWithGameMode.isEmpty()) { + source.sendFeedback(Component.translatable("commands.cgamemode.noneWithGameMode", gameMode.getShortDisplayName())); + } else { + source.sendFeedback(Component.translatable("commands.cgamemode.listWithGameMode", gameMode.getShortDisplayName())); + playersWithGameMode.forEach(p -> source.sendFeedback(Component.literal(p.getProfile().getName()))); + } + + return playersWithGameMode.size(); + } +} diff --git a/src/main/resources/assets/clientcommands/lang/en_us.json b/src/main/resources/assets/clientcommands/lang/en_us.json index 0e82e4bb5..a43bc2ef2 100644 --- a/src/main/resources/assets/clientcommands/lang/en_us.json +++ b/src/main/resources/assets/clientcommands/lang/en_us.json @@ -95,6 +95,11 @@ "commands.cfunction.limitReached": "Command limit (%d) reached", "commands.cfunction.success": "Ran %d commands from function %s", + "commands.cgamemode.playerNotFound": "Player not found", + "commands.cgamemode.playerGameMode": "Player %s is in game mode %s", + "commands.cgamemode.noneWithGameMode": "No players are in game mode %s", + "commands.cgamemode.listWithGameMode": "The following players are in game mode %s:", + "commands.cgamma.success": "Set gamma to %f", "commands.cghostblock.fill.success": "%d ghost blocks filled",