Search in sources :

Example 11 with Block

use of cn.nukkit.block.Block in project Nukkit by Nukkit.

the class EntityLightning method initEntity.

@Override
protected void initEntity() {
    super.initEntity();
    this.setHealth(4);
    this.setMaxHealth(4);
    this.state = 2;
    this.liveTime = ThreadLocalRandom.current().nextInt(3) + 1;
    if (isEffect && this.level.gameRules.getBoolean(GameRule.DO_FIRE_TICK) && (this.server.getDifficulty() >= 2)) {
        Block block = this.getLevelBlock();
        if (block.getId() == 0 || block.getId() == Block.TALL_GRASS) {
            BlockFire fire = new BlockFire();
            fire.x = block.x;
            fire.y = block.y;
            fire.z = block.z;
            fire.level = level;
            this.getLevel().setBlock(fire, fire, true);
            if (fire.isBlockTopFacingSurfaceSolid(fire.down()) || fire.canNeighborBurn()) {
                BlockIgniteEvent e = new BlockIgniteEvent(block, null, this, BlockIgniteEvent.BlockIgniteCause.LIGHTNING);
                getServer().getPluginManager().callEvent(e);
                if (!e.isCancelled()) {
                    level.setBlock(fire, fire, true);
                    level.scheduleUpdate(fire, fire.tickRate() + ThreadLocalRandom.current().nextInt(10));
                }
            }
        }
    }
}
Also used : Block(cn.nukkit.block.Block) BlockIgniteEvent(cn.nukkit.event.block.BlockIgniteEvent) BlockFire(cn.nukkit.block.BlockFire)

Example 12 with Block

use of cn.nukkit.block.Block in project Nukkit by Nukkit.

the class Level method getBlock.

public Block getBlock(int x, int y, int z) {
    int fullState;
    if (y >= 0 && y < 256) {
        int cx = x >> 4;
        int cz = z >> 4;
        BaseFullChunk chunk = getChunk(cx, cz);
        if (chunk != null) {
            fullState = chunk.getFullBlock(x & 0xF, y, z & 0xF);
        } else {
            fullState = 0;
        }
    } else {
        fullState = 0;
    }
    Block block = this.blockStates[fullState & 0xFFF].clone();
    block.x = x;
    block.y = y;
    block.z = z;
    block.level = this;
    return block;
}
Also used : BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block)

Example 13 with Block

use of cn.nukkit.block.Block in project Nukkit by Nukkit.

the class Level method hasCollision.

public boolean hasCollision(Entity entity, AxisAlignedBB bb, boolean entities) {
    int minX = NukkitMath.floorDouble(bb.getMinX());
    int minY = NukkitMath.floorDouble(bb.getMinY());
    int minZ = NukkitMath.floorDouble(bb.getMinZ());
    int maxX = NukkitMath.ceilDouble(bb.getMaxX());
    int maxY = NukkitMath.ceilDouble(bb.getMaxY());
    int maxZ = NukkitMath.ceilDouble(bb.getMaxZ());
    for (int z = minZ; z <= maxZ; ++z) {
        for (int x = minX; x <= maxX; ++x) {
            for (int y = minY; y <= maxY; ++y) {
                Block block = this.getBlock(this.temporalVector.setComponents(x, y, z));
                if (!block.canPassThrough() && block.collidesWithBB(bb)) {
                    return true;
                }
            }
        }
    }
    if (entities) {
        return this.getCollidingEntities(bb.grow(0.25f, 0.25f, 0.25f), entity).length > 0;
    }
    return false;
}
Also used : ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block)

Example 14 with Block

use of cn.nukkit.block.Block in project Nukkit by Nukkit.

the class Level method setBlock.

public boolean setBlock(int x, int y, int z, Block block, boolean direct, boolean update) {
    if (y < 0 || y >= 256) {
        return false;
    }
    BaseFullChunk chunk = this.getChunk(x >> 4, z >> 4, true);
    Block blockPrevious;
    // synchronized (chunk) {
    blockPrevious = chunk.getAndSetBlock(x & 0xF, y, z & 0xF, block);
    if (blockPrevious.getFullId() == block.getFullId()) {
        return false;
    }
    // }
    block.x = x;
    block.y = y;
    block.z = z;
    block.level = this;
    int cx = x >> 4;
    int cz = z >> 4;
    long index = Level.chunkHash(cx, cz);
    if (direct) {
        this.sendBlocks(this.getChunkPlayers(cx, cz).values().stream().toArray(Player[]::new), new Block[] { block }, UpdateBlockPacket.FLAG_ALL_PRIORITY);
    } else {
        addBlockChange(index, x, y, z);
    }
    for (ChunkLoader loader : this.getChunkLoaders(cx, cz)) {
        loader.onBlockChanged(block);
    }
    if (update) {
        if (blockPrevious.isTransparent() != block.isTransparent() || blockPrevious.getLightLevel() != block.getLightLevel()) {
            addLightUpdate(x, y, z);
        }
        BlockUpdateEvent ev = new BlockUpdateEvent(block);
        this.server.getPluginManager().callEvent(ev);
        if (!ev.isCancelled()) {
            for (Entity entity : this.getNearbyEntities(new SimpleAxisAlignedBB(x - 1, y - 1, z - 1, x + 1, y + 1, z + 1))) {
                entity.scheduleUpdate();
            }
            block = ev.getBlock();
            block.onUpdate(BLOCK_UPDATE_NORMAL);
            this.updateAround(x, y, z);
        }
    }
    return true;
}
Also used : BlockEntity(cn.nukkit.blockentity.BlockEntity) Entity(cn.nukkit.entity.Entity) Player(cn.nukkit.Player) BlockUpdateEvent(cn.nukkit.event.block.BlockUpdateEvent) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block)

Example 15 with Block

use of cn.nukkit.block.Block in project Nukkit by Nukkit.

the class Level method getSafeSpawn.

public Position getSafeSpawn(Vector3 spawn) {
    if (spawn == null || spawn.y < 1) {
        spawn = this.getSpawnLocation();
    }
    if (spawn != null) {
        Vector3 v = spawn.floor();
        FullChunk chunk = this.getChunk((int) v.x >> 4, (int) v.z >> 4, false);
        int x = (int) v.x & 0x0f;
        int z = (int) v.z & 0x0f;
        if (chunk != null) {
            int y = (int) Math.min(254, v.y);
            boolean wasAir = chunk.getBlockId(x, y - 1, z) == 0;
            for (; y > 0; --y) {
                int b = chunk.getFullBlock(x, y, z);
                Block block = Block.get(b >> 4, b & 0x0f);
                if (this.isFullBlock(block)) {
                    if (wasAir) {
                        y++;
                        break;
                    }
                } else {
                    wasAir = true;
                }
            }
            for (; y >= 0 && y < 256; ++y) {
                int b = chunk.getFullBlock(x, y + 1, z);
                Block block = Block.get(b >> 4, b & 0x0f);
                if (!this.isFullBlock(block)) {
                    b = chunk.getFullBlock(x, y, z);
                    block = Block.get(b >> 4, b & 0x0f);
                    if (!this.isFullBlock(block)) {
                        return new Position(spawn.x, y == (int) spawn.y ? spawn.y : y, spawn.z, this);
                    }
                } else {
                    ++y;
                }
            }
            v.y = y;
        }
        return new Position(spawn.x, v.y, spawn.z, this);
    }
    return null;
}
Also used : BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block)

Aggregations

Block (cn.nukkit.block.Block)29 ItemBlock (cn.nukkit.item.ItemBlock)13 Entity (cn.nukkit.entity.Entity)7 BlockEntity (cn.nukkit.blockentity.BlockEntity)5 Vector3 (cn.nukkit.math.Vector3)5 Player (cn.nukkit.Player)4 BlockAir (cn.nukkit.block.BlockAir)4 Item (cn.nukkit.item.Item)4 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)4 FullChunk (cn.nukkit.level.format.FullChunk)3 BlockFire (cn.nukkit.block.BlockFire)2 BlockWater (cn.nukkit.block.BlockWater)2 EntityItem (cn.nukkit.entity.item.EntityItem)2 BlockIgniteEvent (cn.nukkit.event.block.BlockIgniteEvent)2 BlockUpdateEvent (cn.nukkit.event.block.BlockUpdateEvent)2 PlayerInteractEvent (cn.nukkit.event.player.PlayerInteractEvent)2 ArrayList (java.util.ArrayList)2 BlockBrewingStand (cn.nukkit.block.BlockBrewingStand)1 BlockDirt (cn.nukkit.block.BlockDirt)1 BlockHugeMushroomBrown (cn.nukkit.block.BlockHugeMushroomBrown)1