Java Code Examples for cn.nukkit.nbt.tag.CompoundTag#putList()

The following examples show how to use cn.nukkit.nbt.tag.CompoundTag#putList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityArmorStand.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public EntityArmorStand(FullChunk chunk, CompoundTag nbt) {
	super(chunk, nbt);

	if (!nbt.contains("HandItems")) {
		nbt.putCompound("HandItems", NBTIO.putItemHelper(Item.get(0)));
	}
	if (!nbt.contains("ArmorItems")) {
		ListTag<CompoundTag> tag = new ListTag<CompoundTag>("ArmorItems")
				.add(NBTIO.putItemHelper(Item.get(0)))
				.add(NBTIO.putItemHelper(Item.get(0)))
				.add(NBTIO.putItemHelper(Item.get(0)))
				.add(NBTIO.putItemHelper(Item.get(0)));
		nbt.putList(tag);
	}

	this.setHealth(2);
	this.setMaxHealth(2);
}
 
Example 2
Source File: BlockEntityShulkerBox.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public CompoundTag getSpawnCompound() {
    CompoundTag nbt = new CompoundTag()
            .putString("id", BlockEntity.SHULKER_BOX)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z);

    if (this.namedTag.contains("Items")){
    	nbt.putList(this.namedTag.getList("Items"));
    }

    if (this.hasName()) {
        nbt.put("CustomName", this.namedTag.get("CustomName"));
    }

    return nbt;
}
 
Example 3
Source File: ItemBookWritable.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Adds a new page with the given page ID.
 * Creates a new page for every page between the given ID and existing pages that doesn't yet exist.
 * @return boolean indicating success
 */
public boolean addPage(int pageId) {
    Preconditions.checkArgument(pageId >= 0 && pageId < 50, "Page number " + pageId + " is out of range");
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();
    ListTag<CompoundTag> pages;
    if (!tag.contains("pages") || !(tag.get("pages") instanceof ListTag)) {
        pages = new ListTag<>("pages");
        tag.putList(pages);
    } else {
        pages = tag.getList("pages", CompoundTag.class);
    }

    for (int current = pages.size(); current <= pageId; current++) {
        pages.add(createPageTag());
    }
    this.setCompoundTag(tag);
    return true;
}
 
Example 4
Source File: ItemBookWritable.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Inserts a new page with the given text and moves other pages upwards.
 * @return boolean indicating success
 */
public boolean insertPage(int pageId, String pageText) {
    Preconditions.checkArgument(pageId >= 0 && pageId < 50, "Page number " + pageId + " is out of range");
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();
    ListTag<CompoundTag> pages;
    if (!tag.contains("pages") || !(tag.get("pages") instanceof ListTag)) {
        pages = new ListTag<>("pages");
        tag.putList(pages);
    } else {
        pages = tag.getList("pages", CompoundTag.class);
    }

    if (pages.size() <= pageId) {
        for (int current = pages.size(); current <= pageId; current++) {
            pages.add(createPageTag());
        }
        pages.get(pageId).putString("text", pageText);
    } else {
        pages.add(pageId, createPageTag(pageText));
    }
    this.setCompoundTag(tag);
    return true;
}
 
Example 5
Source File: BlockUndyedShulkerBox.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    this.getLevel().setBlock(block, this, true);
    CompoundTag nbt = BlockEntity.getDefaultCompound(this, BlockEntity.SHULKER_BOX)
            .putByte("facing", face.getIndex());

    if (item.hasCustomName()) {
        nbt.putString("CustomName", item.getCustomName());
    }

    CompoundTag t = item.getNamedTag();

    // This code gets executed when the player has broken the shulker box and placed it back (©Kevims 2020)
    if (t != null && t.contains("Items")) {
        nbt.putList(t.getList("Items"));
    }

    // This code gets executed when the player has copied the shulker box in creative mode (©Kevims 2020)
    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }

    BlockEntityShulkerBox box = (BlockEntityShulkerBox) BlockEntity.createBlockEntity(BlockEntity.SHULKER_BOX, this.getLevel().getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4), nbt);
    return box != null;
}
 
Example 6
Source File: BlockShulkerBoxUndyed.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    this.getLevel().setBlock(block, this, true, true);
    CompoundTag nbt = new CompoundTag("")
            .putString("id", BlockEntity.SHULKER_BOX)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z);

    if (item.getNamedTag() != null && item.getNamedTag().contains("List")){
        nbt.putList(item.getNamedTag().getList("Items"));
    }

    if (item.hasCustomName()) {
        nbt.putString("CustomName", item.getCustomName());
    }

    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }

    new BlockEntityShulkerBox(this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
    return true;
}
 
