    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            try
    192            {
    193                return !this.tagMap.containsKey(par1Str) ? 0 : ((NBTTagByte)this.tagMap.get(par1Str)).data;
    194            }
    195            catch (ClassCastException var3)
    196            {
    197                throw new ReportedException(this.createCrashReport(par1Str, 1, var3));
    198            }
    199        }
    200
    201        /**
    202         * Retrieves a short value using the specified key, or 0 if no such key was stored.
    203         */
    204        public short getShort(String par1Str)
    205        {
    206            try
    207            {
    208                return !this.tagMap.containsKey(par1Str) ? 0 : ((NBTTagShort)this.tagMap.get(par1Str)).data;
    209            }
    210            catch (ClassCastException var3)
    211            {
    212                throw new ReportedException(this.createCrashReport(par1Str, 2, var3));
    213            }
    214        }
    215
    216        /**
    217         * Retrieves an integer value using the specified key, or 0 if no such key was stored.
    218         */
    219        public int getInteger(String par1Str)
    220        {
    221            try
    222            {
    223                return !this.tagMap.containsKey(par1Str) ? 0 : ((NBTTagInt)this.tagMap.get(par1Str)).data;
    224            }
    225            catch (ClassCastException var3)
    226            {
    227                throw new ReportedException(this.createCrashReport(par1Str, 3, var3));
    228            }
    229        }
    230
    231        /**
    232         * Retrieves a long value using the specified key, or 0 if no such key was stored.
    233         */
    234        public long getLong(String par1Str)
    235        {
    236            try
    237            {
    238                return !this.tagMap.containsKey(par1Str) ? 0L : ((NBTTagLong)this.tagMap.get(par1Str)).data;
    239            }
    240            catch (ClassCastException var3)
    241            {
    242                throw new ReportedException(this.createCrashReport(par1Str, 4, var3));
    243            }
    244        }
    245
    246        /**
    247         * Retrieves a float value using the specified key, or 0 if no such key was stored.
    248         */
    249        public float getFloat(String par1Str)
    250        {
    251            try
    252            {
    253                return !this.tagMap.containsKey(par1Str) ? 0.0F : ((NBTTagFloat)this.tagMap.get(par1Str)).data;
    254            }
    255            catch (ClassCastException var3)
    256            {
    257                throw new ReportedException(this.createCrashReport(par1Str, 5, var3));
    258            }
    259        }
    260
    261        /**
    262         * Retrieves a double value using the specified key, or 0 if no such key was stored.
    263         */
    264        public double getDouble(String par1Str)
    265        {
    266            try
    267            {
    268                return !this.tagMap.containsKey(par1Str) ? 0.0D : ((NBTTagDouble)this.tagMap.get(par1Str)).data;
    269            }
    270            catch (ClassCastException var3)
    271            {
    272                throw new ReportedException(this.createCrashReport(par1Str, 6, var3));
    273            }
    274        }
    275
    276        /**
    277         * Retrieves a string value using the specified key, or an empty string if no such key was stored.
    278         */
    279        public String getString(String par1Str)
    280        {
    281            try
    282            {
    283                return !this.tagMap.containsKey(par1Str) ? "" : ((NBTTagString)this.tagMap.get(par1Str)).data;
    284            }
    285            catch (ClassCastException var3)
    286            {
    287                throw new ReportedException(this.createCrashReport(par1Str, 8, var3));
    288            }
    289        }
    290
    291        /**
    292         * Retrieves a byte array using the specified key, or a zero-length array if no such key was stored.
    293         */
    294        public byte[] getByteArray(String par1Str)
    295        {
    296            try
    297            {
    298                return !this.tagMap.containsKey(par1Str) ? new byte[0] : ((NBTTagByteArray)this.tagMap.get(par1Str)).byteArray;
    299            }
    300            catch (ClassCastException var3)
    301            {
    302                throw new ReportedException(this.createCrashReport(par1Str, 7, var3));
    303            }
    304        }
    305
    306        /**
    307         * Retrieves an int array using the specified key, or a zero-length array if no such key was stored.
    308         */
    309        public int[] getIntArray(String par1Str)
    310        {
    311            try
    312            {
    313                return !this.tagMap.containsKey(par1Str) ? new int[0] : ((NBTTagIntArray)this.tagMap.get(par1Str)).intArray;
    314            }
    315            catch (ClassCastException var3)
    316            {
    317                throw new ReportedException(this.createCrashReport(par1Str, 11, var3));
    318            }
    319        }
    320
    321        /**
    322         * Retrieves a NBTTagCompound subtag matching the specified key, or a new empty NBTTagCompound if no such key was
    323         * stored.
    324         */
    325        public NBTTagCompound getCompoundTag(String par1Str)
    326        {
    327            try
    328            {
    329                return !this.tagMap.containsKey(par1Str) ? new NBTTagCompound(par1Str) : (NBTTagCompound)this.tagMap.get(par1Str);
    330            }
    331            catch (ClassCastException var3)
    332            {
    333                throw new ReportedException(this.createCrashReport(par1Str, 10, var3));
    334            }
    335        }
    336
    337        /**
    338         * Retrieves a NBTTagList subtag matching the specified key, or a new empty NBTTagList if no such key was stored.
    339         */
    340        public NBTTagList getTagList(String par1Str)
    341        {
    342            try
    343            {
    344                return !this.tagMap.containsKey(par1Str) ? new NBTTagList(par1Str) : (NBTTagList)this.tagMap.get(par1Str);
    345            }
    346            catch (ClassCastException var3)
    347            {
    348                throw new ReportedException(this.createCrashReport(par1Str, 9, var3));
    349            }
    350        }
    351
    352        /**
    353         * Retrieves a boolean value using the specified key, or false if no such key was stored. This uses the getByte
    354         * method.
    355         */
    356        public boolean getBoolean(String par1Str)
    357        {
    358            return this.getByte(par1Str) != 0;
    359        }
    360
    361        /**
    362         * Remove the specified tag.
    363         */
    364        public void removeTag(String par1Str)
    365        {
    366            this.tagMap.remove(par1Str);
    367        }
    368
    369        public String toString()
    370        {
    371            return "" + this.tagMap.size() + " entries";
    372        }
    373
    374        /**
    375         * Return whether this compound has no tags.
    376         */
    377        public boolean hasNoTags()
    378        {
    379            return this.tagMap.isEmpty();
    380        }
    381
    382        /**
    383         * Create a crash report which indicates a NBT read error.
    384         */
    385        private CrashReport createCrashReport(String par1Str, int par2, ClassCastException par3ClassCastException)
    386        {
    387            CrashReport var4 = CrashReport.func_85055_a(par3ClassCastException, "Reading NBT data");
    388            CrashReportCategory var5 = var4.func_85057_a("Corrupt NBT tag", 1);
    389            var5.addCrashSectionCallable("Tag type found", new CallableTagCompound1(this, par1Str));
    390            var5.addCrashSectionCallable("Tag type expected", new CallableTagCompound2(this, par2));
    391            var5.addCrashSection("Tag name", par1Str);
    392
    393            if (this.getName() != null && this.getName().length() > 0)
    394            {
    395                var5.addCrashSection("Tag parent", this.getName());
    396            }
    397
    398            return var4;
    399        }
    400
    401        /**
    402         * Creates a clone of the tag.
    403         */
    404        public NBTBase copy()
    405        {
    406            NBTTagCompound var1 = new NBTTagCompound(this.getName());
    407            Iterator var2 = this.tagMap.keySet().iterator();
    408
    409            while (var2.hasNext())
    410            {
    411                String var3 = (String)var2.next();
    412                var1.setTag(var3, ((NBTBase)this.tagMap.get(var3)).copy());
    413            }
    414
    415            return var1;
    416        }
    417
    418        public boolean equals(Object par1Obj)
    419        {
    420            if (super.equals(par1Obj))
    421            {
    422                NBTTagCompound var2 = (NBTTagCompound)par1Obj;
    423                return this.tagMap.entrySet().equals(var2.tagMap.entrySet());
    424            }
    425            else
    426            {
    427                return false;
    428            }
    429        }
    430
    431        public int hashCode()
    432        {
    433            return super.hashCode() ^ this.tagMap.hashCode();
    434        }
    435
    436        /**
    437         * Return the tag map for this compound.
    438         */
    439        static Map getTagMap(NBTTagCompound par0NBTTagCompound)
    440        {
    441            return par0NBTTagCompound.tagMap;
    442        }
    443    }