GameRuleCommand.java

net.minecraft.server.commands.GameRuleCommand

信息

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

    TODO

字段/常量

内部类/嵌套类型

构造器

方法

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

public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) @ L15

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

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

private static <T> LiteralArgumentBuilder<CommandSourceStack> buildRuleArguments(GameRule<T> gameRule, LiteralArgumentBuilder<CommandSourceStack> ruleLiteral) @ L28

  • 方法名:buildRuleArguments
  • 源码定位:L28
  • 返回类型: LiteralArgumentBuilder
  • 修饰符:private static

参数:

  • gameRule: GameRule
  • ruleLiteral: LiteralArgumentBuilder

说明:

TODO

private static <T> int setRule(CommandContext<CommandSourceStack> context, GameRule<T> gameRule) @ L35

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

参数:

  • context: CommandContext
  • gameRule: GameRule

说明:

TODO

private static <T> int queryRule(CommandSourceStack source, GameRule<T> gameRule) @ L43

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

参数:

  • source: CommandSourceStack
  • gameRule: GameRule

说明:

TODO

代码

public class GameRuleCommand {
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        final LiteralArgumentBuilder<CommandSourceStack> base = Commands.literal("gamerule").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS));
        new GameRules(context.enabledFeatures()).visitGameRuleTypes(new GameRuleTypeVisitor() {
            @Override
            public <T> void visit(GameRule<T> gameRule) {
                LiteralArgumentBuilder<CommandSourceStack> unqualified = Commands.literal(gameRule.id());
                LiteralArgumentBuilder<CommandSourceStack> qualified = Commands.literal(gameRule.getIdentifier().toString());
                base.then(GameRuleCommand.buildRuleArguments(gameRule, unqualified)).then(GameRuleCommand.buildRuleArguments(gameRule, qualified));
            }
        });
        dispatcher.register(base);
    }
 
    private static <T> LiteralArgumentBuilder<CommandSourceStack> buildRuleArguments(
        GameRule<T> gameRule, LiteralArgumentBuilder<CommandSourceStack> ruleLiteral
    ) {
        return ruleLiteral.executes(c -> queryRule(c.getSource(), gameRule))
            .then(Commands.argument("value", gameRule.argument()).executes(c -> setRule(c, gameRule)));
    }
 
    private static <T> int setRule(CommandContext<CommandSourceStack> context, GameRule<T> gameRule) {
        CommandSourceStack source = context.getSource();
        T value = context.getArgument("value", gameRule.valueClass());
        source.getLevel().getGameRules().set(gameRule, value, context.getSource().getServer());
        source.sendSuccess(() -> Component.translatable("commands.gamerule.set", gameRule.id(), gameRule.serialize(value)), true);
        return gameRule.getCommandResult(value);
    }
 
    private static <T> int queryRule(CommandSourceStack source, GameRule<T> gameRule) {
        T value = source.getLevel().getGameRules().get(gameRule);
        source.sendSuccess(() -> Component.translatable("commands.gamerule.query", gameRule.id(), gameRule.serialize(value)), false);
        return gameRule.getCommandResult(value);
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数/返回值
  • Commands

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

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

    • 引用位置: 参数
  • GameRuleTypeVisitor

    • 引用位置: 构造调用
    • 关联成员: GameRuleTypeVisitor()
  • GameRules

    • 引用位置: 构造调用
    • 关联成员: GameRules()