001    package net.minecraft.src;
002    
003    import java.io.DataInput;
004    import java.io.DataOutput;
005    import java.io.IOException;
006    
007    public abstract class NBTBase
008    {
009        public static final String[] NBTTypes = new String[] {"END", "BYTE", "SHORT", "INT", "LONG", "FLOAT", "DOUBLE", "BYTE[]", "STRING", "LIST", "COMPOUND", "INT[]"};
010    
011        /** The UTF string key used to lookup values. */
012        private String name;
013    
014        /**
015         * Write the actual data contents of the tag, implemented in NBT extension classes
016         */
017        abstract void write(DataOutput var1) throws IOException;
018    
019        /**
020         * Read the actual data contents of the tag, implemented in NBT extension classes
021         */
022        abstract void load(DataInput var1) throws IOException;
023    
024        /**
025         * Gets the type byte for the tag.
026         */
027        public abstract byte getId();
028    
029        protected NBTBase(String par1Str)
030        {
031            if (par1Str == null)
032            {
033                this.name = "";
034            }
035            else
036            {
037                this.name = par1Str;
038            }
039        }
040    
041        /**
042         * Sets the name for this tag and returns this for convenience.
043         */
044        public NBTBase setName(String par1Str)
045        {
046            if (par1Str == null)
047            {
048                this.name = "";
049            }
050            else
051            {
052                this.name = par1Str;
053            }
054    
055            return this;
056        }
057    
058        /**
059         * Gets the name corresponding to the tag, or an empty string if none set.
060         */
061        public String getName()
062        {
063            return this.name == null ? "" : this.name;
064        }
065    
066        /**
067         * Reads and returns a tag from the given DataInput, or the End tag if no tag could be read.
068         */
069        public static NBTBase readNamedTag(DataInput par0DataInput) throws IOException
070        {
071            byte var1 = par0DataInput.readByte();
072    
073            if (var1 == 0)
074            {
075                return new NBTTagEnd();
076            }
077            else
078            {
079                String var2 = par0DataInput.readUTF();
080                NBTBase var3 = newTag(var1, var2);
081    
082                try
083                {
084                    var3.load(par0DataInput);
085                    return var3;
086                }
087                catch (IOException var7)
088                {
089                    CrashReport var5 = CrashReport.func_85055_a(var7, "Loading NBT data");
090                    CrashReportCategory var6 = var5.func_85058_a("NBT Tag");
091                    var6.addCrashSection("Tag name", var2);
092                    var6.addCrashSection("Tag type", Byte.valueOf(var1));
093                    throw new ReportedException(var5);
094                }
095            }
096        }
097    
098        /**
099         * Writes the specified tag to the given DataOutput, writing the type byte, the UTF string key and then calling the
100         * tag to write its data.
101         */
102        public static void writeNamedTag(NBTBase par0NBTBase, DataOutput par1DataOutput) throws IOException
103        {
104            par1DataOutput.writeByte(par0NBTBase.getId());
105    
106            if (par0NBTBase.getId() != 0)
107            {
108                par1DataOutput.writeUTF(par0NBTBase.getName());
109                par0NBTBase.write(par1DataOutput);
110            }
111        }
112    
113        /**
114         * Creates and returns a new tag of the specified type, or null if invalid.
115         */
116        public static NBTBase newTag(byte par0, String par1Str)
117        {
118            switch (par0)
119            {
120                case 0:
121                    return new NBTTagEnd();
122                case 1:
123                    return new NBTTagByte(par1Str);
124                case 2:
125                    return new NBTTagShort(par1Str);
126                case 3:
127                    return new NBTTagInt(par1Str);
128                case 4:
129                    return new NBTTagLong(par1Str);
130                case 5:
131                    return new NBTTagFloat(par1Str);
132                case 6:
133                    return new NBTTagDouble(par1Str);
134                case 7:
135                    return new NBTTagByteArray(par1Str);
136                case 8:
137                    return new NBTTagString(par1Str);
138                case 9:
139                    return new NBTTagList(par1Str);
140                case 10:
141                    return new NBTTagCompound(par1Str);
142                case 11:
143                    return new NBTTagIntArray(par1Str);
144                default:
145                    return null;
146            }
147        }
148    
149        /**
150         * Returns the string name of a tag with the specified type, or 'UNKNOWN' if invalid.
151         */
152        public static String getTagName(byte par0)
153        {
154            switch (par0)
155            {
156                case 0:
157                    return "TAG_End";
158                case 1:
159                    return "TAG_Byte";
160                case 2:
161                    return "TAG_Short";
162                case 3:
163                    return "TAG_Int";
164                case 4:
165                    return "TAG_Long";
166                case 5:
167                    return "TAG_Float";
168                case 6:
169                    return "TAG_Double";
170                case 7:
171                    return "TAG_Byte_Array";
172                case 8:
173                    return "TAG_String";
174                case 9:
175                    return "TAG_List";
176                case 10:
177                    return "TAG_Compound";
178                case 11:
179                    return "TAG_Int_Array";
180                default:
181                    return "UNKNOWN";
182            }
183        }
184    
185        /**
186         * Creates a clone of the tag.
187         */
188        public abstract NBTBase copy();
189    
190        public boolean equals(Object par1Obj)
191        {
192            if (!(par1Obj instanceof NBTBase))
193            {
194                return false;
195            }
196            else
197            {
198                NBTBase var2 = (NBTBase)par1Obj;
199                return this.getId() != var2.getId() ? false : ((this.name != null || var2.name == null) && (this.name == null || var2.name != null) ? this.name == null || this.name.equals(var2.name) : false);
200            }
201        }
202    
203        public int hashCode()
204        {
205            return this.name.hashCode() ^ this.getId();
206        }
207    }