TriggerCommand.java

net.minecraft.server.commands.TriggerCommand

信息

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

    TODO

字段/常量

  • ERROR_NOT_PRIMED

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

      TODO

  • ERROR_INVALID_OBJECTIVE

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

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

public static CompletableFuture<Suggestions> suggestObjectives(CommandSourceStack source, SuggestionsBuilder builder) @ L72

  • 方法名:suggestObjectives
  • 源码定位:L72
  • 返回类型:CompletableFuture
  • 修饰符:public static

参数:

  • source: CommandSourceStack
  • builder: SuggestionsBuilder

说明:

TODO

private static int addValue(CommandSourceStack source, ServerPlayer player, Objective objective, int amount) @ L91

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

参数:

  • source: CommandSourceStack
  • player: ServerPlayer
  • objective: Objective
  • amount: int

说明:

TODO

private static int setValue(CommandSourceStack source, ServerPlayer player, Objective objective, int amount) @ L98

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

参数:

  • source: CommandSourceStack
  • player: ServerPlayer
  • objective: Objective
  • amount: int

说明:

TODO

private static int simpleTrigger(CommandSourceStack source, ServerPlayer player, Objective objective) @ L105

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

参数:

  • source: CommandSourceStack
  • player: ServerPlayer
  • objective: Objective

说明:

TODO

private static ScoreAccess getScore(Scoreboard scoreboard, ScoreHolder scoreHolder, Objective objective) @ L112

  • 方法名:getScore
  • 源码定位:L112
  • 返回类型:ScoreAccess
  • 修饰符:private static

参数:

  • scoreboard: Scoreboard
  • scoreHolder: ScoreHolder
  • objective: Objective

说明:

TODO

代码

public class TriggerCommand {
    private static final SimpleCommandExceptionType ERROR_NOT_PRIMED = new SimpleCommandExceptionType(
        Component.translatable("commands.trigger.failed.unprimed")
    );
    private static final SimpleCommandExceptionType ERROR_INVALID_OBJECTIVE = new SimpleCommandExceptionType(
        Component.translatable("commands.trigger.failed.invalid")
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("trigger")
                .then(
                    Commands.argument("objective", ObjectiveArgument.objective())
                        .suggests((c, p) -> suggestObjectives(c.getSource(), p))
                        .executes(c -> simpleTrigger(c.getSource(), c.getSource().getPlayerOrException(), ObjectiveArgument.getObjective(c, "objective")))
                        .then(
                            Commands.literal("add")
                                .then(
                                    Commands.argument("value", IntegerArgumentType.integer())
                                        .executes(
                                            c -> addValue(
                                                c.getSource(),
                                                c.getSource().getPlayerOrException(),
                                                ObjectiveArgument.getObjective(c, "objective"),
                                                IntegerArgumentType.getInteger(c, "value")
                                            )
                                        )
                                )
                        )
                        .then(
                            Commands.literal("set")
                                .then(
                                    Commands.argument("value", IntegerArgumentType.integer())
                                        .executes(
                                            c -> setValue(
                                                c.getSource(),
                                                c.getSource().getPlayerOrException(),
                                                ObjectiveArgument.getObjective(c, "objective"),
                                                IntegerArgumentType.getInteger(c, "value")
                                            )
                                        )
                                )
                        )
                )
        );
    }
 
    public static CompletableFuture<Suggestions> suggestObjectives(CommandSourceStack source, SuggestionsBuilder builder) {
        ScoreHolder entity = source.getEntity();
        List<String> result = Lists.newArrayList();
        if (entity != null) {
            Scoreboard scoreboard = source.getServer().getScoreboard();
 
            for (Objective objective : scoreboard.getObjectives()) {
                if (objective.getCriteria() == ObjectiveCriteria.TRIGGER) {
                    ReadOnlyScoreInfo scoreInfo = scoreboard.getPlayerScoreInfo(entity, objective);
                    if (scoreInfo != null && !scoreInfo.isLocked()) {
                        result.add(objective.getName());
                    }
                }
            }
        }
 
        return SharedSuggestionProvider.suggest(result, builder);
    }
 
    private static int addValue(CommandSourceStack source, ServerPlayer player, Objective objective, int amount) throws CommandSyntaxException {
        ScoreAccess score = getScore(source.getServer().getScoreboard(), player, objective);
        int newValue = score.add(amount);
        source.sendSuccess(() -> Component.translatable("commands.trigger.add.success", objective.getFormattedDisplayName(), amount), true);
        return newValue;
    }
 
    private static int setValue(CommandSourceStack source, ServerPlayer player, Objective objective, int amount) throws CommandSyntaxException {
        ScoreAccess score = getScore(source.getServer().getScoreboard(), player, objective);
        score.set(amount);
        source.sendSuccess(() -> Component.translatable("commands.trigger.set.success", objective.getFormattedDisplayName(), amount), true);
        return amount;
    }
 
    private static int simpleTrigger(CommandSourceStack source, ServerPlayer player, Objective objective) throws CommandSyntaxException {
        ScoreAccess score = getScore(source.getServer().getScoreboard(), player, objective);
        int newValue = score.add(1);
        source.sendSuccess(() -> Component.translatable("commands.trigger.simple.success", objective.getFormattedDisplayName()), true);
        return newValue;
    }
 
    private static ScoreAccess getScore(Scoreboard scoreboard, ScoreHolder scoreHolder, Objective objective) throws CommandSyntaxException {
        if (objective.getCriteria() != ObjectiveCriteria.TRIGGER) {
            throw ERROR_INVALID_OBJECTIVE.create();
        } else {
            ReadOnlyScoreInfo scoreInfo = scoreboard.getPlayerScoreInfo(scoreHolder, objective);
            if (scoreInfo != null && !scoreInfo.isLocked()) {
                ScoreAccess score = scoreboard.getOrCreatePlayerScore(scoreHolder, objective);
                score.lock();
                return score;
            } else {
                throw ERROR_NOT_PRIMED.create();
            }
        }
    }
}

引用的其他类