Search in sources :

Example 1 with TilesKey

use of cn.nukkit.level.format.leveldb.key.TilesKey in project Nukkit by Nukkit.

the class Chunk method toBinary.

public byte[] toBinary(boolean saveExtra) {
    try {
        LevelProvider provider = this.getProvider();
        if (saveExtra && provider instanceof LevelDB) {
            List<CompoundTag> entities = new ArrayList<>();
            for (Entity entity : this.getEntities().values()) {
                if (!(entity instanceof Player) && !entity.closed) {
                    entity.saveNBT();
                    entities.add(entity.namedTag);
                }
            }
            EntitiesKey entitiesKey = EntitiesKey.create(this.getX(), this.getZ());
            if (!entities.isEmpty()) {
                ((LevelDB) provider).getDatabase().put(entitiesKey.toArray(), NBTIO.write(entities));
            } else {
                ((LevelDB) provider).getDatabase().delete(entitiesKey.toArray());
            }
            List<CompoundTag> tiles = new ArrayList<>();
            for (BlockEntity blockEntity : this.getBlockEntities().values()) {
                if (!blockEntity.closed) {
                    blockEntity.saveNBT();
                    entities.add(blockEntity.namedTag);
                }
            }
            TilesKey tilesKey = TilesKey.create(this.getX(), this.getZ());
            if (!tiles.isEmpty()) {
                ((LevelDB) provider).getDatabase().put(tilesKey.toArray(), NBTIO.write(tiles));
            } else {
                ((LevelDB) provider).getDatabase().delete(tilesKey.toArray());
            }
            ExtraDataKey extraDataKey = ExtraDataKey.create(this.getX(), this.getZ());
            if (!this.getBlockExtraDataArray().isEmpty()) {
                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));
                }
                ((LevelDB) provider).getDatabase().put(extraDataKey.toArray(), extraData.getBuffer());
            } else {
                ((LevelDB) provider).getDatabase().delete(extraDataKey.toArray());
            }
        }
        byte[] heightMap = this.getHeightMapArray();
        byte[] biomeColors = new byte[this.getBiomeColorArray().length * 4];
        for (int i = 0; i < this.getBiomeColorArray().length; i++) {
            byte[] bytes = Binary.writeInt(this.getBiomeColorArray()[i]);
            biomeColors[i * 4] = bytes[0];
            biomeColors[i * 4 + 1] = bytes[1];
            biomeColors[i * 4 + 2] = bytes[2];
            biomeColors[i * 4 + 3] = bytes[3];
        }
        return Binary.appendBytes(Binary.writeLInt(this.getX()), Binary.writeLInt(this.getZ()), this.getBlockIdArray(), this.getBlockDataArray(), this.getBlockSkyLightArray(), this.getBlockLightArray(), heightMap, biomeColors, new byte[] { (byte) (((this.isLightPopulated ? 0x04 : 0) | (this.isPopulated() ? 0x02 : 0) | (this.isGenerated() ? 0x01 : 0)) & 0xff) });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ExtraDataKey(cn.nukkit.level.format.leveldb.key.ExtraDataKey) Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) LevelProvider(cn.nukkit.level.format.LevelProvider) BinaryStream(cn.nukkit.utils.BinaryStream) EntitiesKey(cn.nukkit.level.format.leveldb.key.EntitiesKey) IOException(java.io.IOException) TilesKey(cn.nukkit.level.format.leveldb.key.TilesKey) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

Player (cn.nukkit.Player)1 BlockEntity (cn.nukkit.blockentity.BlockEntity)1 Entity (cn.nukkit.entity.Entity)1 LevelProvider (cn.nukkit.level.format.LevelProvider)1 EntitiesKey (cn.nukkit.level.format.leveldb.key.EntitiesKey)1 ExtraDataKey (cn.nukkit.level.format.leveldb.key.ExtraDataKey)1 TilesKey (cn.nukkit.level.format.leveldb.key.TilesKey)1 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)1 BinaryStream (cn.nukkit.utils.BinaryStream)1 IOException (java.io.IOException)1