GameRuleCommand .java

net.minecraft.server.commands.GameRuleCommand (class, 61 lines): members: ERROR_GAME_RULE_NOT_SET, gameRule, register. strings: "commands.gamerule.query".

net.minecraft.server.commandsclass61 linesserver3.1 KB

API Surface (2)

void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context)
<T> void visit(final GameRule<T> gameRule)

Fields & Constants (2)

ERROR_GAME_RULE_NOT_SET
gameRule

String literals (4)

"commands.gamerule.not_set"
"gamerule"
"commands.gamerule.set"
"commands.gamerule.query"

Full Source

GameRuleCommand.java61 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.context.CommandContext;
6
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
8
import net.minecraft.commands.CommandBuildContext;
9
import net.minecraft.commands.CommandSourceStack;
10
import net.minecraft.commands.Commands;
11
import net.minecraft.network.chat.Component;
12
import net.minecraft.world.level.gamerules.GameRule;
13
import net.minecraft.world.level.gamerules.GameRuleTypeVisitor;
14
import net.minecraft.world.level.gamerules.GameRules;
15
16
public class GameRuleCommand {
17
	private static final Dynamic2CommandExceptionType ERROR_GAME_RULE_NOT_SET = new Dynamic2CommandExceptionType(
18
		(gameRule, value) -> Component.translatableEscape("commands.gamerule.not_set", gameRule, value)
19
	);
20
21
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
22
		final LiteralArgumentBuilder<CommandSourceStack> base = Commands.literal("gamerule").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS));
23
		new GameRules(context.enabledFeatures()).visitGameRuleTypes(new GameRuleTypeVisitor() {
24
			@Override
25
			public <T> void visit(final GameRule<T> gameRule) {
26
				LiteralArgumentBuilder<CommandSourceStack> unqualified = Commands.literal(gameRule.id());
27
				LiteralArgumentBuilder<CommandSourceStack> qualified = Commands.literal(gameRule.getIdentifier().toString());
28
				base.then(GameRuleCommand.buildRuleArguments(gameRule, unqualified)).then(GameRuleCommand.buildRuleArguments(gameRule, qualified));
29
			}
30
		});
31
		dispatcher.register(base);
32
	}
33
34
	private static <T> LiteralArgumentBuilder<CommandSourceStack> buildRuleArguments(
35
		final GameRule<T> gameRule, final LiteralArgumentBuilder<CommandSourceStack> ruleLiteral
36
	) {
37
		return ruleLiteral.executes(c -> queryRule(c.getSource(), gameRule))
38
			.then(Commands.argument("value", gameRule.argument()).executes(c -> setRule(c, gameRule)));
39
	}
40
41
	private static <T> int setRule(final CommandContext<CommandSourceStack> context, final GameRule<T> gameRule) throws CommandSyntaxException {
42
		CommandSourceStack source = context.getSource();
43
		T value = context.getArgument("value", gameRule.valueClass());
44
		GameRules gameRules = source.getLevel().getGameRules();
45
		String seralizedValue = gameRule.serialize(value);
46
		if (gameRules.get(gameRule).equals(value)) {
47
			throw ERROR_GAME_RULE_NOT_SET.create(gameRule, seralizedValue);
48
		}
49
50
		gameRules.set(gameRule, value, context.getSource().getServer());
51
		source.sendSuccess(() -> Component.translatable("commands.gamerule.set", gameRule.id(), seralizedValue), true);
52
		return gameRule.getCommandResult(value);
53
	}
54
55
	private static <T> int queryRule(final CommandSourceStack source, final GameRule<T> gameRule) {
56
		T value = source.getLevel().getGameRules().get(gameRule);
57
		source.sendSuccess(() -> Component.translatable("commands.gamerule.query", gameRule.id(), gameRule.serialize(value)), false);
58
		return gameRule.getCommandResult(value);
59
	}
60
}
61

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