Skip to content

Commit

Permalink
Add the cgamemode command (#601)
Browse files Browse the repository at this point in the history
* Add the cgamemode command

* player -> query

* Fix capitalisation
  • Loading branch information
xpple authored Feb 19, 2024
1 parent e23b216 commit 9e5a53f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public static void registerCommands(CommandDispatcher<FabricClientCommandSource>
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FabricClientCommandSource> 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<GameProfile> 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<PlayerInfo> 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();
}
}
5 changes: 5 additions & 0 deletions src/main/resources/assets/clientcommands/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9e5a53f

Please sign in to comment.