Search in sources :

Example 11 with BinaryStream

use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.

the class VarIntTest method testWrite.

@DisplayName("Writing")
@Test
void testWrite() throws IOException {
    BinaryStream bs = new BinaryStream();
    VarInt.writeUnsignedVarInt(bs, 237356812);
    VarInt.writeVarInt(bs, 0xea3eca71);
    VarInt.writeUnsignedVarLong(bs, 0x1234567812345678L);
    VarInt.writeVarLong(bs, 0xea3eca710becececL);
    assertAll(() -> assertEquals(237356812, VarInt.readUnsignedVarInt(bs)), () -> assertEquals(0xea3eca71, VarInt.readVarInt(bs)), () -> assertEquals(0x1234567812345678L, VarInt.readUnsignedVarLong(bs)), () -> assertEquals(0xea3eca710becececL, VarInt.readVarLong(bs)));
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    VarInt.writeUnsignedVarInt(os, 237356812);
    VarInt.writeVarInt(os, 0xea3eca71);
    VarInt.writeUnsignedVarLong(os, 0x1234567812345678L);
    VarInt.writeVarLong(os, 0xea3eca710becececL);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    assertAll(() -> assertEquals(237356812, VarInt.readUnsignedVarInt(is)), () -> assertEquals(0xea3eca71, VarInt.readVarInt(is)), () -> assertEquals(0x1234567812345678L, VarInt.readUnsignedVarLong(is)), () -> assertEquals(0xea3eca710becececL, VarInt.readVarLong(is)));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryStream(cn.nukkit.utils.BinaryStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 12 with BinaryStream

use of cn.nukkit.utils.BinaryStream 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)

Example 13 with BinaryStream

use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.

the class Anvil method requestChunkTask.

@Override
public AsyncTask requestChunkTask(int x, int z) throws ChunkException {
    Chunk chunk = (Chunk) this.getChunk(x, z, false);
    if (chunk == null) {
        throw new ChunkException("Invalid Chunk Set");
    }
    long timestamp = chunk.getChanges();
    byte[] blockEntities = new byte[0];
    if (!chunk.getBlockEntities().isEmpty()) {
        List<CompoundTag> tagList = new ArrayList<>();
        for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
            if (blockEntity instanceof BlockEntitySpawnable) {
                tagList.add(((BlockEntitySpawnable) blockEntity).getSpawnCompound());
            }
        }
        try {
            blockEntities = NBTIO.write(tagList, ByteOrder.LITTLE_ENDIAN, true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    Map<Integer, Integer> extra = chunk.getBlockExtraDataArray();
    BinaryStream extraData;
    if (!extra.isEmpty()) {
        extraData = new BinaryStream();
        extraData.putVarInt(extra.size());
        for (Map.Entry<Integer, Integer> entry : extra.entrySet()) {
            extraData.putVarInt(entry.getKey());
            extraData.putLShort(entry.getValue());
        }
    } else {
        extraData = null;
    }
    BinaryStream stream = ThreadCache.binaryStream.get().reset();
    int count = 0;
    cn.nukkit.level.format.ChunkSection[] sections = chunk.getSections();
    for (int i = sections.length - 1; i >= 0; i--) {
        if (!sections[i].isEmpty()) {
            count = i + 1;
            break;
        }
    }
    stream.putByte((byte) count);
    for (int i = 0; i < count; i++) {
        stream.putByte((byte) 0);
        stream.put(sections[i].getBytes());
    }
    for (byte height : chunk.getHeightMapArray()) {
        stream.putByte(height);
    }
    stream.put(PAD_256);
    stream.put(chunk.getBiomeIdArray());
    stream.putByte((byte) 0);
    if (extraData != null) {
        stream.put(extraData.getBuffer());
    } else {
        stream.putVarInt(0);
    }
    stream.put(blockEntities);
    this.getLevel().chunkRequestCallback(timestamp, x, z, stream.getBuffer());
    return null;
}
Also used : BlockEntitySpawnable(cn.nukkit.blockentity.BlockEntitySpawnable) BinaryStream(cn.nukkit.utils.BinaryStream) IOException(java.io.IOException) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk) ChunkException(cn.nukkit.utils.ChunkException) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

BinaryStream (cn.nukkit.utils.BinaryStream)13 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)5 BlockEntity (cn.nukkit.blockentity.BlockEntity)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)3 Player (cn.nukkit.Player)2 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)2 Entity (cn.nukkit.entity.Entity)2 ChunkException (cn.nukkit.utils.ChunkException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CommandOverload (cn.nukkit.command.data.CommandOverload)1 CommandParameter (cn.nukkit.command.data.CommandParameter)1 Item (cn.nukkit.item.Item)1 FullChunk (cn.nukkit.level.format.FullChunk)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 NBTInputStream (cn.nukkit.nbt.stream.NBTInputStream)1