package net.minecraft.command;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.scoreboard.Score;
import net.minecraft.scoreboard.ScoreObjective;
import net.minecraft.scoreboard.Scoreboard;

public class CommandResultStats
{
    private static final int field_179676_a = CommandResultStats.Type.values().length;
    private static final String[] field_179674_b = new String[field_179676_a];
    private String[] field_179675_c;
    private String[] field_179673_d;
    private static final String __OBFID = "CL_00002364";

    public CommandResultStats()
    {
        this.field_179675_c = field_179674_b;
        this.field_179673_d = field_179674_b;
    }

    public void func_179672_a(ICommandSender p_179672_1_, CommandResultStats.Type p_179672_2_, int p_179672_3_)
    {
        String s = this.field_179675_c[p_179672_2_.func_179636_a()];

        if (s != null)
        {
            String s1;

            try
            {
                s1 = CommandBase.func_175758_e(p_179672_1_, s);
            }
            catch (EntityNotFoundException entitynotfoundexception)
            {
                return;
            }

            String s2 = this.field_179673_d[p_179672_2_.func_179636_a()];

            if (s2 != null)
            {
                Scoreboard scoreboard = p_179672_1_.getEntityWorld().getScoreboard();
                ScoreObjective scoreobjective = scoreboard.getObjective(s2);

                if (scoreobjective != null)
                {
                    if (scoreboard.entityHasObjective(s1, scoreobjective))
                    {
                        Score score = scoreboard.getValueFromObjective(s1, scoreobjective);
                        score.setScorePoints(p_179672_3_);
                    }
                }
            }
        }
    }

    public void func_179668_a(NBTTagCompound p_179668_1_)
    {
        if (p_179668_1_.hasKey("CommandStats", 10))
        {
            NBTTagCompound nbttagcompound1 = p_179668_1_.getCompoundTag("CommandStats");
            CommandResultStats.Type[] atype = CommandResultStats.Type.values();
            int i = atype.length;

            for (int j = 0; j < i; ++j)
            {
                CommandResultStats.Type type = atype[j];
                String s = type.func_179637_b() + "Name";
                String s1 = type.func_179637_b() + "Objective";

                if (nbttagcompound1.hasKey(s, 8) && nbttagcompound1.hasKey(s1, 8))
                {
                    String s2 = nbttagcompound1.getString(s);
                    String s3 = nbttagcompound1.getString(s1);
                    func_179667_a(this, type, s2, s3);
                }
            }
        }
    }

    public void func_179670_b(NBTTagCompound p_179670_1_)
    {
        NBTTagCompound nbttagcompound1 = new NBTTagCompound();
        CommandResultStats.Type[] atype = CommandResultStats.Type.values();
        int i = atype.length;

        for (int j = 0; j < i; ++j)
        {
            CommandResultStats.Type type = atype[j];
            String s = this.field_179675_c[type.func_179636_a()];
            String s1 = this.field_179673_d[type.func_179636_a()];

            if (s != null && s1 != null)
            {
                nbttagcompound1.setString(type.func_179637_b() + "Name", s);
                nbttagcompound1.setString(type.func_179637_b() + "Objective", s1);
            }
        }

        if (!nbttagcompound1.hasNoTags())
        {
            p_179670_1_.setTag("CommandStats", nbttagcompound1);
        }
    }

    public static void func_179667_a(CommandResultStats p_179667_0_, CommandResultStats.Type p_179667_1_, String p_179667_2_, String p_179667_3_)
    {
        if (p_179667_2_ != null && p_179667_2_.length() != 0 && p_179667_3_ != null && p_179667_3_.length() != 0)
        {
            if (p_179667_0_.field_179675_c == field_179674_b || p_179667_0_.field_179673_d == field_179674_b)
            {
                p_179667_0_.field_179675_c = new String[field_179676_a];
                p_179667_0_.field_179673_d = new String[field_179676_a];
            }

            p_179667_0_.field_179675_c[p_179667_1_.func_179636_a()] = p_179667_2_;
            p_179667_0_.field_179673_d[p_179667_1_.func_179636_a()] = p_179667_3_;
        }
        else
        {
            func_179669_a(p_179667_0_, p_179667_1_);
        }
    }

    private static void func_179669_a(CommandResultStats p_179669_0_, CommandResultStats.Type p_179669_1_)
    {
        if (p_179669_0_.field_179675_c != field_179674_b && p_179669_0_.field_179673_d != field_179674_b)
        {
            p_179669_0_.field_179675_c[p_179669_1_.func_179636_a()] = null;
            p_179669_0_.field_179673_d[p_179669_1_.func_179636_a()] = null;
            boolean flag = true;
            CommandResultStats.Type[] atype = CommandResultStats.Type.values();
            int i = atype.length;

            for (int j = 0; j < i; ++j)
            {
                CommandResultStats.Type type = atype[j];

                if (p_179669_0_.field_179675_c[type.func_179636_a()] != null && p_179669_0_.field_179673_d[type.func_179636_a()] != null)
                {
                    flag = false;
                    break;
                }
            }

            if (flag)
            {
                p_179669_0_.field_179675_c = field_179674_b;
                p_179669_0_.field_179673_d = field_179674_b;
            }
        }
    }

    public void func_179671_a(CommandResultStats p_179671_1_)
    {
        CommandResultStats.Type[] atype = CommandResultStats.Type.values();
        int i = atype.length;

        for (int j = 0; j < i; ++j)
        {
            CommandResultStats.Type type = atype[j];
            func_179667_a(this, type, p_179671_1_.field_179675_c[type.func_179636_a()], p_179671_1_.field_179673_d[type.func_179636_a()]);
        }
    }

    public static enum Type
    {
        SUCCESS_COUNT(0, "SuccessCount"),
        AFFECTED_BLOCKS(1, "AffectedBlocks"),
        AFFECTED_ENTITIES(2, "AffectedEntities"),
        AFFECTED_ITEMS(3, "AffectedItems"),
        QUERY_RESULT(4, "QueryResult");
        final int field_179639_f;
        final String field_179640_g;

        private static final String __OBFID = "CL_00002363";

        private Type(int p_i46050_3_, String p_i46050_4_)
        {
            this.field_179639_f = p_i46050_3_;
            this.field_179640_g = p_i46050_4_;
        }

        public int func_179636_a()
        {
            return this.field_179639_f;
        }

        public String func_179637_b()
        {
            return this.field_179640_g;
        }

        public static String[] func_179634_c()
        {
            String[] astring = new String[values().length];
            int i = 0;
            CommandResultStats.Type[] atype = values();
            int j = atype.length;

            for (int k = 0; k < j; ++k)
            {
                CommandResultStats.Type type = atype[k];
                astring[i++] = type.func_179637_b();
            }

            return astring;
        }

        public static CommandResultStats.Type func_179635_a(String p_179635_0_)
        {
            CommandResultStats.Type[] atype = values();
            int i = atype.length;

            for (int j = 0; j < i; ++j)
            {
                CommandResultStats.Type type = atype[j];

                if (type.func_179637_b().equals(p_179635_0_))
                {
                    return type;
                }
            }

            return null;
        }
    }
}