51 lines
2.6 KiB
Java
51 lines
2.6 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.brigadier.CommandDispatcher
|
|
* com.mojang.brigadier.builder.LiteralArgumentBuilder
|
|
* com.mojang.brigadier.context.CommandContext
|
|
*/
|
|
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.GameRule;
|
|
import net.minecraft.world.level.gamerules.GameRuleTypeVisitor;
|
|
import net.minecraft.world.level.gamerules.GameRules;
|
|
|
|
public class GameRuleCommand {
|
|
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
|
|
final LiteralArgumentBuilder base = (LiteralArgumentBuilder)Commands.literal("gamerule").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS));
|
|
new GameRules(context.enabledFeatures()).visitGameRuleTypes(new GameRuleTypeVisitor(){
|
|
|
|
@Override
|
|
public <T> void visit(GameRule<T> gameRule) {
|
|
LiteralArgumentBuilder<CommandSourceStack> rule = Commands.literal(gameRule.id());
|
|
base.then(((LiteralArgumentBuilder)rule.executes(c -> GameRuleCommand.queryRule((CommandSourceStack)c.getSource(), gameRule))).then(Commands.argument("value", gameRule.argument()).executes(c -> GameRuleCommand.setRule((CommandContext<CommandSourceStack>)c, gameRule))));
|
|
}
|
|
});
|
|
dispatcher.register(base);
|
|
}
|
|
|
|
private static <T> int setRule(CommandContext<CommandSourceStack> context, GameRule<T> gameRule) {
|
|
CommandSourceStack source = (CommandSourceStack)context.getSource();
|
|
Object value = context.getArgument("value", gameRule.valueClass());
|
|
source.getLevel().getGameRules().set(gameRule, value, ((CommandSourceStack)context.getSource()).getServer());
|
|
source.sendSuccess(() -> Component.translatable("commands.gamerule.set", gameRule.id(), gameRule.serialize(value)), true);
|
|
return gameRule.getCommandResult(value);
|
|
}
|
|
|
|
private static <T> int queryRule(CommandSourceStack source, GameRule<T> gameRule) {
|
|
Object value = source.getLevel().getGameRules().get(gameRule);
|
|
source.sendSuccess(() -> Component.translatable("commands.gamerule.query", gameRule.id(), gameRule.serialize(value)), false);
|
|
return gameRule.getCommandResult(value);
|
|
}
|
|
}
|
|
|