DifficultyCommand.java

net.minecraft.server.commands.DifficultyCommand

信息

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

    TODO

字段/常量

  • ERROR_ALREADY_SAME_DIFFICULTY
    • 类型: DynamicCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L14
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

public static int setDifficulty(CommandSourceStack source, Difficulty difficulty) @ L32

  • 方法名:setDifficulty
  • 源码定位:L32
  • 返回类型:int
  • 修饰符:public static

参数:

  • source: CommandSourceStack
  • difficulty: Difficulty

说明:

TODO

代码

public class DifficultyCommand {
    private static final DynamicCommandExceptionType ERROR_ALREADY_SAME_DIFFICULTY = new DynamicCommandExceptionType(
        difficulty -> Component.translatableEscape("commands.difficulty.failure", difficulty)
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        LiteralArgumentBuilder<CommandSourceStack> command = Commands.literal("difficulty");
 
        for (Difficulty difficulty : Difficulty.values()) {
            command.then(Commands.literal(difficulty.getSerializedName()).executes(c -> setDifficulty(c.getSource(), difficulty)));
        }
 
        dispatcher.register(command.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS)).executes(c -> {
            Difficulty difficultyx = c.getSource().getLevel().getDifficulty();
            c.getSource().sendSuccess(() -> Component.translatable("commands.difficulty.query", difficultyx.getDisplayName()), false);
            return difficultyx.getId();
        }));
    }
 
    public static int setDifficulty(CommandSourceStack source, Difficulty difficulty) throws CommandSyntaxException {
        MinecraftServer server = source.getServer();
        if (server.getWorldData().getDifficulty() == difficulty) {
            throw ERROR_ALREADY_SAME_DIFFICULTY.create(difficulty.getSerializedName());
        } else {
            server.setDifficulty(difficulty, true);
            source.sendSuccess(() -> Component.translatable("commands.difficulty.success", difficulty.getDisplayName()), true);
            return 0;
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

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

    • 引用位置: 参数/方法调用
    • 关联成员: Difficulty.values()