package net.minecraft.server.commands;

import com.mojang.brigadier.Message;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.MinecraftServer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.world.Difficulty;
import net.minecraft.commands.Commands;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;

public class DifficultyCommand {
    private static final DynamicCommandExceptionType ERROR_ALREADY_DIFFICULT;
    
    public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
        final LiteralArgumentBuilder<CommandSourceStack> literalArgumentBuilder2 = Commands.literal("difficulty");
        for (final Difficulty and6 : Difficulty.values()) {
            literalArgumentBuilder2.then(Commands.literal(and6.getKey()).executes(commandContext -> setDifficulty((CommandSourceStack)commandContext.getSource(), and6)));
        }
        commandDispatcher.register((LiteralArgumentBuilder)((LiteralArgumentBuilder)literalArgumentBuilder2.requires(cz -> cz.hasPermission(2))).executes(commandContext -> {
            final Difficulty and2 = ((CommandSourceStack)commandContext.getSource()).getLevel().getDifficulty();
            ((CommandSourceStack)commandContext.getSource()).sendSuccess(new TranslatableComponent("commands.difficulty.query", new Object[] { and2.getDisplayName() }), false);
            return and2.getId();
        }));
    }
    
    public static int setDifficulty(final CommandSourceStack cz, final Difficulty and) throws CommandSyntaxException {
        final MinecraftServer minecraftServer3 = cz.getServer();
        if (minecraftServer3.getWorldData().getDifficulty() == and) {
            throw DifficultyCommand.ERROR_ALREADY_DIFFICULT.create(and.getKey());
        }
        minecraftServer3.setDifficulty(and, true);
        cz.sendSuccess(new TranslatableComponent("commands.difficulty.success", new Object[] { and.getDisplayName() }), true);
        return 0;
    }
    
    static {
        final TranslatableComponent translatableComponent;
        ERROR_ALREADY_DIFFICULT = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("commands.difficulty.failure", new Object[] { object });
            return translatableComponent;
        });
    }
}
