RandomCommand.java

net.minecraft.server.commands.RandomCommand

信息

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

    TODO

字段/常量

  • ERROR_RANGE_TOO_LARGE

    • 类型: SimpleCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L30
    • 说明:

      TODO

  • ERROR_RANGE_TOO_SMALL

    • 类型: SimpleCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L33
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static LiteralArgumentBuilder<CommandSourceStack> drawRandomValueTree(String name, boolean announce) @ L116

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

参数:

  • name: String
  • announce: boolean

说明:

TODO

private static CompletableFuture<Suggestions> suggestRandomSequence(CommandContext<CommandSourceStack> context, SuggestionsBuilder builder) @ L132

  • 方法名:suggestRandomSequence
  • 源码定位:L132
  • 返回类型:CompletableFuture
  • 修饰符:private static

参数:

  • context: CommandContext
  • builder: SuggestionsBuilder

说明:

TODO

private static int randomSample(CommandSourceStack source, MinMaxBounds.Ints range, Identifier sequence, boolean announce) @ L138

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

参数:

  • source: CommandSourceStack
  • range: MinMaxBounds.Ints
  • sequence: Identifier
  • announce: boolean

说明:

TODO

private static int resetSequence(CommandSourceStack source, Identifier sequence) @ L167

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

参数:

  • source: CommandSourceStack
  • sequence: Identifier

说明:

TODO

private static int resetSequence(CommandSourceStack source, Identifier sequence, int salt, boolean includeWorldSeed, boolean includeSequenceId) @ L174

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

参数:

  • source: CommandSourceStack
  • sequence: Identifier
  • salt: int
  • includeWorldSeed: boolean
  • includeSequenceId: boolean

说明:

TODO

private static int resetAllSequences(CommandSourceStack source) @ L181

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int resetAllSequencesAndSetNewDefaults(CommandSourceStack source, int salt, boolean includeWorldSeed, boolean includeSequenceId) @ L187

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

参数:

  • source: CommandSourceStack
  • salt: int
  • includeWorldSeed: boolean
  • includeSequenceId: boolean

说明:

TODO

代码

