package common.nbt;

public final class NBTUtil
{
    public static boolean compareTags(NBTBase tag1, NBTBase tag2, boolean lists)
    {
        if (tag1 == tag2)
        {
            return true;
        }
        else if (tag1 == null)
        {
            return true;
        }
        else if (tag2 == null)
        {
            return false;
        }
        else if (!tag1.getClass().equals(tag2.getClass()))
        {
            return false;
        }
        else if (tag1 instanceof NBTTagCompound)
        {
            NBTTagCompound nbttagcompound = (NBTTagCompound)tag1;
            NBTTagCompound nbttagcompound1 = (NBTTagCompound)tag2;

            for (String s : nbttagcompound.getKeySet())
            {
                NBTBase nbtbase1 = nbttagcompound.getTag(s);

                if (!compareTags(nbtbase1, nbttagcompound1.getTag(s), lists))
                {
                    return false;
                }
            }

            return true;
        }
        else if (tag1 instanceof NBTTagList && lists)
        {
            NBTTagList nbttaglist = (NBTTagList)tag1;
            NBTTagList nbttaglist1 = (NBTTagList)tag2;

            if (nbttaglist.tagCount() == 0)
            {
                return nbttaglist1.tagCount() == 0;
            }
            else
            {
                for (int i = 0; i < nbttaglist.tagCount(); ++i)
                {
                    NBTBase nbtbase = nbttaglist.get(i);
                    boolean flag = false;

                    for (int j = 0; j < nbttaglist1.tagCount(); ++j)
                    {
                        if (compareTags(nbtbase, nbttaglist1.get(j), lists))
                        {
                            flag = true;
                            break;
                        }
                    }

                    if (!flag)
                    {
                        return false;
                    }
                }

                return true;
            }
        }
        else
        {
            return tag1.equals(tag2);
        }
    }
}
