| 1 | package net.minecraft.nbt;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.io.DataInput;
|
| 5 | import java.io.DataOutput;
|
| 6 | import java.io.IOException;
|
| 7 | import java.util.List;
|
| 8 | import org.apache.logging.log4j.LogManager;
|
| 9 | import org.apache.logging.log4j.Logger;
|
| 10 |
|
| 11 | public class NBTTagList extends NBTBase
|
| 12 | {
|
| 13 | private static final Logger LOGGER = LogManager.getLogger();
|
| 14 | private List<NBTBase> tagList = Lists.<NBTBase>newArrayList();
|
| 15 |
|
| 16 | /**
|
| 17 | * The type byte for the tags in the list - they must all be of the same type.
|
| 18 | */
|
| 19 | private byte tagType = 0;
|
| 20 |
|
| 21 | /**
|
| 22 | * Write the actual data contents of the tag, implemented in NBT extension classes
|
| 23 | */
|
| 24 | void write(DataOutput output) throws IOException
|
| 25 | {
|
| 26 | if (!this.tagList.isEmpty())
|
| 27 | {
|
| 28 | this.tagType = ((NBTBase)this.tagList.get(0)).getId();
|
| 29 | }
|
| 30 | else
|
| 31 | {
|
| 32 | this.tagType = 0;
|
| 33 | }
|
| 34 |
|
| 35 | output.writeByte(this.tagType);
|
| 36 | output.writeInt(this.tagList.size());
|
| 37 |
|
| 38 | for (int i = 0; i < this.tagList.size(); ++i)
|
| 39 | {
|
| 40 | ((NBTBase)this.tagList.get(i)).write(output);
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | void read(DataInput input, int depth, NBTSizeTracker sizeTracker) throws IOException
|
| 45 | {
|
| 46 | sizeTracker.read(296L);
|
| 47 |
|
| 48 | if (depth > 512)
|
| 49 | {
|
| 50 | throw new RuntimeException("Tried to read NBT tag with too high complexity, depth > 512");
|
| 51 | }
|
| 52 | else
|
| 53 | {
|
| 54 | this.tagType = input.readByte();
|
| 55 | int i = input.readInt();
|
| 56 |
|
| 57 | if (this.tagType == 0 && i > 0)
|
| 58 | {
|
| 59 | throw new RuntimeException("Missing type on ListTag");
|
| 60 | }
|
| 61 | else
|
| 62 | {
|
| 63 | sizeTracker.read(32L * (long)i);
|
| 64 | this.tagList = Lists.<NBTBase>newArrayListWithCapacity(i);
|
| 65 |
|
| 66 | for (int j = 0; j < i; ++j)
|
| 67 | {
|
| 68 | NBTBase nbtbase = NBTBase.createNewByType(this.tagType);
|
| 69 | nbtbase.read(input, depth + 1, sizeTracker);
|
| 70 | this.tagList.add(nbtbase);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | /**
|
| 77 | * Gets the type byte for the tag.
|
| 78 | */
|
| 79 | public byte getId()
|
| 80 | {
|
| 81 | return (byte)9;
|
| 82 | }
|
| 83 |
|
| 84 | public String toString()
|
| 85 | {
|
| 86 | StringBuilder stringbuilder = new StringBuilder("[");
|
| 87 |
|
| 88 | for (int i = 0; i < this.tagList.size(); ++i)
|
| 89 | {
|
| 90 | if (i != 0)
|
| 91 | {
|
| 92 | stringbuilder.append(',');
|
| 93 | }
|
| 94 |
|
| 95 | stringbuilder.append(i).append(':').append(this.tagList.get(i));
|
| 96 | }
|
| 97 |
|
| 98 | return stringbuilder.append(']').toString();
|
| 99 | }
|
| 100 |
|
| 101 | /**
|
| 102 | * Adds the provided tag to the end of the list. There is no check to verify this tag is of the same type as any
|
| 103 | * previous tag.
|
| 104 | */
|
| 105 | public void appendTag(NBTBase nbt)
|
| 106 | {
|
| 107 | if (nbt.getId() == 0)
|
| 108 | {
|
| 109 | LOGGER.warn("Invalid TagEnd added to ListTag");
|
| 110 | }
|
| 111 | else
|
| 112 | {
|
| 113 | if (this.tagType == 0)
|
| 114 | {
|
| 115 | this.tagType = nbt.getId();
|
| 116 | }
|
| 117 | else if (this.tagType != nbt.getId())
|
| 118 | {
|
| 119 | LOGGER.warn("Adding mismatching tag types to tag list");
|
| 120 | return;
|
| 121 | }
|
| 122 |
|
| 123 | this.tagList.add(nbt);
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | /**
|
| 128 | * Set the given index to the given tag
|
| 129 | */
|
| 130 | public void set(int idx, NBTBase nbt)
|
| 131 | {
|
| 132 | if (nbt.getId() == 0)
|
| 133 | {
|
| 134 | LOGGER.warn("Invalid TagEnd added to ListTag");
|
| 135 | }
|
| 136 | else if (idx >= 0 && idx < this.tagList.size())
|
| 137 | {
|
| 138 | if (this.tagType == 0)
|
| 139 | {
|
| 140 | this.tagType = nbt.getId();
|
| 141 | }
|
| 142 | else if (this.tagType != nbt.getId())
|
| 143 | {
|
| 144 | LOGGER.warn("Adding mismatching tag types to tag list");
|
| 145 | return;
|
| 146 | }
|
| 147 |
|
| 148 | this.tagList.set(idx, nbt);
|
| 149 | }
|
| 150 | else
|
| 151 | {
|
| 152 | LOGGER.warn("index out of bounds to set tag in tag list");
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | /**
|
| 157 | * Removes a tag at the given index.
|
| 158 | */
|
| 159 | public NBTBase removeTag(int i)
|
| 160 | {
|
| 161 | return (NBTBase)this.tagList.remove(i);
|
| 162 | }
|
| 163 |
|
| 164 | /**
|
| 165 | * Return whether this compound has no tags.
|
| 166 | */
|
| 167 | public boolean hasNoTags()
|
| 168 | {
|
| 169 | return this.tagList.isEmpty();
|
| 170 | }
|
| 171 |
|
| 172 | /**
|
| 173 | * Retrieves the NBTTagCompound at the specified index in the list
|
| 174 | */
|
| 175 | public NBTTagCompound getCompoundTagAt(int i)
|
| 176 | {
|
| 177 | if (i >= 0 && i < this.tagList.size())
|
| 178 | {
|
| 179 | NBTBase nbtbase = (NBTBase)this.tagList.get(i);
|
| 180 | return nbtbase.getId() == 10 ? (NBTTagCompound)nbtbase : new NBTTagCompound();
|
| 181 | }
|
| 182 | else
|
| 183 | {
|
| 184 | return new NBTTagCompound();
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | public int[] getIntArrayAt(int i)
|
| 189 | {
|
| 190 | if (i >= 0 && i < this.tagList.size())
|
| 191 | {
|
| 192 | NBTBase nbtbase = (NBTBase)this.tagList.get(i);
|
| 193 | return nbtbase.getId() == 11 ? ((NBTTagIntArray)nbtbase).getIntArray() : new int[0];
|
| 194 | }
|
| 195 | else
|
| 196 | {
|
| 197 | return new int[0];
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | public double getDoubleAt(int i)
|
| 202 | {
|
| 203 | if (i >= 0 && i < this.tagList.size())
|
| 204 | {
|
| 205 | NBTBase nbtbase = (NBTBase)this.tagList.get(i);
|
| 206 | return nbtbase.getId() == 6 ? ((NBTTagDouble)nbtbase).getDouble() : 0.0D;
|
| 207 | }
|
| 208 | else
|
| 209 | {
|
| 210 | return 0.0D;
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | public float getFloatAt(int i)
|
| 215 | {
|
| 216 | if (i >= 0 && i < this.tagList.size())
|
| 217 | {
|
| 218 | NBTBase nbtbase = (NBTBase)this.tagList.get(i);
|
| 219 | return nbtbase.getId() == 5 ? ((NBTTagFloat)nbtbase).getFloat() : 0.0F;
|
| 220 | }
|
| 221 | else
|
| 222 | {
|
| 223 | return 0.0F;
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | /**
|
| 228 | * Retrieves the tag String value at the specified index in the list
|
| 229 | */
|
| 230 | public String getStringTagAt(int i)
|
| 231 | {
|
| 232 | if (i >= 0 && i < this.tagList.size())
|
| 233 | {
|
| 234 | NBTBase nbtbase = (NBTBase)this.tagList.get(i);
|
| 235 | return nbtbase.getId() == 8 ? nbtbase.getString() : nbtbase.toString();
|
| 236 | }
|
| 237 | else
|
| 238 | {
|
| 239 | return "";
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | /**
|
| 244 | * Get the tag at the given position
|
| 245 | */
|
| 246 | public NBTBase get(int idx)
|
| 247 | {
|
| 248 | return (NBTBase)(idx >= 0 && idx < this.tagList.size() ? (NBTBase)this.tagList.get(idx) : new NBTTagEnd());
|
| 249 | }
|
| 250 |
|
| 251 | /**
|
| 252 | * Returns the number of tags in the list.
|
| 253 | */
|
| 254 | public int tagCount()
|
| 255 | {
|
| 256 | return this.tagList.size();
|
| 257 | }
|
| 258 |
|
| 259 | /**
|
| 260 | * Creates a clone of the tag.
|
| 261 | */
|
| 262 | public NBTBase copy()
|
| 263 | {
|
| 264 | NBTTagList nbttaglist = new NBTTagList();
|
| 265 | nbttaglist.tagType = this.tagType;
|
| 266 |
|
| 267 | for (NBTBase nbtbase : this.tagList)
|
| 268 | {
|
| 269 | NBTBase nbtbase1 = nbtbase.copy();
|
| 270 | nbttaglist.tagList.add(nbtbase1);
|
| 271 | }
|
| 272 |
|
| 273 | return nbttaglist;
|
| 274 | }
|
| 275 |
|
| 276 | public boolean equals(Object p_equals_1_)
|
| 277 | {
|
| 278 | if (super.equals(p_equals_1_))
|
| 279 | {
|
| 280 | NBTTagList nbttaglist = (NBTTagList)p_equals_1_;
|
| 281 |
|
| 282 | if (this.tagType == nbttaglist.tagType)
|
| 283 | {
|
| 284 | return this.tagList.equals(nbttaglist.tagList);
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | return false;
|
| 289 | }
|
| 290 |
|
| 291 | public int hashCode()
|
| 292 | {
|
| 293 | return super.hashCode() ^ this.tagList.hashCode();
|
| 294 | }
|
| 295 |
|
| 296 | public int getTagType()
|
| 297 | {
|
| 298 | return this.tagType;
|
| 299 | }
|
| 300 | }
|
| 301 |
|