package net.minecraft.server.commands; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.network.chat.Component; import net.minecraft.world.level.GameRules; public class GameRuleCommand { public static void register(CommandDispatcher dispatcher, CommandBuildContext commandBuildContext) { final LiteralArgumentBuilder literalArgumentBuilder = Commands.literal("gamerule") .requires(commandSourceStack -> commandSourceStack.hasPermission(2)); new GameRules(commandBuildContext.enabledFeatures()) .visitGameRuleTypes( new GameRules.GameRuleTypeVisitor() { @Override public > void visit(GameRules.Key key, GameRules.Type type) { LiteralArgumentBuilder literalArgumentBuilderx = Commands.literal(key.getId()); literalArgumentBuilder.then( literalArgumentBuilderx.executes(commandContext -> GameRuleCommand.queryRule(commandContext.getSource(), key)) .then(type.createArgument("value").executes(commandContext -> GameRuleCommand.setRule(commandContext, key))) ); } } ); dispatcher.register(literalArgumentBuilder); } static > int setRule(CommandContext source, GameRules.Key gameRule) { CommandSourceStack commandSourceStack = source.getSource(); T value = commandSourceStack.getServer().getGameRules().getRule(gameRule); value.setFromArgument(source, "value"); commandSourceStack.sendSuccess(() -> Component.translatable("commands.gamerule.set", gameRule.getId(), value.toString()), true); return value.getCommandResult(); } static > int queryRule(CommandSourceStack source, GameRules.Key gameRule) { T value = source.getServer().getGameRules().getRule(gameRule); source.sendSuccess(() -> Component.translatable("commands.gamerule.query", gameRule.getId(), value.toString()), false); return value.getCommandResult(); } }