Search in sources :

Example 1 with NBTInputStream

use of cn.nukkit.nbt.stream.NBTInputStream in project Nukkit by Nukkit.

the class Chunk method fromBinary.

public static Chunk fromBinary(byte[] data, LevelProvider provider) {
    try {
        int chunkX = Binary.readLInt(new byte[] { data[0], data[1], data[2], data[3] });
        int chunkZ = Binary.readLInt(new byte[] { data[4], data[5], data[6], data[7] });
        byte[] chunkData = Binary.subBytes(data, 8, data.length - 1);
        int flags = data[data.length - 1];
        List<CompoundTag> entities = new ArrayList<>();
        List<CompoundTag> tiles = new ArrayList<>();
        Map<Integer, Integer> extraDataMap = new HashMap<>();
        if (provider instanceof LevelDB) {
            byte[] entityData = ((LevelDB) provider).getDatabase().get(EntitiesKey.create(chunkX, chunkZ).toArray());
            if (entityData != null && entityData.length > 0) {
                try (NBTInputStream nbtInputStream = new NBTInputStream(new ByteArrayInputStream(entityData), ByteOrder.LITTLE_ENDIAN)) {
                    while (nbtInputStream.available() > 0) {
                        Tag tag = Tag.readNamedTag(nbtInputStream);
                        if (!(tag instanceof CompoundTag)) {
                            throw new IOException("Root tag must be a named compound tag");
                        }
                        entities.add((CompoundTag) tag);
                    }
                }
            }
            byte[] tileData = ((LevelDB) provider).getDatabase().get(TilesKey.create(chunkX, chunkZ).toArray());
            if (tileData != null && tileData.length > 0) {
                try (NBTInputStream nbtInputStream = new NBTInputStream(new ByteArrayInputStream(tileData), ByteOrder.LITTLE_ENDIAN)) {
                    while (nbtInputStream.available() > 0) {
                        Tag tag = Tag.readNamedTag(nbtInputStream);
                        if (!(tag instanceof CompoundTag)) {
                            throw new IOException("Root tag must be a named compound tag");
                        }
                        tiles.add((CompoundTag) tag);
                    }
                }
            }
            byte[] extraData = ((LevelDB) provider).getDatabase().get(ExtraDataKey.create(chunkX, chunkZ).toArray());
            if (extraData != null && extraData.length > 0) {
                BinaryStream stream = new BinaryStream(tileData);
                int count = stream.getInt();
                for (int i = 0; i < count; ++i) {
                    int key = stream.getInt();
                    int value = stream.getShort();
                    extraDataMap.put(key, value);
                }
            }
            /*if (!entities.isEmpty() || !blockEntities.isEmpty()) {
                    CompoundTag ct = new CompoundTag();
                    ListTag<CompoundTag> entityList = new ListTag<>("entities");
                    ListTag<CompoundTag> tileList = new ListTag<>("blockEntities");

                    entityList.list = entities;
                    tileList.list = blockEntities;
                    ct.putList(entityList);
                    ct.putList(tileList);
                    NBTIO.write(ct, new File(Nukkit.DATA_PATH + chunkX + "_" + chunkZ + ".dat"));
                }*/
            Chunk chunk = new Chunk(provider, chunkX, chunkZ, chunkData, entities, tiles, extraDataMap);
            if ((flags & 0x01) > 0) {
                chunk.setGenerated();
            }
            if ((flags & 0x02) > 0) {
                chunk.setPopulated();
            }
            if ((flags & 0x04) > 0) {
                chunk.setLightPopulated();
            }
            return chunk;
        }
    } catch (Exception e) {
        Server.getInstance().getLogger().logException(e);
    }
    return null;
}
Also used : BinaryStream(cn.nukkit.utils.BinaryStream) IOException(java.io.IOException) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) NBTInputStream(cn.nukkit.nbt.stream.NBTInputStream) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 2 with NBTInputStream

use of cn.nukkit.nbt.stream.NBTInputStream in project Nukkit by Nukkit.

the class Chunk method clone.

@Override
public BaseChunk clone() {
    Chunk chunk = (Chunk) super.clone();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    NBTOutputStream out = new NBTOutputStream(baos);
    try {
        nbt.write(out);
        NBTInputStream in = new NBTInputStream(new ByteArrayInputStream(baos.toByteArray()));
        chunk.nbt = new CompoundTag();
        chunk.nbt.load(in);
    } catch (IOException e) {
    }
    return chunk;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NBTInputStream(cn.nukkit.nbt.stream.NBTInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BaseChunk(cn.nukkit.level.format.generic.BaseChunk) NBTOutputStream(cn.nukkit.nbt.stream.NBTOutputStream)

Aggregations

NBTInputStream (cn.nukkit.nbt.stream.NBTInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 BaseChunk (cn.nukkit.level.format.generic.BaseChunk)1 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)1 NBTOutputStream (cn.nukkit.nbt.stream.NBTOutputStream)1 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)1 Tag (cn.nukkit.nbt.tag.Tag)1 BinaryStream (cn.nukkit.utils.BinaryStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1