public class RandomCommand {
    private static final SimpleCommandExceptionType ERROR_RANGE_TOO_LARGE = new SimpleCommandExceptionType(
        Component.translatable("commands.random.error.range_too_large")
    );
    private static final SimpleCommandExceptionType ERROR_RANGE_TOO_SMALL = new SimpleCommandExceptionType(
        Component.translatable("commands.random.error.range_too_small")
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("random")
                .then(drawRandomValueTree("value", false))
                .then(drawRandomValueTree("roll", true))
                .then(
                    Commands.literal("reset")
                        .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                        .then(
                            Commands.literal("*")
                                .executes(c -> resetAllSequences(c.getSource()))
                                .then(
                                    Commands.argument("seed", IntegerArgumentType.integer())
                                        .executes(c -> resetAllSequencesAndSetNewDefaults(c.getSource(), IntegerArgumentType.getInteger(c, "seed"), true, true))
                                        .then(
                                            Commands.argument("includeWorldSeed", BoolArgumentType.bool())
                                                .executes(
                                                    c -> resetAllSequencesAndSetNewDefaults(
                                                        c.getSource(),
                                                        IntegerArgumentType.getInteger(c, "seed"),
                                                        BoolArgumentType.getBool(c, "includeWorldSeed"),
                                                        true
                                                    )
                                                )
                                                .then(
                                                    Commands.argument("includeSequenceId", BoolArgumentType.bool())
                                                        .executes(
                                                            c -> resetAllSequencesAndSetNewDefaults(
                                                                c.getSource(),
                                                                IntegerArgumentType.getInteger(c, "seed"),
                                                                BoolArgumentType.getBool(c, "includeWorldSeed"),
                                                                BoolArgumentType.getBool(c, "includeSequenceId")
                                                            )
                                                        )
                                                )
                                        )
                                )
                        )
                        .then(
                            Commands.argument("sequence", IdentifierArgument.id())
                                .suggests(RandomCommand::suggestRandomSequence)
                                .executes(c -> resetSequence(c.getSource(), IdentifierArgument.getId(c, "sequence")))
                                .then(
                                    Commands.argument("seed", IntegerArgumentType.integer())
                                        .executes(
                                            c -> resetSequence(
                                                c.getSource(), IdentifierArgument.getId(c, "sequence"), IntegerArgumentType.getInteger(c, "seed"), true, true
                                            )
                                        )
                                        .then(
                                            Commands.argument("includeWorldSeed", BoolArgumentType.bool())
                                                .executes(
                                                    c -> resetSequence(
                                                        c.getSource(),
                                                        IdentifierArgument.getId(c, "sequence"),
                                                        IntegerArgumentType.getInteger(c, "seed"),
                                                        BoolArgumentType.getBool(c, "includeWorldSeed"),
                                                        true
                                                    )
                                                )
                                                .then(
                                                    Commands.argument("includeSequenceId", BoolArgumentType.bool())
                                                        .executes(
                                                            c -> resetSequence(
                                                                c.getSource(),
                                                                IdentifierArgument.getId(c, "sequence"),
                                                                IntegerArgumentType.getInteger(c, "seed"),
                                                                BoolArgumentType.getBool(c, "includeWorldSeed"),
                                                                BoolArgumentType.getBool(c, "includeSequenceId")
                                                            )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        );
    }
 
    private static LiteralArgumentBuilder<CommandSourceStack> drawRandomValueTree(String name, boolean announce) {
        return Commands.literal(name)
            .then(
                Commands.argument("range", RangeArgument.intRange())
                    .executes(c -> randomSample(c.getSource(), RangeArgument.Ints.getRange(c, "range"), null, announce))
                    .then(
                        Commands.argument("sequence", IdentifierArgument.id())
                            .suggests(RandomCommand::suggestRandomSequence)
                            .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                            .executes(
                                c -> randomSample(c.getSource(), RangeArgument.Ints.getRange(c, "range"), IdentifierArgument.getId(c, "sequence"), announce)
                            )
                    )
            );
    }
 
    private static CompletableFuture<Suggestions> suggestRandomSequence(CommandContext<CommandSourceStack> context, SuggestionsBuilder builder) {
        List<String> result = Lists.newArrayList();
        context.getSource().getServer().getRandomSequences().forAllSequences((key, sequence) -> result.add(key.toString()));
        return SharedSuggestionProvider.suggest(result, builder);
    }
 
    private static int randomSample(CommandSourceStack source, MinMaxBounds.Ints range, @Nullable Identifier sequence, boolean announce) throws CommandSyntaxException {
        RandomSource random;
        if (sequence != null) {
            random = source.getServer().getRandomSequence(sequence);
        } else {
            random = source.getLevel().getRandom();
        }
 
        int min = range.min().orElse(Integer.MIN_VALUE);
        int max = range.max().orElse(Integer.MAX_VALUE);
        long span = (long)max - min;
        if (span == 0L) {
            throw ERROR_RANGE_TOO_SMALL.create();
        } else if (span >= 2147483647L) {
            throw ERROR_RANGE_TOO_LARGE.create();
        } else {
            int value = Mth.randomBetweenInclusive(random, min, max);
            if (announce) {
                source.getServer()
                    .getPlayerList()
                    .broadcastSystemMessage(Component.translatable("commands.random.roll", source.getDisplayName(), value, min, max), false);
            } else {
                source.sendSuccess(() -> Component.translatable("commands.random.sample.success", value), false);
            }
 
            return value;
        }
    }
 
    private static int resetSequence(CommandSourceStack source, Identifier sequence) throws CommandSyntaxException {
        ServerLevel level = source.getLevel();
        source.getServer().getRandomSequences().reset(sequence, level.getSeed());
        source.sendSuccess(() -> Component.translatable("commands.random.reset.success", Component.translationArg(sequence)), false);
        return 1;
    }
 
    private static int resetSequence(CommandSourceStack source, Identifier sequence, int salt, boolean includeWorldSeed, boolean includeSequenceId) throws CommandSyntaxException {
        ServerLevel level = source.getLevel();
        source.getServer().getRandomSequences().reset(sequence, level.getSeed(), salt, includeWorldSeed, includeSequenceId);
        source.sendSuccess(() -> Component.translatable("commands.random.reset.success", Component.translationArg(sequence)), false);
        return 1;
    }
 
    private static int resetAllSequences(CommandSourceStack source) {
        int count = source.getServer().getRandomSequences().clear();
        source.sendSuccess(() -> Component.translatable("commands.random.reset.all.success", count), false);
        return count;
    }
 
    private static int resetAllSequencesAndSetNewDefaults(CommandSourceStack source, int salt, boolean includeWorldSeed, boolean includeSequenceId) {
        RandomSequences randomSequences = source.getServer().getRandomSequences();
        randomSequences.setSeedDefaults(salt, includeWorldSeed, includeSequenceId);
        int count = randomSequences.clear();
        source.sendSuccess(() -> Component.translatable("commands.random.reset.all.success", count), false);
        return count;
    }
}

引用的其他类

  • MinMaxBounds

    • 引用位置: 参数
  • CommandSourceStack

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

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

    • 引用位置: 方法调用
    • 关联成员: SharedSuggestionProvider.suggest()
  • IdentifierArgument

    • 引用位置: 方法调用
    • 关联成员: IdentifierArgument.getId(), IdentifierArgument.id()
  • RangeArgument

    • 引用位置: 方法调用
    • 关联成员: RangeArgument.Ints.getRange(), RangeArgument.intRange()
  • Component

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

    • 引用位置: 参数
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.randomBetweenInclusive()