Example 7
Source File: BlockBannerStanding.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
	if (face == BlockFace.DOWN) {
		return false;
	}

	CompoundTag nbt = new CompoundTag()
			.putString("id", BlockEntity.BANNER)
            .putInt("x", (int) block.x)
            .putInt("y", (int) block.y)
            .putInt("z", (int) block.z)
            .putInt("Base", item.getDamage());

	if (item.hasCompoundTag()) {
		CompoundTag tag = item.getNamedTag();
		if (tag.contains("Patterns")) {
			nbt.putList(tag.getList("Patterns"));
		}
	}

    if (face == BlockFace.UP) {
        meta = (int) Math.floor(((player.yaw + 180) * 16 / 360) + 0.5) & 0x0f;
        getLevel().setBlock(block, new BlockBannerStanding(meta), true);
    } else {
        meta = face.getIndex();
        getLevel().setBlock(block, new BlockBannerWall(meta), true);
    }

    new BlockEntityBanner(getLevel().getChunk((int) block.x >> 4, (int) block.z >> 4), nbt);
    return true;
}
 
Example 8
Source File: BlockShulkerBox.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    this.getLevel().setBlock(block, this, true, true);
    CompoundTag nbt = new CompoundTag("")
            .putString("id", BlockEntity.SHULKER_BOX)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z);

    if (item.getNamedTag() != null && item.getNamedTag().contains("List")){
        nbt.putList(item.getNamedTag().getList("Items"));
    }

    if (item.hasCustomName()) {
        nbt.putString("CustomName", item.getCustomName());
    }

    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }

    new BlockEntityShulkerBox(this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
    return true;
}
 
Example 9
Source File: BlockEntityPistonArm.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public BlockEntityPistonArm(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);
    if (nbt.contains("Progress")) {
        this.progress = nbt.getFloat("Progress");
    }

    if (nbt.contains("LastProgress")) {
        this.lastProgress = (float) nbt.getInt("LastProgress");
    }

    if (nbt.contains("Sticky")) {
        this.sticky = nbt.getBoolean("Sticky");
    }

    if (nbt.contains("Extending")) {
        this.extending = nbt.getBoolean("Extending");
    }

    if (nbt.contains("powered")) {
        this.powered = nbt.getBoolean("powered");
    }

    if (nbt.contains("AttachedBlocks")) {
        ListTag blocks = nbt.getList("AttachedBlocks", IntTag.class);
        if (blocks != null && blocks.size() > 0) {
            this.attachedBlock = new Vector3((double) ((IntTag) blocks.get(0)).getData().intValue(), (double) ((IntTag) blocks.get(1)).getData().intValue(), (double) ((IntTag) blocks.get(2)).getData().intValue());
        }
    } else {
        nbt.putList(new ListTag("AttachedBlocks"));
    }

}
 
Example 10
Source File: BlockEntityBanner.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public BlockEntityBanner(FullChunk chunk, CompoundTag nbt) {
	super(chunk, nbt);

	if (!nbt.contains("Base")) {
		nbt.putInt("Base", 0);
	}
	if (!nbt.contains("Patterns")) {
		nbt.putList(new ListTag<>("Patterns"));
	}
}
 
Example 11
Source File: ItemBanner.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void addPattern(BannerPattern pattern) {
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();
    ListTag<CompoundTag> patterns = tag.getList("Patterns", CompoundTag.class);
    patterns.add(new CompoundTag("").
            putInt("Color", pattern.getColor().getDyeData() & 0x0f).
            putString("Pattern", pattern.getType().getName()));
    tag.putList(patterns);
    this.setNamedTag(tag);
}
 
Example 12
Source File: ItemBookWritten.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public Item writeBook(String author, String title, ListTag<CompoundTag> pages) {
    if (pages.size() > 50 || pages.size() <= 0) return this; //Minecraft does not support more than 50 pages
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();

    tag.putString("author", author);
    tag.putString("title", title);
    tag.putList(pages);

    tag.putInt("generation", GENERATION_ORIGINAL);
    tag.putString("xuid", "");

    return this.setNamedTag(tag);
}
 
Example 13
Source File: ItemBookWritable.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns an list containing all pages of this book.
 */
public List getPages() {
    CompoundTag tag = this.hasCompoundTag() ? this.getNamedTag() : new CompoundTag();
    ListTag<CompoundTag> pages;
    if (!tag.contains("pages") || !(tag.get("pages") instanceof ListTag)) {
        pages = new ListTag<>("pages");
        tag.putList(pages);
    } else {
        pages = tag.getList("pages", CompoundTag.class);
    }
    return pages.parseValue();
}
 
