GameModeCommand.java

net.minecraft.server.commands.GameModeCommand

信息

  • 全限定名:net.minecraft.server.commands.GameModeCommand
  • 类型:public class
  • 包:net.minecraft.server.commands
  • 源码路径:src/main/java/net/minecraft/server/commands/GameModeCommand.java
  • 起始行号:L18
  • 职责:

    TODO

字段/常量

  • PERMISSION_CHECK
    • 类型: PermissionCheck
    • 修饰符: public static final
    • 源码定位: L19
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) @ L21

  • 方法名:register
  • 源码定位:L21
  • 返回类型:void
  • 修饰符:public static

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static void logGamemodeChange(CommandSourceStack source, ServerPlayer target, GameType newType) @ L36

  • 方法名:logGamemodeChange
  • 源码定位:L36
  • 返回类型:void
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • target: ServerPlayer
  • newType: GameType

说明:

TODO

private static int setMode(CommandContext<CommandSourceStack> context, Collection<ServerPlayer> players, GameType type) @ L49

  • 方法名:setMode
  • 源码定位:L49
  • 返回类型:int
  • 修饰符:private static

参数:

  • context: CommandContext
  • players: Collection
  • type: GameType

说明:

TODO

public static void setGameMode(ServerPlayer player, GameType type) @ L61

  • 方法名:setGameMode
  • 源码定位:L61
  • 返回类型:void
  • 修饰符:public static

参数:

  • player: ServerPlayer
  • type: GameType

说明:

TODO

private static boolean setGameMode(CommandSourceStack source, ServerPlayer player, GameType type) @ L65

  • 方法名:setGameMode
  • 源码定位:L65
  • 返回类型:boolean
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • player: ServerPlayer
  • type: GameType

说明:

TODO

代码

public class GameModeCommand {
    public static final PermissionCheck PERMISSION_CHECK = new PermissionCheck.Require(Permissions.COMMANDS_GAMEMASTER);
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("gamemode")
                .requires(Commands.hasPermission(PERMISSION_CHECK))
                .then(
                    Commands.argument("gamemode", GameModeArgument.gameMode())
                        .executes(c -> setMode(c, Collections.singleton(c.getSource().getPlayerOrException()), GameModeArgument.getGameMode(c, "gamemode")))
                        .then(
                            Commands.argument("target", EntityArgument.players())
                                .executes(c -> setMode(c, EntityArgument.getPlayers(c, "target"), GameModeArgument.getGameMode(c, "gamemode")))
                        )
                )
        );
    }
 
    private static void logGamemodeChange(CommandSourceStack source, ServerPlayer target, GameType newType) {
        Component mode = Component.translatable("gameMode." + newType.getName());
        if (source.getEntity() == target) {
            source.sendSuccess(() -> Component.translatable("commands.gamemode.success.self", mode), true);
        } else {
            if (source.getLevel().getGameRules().get(GameRules.SEND_COMMAND_FEEDBACK)) {
                target.sendSystemMessage(Component.translatable("gameMode.changed", mode));
            }
 
            source.sendSuccess(() -> Component.translatable("commands.gamemode.success.other", target.getDisplayName(), mode), true);
        }
    }
 
    private static int setMode(CommandContext<CommandSourceStack> context, Collection<ServerPlayer> players, GameType type) {
        int count = 0;
 
        for (ServerPlayer player : players) {
            if (setGameMode(context.getSource(), player, type)) {
                count++;
            }
        }
 
        return count;
    }
 
    public static void setGameMode(ServerPlayer player, GameType type) {
        setGameMode(player.createCommandSourceStack(), player, type);
    }
 
    private static boolean setGameMode(CommandSourceStack source, ServerPlayer player, GameType type) {
        if (player.setGameMode(type)) {
            logGamemodeChange(source, player, type);
            return true;
        } else {
            return false;
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

    • 引用位置: 方法调用
    • 关联成员: Commands.argument(), Commands.hasPermission(), Commands.literal()
  • EntityArgument

    • 引用位置: 方法调用
    • 关联成员: EntityArgument.getPlayers(), EntityArgument.players()
  • GameModeArgument

    • 引用位置: 方法调用
    • 关联成员: GameModeArgument.gameMode(), GameModeArgument.getGameMode()
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.translatable()
  • ServerPlayer

    • 引用位置: 参数
  • PermissionCheck

    • 引用位置: 字段/方法调用/构造调用
    • 关联成员: PermissionCheck.Require(), Require()
  • GameType

    • 引用位置: 参数