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 var6) 088 { 089 CrashReport var5 = new CrashReport("loading nbt data", var6); 090 var5.addCrashSection("Tag name", var2); 091 var5.addCrashSection("Tag type", Byte.valueOf(var1)); 092 throw new ReportedException(var5); 093 } 094 } 095 } 096 097 /** 098 * Writes the specified tag to the given DataOutput, writing the type byte, the UTF string key and then calling the 099 * tag to write its data. 100 */ 101 public static void writeNamedTag(NBTBase par0NBTBase, DataOutput par1DataOutput) throws IOException 102 { 103 par1DataOutput.writeByte(par0NBTBase.getId()); 104 105 if (par0NBTBase.getId() != 0) 106 { 107 par1DataOutput.writeUTF(par0NBTBase.getName()); 108 par0NBTBase.write(par1DataOutput); 109 } 110 } 111 112 /** 113 * Creates and returns a new tag of the specified type, or null if invalid. 114 */ 115 public static NBTBase newTag(byte par0, String par1Str) 116 { 117 switch (par0) 118 { 119 case 0: 120 return new NBTTagEnd(); 121 case 1: 122 return new NBTTagByte(par1Str); 123 case 2: 124 return new NBTTagShort(par1Str); 125 case 3: 126 return new NBTTagInt(par1Str); 127 case 4: 128 return new NBTTagLong(par1Str); 129 case 5: 130 return new NBTTagFloat(par1Str); 131 case 6: 132 return new NBTTagDouble(par1Str); 133 case 7: 134 return new NBTTagByteArray(par1Str); 135 case 8: 136 return new NBTTagString(par1Str); 137 case 9: 138 return new NBTTagList(par1Str); 139 case 10: 140 return new NBTTagCompound(par1Str); 141 case 11: 142 return new NBTTagIntArray(par1Str); 143 default: 144 return null; 145 } 146 } 147 148 /** 149 * Returns the string name of a tag with the specified type, or 'UNKNOWN' if invalid. 150 */ 151 public static String getTagName(byte par0) 152 { 153 switch (par0) 154 { 155 case 0: 156 return "TAG_End"; 157 case 1: 158 return "TAG_Byte"; 159 case 2: 160 return "TAG_Short"; 161 case 3: 162 return "TAG_Int"; 163 case 4: 164 return "TAG_Long"; 165 case 5: 166 return "TAG_Float"; 167 case 6: 168 return "TAG_Double"; 169 case 7: 170 return "TAG_Byte_Array"; 171 case 8: 172 return "TAG_String"; 173 case 9: 174 return "TAG_List"; 175 case 10: 176 return "TAG_Compound"; 177 case 11: 178 return "TAG_Int_Array"; 179 default: 180 return "UNKNOWN"; 181 } 182 } 183 184 /** 185 * Creates a clone of the tag. 186 */ 187 public abstract NBTBase copy(); 188 189 public boolean equals(Object par1Obj) 190 { 191 if (!(par1Obj instanceof NBTBase)) 192 { 193 return false; 194 } 195 else 196 { 197 NBTBase var2 = (NBTBase)par1Obj; 198 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); 199 } 200 } 201 202 public int hashCode() 203 { 204 return this.name.hashCode() ^ this.getId(); 205 } 206 }