| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 7 | import net.minecraft.commands.CommandSourceStack;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.server.MinecraftServer;
|
| 11 | import net.minecraft.world.Difficulty;
|
| 12 |
|
| 13 | public class DifficultyCommand {
|
| 14 | private static final DynamicCommandExceptionType ERROR_ALREADY_SAME_DIFFICULTY = new DynamicCommandExceptionType(
|
| 15 | difficulty -> Component.translatableEscape("commands.difficulty.failure", difficulty)
|
| 16 | );
|
| 17 |
|
| 18 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 19 | LiteralArgumentBuilder<CommandSourceStack> command = Commands.literal("difficulty");
|
| 20 |
|
| 21 | for (Difficulty difficulty : Difficulty.values()) {
|
| 22 | command.then(Commands.literal(difficulty.getSerializedName()).executes(c -> setDifficulty(c.getSource(), difficulty)));
|
| 23 | }
|
| 24 |
|
| 25 | dispatcher.register(command.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS)).executes(c -> {
|
| 26 | Difficulty difficultyx = c.getSource().getLevel().getDifficulty();
|
| 27 | c.getSource().sendSuccess(() -> Component.translatable("commands.difficulty.query", difficultyx.getDisplayName()), false);
|
| 28 | return difficultyx.getId();
|
| 29 | }));
|
| 30 | }
|
| 31 |
|
| 32 | public static int setDifficulty(final CommandSourceStack source, final Difficulty difficulty) throws CommandSyntaxException {
|
| 33 | MinecraftServer server = source.getServer();
|
| 34 | if (server.getWorldData().getDifficulty() == difficulty) {
|
| 35 | throw ERROR_ALREADY_SAME_DIFFICULTY.create(difficulty.getDisplayName());
|
| 36 | }
|
| 37 |
|
| 38 | server.setDifficulty(difficulty, true);
|
| 39 | source.sendSuccess(() -> Component.translatable("commands.difficulty.success", difficulty.getDisplayName()), true);
|
| 40 | return 0;
|
| 41 | }
|
| 42 | }
|
| 43 |
|