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