| 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.BoolArgumentType;
|
| 6 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 7 | import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
| 8 | import com.mojang.brigadier.context.CommandContext;
|
| 9 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 10 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 11 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 12 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 13 | import java.util.List;
|
| 14 | import java.util.concurrent.CompletableFuture;
|
| 15 | import net.minecraft.advancements.predicates.MinMaxBounds;
|
| 16 | import net.minecraft.commands.CommandSourceStack;
|
| 17 | import net.minecraft.commands.Commands;
|
| 18 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 19 | import net.minecraft.commands.arguments.IdentifierArgument;
|
| 20 | import net.minecraft.commands.arguments.RangeArgument;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import net.minecraft.resources.Identifier;
|
| 23 | import net.minecraft.server.level.ServerLevel;
|
| 24 | import net.minecraft.util.Mth;
|
| 25 | import net.minecraft.util.RandomSource;
|
| 26 | import net.minecraft.world.RandomSequences;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | public class RandomCommand {
|
| 30 | private static final SimpleCommandExceptionType ERROR_RANGE_TOO_LARGE = new SimpleCommandExceptionType(
|
| 31 | Component.translatable("commands.random.error.range_too_large")
|
| 32 | );
|
| 33 | private static final SimpleCommandExceptionType ERROR_RANGE_TOO_SMALL = new SimpleCommandExceptionType(
|
| 34 | Component.translatable("commands.random.error.range_too_small")
|
| 35 | );
|
| 36 |
|
| 37 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 38 | dispatcher.register(
|
| 39 | Commands.literal("random")
|
| 40 | .then(drawRandomValueTree("value", false))
|
| 41 | .then(drawRandomValueTree("roll", true))
|
| 42 | .then(
|
| 43 | Commands.literal("reset")
|
| 44 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 45 | .then(
|
| 46 | Commands.literal("*")
|
| 47 | .executes(c -> resetAllSequences(c.getSource()))
|
| 48 | .then(
|
| 49 | Commands.argument("seed", IntegerArgumentType.integer())
|
| 50 | .executes(c -> resetAllSequencesAndSetNewDefaults(c.getSource(), IntegerArgumentType.getInteger(c, "seed"), true, true))
|
| 51 | .then(
|
| 52 | Commands.argument("includeWorldSeed", BoolArgumentType.bool())
|
| 53 | .executes(
|
| 54 | c -> resetAllSequencesAndSetNewDefaults(
|
| 55 | c.getSource(), IntegerArgumentType.getInteger(c, "seed"), BoolArgumentType.getBool(c, "includeWorldSeed"), true
|
| 56 | )
|
| 57 | )
|
| 58 | .then(
|
| 59 | Commands.argument("includeSequenceId", BoolArgumentType.bool())
|
| 60 | .executes(
|
| 61 | c -> resetAllSequencesAndSetNewDefaults(
|
| 62 | c.getSource(),
|
| 63 | IntegerArgumentType.getInteger(c, "seed"),
|
| 64 | BoolArgumentType.getBool(c, "includeWorldSeed"),
|
| 65 | BoolArgumentType.getBool(c, "includeSequenceId")
|
| 66 | )
|
| 67 | )
|
| 68 | )
|
| 69 | )
|
| 70 | )
|
| 71 | )
|
| 72 | .then(
|
| 73 | Commands.argument("sequence", IdentifierArgument.id())
|
| 74 | .suggests(RandomCommand::suggestRandomSequence)
|
| 75 | .executes(c -> resetSequence(c.getSource(), IdentifierArgument.getId(c, "sequence")))
|
| 76 | .then(
|
| 77 | Commands.argument("seed", IntegerArgumentType.integer())
|
| 78 | .executes(c -> resetSequence(c.getSource(), IdentifierArgument.getId(c, "sequence"), IntegerArgumentType.getInteger(c, "seed"), true, true))
|
| 79 | .then(
|
| 80 | Commands.argument("includeWorldSeed", BoolArgumentType.bool())
|
| 81 | .executes(
|
| 82 | c -> resetSequence(
|
| 83 | c.getSource(),
|
| 84 | IdentifierArgument.getId(c, "sequence"),
|
| 85 | IntegerArgumentType.getInteger(c, "seed"),
|
| 86 | BoolArgumentType.getBool(c, "includeWorldSeed"),
|
| 87 | true
|
| 88 | )
|
| 89 | )
|
| 90 | .then(
|
| 91 | Commands.argument("includeSequenceId", BoolArgumentType.bool())
|
| 92 | .executes(
|
| 93 | c -> resetSequence(
|
| 94 | c.getSource(),
|
| 95 | IdentifierArgument.getId(c, "sequence"),
|
| 96 | IntegerArgumentType.getInteger(c, "seed"),
|
| 97 | BoolArgumentType.getBool(c, "includeWorldSeed"),
|
| 98 | BoolArgumentType.getBool(c, "includeSequenceId")
|
| 99 | )
|
| 100 | )
|
| 101 | )
|
| 102 | )
|
| 103 | )
|
| 104 | )
|
| 105 | )
|
| 106 | );
|
| 107 | }
|
| 108 |
|
| 109 | private static LiteralArgumentBuilder<CommandSourceStack> drawRandomValueTree(final String name, final boolean announce) {
|
| 110 | return Commands.literal(name)
|
| 111 | .then(
|
| 112 | Commands.argument("range", RangeArgument.intRange())
|
| 113 | .executes(c -> randomSample(c.getSource(), RangeArgument.Ints.getRange(c, "range"), null, announce))
|
| 114 | .then(
|
| 115 | Commands.argument("sequence", IdentifierArgument.id())
|
| 116 | .suggests(RandomCommand::suggestRandomSequence)
|
| 117 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 118 | .executes(c -> randomSample(c.getSource(), RangeArgument.Ints.getRange(c, "range"), IdentifierArgument.getId(c, "sequence"), announce))
|
| 119 | )
|
| 120 | );
|
| 121 | }
|
| 122 |
|
| 123 | private static CompletableFuture<Suggestions> suggestRandomSequence(final CommandContext<CommandSourceStack> context, final SuggestionsBuilder builder) {
|
| 124 | List<String> result = Lists.newArrayList();
|
| 125 | context.getSource().getServer().getRandomSequences().forAllSequences((key, sequence) -> result.add(key.toString()));
|
| 126 | return SharedSuggestionProvider.suggest(result, builder);
|
| 127 | }
|
| 128 |
|
| 129 | private static int randomSample(final CommandSourceStack source, final MinMaxBounds.Ints range, final @Nullable Identifier sequence, final boolean announce) throws CommandSyntaxException {
|
| 130 | RandomSource random;
|
| 131 | if (sequence != null) {
|
| 132 | random = source.getServer().getRandomSequence(sequence);
|
| 133 | } else {
|
| 134 | random = source.getLevel().getRandom();
|
| 135 | }
|
| 136 |
|
| 137 | int min = range.min().orElse(Integer.MIN_VALUE);
|
| 138 | int max = range.max().orElse(Integer.MAX_VALUE);
|
| 139 | long span = (long)max - min;
|
| 140 | if (span == 0L) {
|
| 141 | throw ERROR_RANGE_TOO_SMALL.create();
|
| 142 | }
|
| 143 |
|
| 144 | if (span >= 2147483647L) {
|
| 145 | throw ERROR_RANGE_TOO_LARGE.create();
|
| 146 | }
|
| 147 |
|
| 148 | int value = Mth.randomBetweenInclusive(random, min, max);
|
| 149 | if (announce) {
|
| 150 | source.getServer().getPlayerList().broadcastSystemMessage(Component.translatable("commands.random.roll", source.getDisplayName(), value, min, max), false);
|
| 151 | } else {
|
| 152 | source.sendSuccess(() -> Component.translatable("commands.random.sample.success", value), false);
|
| 153 | }
|
| 154 |
|
| 155 | return value;
|
| 156 | }
|
| 157 |
|
| 158 | private static int resetSequence(final CommandSourceStack source, final Identifier sequence) throws CommandSyntaxException {
|
| 159 | ServerLevel level = source.getLevel();
|
| 160 | source.getServer().getRandomSequences().reset(sequence, level.getSeed());
|
| 161 | source.sendSuccess(() -> Component.translatable("commands.random.reset.success", Component.translationArg(sequence)), false);
|
| 162 | return 1;
|
| 163 | }
|
| 164 |
|
| 165 | private static int resetSequence(
|
| 166 | final CommandSourceStack source, final Identifier sequence, final int salt, final boolean includeWorldSeed, final boolean includeSequenceId
|
| 167 | ) throws CommandSyntaxException {
|
| 168 | ServerLevel level = source.getLevel();
|
| 169 | source.getServer().getRandomSequences().reset(sequence, level.getSeed(), salt, includeWorldSeed, includeSequenceId);
|
| 170 | source.sendSuccess(() -> Component.translatable("commands.random.reset.success", Component.translationArg(sequence)), false);
|
| 171 | return 1;
|
| 172 | }
|
| 173 |
|
| 174 | private static int resetAllSequences(final CommandSourceStack source) {
|
| 175 | int count = source.getServer().getRandomSequences().clear();
|
| 176 | source.sendSuccess(() -> Component.translatable("commands.random.reset.all.success", count), false);
|
| 177 | return count;
|
| 178 | }
|
| 179 |
|
| 180 | private static int resetAllSequencesAndSetNewDefaults(
|
| 181 | final CommandSourceStack source, final int salt, final boolean includeWorldSeed, final boolean includeSequenceId
|
| 182 | ) {
|
| 183 | RandomSequences randomSequences = source.getServer().getRandomSequences();
|
| 184 | randomSequences.setSeedDefaults(salt, includeWorldSeed, includeSequenceId);
|
| 185 | int count = randomSequences.clear();
|
| 186 | source.sendSuccess(() -> Component.translatable("commands.random.reset.all.success", count), false);
|
| 187 | return count;
|
| 188 | }
|
| 189 | }
|
| 190 |
|