| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.context.CommandContext;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.Collections;
|
| 7 | import net.minecraft.commands.CommandSourceStack;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.commands.arguments.EntityArgument;
|
| 10 | import net.minecraft.commands.arguments.GameModeArgument;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.server.level.ServerPlayer;
|
| 13 | import net.minecraft.server.permissions.PermissionCheck;
|
| 14 | import net.minecraft.server.permissions.Permissions;
|
| 15 | import net.minecraft.world.level.GameType;
|
| 16 | import net.minecraft.world.level.gamerules.GameRules;
|
| 17 |
|
| 18 | public class GameModeCommand {
|
| 19 | public static final PermissionCheck PERMISSION_CHECK = new PermissionCheck.Require(Permissions.COMMANDS_GAMEMASTER);
|
| 20 |
|
| 21 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 22 | dispatcher.register(
|
| 23 | Commands.literal("gamemode")
|
| 24 | .requires(Commands.hasPermission(PERMISSION_CHECK))
|
| 25 | .then(
|
| 26 | Commands.argument("gamemode", GameModeArgument.gameMode())
|
| 27 | .executes(c -> setMode(c, Collections.singleton(c.getSource().getPlayerOrException()), GameModeArgument.getGameMode(c, "gamemode")))
|
| 28 | .then(
|
| 29 | Commands.argument("target", EntityArgument.players())
|
| 30 | .executes(c -> setMode(c, EntityArgument.getPlayers(c, "target"), GameModeArgument.getGameMode(c, "gamemode")))
|
| 31 | )
|
| 32 | )
|
| 33 | );
|
| 34 | }
|
| 35 |
|
| 36 | private static void logGamemodeChange(final CommandSourceStack source, final ServerPlayer target, final GameType newType) {
|
| 37 | Component mode = Component.translatable("gameMode." + newType.getName());
|
| 38 | if (source.getEntity() == target) {
|
| 39 | source.sendSuccess(() -> Component.translatable("commands.gamemode.success.self", mode), true);
|
| 40 | } else {
|
| 41 | if (source.getLevel().getGameRules().get(GameRules.SEND_COMMAND_FEEDBACK)) {
|
| 42 | target.sendSystemMessage(Component.translatable("gameMode.changed", mode));
|
| 43 | }
|
| 44 |
|
| 45 | source.sendSuccess(() -> Component.translatable("commands.gamemode.success.other", target.getDisplayName(), mode), true);
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | private static int setMode(final CommandContext<CommandSourceStack> context, final Collection<ServerPlayer> players, final GameType type) {
|
| 50 | int count = 0;
|
| 51 |
|
| 52 | for (ServerPlayer player : players) {
|
| 53 | if (setGameMode(context.getSource(), player, type)) {
|
| 54 | count++;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | return count;
|
| 59 | }
|
| 60 |
|
| 61 | public static void setGameMode(final ServerPlayer player, final GameType type) {
|
| 62 | setGameMode(player.createCommandSourceStack(), player, type);
|
| 63 | }
|
| 64 |
|
| 65 | private static boolean setGameMode(final CommandSourceStack source, final ServerPlayer player, final GameType type) {
|
| 66 | if (player.setGameMode(type)) {
|
| 67 | logGamemodeChange(source, player, type);
|
| 68 | return true;
|
| 69 | } else {
|
| 70 | return false;
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|