Search in sources :

Example 11 with BlockEntity

use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.

the class Level method setChunk.

public void setChunk(int chunkX, int chunkZ, BaseFullChunk chunk, boolean unload) {
    if (chunk == null) {
        return;
    }
    long index = Level.chunkHash(chunkX, chunkZ);
    FullChunk oldChunk = this.getChunk(chunkX, chunkZ, false);
    if (unload && oldChunk != null) {
        this.unloadChunk(chunkX, chunkZ, false, false);
        this.provider.setChunk(chunkX, chunkZ, chunk);
    } else {
        Map<Long, Entity> oldEntities = oldChunk != null ? oldChunk.getEntities() : Collections.emptyMap();
        Map<Long, BlockEntity> oldBlockEntities = oldChunk != null ? oldChunk.getBlockEntities() : Collections.emptyMap();
        if (!oldEntities.isEmpty()) {
            Iterator<Map.Entry<Long, Entity>> iter = oldEntities.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<Long, Entity> entry = iter.next();
                Entity entity = entry.getValue();
                chunk.addEntity(entity);
                if (oldChunk != null) {
                    iter.remove();
                    oldChunk.removeEntity(entity);
                    entity.chunk = chunk;
                }
            }
        }
        if (!oldBlockEntities.isEmpty()) {
            Iterator<Map.Entry<Long, BlockEntity>> iter = oldBlockEntities.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<Long, BlockEntity> entry = iter.next();
                BlockEntity blockEntity = entry.getValue();
                chunk.addBlockEntity(blockEntity);
                if (oldChunk != null) {
                    iter.remove();
                    oldChunk.removeBlockEntity(blockEntity);
                    blockEntity.chunk = chunk;
                }
            }
        }
        this.provider.setChunk(chunkX, chunkZ, chunk);
    }
    chunk.setChanged();
    if (!this.isChunkInUse(index)) {
        this.unloadChunkRequest(chunkX, chunkZ);
    } else {
        for (ChunkLoader loader : this.getChunkLoaders(chunkX, chunkZ)) {
            loader.onChunkChanged(chunk);
        }
    }
}
Also used : BlockEntity(cn.nukkit.blockentity.BlockEntity) Entity(cn.nukkit.entity.Entity) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 12 with BlockEntity

use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.

the class Chunk method toBinary.

@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);
    }
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) ArrayList(java.util.ArrayList) BinaryStream(cn.nukkit.utils.BinaryStream) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 13 with BlockEntity

use of cn.nukkit.blockentity.BlockEntity 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 14 with BlockEntity

use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.

the class Chunk method toFastBinary.

@Override
public byte[] toFastBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.putInt("xPos", this.getX());
    nbt.putInt("zPos", this.getZ());
    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;
    }
    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);
    Set<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.getSaveId()).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);
    }
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) IOException(java.io.IOException) EmptyChunkSection(cn.nukkit.level.format.generic.EmptyChunkSection) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 15 with BlockEntity

use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.

the class BaseFullChunk method initChunk.

public void initChunk() {
    if (this.getProvider() != null && !this.isInit) {
        boolean changed = false;
        if (this.NBTentities != null) {
            this.getProvider().getLevel().timings.syncChunkLoadEntitiesTimer.startTiming();
            for (CompoundTag nbt : NBTentities) {
                if (!nbt.contains("id")) {
                    this.setChanged();
                    continue;
                }
                ListTag pos = nbt.getList("Pos");
                if ((((NumberTag) pos.get(0)).getData().intValue() >> 4) != this.getX() || ((((NumberTag) pos.get(2)).getData().intValue() >> 4) != this.getZ())) {
                    changed = true;
                    continue;
                }
                Entity entity = Entity.createEntity(nbt.getString("id"), this, nbt);
                if (entity != null) {
                    changed = true;
                    continue;
                }
            }
            this.getProvider().getLevel().timings.syncChunkLoadEntitiesTimer.stopTiming();
            this.NBTentities = null;
        }
        if (this.NBTtiles != null) {
            this.getProvider().getLevel().timings.syncChunkLoadBlockEntitiesTimer.startTiming();
            for (CompoundTag nbt : NBTtiles) {
                if (nbt != null) {
                    if (!nbt.contains("id")) {
                        changed = true;
                        continue;
                    }
                    if ((nbt.getInt("x") >> 4) != this.getX() || ((nbt.getInt("z") >> 4) != this.getZ())) {
                        changed = true;
                        continue;
                    }
                    BlockEntity blockEntity = BlockEntity.createBlockEntity(nbt.getString("id"), this, nbt);
                    if (blockEntity == null) {
                        changed = true;
                        continue;
                    }
                }
            }
            this.getProvider().getLevel().timings.syncChunkLoadBlockEntitiesTimer.stopTiming();
            this.NBTtiles = null;
        }
        this.setChanged(changed);
        this.isInit = true;
    }
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) NumberTag(cn.nukkit.nbt.tag.NumberTag) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

BlockEntity (cn.nukkit.blockentity.BlockEntity)32 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)13 Entity (cn.nukkit.entity.Entity)7 Player (cn.nukkit.Player)6 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)6 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)6 StringTag (cn.nukkit.nbt.tag.StringTag)6 IOException (java.io.IOException)6 Item (cn.nukkit.item.Item)5 FullChunk (cn.nukkit.level.format.FullChunk)5 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)4 ListTag (cn.nukkit.nbt.tag.ListTag)4 BinaryStream (cn.nukkit.utils.BinaryStream)4 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)3 ArrayList (java.util.ArrayList)3 BlockEntityComparator (cn.nukkit.blockentity.BlockEntityComparator)2 BlockEntityFlowerPot (cn.nukkit.blockentity.BlockEntityFlowerPot)2 Tag (cn.nukkit.nbt.tag.Tag)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 Long2ObjectMap (it.unimi.dsi.fastutil.longs.Long2ObjectMap)2