| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.brigadier.CommandDispatcher;
|
| 5 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 6 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 7 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 8 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 9 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 10 | import java.util.List;
|
| 11 | import java.util.concurrent.CompletableFuture;
|
| 12 | import net.minecraft.commands.CommandSourceStack;
|
| 13 | import net.minecraft.commands.Commands;
|
| 14 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 15 | import net.minecraft.commands.arguments.ObjectiveArgument;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.server.level.ServerPlayer;
|
| 18 | import net.minecraft.world.scores.Objective;
|
| 19 | import net.minecraft.world.scores.ReadOnlyScoreInfo;
|
| 20 | import net.minecraft.world.scores.ScoreAccess;
|
| 21 | import net.minecraft.world.scores.ScoreHolder;
|
| 22 | import net.minecraft.world.scores.Scoreboard;
|
| 23 | import net.minecraft.world.scores.criteria.ObjectiveCriteria;
|
| 24 |
|
| 25 | public class TriggerCommand {
|
| 26 | private static final SimpleCommandExceptionType ERROR_NOT_PRIMED = new SimpleCommandExceptionType(Component.translatable("commands.trigger.failed.unprimed"));
|
| 27 | private static final SimpleCommandExceptionType ERROR_INVALID_OBJECTIVE = new SimpleCommandExceptionType(
|
| 28 | Component.translatable("commands.trigger.failed.invalid")
|
| 29 | );
|
| 30 |
|
| 31 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 32 | dispatcher.register(
|
| 33 | Commands.literal("trigger")
|
| 34 | .then(
|
| 35 | Commands.argument("objective", ObjectiveArgument.objective())
|
| 36 | .suggests((c, p) -> suggestObjectives(c.getSource(), p))
|
| 37 | .executes(c -> simpleTrigger(c.getSource(), c.getSource().getPlayerOrException(), ObjectiveArgument.getObjective(c, "objective")))
|
| 38 | .then(
|
| 39 | Commands.literal("add")
|
| 40 | .then(
|
| 41 | Commands.argument("value", IntegerArgumentType.integer())
|
| 42 | .executes(
|
| 43 | c -> addValue(
|
| 44 | c.getSource(), c.getSource().getPlayerOrException(), ObjectiveArgument.getObjective(c, "objective"), IntegerArgumentType.getInteger(c, "value")
|
| 45 | )
|
| 46 | )
|
| 47 | )
|
| 48 | )
|
| 49 | .then(
|
| 50 | Commands.literal("set")
|
| 51 | .then(
|
| 52 | Commands.argument("value", IntegerArgumentType.integer())
|
| 53 | .executes(
|
| 54 | c -> setValue(
|
| 55 | c.getSource(), c.getSource().getPlayerOrException(), ObjectiveArgument.getObjective(c, "objective"), IntegerArgumentType.getInteger(c, "value")
|
| 56 | )
|
| 57 | )
|
| 58 | )
|
| 59 | )
|
| 60 | )
|
| 61 | );
|
| 62 | }
|
| 63 |
|
| 64 | public static CompletableFuture<Suggestions> suggestObjectives(final CommandSourceStack source, final SuggestionsBuilder builder) {
|
| 65 | ScoreHolder entity = source.getEntity();
|
| 66 | List<String> result = Lists.newArrayList();
|
| 67 | if (entity != null) {
|
| 68 | Scoreboard scoreboard = source.getServer().getScoreboard();
|
| 69 |
|
| 70 | for (Objective objective : scoreboard.getObjectives()) {
|
| 71 | if (objective.getCriteria() == ObjectiveCriteria.TRIGGER) {
|
| 72 | ReadOnlyScoreInfo scoreInfo = scoreboard.getPlayerScoreInfo(entity, objective);
|
| 73 | if (scoreInfo != null && !scoreInfo.isLocked()) {
|
| 74 | result.add(objective.getName());
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | return SharedSuggestionProvider.suggest(result, builder);
|
| 81 | }
|
| 82 |
|
| 83 | private static int addValue(final CommandSourceStack source, final ServerPlayer player, final Objective objective, final int amount) throws CommandSyntaxException {
|
| 84 | ScoreAccess score = getScore(source.getServer().getScoreboard(), player, objective);
|
| 85 | int newValue = score.add(amount);
|
| 86 | source.sendSuccess(() -> Component.translatable("commands.trigger.add.success", objective.getFormattedDisplayName(), amount), true);
|
| 87 | return newValue;
|
| 88 | }
|
| 89 |
|
| 90 | private static int setValue(final CommandSourceStack source, final ServerPlayer player, final Objective objective, final int amount) throws CommandSyntaxException {
|
| 91 | ScoreAccess score = getScore(source.getServer().getScoreboard(), player, objective);
|
| 92 | score.set(amount);
|
| 93 | source.sendSuccess(() -> Component.translatable("commands.trigger.set.success", objective.getFormattedDisplayName(), amount), true);
|
| 94 | return amount;
|
| 95 | }
|
| 96 |
|
| 97 | private static int simpleTrigger(final CommandSourceStack source, final ServerPlayer player, final Objective objective) throws CommandSyntaxException {
|
| 98 | ScoreAccess score = getScore(source.getServer().getScoreboard(), player, objective);
|
| 99 | int newValue = score.add(1);
|
| 100 | source.sendSuccess(() -> Component.translatable("commands.trigger.simple.success", objective.getFormattedDisplayName()), true);
|
| 101 | return newValue;
|
| 102 | }
|
| 103 |
|
| 104 | private static ScoreAccess getScore(final Scoreboard scoreboard, final ScoreHolder scoreHolder, final Objective objective) throws CommandSyntaxException {
|
| 105 | if (objective.getCriteria() != ObjectiveCriteria.TRIGGER) {
|
| 106 | throw ERROR_INVALID_OBJECTIVE.create();
|
| 107 | } else {
|
| 108 | ReadOnlyScoreInfo scoreInfo = scoreboard.getPlayerScoreInfo(scoreHolder, objective);
|
| 109 | if (scoreInfo != null && !scoreInfo.isLocked()) {
|
| 110 | ScoreAccess score = scoreboard.getOrCreatePlayerScore(scoreHolder, objective);
|
| 111 | score.lock();
|
| 112 | return score;
|
| 113 | } else {
|
| 114 | throw ERROR_NOT_PRIMED.create();
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 |
|