DifficultyCommand .java

net.minecraft.server.commands.DifficultyCommand (class, 43 lines): members: ERROR_ALREADY_SAME_DIFFICULTY, setDifficulty, register. strings: "difficulty".

net.minecraft.server.commandsclass43 linesserver1.9 KB

API Surface (2)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)
int setDifficulty(final CommandSourceStack source, final Difficulty difficulty)

Fields & Constants (1)

ERROR_ALREADY_SAME_DIFFICULTY

String literals (4)

"commands.difficulty.failure"
"difficulty"
"commands.difficulty.query"
"commands.difficulty.success"

Code Graph: Dependencies (5)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

DifficultyCommand.java43 lines · Minecraft 26.3 · verbatim
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

Decompiled Minecraft 26.3 source of DifficultyCommand (net/minecraft/server/commands/DifficultyCommand.java) with Mojang official mappings, stored verbatim on the site.