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 cq, final SuggestionsBuilder suggestionsBuilder) {
        final Entity akq3 = cq.getEntity();
        final List<String> list4 = Lists.newArrayList();
        if (akq3 != null) {
            final Scoreboard cwl5 = cq.getServer().getScoreboard();
            final String string6 = akq3.getScoreboardName();
            for (final Objective cwi8 : cwl5.getObjectives()) {
                if (cwi8.getCriteria() == ObjectiveCriteria.TRIGGER && cwl5.hasPlayerScore(string6, cwi8)) {
                    final Score cwk9 = cwl5.getOrCreatePlayerScore(string6, cwi8);
                    if (cwk9.isLocked()) {
                        continue;
                    }
                    list4.add(cwi8.getName());
                }
            }
        }
        return SharedSuggestionProvider.suggest(list4, suggestionsBuilder);
    }
    
    private static int addValue(final CommandSourceStack cq, final Score cwk, final int integer) {
        cwk.add(integer);
        cq.sendSuccess(new TranslatableComponent("commands.trigger.add.success", new Object[] { cwk.getObjective().getFormattedDisplayName(), integer }), true);
        return cwk.getScore();
    }
    
    private static int setValue(final CommandSourceStack cq, final Score cwk, final int integer) {
        cwk.setScore(integer);
        cq.sendSuccess(new TranslatableComponent("commands.trigger.set.success", new Object[] { cwk.getObjective().getFormattedDisplayName(), integer }), true);
        return integer;
    }
    
    private static int simpleTrigger(final CommandSourceStack cq, final Score cwk) {
        cwk.add(1);
        cq.sendSuccess(new TranslatableComponent("commands.trigger.simple.success", new Object[] { cwk.getObjective().getFormattedDisplayName() }), true);
        return cwk.getScore();
    }
    
    private static Score getScore(final ServerPlayer xe, final Objective cwi) throws CommandSyntaxException {
        if (cwi.getCriteria() != ObjectiveCriteria.TRIGGER) {
            throw TriggerCommand.ERROR_INVALID_OBJECTIVE.create();
        }
        final Scoreboard cwl3 = xe.getScoreboard();
        final String string4 = xe.getScoreboardName();
        if (!cwl3.hasPlayerScore(string4, cwi)) {
            throw TriggerCommand.ERROR_NOT_PRIMED.create();
        }
        final Score cwk5 = cwl3.getOrCreatePlayerScore(string4, cwi);
        if (cwk5.isLocked()) {
            throw TriggerCommand.ERROR_NOT_PRIMED.create();
        }
        cwk5.setLocked(true);
        return cwk5;
    }
    
    static {
        ERROR_NOT_PRIMED = new SimpleCommandExceptionType((Message)new TranslatableComponent("commands.trigger.failed.unprimed", new Object[0]));
        ERROR_INVALID_OBJECTIVE = new SimpleCommandExceptionType((Message)new TranslatableComponent("commands.trigger.failed.invalid", new Object[0]));
    }
}
