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.level.ServerPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.scores.Score;
import java.util.Iterator;
import net.minecraft.world.scores.Scoreboard;
import java.util.List;
import net.minecraft.world.entity.Entity;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.world.scores.criteria.ObjectiveCriteria;
import net.minecraft.world.scores.Objective;
import com.google.common.collect.Lists;
import com.mojang.brigadier.suggestion.Suggestions;
import java.util.concurrent.CompletableFuture;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.ArgumentType;
import net.minecraft.commands.arguments.ObjectiveArgument;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import net.minecraft.commands.Commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;

public class TriggerCommand {
    private static final SimpleCommandExceptionType ERROR_NOT_PRIMED;
    private static final SimpleCommandExceptionType ERROR_INVALID_OBJECTIVE;
    
    public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
        commandDispatcher.register((LiteralArgumentBuilder)Commands.literal("trigger").then(((RequiredArgumentBuilder)((RequiredArgumentBuilder)Commands.argument("objective", (com.mojang.brigadier.arguments.ArgumentType<Object>)ObjectiveArgument.objective()).suggests((commandContext, suggestionsBuilder) -> suggestObjectives((CommandSourceStack)commandContext.getSource(), suggestionsBuilder)).executes(commandContext -> simpleTrigger((CommandSourceStack)commandContext.getSource(), getScore(((CommandSourceStack)commandContext.getSource()).getPlayerOrException(), ObjectiveArgument.getObjective((CommandContext<CommandSourceStack>)commandContext, "objective"))))).then(Commands.literal("add").then(Commands.argument("value", (com.mojang.brigadier.arguments.ArgumentType<Object>)IntegerArgumentType.integer()).executes(commandContext -> addValue((CommandSourceStack)commandContext.getSource(), getScore(((CommandSourceStack)commandContext.getSource()).getPlayerOrException(), ObjectiveArgument.getObjective((CommandContext<CommandSourceStack>)commandContext, "objective")), IntegerArgumentType.getInteger(commandContext, "value")))))).then(Commands.literal("set").then(Commands.argument("value", (com.mojang.brigadier.arguments.ArgumentType<Object>)IntegerArgumentType.integer()).executes(commandContext -> setValue((CommandSourceStack)commandContext.getSource(), getScore(((CommandSourceStack)commandContext.getSource()).getPlayerOrException(), ObjectiveArgument.getObjective((CommandContext<CommandSourceStack>)commandContext, "objective")), IntegerArgumentType.getInteger(commandContext, "value")))))));
    }
    
    public static CompletableFuture<Suggestions> suggestObjectives(final CommandSourceStack cz, final SuggestionsBuilder suggestionsBuilder) {
        final Entity aom3 = cz.getEntity();
        final List<String> list4 = Lists.newArrayList();
        if (aom3 != null) {
            final Scoreboard dfm5 = cz.getServer().getScoreboard();
            final String string6 = aom3.getScoreboardName();
            for (final Objective dfj8 : dfm5.getObjectives()) {
                if (dfj8.getCriteria() == ObjectiveCriteria.TRIGGER && dfm5.hasPlayerScore(string6, dfj8)) {
                    final Score dfl9 = dfm5.getOrCreatePlayerScore(string6, dfj8);
                    if (dfl9.isLocked()) {
                        continue;
                    }
                    list4.add(dfj8.getName());
                }
            }
        }
        return SharedSuggestionProvider.suggest(list4, suggestionsBuilder);
    }
    
    private static int addValue(final CommandSourceStack cz, final Score dfl, final int integer) {
        dfl.add(integer);
        cz.sendSuccess(new TranslatableComponent("commands.trigger.add.success", new Object[] { dfl.getObjective().getFormattedDisplayName(), integer }), true);
        return dfl.getScore();
    }
    
    private static int setValue(final CommandSourceStack cz, final Score dfl, final int integer) {
        dfl.setScore(integer);
        cz.sendSuccess(new TranslatableComponent("commands.trigger.set.success", new Object[] { dfl.getObjective().getFormattedDisplayName(), integer }), true);
        return integer;
    }
    
    private static int simpleTrigger(final CommandSourceStack cz, final Score dfl) {
        dfl.add(1);
        cz.sendSuccess(new TranslatableComponent("commands.trigger.simple.success", new Object[] { dfl.getObjective().getFormattedDisplayName() }), true);
        return dfl.getScore();
    }
    
    private static Score getScore(final ServerPlayer ze, final Objective dfj) throws CommandSyntaxException {
        if (dfj.getCriteria() != ObjectiveCriteria.TRIGGER) {
            throw TriggerCommand.ERROR_INVALID_OBJECTIVE.create();
        }
        final Scoreboard dfm3 = ze.getScoreboard();
        final String string4 = ze.getScoreboardName();
        if (!dfm3.hasPlayerScore(string4, dfj)) {
            throw TriggerCommand.ERROR_NOT_PRIMED.create();
        }
        final Score dfl5 = dfm3.getOrCreatePlayerScore(string4, dfj);
        if (dfl5.isLocked()) {
            throw TriggerCommand.ERROR_NOT_PRIMED.create();
        }
        dfl5.setLocked(true);
        return dfl5;
    }
    
    static {
        ERROR_NOT_PRIMED = new SimpleCommandExceptionType((Message)new TranslatableComponent("commands.trigger.failed.unprimed"));
        ERROR_INVALID_OBJECTIVE = new SimpleCommandExceptionType((Message)new TranslatableComponent("commands.trigger.failed.invalid"));
    }
}