Example 14
Source File: Chunk.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toBinary() {
    CompoundTag nbt = this.getNBT().copy();

    nbt.putInt("xPos", this.getX());
    nbt.putInt("zPos", this.getZ());

    if (this.isGenerated()) {
        nbt.putByteArray("Blocks", this.getBlockIdArray());
        nbt.putByteArray("Data", this.getBlockDataArray());
        nbt.putByteArray("SkyLight", this.getBlockSkyLightArray());
        nbt.putByteArray("BlockLight", this.getBlockLightArray());
        nbt.putIntArray("BiomeColors", this.getBiomeColorArray());

        int[] heightInts = new int[256];
        byte[] heightBytes = this.getHeightMapArray();
        for (int i = 0; i < heightInts.length; i++) {
            heightInts[i] = heightBytes[i] & 0xFF;
        }
        nbt.putIntArray("HeightMap", heightInts);
    }


    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);

    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);

    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }

    nbt.putByteArray("ExtraData", extraData.getBuffer());

    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);

    try {
        return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 15
Source File: Chunk.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.remove("BiomeColors");

    nbt.putInt("xPos", this.getX());
    nbt.putInt("zPos", this.getZ());

    if (this.isGenerated()) {
        nbt.putByteArray("Blocks", this.getBlockIdArray());
        nbt.putByteArray("Data", this.getBlockDataArray());
        nbt.putByteArray("SkyLight", this.getBlockSkyLightArray());
        nbt.putByteArray("BlockLight", this.getBlockLightArray());
        nbt.putByteArray("Biomes", this.getBiomeIdArray());

        int[] heightInts = new int[256];
        byte[] heightBytes = this.getHeightMapArray();
        for (int i = 0; i < heightInts.length; i++) {
            heightInts[i] = heightBytes[i] & 0xFF;
        }
        nbt.putIntArray("HeightMap", heightInts);
    }


    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);

    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);

    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }

    nbt.putByteArray("ExtraData", extraData.getBuffer());

    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);

    try {
        return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 16
Source File: EntityHuman.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void saveNBT() {
    super.saveNBT();

    if (skin != null) {
        CompoundTag skinTag = new CompoundTag()
                .putByteArray("Data", this.getSkin().getSkinData().data)
                .putInt("SkinImageWidth", this.getSkin().getSkinData().width)
                .putInt("SkinImageHeight", this.getSkin().getSkinData().height)
                .putString("ModelId", this.getSkin().getSkinId())
                .putString("CapeId", this.getSkin().getCapeId())
                .putByteArray("CapeData", this.getSkin().getCapeData().data)
                .putInt("CapeImageWidth", this.getSkin().getCapeData().width)
                .putInt("CapeImageHeight", this.getSkin().getCapeData().height)
                .putByteArray("SkinResourcePatch", this.getSkin().getSkinResourcePatch().getBytes(StandardCharsets.UTF_8))
                .putByteArray("GeometryData", this.getSkin().getGeometryData().getBytes(StandardCharsets.UTF_8))
                .putByteArray("AnimationData", this.getSkin().getAnimationData().getBytes(StandardCharsets.UTF_8))
                .putBoolean("PremiumSkin", this.getSkin().isPremium())
                .putBoolean("PersonaSkin", this.getSkin().isPersona())
                .putBoolean("CapeOnClassicSkin", this.getSkin().isCapeOnClassic())
                .putString("ArmSize", this.getSkin().getArmSize())
                .putString("SkinColor", this.getSkin().getSkinColor())
                .putBoolean("IsTrustedSkin", this.getSkin().isTrusted());
        List<SkinAnimation> animations = this.getSkin().getAnimations();
        if (!animations.isEmpty()) {
            ListTag<CompoundTag> animationsTag = new ListTag<>("AnimationImageData");
            for (SkinAnimation animation : animations) {
                animationsTag.add(new CompoundTag()
                        .putFloat("Frames", animation.frames)
                        .putInt("Type", animation.type)
                        .putInt("ImageWidth", animation.image.width)
                        .putInt("ImageHeight", animation.image.height)
                        .putByteArray("Image", animation.image.data));
            }
            skinTag.putList(animationsTag);
        }
        List<PersonaPiece> personaPieces = this.getSkin().getPersonaPieces();
        if (!personaPieces.isEmpty()) {
            ListTag<CompoundTag> piecesTag = new ListTag<>("PersonaPieces");
            for (PersonaPiece piece : personaPieces) {
                piecesTag.add(new CompoundTag().putString("PieceId", piece.id)
                        .putString("PieceType", piece.type)
                        .putString("PackId", piece.packId)
                        .putBoolean("IsDefault", piece.isDefault)
                        .putString("ProductId", piece.productId));
            }
        }
        List<PersonaPieceTint> tints = this.getSkin().getTintColors();
        if (!tints.isEmpty()) {
            ListTag<CompoundTag> tintsTag = new ListTag<>("PieceTintColors");
            for (PersonaPieceTint tint : tints) {
                ListTag<StringTag> colors = new ListTag<>("Colors");
                colors.setAll(tint.colors.stream().map(s -> new StringTag("", s)).collect(Collectors.toList()));
                tintsTag.add(new CompoundTag()
                        .putString("PieceType", tint.pieceType)
                        .putList(colors));
            }
        }
        this.namedTag.putCompound("Skin", skinTag);
    }
}
 
Example 17
Source File: Chunk.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toBinary() {
    CompoundTag nbt = this.getNBT().copy();

    nbt.putInt("xPos", this.x);
    nbt.putInt("zPos", this.z);

    ListTag<CompoundTag> sectionList = new ListTag<>("Sections");
    for (cn.nukkit.level.format.ChunkSection section : this.getSections()) {
        if (section instanceof EmptyChunkSection) {
            continue;
        }
        CompoundTag s = new CompoundTag(null);
        s.putByte("Y", (section.getY()));
        s.putByteArray("Blocks", section.getIdArray());
        s.putByteArray("Data", section.getDataArray());
        s.putByteArray("BlockLight", section.getLightArray());
        s.putByteArray("SkyLight", section.getSkyLightArray());
        sectionList.add(s);
    }
    nbt.putList(sectionList);

    nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
    nbt.putIntArray("HeightMap", this.getHeightMapArray());

    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);

    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);

    List<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this);

    if (entries != null) {
        ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks");
        long totalTime = this.provider.getLevel().getCurrentTick();

        for (BlockUpdateEntry entry : entries) {
            CompoundTag entryNBT = new CompoundTag()
                    .putString("i", entry.block.getClass().getSimpleName())
                    .putInt("x", entry.pos.getFloorX())
                    .putInt("y", entry.pos.getFloorY())
                    .putInt("z", entry.pos.getFloorZ())
                    .putInt("t", (int) (entry.delay - totalTime))
                    .putInt("p", entry.priority);
            tileTickTag.add(entryNBT);
        }

        nbt.putList(tileTickTag);
    }

    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }

    nbt.putByteArray("ExtraData", extraData.getBuffer());

    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);

    try {
        return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 18
Source File: Chunk.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toFastBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.putInt("xPos", this.x);
    nbt.putInt("zPos", this.z);

    nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
    nbt.putIntArray("HeightMap", this.getHeightMapArray());

    for (cn.nukkit.level.format.ChunkSection section : this.getSections()) {
        if (section instanceof EmptyChunkSection) {
            continue;
        }
        CompoundTag s = new CompoundTag(null);
        s.putByte("Y", section.getY());
        s.putByteArray("Blocks", section.getIdArray());
        s.putByteArray("Data", section.getDataArray());
        s.putByteArray("BlockLight", section.getLightArray());
        s.putByteArray("SkyLight", section.getSkyLightArray());
        nbt.getList("Sections", CompoundTag.class).add(s);
    }

    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);

    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);

    List<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this);

    if (entries != null) {
        ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks");
        long totalTime = this.provider.getLevel().getCurrentTick();

        for (BlockUpdateEntry entry : entries) {
            CompoundTag entryNBT = new CompoundTag()
                    .putString("i", entry.block.getClass().getSimpleName())
                    .putInt("x", entry.pos.getFloorX())
                    .putInt("y", entry.pos.getFloorY())
                    .putInt("z", entry.pos.getFloorZ())
                    .putInt("t", (int) (entry.delay - totalTime))
                    .putInt("p", entry.priority);
            tileTickTag.add(entryNBT);
        }

        nbt.putList(tileTickTag);
    }

    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }

    nbt.putByteArray("ExtraData", extraData.getBuffer());

    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);

    try {
        return NBTIO.write(chunk, ByteOrder.BIG_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}
 
Example 19
Source File: Chunk.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toBinary() {
    CompoundTag nbt = this.getNBT().copy();

    nbt.putInt("xPos", this.x);
    nbt.putInt("zPos", this.z);

    if (this.isGenerated()) {
        nbt.putByteArray("Blocks", this.getBlockIdArray());
        nbt.putByteArray("Data", this.getBlockDataArray());
        nbt.putByteArray("SkyLight", this.getBlockSkyLightArray());
        nbt.putByteArray("BlockLight", this.getBlockLightArray());
        nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
        nbt.putIntArray("HeightMap", this.getHeightMapArray());
    }


    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);

    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);

    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }

    nbt.putByteArray("ExtraData", extraData.getBuffer());

    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);

    try {
        return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}