| 1 | package net.minecraft.nbt;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.io.DataInput;
|
| 5 | import java.io.DataOutput;
|
| 6 | import java.io.IOException;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.Map.Entry;
|
| 10 | import java.util.concurrent.Callable;
|
| 11 | import net.minecraft.crash.CrashReport;
|
| 12 | import net.minecraft.crash.CrashReportCategory;
|
| 13 | import net.minecraft.util.ReportedException;
|
| 14 |
|
| 15 | public class NBTTagCompound extends NBTBase
|
| 16 | {
|
| 17 | private Map<String, NBTBase> tagMap = Maps.<String, NBTBase>newHashMap();
|
| 18 |
|
| 19 | /**
|
| 20 | * Write the actual data contents of the tag, implemented in NBT extension classes
|
| 21 | */
|
| 22 | void write(DataOutput output) throws IOException
|
| 23 | {
|
| 24 | for (String s : this.tagMap.keySet())
|
| 25 | {
|
| 26 | NBTBase nbtbase = (NBTBase)this.tagMap.get(s);
|
| 27 | writeEntry(s, nbtbase, output);
|
| 28 | }
|
| 29 |
|
| 30 | output.writeByte(0);
|
| 31 | }
|
| 32 |
|
| 33 | void read(DataInput input, int depth, NBTSizeTracker sizeTracker) throws IOException
|
| 34 | {
|
| 35 | sizeTracker.read(384L);
|
| 36 |
|
| 37 | if (depth > 512)
|
| 38 | {
|
| 39 | throw new RuntimeException("Tried to read NBT tag with too high complexity, depth > 512");
|
| 40 | }
|
| 41 | else
|
| 42 | {
|
| 43 | this.tagMap.clear();
|
| 44 | byte b0;
|
| 45 |
|
| 46 | while ((b0 = readType(input, sizeTracker)) != 0)
|
| 47 | {
|
| 48 | String s = readKey(input, sizeTracker);
|
| 49 | sizeTracker.read((long)(224 + 16 * s.length()));
|
| 50 | NBTBase nbtbase = readNBT(b0, s, input, depth + 1, sizeTracker);
|
| 51 |
|
| 52 | if (this.tagMap.put(s, nbtbase) != null)
|
| 53 | {
|
| 54 | sizeTracker.read(288L);
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | public Set<String> getKeySet()
|
| 61 | {
|
| 62 | return this.tagMap.keySet();
|
| 63 | }
|
| 64 |
|
| 65 | /**
|
| 66 | * Gets the type byte for the tag.
|
| 67 | */
|
| 68 | public byte getId()
|
| 69 | {
|
| 70 | return (byte)10;
|
| 71 | }
|
| 72 |
|
| 73 | /**
|
| 74 | * Stores the given tag into the map with the given string key. This is mostly used to store tag lists.
|
| 75 | */
|
| 76 | public void setTag(String key, NBTBase value)
|
| 77 | {
|
| 78 | this.tagMap.put(key, value);
|
| 79 | }
|
| 80 |
|
| 81 | /**
|
| 82 | * Stores a new NBTTagByte with the given byte value into the map with the given string key.
|
| 83 | */
|
| 84 | public void setByte(String key, byte value)
|
| 85 | {
|
| 86 | this.tagMap.put(key, new NBTTagByte(value));
|
| 87 | }
|
| 88 |
|
| 89 | /**
|
| 90 | * Stores a new NBTTagShort with the given short value into the map with the given string key.
|
| 91 | */
|
| 92 | public void setShort(String key, short value)
|
| 93 | {
|
| 94 | this.tagMap.put(key, new NBTTagShort(value));
|
| 95 | }
|
| 96 |
|
| 97 | /**
|
| 98 | * Stores a new NBTTagInt with the given integer value into the map with the given string key.
|
| 99 | */
|
| 100 | public void setInteger(String key, int value)
|
| 101 | {
|
| 102 | this.tagMap.put(key, new NBTTagInt(value));
|
| 103 | }
|
| 104 |
|
| 105 | /**
|
| 106 | * Stores a new NBTTagLong with the given long value into the map with the given string key.
|
| 107 | */
|
| 108 | public void setLong(String key, long value)
|
| 109 | {
|
| 110 | this.tagMap.put(key, new NBTTagLong(value));
|
| 111 | }
|
| 112 |
|
| 113 | /**
|
| 114 | * Stores a new NBTTagFloat with the given float value into the map with the given string key.
|
| 115 | */
|
| 116 | public void setFloat(String key, float value)
|
| 117 | {
|
| 118 | this.tagMap.put(key, new NBTTagFloat(value));
|
| 119 | }
|
| 120 |
|
| 121 | /**
|
| 122 | * Stores a new NBTTagDouble with the given double value into the map with the given string key.
|
| 123 | */
|
| 124 | public void setDouble(String key, double value)
|
| 125 | {
|
| 126 | this.tagMap.put(key, new NBTTagDouble(value));
|
| 127 | }
|
| 128 |
|
| 129 | /**
|
| 130 | * Stores a new NBTTagString with the given string value into the map with the given string key.
|
| 131 | */
|
| 132 | public void setString(String key, String value)
|
| 133 | {
|
| 134 | this.tagMap.put(key, new NBTTagString(value));
|
| 135 | }
|
| 136 |
|
| 137 | /**
|
| 138 | * Stores a new NBTTagByteArray with the given array as data into the map with the given string key.
|
| 139 | */
|
| 140 | public void setByteArray(String key, byte[] value)
|
| 141 | {
|
| 142 | this.tagMap.put(key, new NBTTagByteArray(value));
|
| 143 | }
|
| 144 |
|
| 145 | /**
|
| 146 | * Stores a new NBTTagIntArray with the given array as data into the map with the given string key.
|
| 147 | */
|
| 148 | public void setIntArray(String key, int[] value)
|
| 149 | {
|
| 150 | this.tagMap.put(key, new NBTTagIntArray(value));
|
| 151 | }
|
| 152 |
|
| 153 | /**
|
| 154 | * Stores the given boolean value as a NBTTagByte, storing 1 for true and 0 for false, using the given string key.
|
| 155 | */
|
| 156 | public void setBoolean(String key, boolean value)
|
| 157 | {
|
| 158 | this.setByte(key, (byte)(value ? 1 : 0));
|
| 159 | }
|
| 160 |
|
| 161 | /**
|
| 162 | * gets a generic tag with the specified name
|
| 163 | */
|
| 164 | public NBTBase getTag(String key)
|
| 165 | {
|
| 166 | return (NBTBase)this.tagMap.get(key);
|
| 167 | }
|
| 168 |
|
| 169 | /**
|
| 170 | * Gets the ID byte for the given tag key
|
| 171 | */
|
| 172 | public byte getTagId(String key)
|
| 173 | {
|
| 174 | NBTBase nbtbase = (NBTBase)this.tagMap.get(key);
|
| 175 | return nbtbase != null ? nbtbase.getId() : 0;
|
| 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 key)
|
| 182 | {
|
| 183 | return this.tagMap.containsKey(key);
|
| 184 | }
|
| 185 |
|
| 186 | public boolean hasKey(String key, int type)
|
| 187 | {
|
| 188 | int i = this.getTagId(key);
|
| 189 |
|
| 190 | if (i == type)
|
| 191 | {
|
| 192 | return true;
|
| 193 | }
|
| 194 | else if (type != 99)
|
| 195 | {
|
| 196 | if (i > 0)
|
| 197 | {
|
| 198 | ;
|
| 199 | }
|
| 200 |
|
| 201 | return false;
|
| 202 | }
|
| 203 | else
|
| 204 | {
|
| 205 | return i == 1 || i == 2 || i == 3 || i == 4 || i == 5 || i == 6;
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | /**
|
| 210 | * Retrieves a byte value using the specified key, or 0 if no such key was stored.
|
| 211 | */
|
| 212 | public byte getByte(String key)
|
| 213 | {
|
| 214 | try
|
| 215 | {
|
| 216 | return !this.hasKey(key, 99) ? 0 : ((NBTBase.NBTPrimitive)this.tagMap.get(key)).getByte();
|
| 217 | }
|
| 218 | catch (ClassCastException var3)
|
| 219 | {
|
| 220 | return (byte)0;
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | /**
|
| 225 | * Retrieves a short value using the specified key, or 0 if no such key was stored.
|
| 226 | */
|
| 227 | public short getShort(String key)
|
| 228 | {
|
| 229 | try
|
| 230 | {
|
| 231 | return !this.hasKey(key, 99) ? 0 : ((NBTBase.NBTPrimitive)this.tagMap.get(key)).getShort();
|
| 232 | }
|
| 233 | catch (ClassCastException var3)
|
| 234 | {
|
| 235 | return (short)0;
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | /**
|
| 240 | * Retrieves an integer value using the specified key, or 0 if no such key was stored.
|
| 241 | */
|
| 242 | public int getInteger(String key)
|
| 243 | {
|
| 244 | try
|
| 245 | {
|
| 246 | return !this.hasKey(key, 99) ? 0 : ((NBTBase.NBTPrimitive)this.tagMap.get(key)).getInt();
|
| 247 | }
|
| 248 | catch (ClassCastException var3)
|
| 249 | {
|
| 250 | return 0;
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | /**
|
| 255 | * Retrieves a long value using the specified key, or 0 if no such key was stored.
|
| 256 | */
|
| 257 | public long getLong(String key)
|
| 258 | {
|
| 259 | try
|
| 260 | {
|
| 261 | return !this.hasKey(key, 99) ? 0L : ((NBTBase.NBTPrimitive)this.tagMap.get(key)).getLong();
|
| 262 | }
|
| 263 | catch (ClassCastException var3)
|
| 264 | {
|
| 265 | return 0L;
|
| 266 | }
|
| 267 | }
|
| 268 |
|
| 269 | /**
|
| 270 | * Retrieves a float value using the specified key, or 0 if no such key was stored.
|
| 271 | */
|
| 272 | public float getFloat(String key)
|
| 273 | {
|
| 274 | try
|
| 275 | {
|
| 276 | return !this.hasKey(key, 99) ? 0.0F : ((NBTBase.NBTPrimitive)this.tagMap.get(key)).getFloat();
|
| 277 | }
|
| 278 | catch (ClassCastException var3)
|
| 279 | {
|
| 280 | return 0.0F;
|
| 281 | }
|
| 282 | }
|
| 283 |
|
| 284 | /**
|
| 285 | * Retrieves a double value using the specified key, or 0 if no such key was stored.
|
| 286 | */
|
| 287 | public double getDouble(String key)
|
| 288 | {
|
| 289 | try
|
| 290 | {
|
| 291 | return !this.hasKey(key, 99) ? 0.0D : ((NBTBase.NBTPrimitive)this.tagMap.get(key)).getDouble();
|
| 292 | }
|
| 293 | catch (ClassCastException var3)
|
| 294 | {
|
| 295 | return 0.0D;
|
| 296 | }
|
| 297 | }
|
| 298 |
|
| 299 | /**
|
| 300 | * Retrieves a string value using the specified key, or an empty string if no such key was stored.
|
| 301 | */
|
| 302 | public String getString(String key)
|
| 303 | {
|
| 304 | try
|
| 305 | {
|
| 306 | return !this.hasKey(key, 8) ? "" : ((NBTBase)this.tagMap.get(key)).getString();
|
| 307 | }
|
| 308 | catch (ClassCastException var3)
|
| 309 | {
|
| 310 | return "";
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | /**
|
| 315 | * Retrieves a byte array using the specified key, or a zero-length array if no such key was stored.
|
| 316 | */
|
| 317 | public byte[] getByteArray(String key)
|
| 318 | {
|
| 319 | try
|
| 320 | {
|
| 321 | return !this.hasKey(key, 7) ? new byte[0] : ((NBTTagByteArray)this.tagMap.get(key)).getByteArray();
|
| 322 | }
|
| 323 | catch (ClassCastException classcastexception)
|
| 324 | {
|
| 325 | throw new ReportedException(this.createCrashReport(key, 7, classcastexception));
|
| 326 | }
|
| 327 | }
|
| 328 |
|
| 329 | /**
|
| 330 | * Retrieves an int array using the specified key, or a zero-length array if no such key was stored.
|
| 331 | */
|
| 332 | public int[] getIntArray(String key)
|
| 333 | {
|
| 334 | try
|
| 335 | {
|
| 336 | return !this.hasKey(key, 11) ? new int[0] : ((NBTTagIntArray)this.tagMap.get(key)).getIntArray();
|
| 337 | }
|
| 338 | catch (ClassCastException classcastexception)
|
| 339 | {
|
| 340 | throw new ReportedException(this.createCrashReport(key, 11, classcastexception));
|
| 341 | }
|
| 342 | }
|
| 343 |
|
| 344 | /**
|
| 345 | * Retrieves a NBTTagCompound subtag matching the specified key, or a new empty NBTTagCompound if no such key was
|
| 346 | * stored.
|
| 347 | */
|
| 348 | public NBTTagCompound getCompoundTag(String key)
|
| 349 | {
|
| 350 | try
|
| 351 | {
|
| 352 | return !this.hasKey(key, 10) ? new NBTTagCompound() : (NBTTagCompound)this.tagMap.get(key);
|
| 353 | }
|
| 354 | catch (ClassCastException classcastexception)
|
| 355 | {
|
| 356 | throw new ReportedException(this.createCrashReport(key, 10, classcastexception));
|
| 357 | }
|
| 358 | }
|
| 359 |
|
| 360 | /**
|
| 361 | * Gets the NBTTagList object with the given name. Args: name, NBTBase type
|
| 362 | */
|
| 363 | public NBTTagList getTagList(String key, int type)
|
| 364 | {
|
| 365 | try
|
| 366 | {
|
| 367 | if (this.getTagId(key) != 9)
|
| 368 | {
|
| 369 | return new NBTTagList();
|
| 370 | }
|
| 371 | else
|
| 372 | {
|
| 373 | NBTTagList nbttaglist = (NBTTagList)this.tagMap.get(key);
|
| 374 | return nbttaglist.tagCount() > 0 && nbttaglist.getTagType() != type ? new NBTTagList() : nbttaglist;
|
| 375 | }
|
| 376 | }
|
| 377 | catch (ClassCastException classcastexception)
|
| 378 | {
|
| 379 | throw new ReportedException(this.createCrashReport(key, 9, classcastexception));
|
| 380 | }
|
| 381 | }
|
| 382 |
|
| 383 | /**
|
| 384 | * Retrieves a boolean value using the specified key, or false if no such key was stored. This uses the getByte
|
| 385 | * method.
|
| 386 | */
|
| 387 | public boolean getBoolean(String key)
|
| 388 | {
|
| 389 | return this.getByte(key) != 0;
|
| 390 | }
|
| 391 |
|
| 392 | /**
|
| 393 | * Remove the specified tag.
|
| 394 | */
|
| 395 | public void removeTag(String key)
|
| 396 | {
|
| 397 | this.tagMap.remove(key);
|
| 398 | }
|
| 399 |
|
| 400 | public String toString()
|
| 401 | {
|
| 402 | StringBuilder stringbuilder = new StringBuilder("{");
|
| 403 |
|
| 404 | for (Entry<String, NBTBase> entry : this.tagMap.entrySet())
|
| 405 | {
|
| 406 | if (stringbuilder.length() != 1)
|
| 407 | {
|
| 408 | stringbuilder.append(',');
|
| 409 | }
|
| 410 |
|
| 411 | stringbuilder.append((String)entry.getKey()).append(':').append(entry.getValue());
|
| 412 | }
|
| 413 |
|
| 414 | return stringbuilder.append('}').toString();
|
| 415 | }
|
| 416 |
|
| 417 | /**
|
| 418 | * Return whether this compound has no tags.
|
| 419 | */
|
| 420 | public boolean hasNoTags()
|
| 421 | {
|
| 422 | return this.tagMap.isEmpty();
|
| 423 | }
|
| 424 |
|
| 425 | /**
|
| 426 | * Create a crash report which indicates a NBT read error.
|
| 427 | */
|
| 428 | private CrashReport createCrashReport(final String key, final int expectedType, ClassCastException ex)
|
| 429 | {
|
| 430 | CrashReport crashreport = CrashReport.makeCrashReport(ex, "Reading NBT data");
|
| 431 | CrashReportCategory crashreportcategory = crashreport.makeCategoryDepth("Corrupt NBT tag", 1);
|
| 432 | crashreportcategory.addCrashSectionCallable("Tag type found", new Callable<String>()
|
| 433 | {
|
| 434 | public String call() throws Exception
|
| 435 | {
|
| 436 | return NBTBase.NBT_TYPES[((NBTBase)NBTTagCompound.this.tagMap.get(key)).getId()];
|
| 437 | }
|
| 438 | });
|
| 439 | crashreportcategory.addCrashSectionCallable("Tag type expected", new Callable<String>()
|
| 440 | {
|
| 441 | public String call() throws Exception
|
| 442 | {
|
| 443 | return NBTBase.NBT_TYPES[expectedType];
|
| 444 | }
|
| 445 | });
|
| 446 | crashreportcategory.addCrashSection("Tag name", key);
|
| 447 | return crashreport;
|
| 448 | }
|
| 449 |
|
| 450 | /**
|
| 451 | * Creates a clone of the tag.
|
| 452 | */
|
| 453 | public NBTBase copy()
|
| 454 | {
|
| 455 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 456 |
|
| 457 | for (String s : this.tagMap.keySet())
|
| 458 | {
|
| 459 | nbttagcompound.setTag(s, ((NBTBase)this.tagMap.get(s)).copy());
|
| 460 | }
|
| 461 |
|
| 462 | return nbttagcompound;
|
| 463 | }
|
| 464 |
|
| 465 | public boolean equals(Object p_equals_1_)
|
| 466 | {
|
| 467 | if (super.equals(p_equals_1_))
|
| 468 | {
|
| 469 | NBTTagCompound nbttagcompound = (NBTTagCompound)p_equals_1_;
|
| 470 | return this.tagMap.entrySet().equals(nbttagcompound.tagMap.entrySet());
|
| 471 | }
|
| 472 | else
|
| 473 | {
|
| 474 | return false;
|
| 475 | }
|
| 476 | }
|
| 477 |
|
| 478 | public int hashCode()
|
| 479 | {
|
| 480 | return super.hashCode() ^ this.tagMap.hashCode();
|
| 481 | }
|
| 482 |
|
| 483 | private static void writeEntry(String name, NBTBase data, DataOutput output) throws IOException
|
| 484 | {
|
| 485 | output.writeByte(data.getId());
|
| 486 |
|
| 487 | if (data.getId() != 0)
|
| 488 | {
|
| 489 | output.writeUTF(name);
|
| 490 | data.write(output);
|
| 491 | }
|
| 492 | }
|
| 493 |
|
| 494 | private static byte readType(DataInput input, NBTSizeTracker sizeTracker) throws IOException
|
| 495 | {
|
| 496 | return input.readByte();
|
| 497 | }
|
| 498 |
|
| 499 | private static String readKey(DataInput input, NBTSizeTracker sizeTracker) throws IOException
|
| 500 | {
|
| 501 | return input.readUTF();
|
| 502 | }
|
| 503 |
|
| 504 | static NBTBase readNBT(byte id, String key, DataInput input, int depth, NBTSizeTracker sizeTracker) throws IOException
|
| 505 | {
|
| 506 | NBTBase nbtbase = NBTBase.createNewByType(id);
|
| 507 |
|
| 508 | try
|
| 509 | {
|
| 510 | nbtbase.read(input, depth, sizeTracker);
|
| 511 | return nbtbase;
|
| 512 | }
|
| 513 | catch (IOException ioexception)
|
| 514 | {
|
| 515 | CrashReport crashreport = CrashReport.makeCrashReport(ioexception, "Loading NBT data");
|
| 516 | CrashReportCategory crashreportcategory = crashreport.makeCategory("NBT Tag");
|
| 517 | crashreportcategory.addCrashSection("Tag name", key);
|
| 518 | crashreportcategory.addCrashSection("Tag type", Byte.valueOf(id));
|
| 519 | throw new ReportedException(crashreport);
|
| 520 | }
|
| 521 | }
|
| 522 |
|
| 523 | /**
|
| 524 | * Merges this NBTTagCompound with the given compound. Any sub-compounds are merged using the same methods, other
|
| 525 | * types of tags are overwritten from the given compound.
|
| 526 | */
|
| 527 | public void merge(NBTTagCompound other)
|
| 528 | {
|
| 529 | for (String s : other.tagMap.keySet())
|
| 530 | {
|
| 531 | NBTBase nbtbase = (NBTBase)other.tagMap.get(s);
|
| 532 |
|
| 533 | if (nbtbase.getId() == 10)
|
| 534 | {
|
| 535 | if (this.hasKey(s, 10))
|
| 536 | {
|
| 537 | NBTTagCompound nbttagcompound = this.getCompoundTag(s);
|
| 538 | nbttagcompound.merge((NBTTagCompound)nbtbase);
|
| 539 | }
|
| 540 | else
|
| 541 | {
|
| 542 | this.setTag(s, nbtbase.copy());
|
| 543 | }
|
| 544 | }
|
| 545 | else
|
| 546 | {
|
| 547 | this.setTag(s, nbtbase.copy());
|
| 548 | }
|
| 549 | }
|
| 550 | }
|
| 551 | }
|
| 552 |
|