public Tag<?> readTag() throws IOException { return readTag(0); }
public String getTypeName() { return "TAG_" + name(); }
private Tag<?> readTag(int depth) throws IOException { if (closed) { throw new IllegalStateException("Trying to read from a closed reader!"); } int typeId = input.readByte() & 0xFF; TagType type = TagType.fromId(typeId); if (type == null) { throw new IOException("Invalid encoding ID " + typeId); } return deserialize(type, false, depth); }
Map<String, Tag<?>> map = new HashMap<>(); Tag<?> inTag1; while ((inTag1 = readTag(depth + 1)) != EndTag.INSTANCE) { map.put(inTag1.getName(), inTag1); case LIST: int inId = input.readByte() & 0xFF; TagType listType = TagType.fromId(inId); if (listType == null) { String append = tagName == null ? "" : "('" + tagName + "')"; int listLength = encoding == MCPE_0_16_NETWORK ? Varints.decodeSigned(input) : input.readInt(); for (int i = 0; i < listLength; i++) { list.add(deserialize(listType, true, depth + 1)); return new ListTag(tagName, listType.getTagClass(), list); case INT_ARRAY: int arraySz2 = encoding == MCPE_0_16_NETWORK ? Varints.decodeSigned(input) : input.readInt();
public static AnvilLevelDataProvider load(@NonNull Path levelDatPath) throws IOException { // level.dat is Notchian, so it's big-endian and GZIP compressed CompoundTag tag; try (NBTReader reader = NBTReaders.createBigEndianReader(new GZIPInputStream(Files.newInputStream(levelDatPath)))) { tag = (CompoundTag) reader.readTag(); } CompoundTag dataTag = (CompoundTag) tag.getValue().get("Data"); Map<String, Tag<?>> map = dataTag.getValue(); Vector3i out = new Vector3i(((IntTag) map.get("SpawnX")).getPrimitiveValue(), ((IntTag) map.get("SpawnY")).getPrimitiveValue(), ((IntTag) map.get("SpawnZ")).getPrimitiveValue()); long dayTime = ((LongTag) map.get("DayTime")).getPrimitiveValue(); long seed = ((LongTag) map.get("RandomSeed")).getPrimitiveValue(); return new AnvilLevelDataProvider(out.toFloat(), (int) dayTime, seed); }
throw new IllegalArgumentException("Reached depth limit"); TagType type = TagType.fromClass(tag.getClass()); if (type == null) { throw new IllegalArgumentException("Tag " + tag + " is not valid."); output.writeByte(type.ordinal() & 0xFF); writeString(tag.getName()); case STRING: StringTag strt = (StringTag) tag; writeString(strt.getValue()); break; case LIST: ListTag<?> listt = (ListTag<?>) tag; output.writeByte(TagType.fromClass(listt.getTagClass()).ordinal()); if (encoding == NBTEncoding.MCPE_0_16_NETWORK) { Varints.encodeSigned(output, listt.getValue().size()); serialize(tag1, true, depth+1); CompoundTag compoundTag = (CompoundTag) tag; for (Tag<?> tag1 : compoundTag.getValue().values()) { serialize(tag1, false, depth+1);
public static ItemStack readItemStack(ByteBuf buf) { int id = Varints.decodeSigned(buf); if (id == 0) { return new VoxelwindItemStack(BlockTypes.AIR, 1, null); } int aux = Varints.decodeSigned(buf); int damage = aux >> 8; int count = aux & 0xff; short nbtSize = buf.readShortLE(); ItemType type = ItemTypes.forId(id); ItemStackBuilder builder = new VoxelwindItemStackBuilder() .itemType(type) .itemData(MetadataSerializer.deserializeMetadata(type, (short) damage)) .amount(count); if (nbtSize > 0) { try (NBTReader reader = new NBTReader(new LittleEndianByteBufInputStream(buf.readSlice(nbtSize)))) { Tag<?> tag = reader.readTag(); if (tag instanceof CompoundTag) { VoxelwindNBTUtils.applyItemData(builder, ((CompoundTag) tag).getValue()); } } catch (IOException e) { throw new IllegalStateException("Unable to load NBT data", e); } } return builder.build(); }
@Override public void encode(ByteBuf buffer) { McpeUtil.writeBlockCoords(buffer, position); try (NBTWriter writer = new NBTWriter(new LittleEndianByteBufOutputStream(buffer), NBTEncoding.MCPE_0_16_NETWORK)) { writer.write(blockEntityData); } catch (IOException e) { throw new RuntimeException(e); } } }
public static NBTReader createBigEndianReader(InputStream stream) { Objects.requireNonNull(stream, "stream"); return new NBTReader(new DataInputStream(stream)); } }
@Override public String toString() { String append = ""; if (name != null && !name.equals("")) { append = "(\"" + this.getName() + "\")"; } StringBuilder bldr = new StringBuilder(); bldr.append("TAG_List").append(append).append(": ").append(value.size()).append(" entries of type ").append(TagType.fromClass(tagClass).getTypeName()).append("\r\n{\r\n"); for (Tag t : value) { bldr.append(" ").append(t.toString().replaceAll("\r\n", "\r\n ")).append("\r\n"); } bldr.append("}"); return bldr.toString(); } }
public void write(Tag<?> tag) throws IOException { if (closed) { throw new IllegalStateException("closed"); } Objects.requireNonNull(tag, "tag"); if (!(tag instanceof CompoundTag)) { throw new IllegalArgumentException("Trying to write a non-compound tag!"); } serialize(tag, false, 0); }
try (NBTReader stream = NBTReaders.createBigEndianReader(regionReader.readChunk(irXZ.x, irXZ.z))) { tag = stream.readTag();
public static void writeItemStack(ByteBuf buf, ItemStack stack) { if (stack == null || stack.getItemType() == BlockTypes.AIR) { buf.writeByte(0); // 0 byte means 0 in varint return; } Varints.encodeSigned(buf, stack.getItemType().getId()); short metadataValue = MetadataSerializer.serializeMetadata(stack); Varints.encodeSigned(buf, (metadataValue << 8) | stack.getAmount()); // Remember this position, since we'll be writing the true NBT size here later: int sizeIndex = buf.writerIndex(); buf.writeShort(0); int afterSizeIndex = buf.writerIndex(); if (stack instanceof VoxelwindItemStack) { try (NBTWriter stream = new NBTWriter(new LittleEndianByteBufOutputStream(buf))) { stream.write(((VoxelwindItemStack) stack).toSpecificNBT()); } catch (IOException e) { // This shouldn't happen (as this is backed by a Netty ByteBuf), but okay... throw new IllegalStateException("Unable to save NBT data", e); } // Set to the written NBT size buf.setShortLE(sizeIndex, buf.writerIndex() - afterSizeIndex); } }
public static NBTReader createLittleEndianReader(InputStream stream) { Objects.requireNonNull(stream, "stream"); return new NBTReader(new SwappedDataInputStream(stream)); }