-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add the cgamemode command * player -> query * Fix capitalisation
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/net/earthcomputer/clientcommands/command/CGameModeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters