Search in sources :

Example 16 with BlockEntity

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

the class BaseFullChunk method unload.

@Override
public boolean unload(boolean save, boolean safe) {
    LevelProvider level = this.getProvider();
    if (level == null) {
        return true;
    }
    if (save && this.changes != 0) {
        level.saveChunk(this.getX(), this.getZ());
    }
    if (safe) {
        for (Entity entity : this.getEntities().values()) {
            if (entity instanceof Player) {
                return false;
            }
        }
    }
    for (Entity entity : new ArrayList<>(this.getEntities().values())) {
        if (entity instanceof Player) {
            continue;
        }
        entity.close();
    }
    for (BlockEntity blockEntity : new ArrayList<>(this.getBlockEntities().values())) {
        blockEntity.close();
    }
    this.provider = null;
    return true;
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) LevelProvider(cn.nukkit.level.format.LevelProvider) ArrayList(java.util.ArrayList) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 17 with BlockEntity

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

the class LevelDB method requestChunkTask.

@Override
public AsyncTask requestChunkTask(int x, int z) {
    Chunk 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);
        } 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 (Integer key : extra.values()) {
            extraData.putLInt(key);
            extraData.putLShort(extra.get(key));
        }
    } 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) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 18 with BlockEntity

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

the class BlockTrappedChest method getWeakPower.

@Override
public int getWeakPower(BlockFace face) {
    int playerCount = 0;
    BlockEntity blockEntity = this.level.getBlockEntity(this);
    if (blockEntity instanceof BlockEntityChest) {
        playerCount = ((BlockEntityChest) blockEntity).getInventory().getViewers().size();
    }
    return playerCount < 0 ? 0 : playerCount > 15 ? 15 : playerCount;
}
Also used : BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 19 with BlockEntity

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

the class BlockTrappedChest method place.

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    int[] faces = { 2, 5, 3, 4 };
    BlockEntityChest chest = null;
    this.setDamage(faces[player != null ? player.getDirection().getHorizontalIndex() : 0]);
    for (BlockFace side : Plane.HORIZONTAL) {
        if ((this.getDamage() == 4 || this.getDamage() == 5) && (side == BlockFace.WEST || side == BlockFace.EAST)) {
            continue;
        } else if ((this.getDamage() == 3 || this.getDamage() == 2) && (side == BlockFace.NORTH || side == BlockFace.SOUTH)) {
            continue;
        }
        Block c = this.getSide(side);
        if (c instanceof BlockTrappedChest && c.getDamage() == this.getDamage()) {
            BlockEntity blockEntity = this.getLevel().getBlockEntity(c);
            if (blockEntity instanceof BlockEntityChest && !((BlockEntityChest) blockEntity).isPaired()) {
                chest = (BlockEntityChest) blockEntity;
                break;
            }
        }
    }
    this.getLevel().setBlock(block, this, true, true);
    CompoundTag nbt = new CompoundTag("").putList(new ListTag<>("Items")).putString("id", BlockEntity.CHEST).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
    if (item.hasCustomName()) {
        nbt.putString("CustomName", item.getCustomName());
    }
    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }
    BlockEntity blockEntity = new BlockEntityChest(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
    if (chest != null) {
        chest.pairWith(((BlockEntityChest) blockEntity));
        ((BlockEntityChest) blockEntity).pairWith(chest);
    }
    return true;
}
Also used : BlockFace(cn.nukkit.math.BlockFace) BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) ListTag(cn.nukkit.nbt.tag.ListTag) Map(java.util.Map) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 20 with BlockEntity

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

the class BlockPistonBase method onUpdate.

@Override
public int onUpdate(int type) {
    if (type != 6 && type != 1) {
        return 0;
    } else {
        BlockEntity blockEntity = this.level.getBlockEntity(this);
        if (blockEntity instanceof BlockEntityPistonArm) {
            BlockEntityPistonArm arm = (BlockEntityPistonArm) blockEntity;
            boolean powered = this.isPowered();
            if (arm.powered != powered) {
                this.level.getServer().getPluginManager().callEvent(new BlockPistonChangeEvent(this, powered ? 0 : 15, powered ? 15 : 0));
                arm.powered = !arm.powered;
                if (arm.chunk != null) {
                    arm.chunk.setChanged();
                }
            }
        }
        return type;
    }
}
Also used : BlockEntityPistonArm(cn.nukkit.blockentity.BlockEntityPistonArm) BlockPistonChangeEvent(cn.nukkit.event.block.BlockPistonChangeEvent) 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