001package net.minecraft.nbt;
002
003import java.io.DataInput;
004import java.io.DataOutput;
005import java.io.IOException;
006import java.util.ArrayList;
007import java.util.Iterator;
008import java.util.List;
009
010public class NBTTagList extends NBTBase
011{
012    /** The array list containing the tags encapsulated in this list. */
013    private List tagList = new ArrayList();
014
015    /**
016     * The type byte for the tags in the list - they must all be of the same type.
017     */
018    private byte tagType;
019
020    public NBTTagList()
021    {
022        super("");
023    }
024
025    public NBTTagList(String par1Str)
026    {
027        super(par1Str);
028    }
029
030    /**
031     * Write the actual data contents of the tag, implemented in NBT extension classes
032     */
033    void write(DataOutput par1DataOutput) throws IOException
034    {
035        if (!this.tagList.isEmpty())
036        {
037            this.tagType = ((NBTBase)this.tagList.get(0)).getId();
038        }
039        else
040        {
041            this.tagType = 1;
042        }
043
044        par1DataOutput.writeByte(this.tagType);
045        par1DataOutput.writeInt(this.tagList.size());
046
047        for (int i = 0; i < this.tagList.size(); ++i)
048        {
049            ((NBTBase)this.tagList.get(i)).write(par1DataOutput);
050        }
051    }
052
053    /**
054     * Read the actual data contents of the tag, implemented in NBT extension classes
055     */
056    void load(DataInput par1DataInput) throws IOException
057    {
058        this.tagType = par1DataInput.readByte();
059        int i = par1DataInput.readInt();
060        this.tagList = new ArrayList();
061
062        for (int j = 0; j < i; ++j)
063        {
064            NBTBase nbtbase = NBTBase.newTag(this.tagType, (String)null);
065            nbtbase.load(par1DataInput);
066            this.tagList.add(nbtbase);
067        }
068    }
069
070    /**
071     * Gets the type byte for the tag.
072     */
073    public byte getId()
074    {
075        return (byte)9;
076    }
077
078    public String toString()
079    {
080        return "" + this.tagList.size() + " entries of type " + NBTBase.getTagName(this.tagType);
081    }
082
083    /**
084     * Adds the provided tag to the end of the list. There is no check to verify this tag is of the same type as any
085     * previous tag.
086     */
087    public void appendTag(NBTBase par1NBTBase)
088    {
089        this.tagType = par1NBTBase.getId();
090        this.tagList.add(par1NBTBase);
091    }
092
093    /**
094     * Removes a tag at the given index.
095     */
096    public NBTBase removeTag(int par1)
097    {
098        return (NBTBase)this.tagList.remove(par1);
099    }
100
101    /**
102     * Retrieves the tag at the specified index from the list.
103     */
104    public NBTBase tagAt(int par1)
105    {
106        return (NBTBase)this.tagList.get(par1);
107    }
108
109    /**
110     * Returns the number of tags in the list.
111     */
112    public int tagCount()
113    {
114        return this.tagList.size();
115    }
116
117    /**
118     * Creates a clone of the tag.
119     */
120    public NBTBase copy()
121    {
122        NBTTagList nbttaglist = new NBTTagList(this.getName());
123        nbttaglist.tagType = this.tagType;
124        Iterator iterator = this.tagList.iterator();
125
126        while (iterator.hasNext())
127        {
128            NBTBase nbtbase = (NBTBase)iterator.next();
129            NBTBase nbtbase1 = nbtbase.copy();
130            nbttaglist.tagList.add(nbtbase1);
131        }
132
133        return nbttaglist;
134    }
135
136    public boolean equals(Object par1Obj)
137    {
138        if (super.equals(par1Obj))
139        {
140            NBTTagList nbttaglist = (NBTTagList)par1Obj;
141
142            if (this.tagType == nbttaglist.tagType)
143            {
144                return this.tagList.equals(nbttaglist.tagList);
145            }
146        }
147
148        return false;
149    }
150
151    public int hashCode()
152    {
153        return super.hashCode() ^ this.tagList.hashCode();
154    }
155}