Search in sources :

Example 1 with ChunkException

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

the class McRegion method requestChunkTask.

@Override
public AsyncTask requestChunkTask(int x, int z) throws ChunkException {
    BaseFullChunk chunk = this.getChunk(x, z, false);
    if (chunk == null) {
        throw new ChunkException("Invalid Chunk Sent");
    }
    long timestamp = chunk.getChanges();
    byte[] tiles = 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 {
            tiles = 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.putLInt(extra.size());
        for (Map.Entry<Integer, Integer> entry : extra.entrySet()) {
            extraData.putLInt(entry.getKey());
            extraData.putLShort(entry.getValue());
        }
    } else {
        extraData = null;
    }
    BinaryStream stream = new BinaryStream();
    stream.put(chunk.getBlockIdArray());
    stream.put(chunk.getBlockDataArray());
    stream.put(chunk.getBlockSkyLightArray());
    stream.put(chunk.getBlockLightArray());
    stream.put(chunk.getHeightMapArray());
    for (int color : chunk.getBiomeColorArray()) {
        stream.put(Binary.writeInt(color));
    }
    if (extraData != null) {
        stream.put(extraData.getBuffer());
    } else {
        stream.putLInt(0);
    }
    stream.put(tiles);
    this.getLevel().chunkRequestCallback(timestamp, x, z, stream.getBuffer());
    return null;
}
Also used : BlockEntitySpawnable(cn.nukkit.blockentity.BlockEntitySpawnable) ArrayList(java.util.ArrayList) BinaryStream(cn.nukkit.utils.BinaryStream) IOException(java.io.IOException) ChunkException(cn.nukkit.utils.ChunkException) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) HashMap(java.util.HashMap) Map(java.util.Map) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 2 with ChunkException

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

BlockEntity (cn.nukkit.blockentity.BlockEntity)2 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)2 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)2 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)2 BinaryStream (cn.nukkit.utils.BinaryStream)2 ChunkException (cn.nukkit.utils.ChunkException)2 IOException (java.io.IOException)2 FullChunk (cn.nukkit.level.format.FullChunk)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1