    001    package net.minecraft.src;
    002
    003    import java.io.DataInput;
    004    import java.io.DataOutput;
    005    import java.io.IOException;
    006    import java.util.Collection;
    007    import java.util.HashMap;
    008    import java.util.Iterator;
    009    import java.util.Map;
    010
    011    public class NBTTagCompound extends NBTBase
    012    {
    013        /**
    014         * The key-value pairs for the tag. Each key is a UTF string, each value is a tag.
    015         */
    016        private Map tagMap = new HashMap();
    017
    018        public NBTTagCompound()
    019        {
    020            super("");
    021        }
    022
    023        public NBTTagCompound(String par1Str)
    024        {
    025            super(par1Str);
    026        }
    027
    028        /**
    029         * Write the actual data contents of the tag, implemented in NBT extension classes
    030         */
    031        void write(DataOutput par1DataOutput) throws IOException
    032        {
    033            Iterator var2 = this.tagMap.values().iterator();
    034
    035            while (var2.hasNext())
    036            {
    037                NBTBase var3 = (NBTBase)var2.next();
    038                NBTBase.writeNamedTag(var3, par1DataOutput);
    039            }
    040
    041            par1DataOutput.writeByte(0);
    042        }
    043
    044        /**
    045         * Read the actual data contents of the tag, implemented in NBT extension classes
    046         */
    047        void load(DataInput par1DataInput) throws IOException
    048        {
    049            this.tagMap.clear();
    050            NBTBase var2;
    051
    052            while ((var2 = NBTBase.readNamedTag(par1DataInput)).getId() != 0)
    053            {
    054                this.tagMap.put(var2.getName(), var2);
    055            }
    056        }
    057
    058        /**
    059         * Returns all the values in the tagMap HashMap.
    060         */
    061        public Collection getTags()
    062        {
    063            return this.tagMap.values();
    064        }
    065
    066        /**
    067         * Gets the type byte for the tag.
    068         */
    069        public byte getId()
    070        {
    071            return (byte)10;
    072        }
    073
    074        /**
    075         * Stores the given tag into the map with the given string key. This is mostly used to store tag lists.
    076         */
    077        public void setTag(String par1Str, NBTBase par2NBTBase)
    078        {
    079            this.tagMap.put(par1Str, par2NBTBase.setName(par1Str));
    080        }
    081
    082        /**
    083         * Stores a new NBTTagByte with the given byte value into the map with the given string key.
    084         */
    085        public void setByte(String par1Str, byte par2)
    086        {
    087            this.tagMap.put(par1Str, new NBTTagByte(par1Str, par2));
    088        }
    089
    090        /**
    091         * Stores a new NBTTagShort with the given short value into the map with the given string key.
    092         */
    093        public void setShort(String par1Str, short par2)
    094        {
    095            this.tagMap.put(par1Str, new NBTTagShort(par1Str, par2));
    096        }
    097
    098        /**
    099         * Stores a new NBTTagInt with the given integer value into the map with the given string key.
    100         */
    101        public void setInteger(String par1Str, int par2)
    102        {
    103            this.tagMap.put(par1Str, new NBTTagInt(par1Str, par2));
    104        }
    105
    106        /**
    107         * Stores a new NBTTagLong with the given long value into the map with the given string key.
    108         */
    109        public void setLong(String par1Str, long par2)
    110        {
    111            this.tagMap.put(par1Str, new NBTTagLong(par1Str, par2));
    112        }
    113
    114        /**
    115         * Stores a new NBTTagFloat with the given float value into the map with the given string key.
    116         */
    117        public void setFloat(String par1Str, float par2)
    118        {
    119            this.tagMap.put(par1Str, new NBTTagFloat(par1Str, par2));
    120        }
    121
    122        /**
    123         * Stores a new NBTTagDouble with the given double value into the map with the given string key.
    124         */
    125        public void setDouble(String par1Str, double par2)
    126        {
    127            this.tagMap.put(par1Str, new NBTTagDouble(par1Str, par2));
    128        }
    129
    130        /**
    131         * Stores a new NBTTagString with the given string value into the map with the given string key.
    132         */
    133        public void setString(String par1Str, String par2Str)
    134        {
    135            this.tagMap.put(par1Str, new NBTTagString(par1Str, par2Str));
    136        }
    137
    138        /**
    139         * Stores a new NBTTagByteArray with the given array as data into the map with the given string key.
    140         */
    141        public void setByteArray(String par1Str, byte[] par2ArrayOfByte)
    142        {
    143            this.tagMap.put(par1Str, new NBTTagByteArray(par1Str, par2ArrayOfByte));
    144        }
    145
    146        /**
    147         * Stores a new NBTTagIntArray with the given array as data into the map with the given string key.
    148         */
    149        public void setIntArray(String par1Str, int[] par2ArrayOfInteger)
    150        {
    151            this.tagMap.put(par1Str, new NBTTagIntArray(par1Str, par2ArrayOfInteger));
    152        }
    153
    154        /**
    155         * Stores the given NBTTagCompound into the map with the given string key.
    156         */
    157        public void setCompoundTag(String par1Str, NBTTagCompound par2NBTTagCompound)
    158        {
    159            this.tagMap.put(par1Str, par2NBTTagCompound.setName(par1Str));
    160        }
    161
    162        /**
    163         * Stores the given boolean value as a NBTTagByte, storing 1 for true and 0 for false, using the given string key.
    164         */
    165        public void setBoolean(String par1Str, boolean par2)
    166        {
    167            this.setByte(par1Str, (byte)(par2 ? 1 : 0));
    168        }
    169
    170        /**
    171         * gets a generic tag with the specified name
    172         */
    173        public NBTBase getTag(String par1Str)
    174        {
    175            return (NBTBase)this.tagMap.get(par1Str);
    176        }
    177
    178        /**
    179         * Returns whether the given string has been previously stored as a key in the map.
    180         */
    181        public boolean hasKey(String par1Str)
    182        {
    183            return this.tagMap.containsKey(par1Str);
    184        }
    185
    186        /**
    187         * Retrieves a byte value using the specified key, or 0 if no such key was stored.
    188         */
    189        public byte getByte(String par1Str)
    190        {
    191            return !this.tagMap.containsKey(par1Str) ? 0 : ((NBTTagByte)this.tagMap.get(par1Str)).data;
    192        }
    193
    194        /**
    195         * Retrieves a short value using the specified key, or 0 if no such key was stored.
    196         */
    197        public short getShort(String par1Str)
    198        {
    199            return !this.tagMap.containsKey(par1Str) ? 0 : ((NBTTagShort)this.tagMap.get(par1Str)).data;
    200        }
    201
    202        /**
    203         * Retrieves an integer value using the specified key, or 0 if no such key was stored.
    204         */
    205        public int getInteger(String par1Str)
    206        {
    207            return !this.tagMap.containsKey(par1Str) ? 0 : ((NBTTagInt)this.tagMap.get(par1Str)).data;
    208        }
    209
    210        /**
    211         * Retrieves a long value using the specified key, or 0 if no such key was stored.
    212         */
    213        public long getLong(String par1Str)
    214        {
    215            return !this.tagMap.containsKey(par1Str) ? 0L : ((NBTTagLong)this.tagMap.get(par1Str)).data;
    216        }
    217
    218        /**
    219         * Retrieves a float value using the specified key, or 0 if no such key was stored.
    220         */
    221        public float getFloat(String par1Str)
    222        {
    223            return !this.tagMap.containsKey(par1Str) ? 0.0F : ((NBTTagFloat)this.tagMap.get(par1Str)).data;
    224        }
    225
    226        /**
    227         * Retrieves a double value using the specified key, or 0 if no such key was stored.
    228         */
    229        public double getDouble(String par1Str)
    230        {
    231            return !this.tagMap.containsKey(par1Str) ? 0.0D : ((NBTTagDouble)this.tagMap.get(par1Str)).data;
    232        }
    233
    234        /**
    235         * Retrieves a string value using the specified key, or an empty string if no such key was stored.
    236         */
    237        public String getString(String par1Str)
    238        {
    239            return !this.tagMap.containsKey(par1Str) ? "" : ((NBTTagString)this.tagMap.get(par1Str)).data;
    240        }
    241
    242        /**
    243         * Retrieves a byte array using the specified key, or a zero-length array if no such key was stored.
    244         */
    245        public byte[] getByteArray(String par1Str)
    246        {
    247            return !this.tagMap.containsKey(par1Str) ? new byte[0] : ((NBTTagByteArray)this.tagMap.get(par1Str)).byteArray;
    248        }
    249
    250        /**
    251         * Retrieves an int array using the specified key, or a zero-length array if no such key was stored.
    252         */
    253        public int[] getIntArray(String par1Str)
    254        {
    255            return !this.tagMap.containsKey(par1Str) ? new int[0] : ((NBTTagIntArray)this.tagMap.get(par1Str)).intArray;
    256        }
    257
    258        /**
    259         * Retrieves a NBTTagCompound subtag matching the specified key, or a new empty NBTTagCompound if no such key was
    260         * stored.
    261         */
    262        public NBTTagCompound getCompoundTag(String par1Str)
    263        {
    264            return !this.tagMap.containsKey(par1Str) ? new NBTTagCompound(par1Str) : (NBTTagCompound)this.tagMap.get(par1Str);
    265        }
    266
    267        /**
    268         * Retrieves a NBTTagList subtag matching the specified key, or a new empty NBTTagList if no such key was stored.
    269         */
    270        public NBTTagList getTagList(String par1Str)
    271        {
    272            return !this.tagMap.containsKey(par1Str) ? new NBTTagList(par1Str) : (NBTTagList)this.tagMap.get(par1Str);
    273        }
    274
    275        /**
    276         * Retrieves a boolean value using the specified key, or false if no such key was stored. This uses the getByte
    277         * method.
    278         */
    279        public boolean getBoolean(String par1Str)
    280        {
    281            return this.getByte(par1Str) != 0;
    282        }
    283
    284        public String toString()
    285        {
    286            return "" + this.tagMap.size() + " entries";
    287        }
    288
    289        /**
    290         * Creates a clone of the tag.
    291         */
    292        public NBTBase copy()
    293        {
    294            NBTTagCompound var1 = new NBTTagCompound(this.getName());
    295            Iterator var2 = this.tagMap.keySet().iterator();
    296
    297            while (var2.hasNext())
    298            {
    299                String var3 = (String)var2.next();
    300                var1.setTag(var3, ((NBTBase)this.tagMap.get(var3)).copy());
    301            }
    302
    303            return var1;
    304        }
    305
    306        public boolean equals(Object par1Obj)
    307        {
    308            if (super.equals(par1Obj))
    309            {
    310                NBTTagCompound var2 = (NBTTagCompound)par1Obj;
    311                return this.tagMap.entrySet().equals(var2.tagMap.entrySet());
    312            }
    313            else
    314            {
    315                return false;
    316            }
    317        }
    318
    319        public int hashCode()
    320        {
    321            return super.hashCode() ^ this.tagMap.hashCode();
    322        }
    323    }