Search in sources :

Example 16 with Block

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

the class Level method getCollisionCubes.

public AxisAlignedBB[] getCollisionCubes(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());
    List<AxisAlignedBB> collides = new ArrayList<>();
    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)) {
                    collides.add(block.getBoundingBox());
                }
            }
        }
    }
    if (entities) {
        for (Entity ent : this.getCollidingEntities(bb.grow(0.25f, 0.25f, 0.25f), entity)) {
            collides.add(ent.boundingBox.clone());
        }
    }
    return collides.stream().toArray(AxisAlignedBB[]::new);
}
Also used : BlockEntity(cn.nukkit.blockentity.BlockEntity) Entity(cn.nukkit.entity.Entity) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block)

Example 17 with Block

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

the class BigMushroom method generate.

public boolean generate(ChunkManager level, NukkitRandom rand, Vector3 position) {
    int block = this.mushroomType;
    if (block < 0) {
        block = rand.nextBoolean() ? RED : BROWN;
    }
    Block mushroom = block == 0 ? new BlockHugeMushroomBrown() : new BlockHugeMushroomRed();
    int i = rand.nextBoundedInt(3) + 4;
    if (rand.nextBoundedInt(12) == 0) {
        i *= 2;
    }
    boolean flag = true;
    if (position.getY() >= 1 && position.getY() + i + 1 < 256) {
        for (int j = position.getFloorY(); j <= position.getY() + 1 + i; ++j) {
            int k = 3;
            if (j <= position.getY() + 3) {
                k = 0;
            }
            Vector3 pos = new Vector3();
            for (int l = position.getFloorX() - k; l <= position.getX() + k && flag; ++l) {
                for (int i1 = position.getFloorZ() - k; i1 <= position.getZ() + k && flag; ++i1) {
                    if (j >= 0 && j < 256) {
                        pos.setComponents(l, j, i1);
                        int material = level.getBlockIdAt(pos.getFloorX(), pos.getFloorY(), pos.getFloorZ());
                        if (material != Block.AIR && material != Block.LEAVES) {
                            flag = false;
                        }
                    } else {
                        flag = false;
                    }
                }
            }
        }
        if (!flag) {
            return false;
        } else {
            Vector3 pos2 = position.down();
            int block1 = level.getBlockIdAt(pos2.getFloorX(), pos2.getFloorY(), pos2.getFloorZ());
            if (block1 != Block.DIRT && block1 != Block.GRASS && block1 != Block.MYCELIUM) {
                return false;
            } else {
                int k2 = position.getFloorY() + i;
                if (block == RED) {
                    k2 = position.getFloorY() + i - 3;
                }
                for (int l2 = k2; l2 <= position.getY() + i; ++l2) {
                    int j3 = 1;
                    if (l2 < position.getY() + i) {
                        ++j3;
                    }
                    if (block == BROWN) {
                        j3 = 3;
                    }
                    int k3 = position.getFloorX() - j3;
                    int l3 = position.getFloorX() + j3;
                    int j1 = position.getFloorZ() - j3;
                    int k1 = position.getFloorZ() + j3;
                    for (int l1 = k3; l1 <= l3; ++l1) {
                        for (int i2 = j1; i2 <= k1; ++i2) {
                            int j2 = 5;
                            if (l1 == k3) {
                                --j2;
                            } else if (l1 == l3) {
                                ++j2;
                            }
                            if (i2 == j1) {
                                j2 -= 3;
                            } else if (i2 == k1) {
                                j2 += 3;
                            }
                            int meta = j2;
                            if (block == BROWN || l2 < position.getY() + i) {
                                if ((l1 == k3 || l1 == l3) && (i2 == j1 || i2 == k1)) {
                                    continue;
                                }
                                if (l1 == position.getX() - (j3 - 1) && i2 == j1) {
                                    meta = NORTH_WEST;
                                }
                                if (l1 == k3 && i2 == position.getZ() - (j3 - 1)) {
                                    meta = NORTH_WEST;
                                }
                                if (l1 == position.getX() + (j3 - 1) && i2 == j1) {
                                    meta = NORTH_EAST;
                                }
                                if (l1 == l3 && i2 == position.getZ() - (j3 - 1)) {
                                    meta = NORTH_EAST;
                                }
                                if (l1 == position.getX() - (j3 - 1) && i2 == k1) {
                                    meta = SOUTH_WEST;
                                }
                                if (l1 == k3 && i2 == position.getZ() + (j3 - 1)) {
                                    meta = SOUTH_WEST;
                                }
                                if (l1 == position.getX() + (j3 - 1) && i2 == k1) {
                                    meta = SOUTH_EAST;
                                }
                                if (l1 == l3 && i2 == position.getZ() + (j3 - 1)) {
                                    meta = SOUTH_EAST;
                                }
                            }
                            if (meta == CENTER && l2 < position.getY() + i) {
                                meta = ALL_INSIDE;
                            }
                            if (position.getY() >= position.getY() + i - 1 || meta != ALL_INSIDE) {
                                Vector3 blockPos = new Vector3(l1, l2, i2);
                                if (!Block.solid[level.getBlockIdAt(blockPos.getFloorX(), blockPos.getFloorY(), blockPos.getFloorZ())]) {
                                    mushroom.setDamage(meta);
                                    this.setBlockAndNotifyAdequately(level, blockPos, mushroom);
                                }
                            }
                        }
                    }
                }
                for (int i3 = 0; i3 < i; ++i3) {
                    Vector3 pos = position.up(i3);
                    int id = level.getBlockIdAt(pos.getFloorX(), pos.getFloorY(), pos.getFloorZ());
                    if (!Block.solid[id]) {
                        mushroom.setDamage(STEM);
                        this.setBlockAndNotifyAdequately(level, pos, mushroom);
                    }
                }
                return true;
            }
        }
    } else {
        return false;
    }
}
Also used : BlockHugeMushroomBrown(cn.nukkit.block.BlockHugeMushroomBrown) BlockHugeMushroomRed(cn.nukkit.block.BlockHugeMushroomRed) Block(cn.nukkit.block.Block) Vector3(cn.nukkit.math.Vector3)

Example 18 with Block

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

the class BlockUpdateScheduler method perform.

private void perform(long tick) {
    try {
        lastTick = tick;
        Set<BlockUpdateEntry> updates = pendingUpdates = queuedUpdates.remove(tick);
        if (updates != null) {
            for (BlockUpdateEntry entry : updates) {
                Vector3 pos = entry.pos;
                if (level.isChunkLoaded(NukkitMath.floorDouble(pos.x) >> 4, NukkitMath.floorDouble(pos.z) >> 4)) {
                    Block block = level.getBlock(entry.pos);
                    if (Block.equals(block, entry.block, false)) {
                        block.onUpdate(level.BLOCK_UPDATE_SCHEDULED);
                    }
                } else {
                    level.scheduleUpdate(entry.block, entry.pos, 0);
                }
            }
        }
    } finally {
        pendingUpdates = null;
    }
}
Also used : BlockUpdateEntry(cn.nukkit.utils.BlockUpdateEntry) Block(cn.nukkit.block.Block) Vector3(cn.nukkit.math.Vector3)

Example 19 with Block

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

the class ItemBucket method onActivate.

@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    Block targetBlock = Block.get(this.meta);
    if (targetBlock instanceof BlockAir) {
        if (target instanceof BlockLiquid && target.getDamage() == 0) {
            Item result = Item.get(BUCKET, this.getDamageByTarget(target.getId()), 1);
            PlayerBucketFillEvent ev;
            player.getServer().getPluginManager().callEvent(ev = new PlayerBucketFillEvent(player, block, face, this, result));
            if (!ev.isCancelled()) {
                player.getLevel().setBlock(target, new BlockAir(), true, true);
                // replaced with water that can flow.
                for (BlockFace side : Plane.HORIZONTAL) {
                    Block b = target.getSide(side);
                    if (b.getId() == STILL_WATER) {
                        level.setBlock(b, new BlockWater());
                    }
                }
                if (player.isSurvival()) {
                    Item clone = this.clone();
                    clone.setCount(this.getCount() - 1);
                    player.getInventory().setItemInHand(clone);
                    player.getInventory().addItem(ev.getItem());
                }
                return true;
            } else {
                player.getInventory().sendContents(player);
            }
        }
    } else if (targetBlock instanceof BlockLiquid) {
        Item result = Item.get(BUCKET, 0, 1);
        PlayerBucketEmptyEvent ev;
        player.getServer().getPluginManager().callEvent(ev = new PlayerBucketEmptyEvent(player, block, face, this, result));
        if (!ev.isCancelled()) {
            player.getLevel().setBlock(block, targetBlock, true, true);
            if (player.isSurvival()) {
                Item clone = this.clone();
                clone.setCount(this.getCount() - 1);
                player.getInventory().setItemInHand(clone);
                player.getInventory().addItem(ev.getItem());
            }
            return true;
        } else {
            player.getInventory().sendContents(player);
        }
    }
    return false;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) PlayerBucketFillEvent(cn.nukkit.event.player.PlayerBucketFillEvent) BlockLiquid(cn.nukkit.block.BlockLiquid) BlockFace(cn.nukkit.math.BlockFace) Block(cn.nukkit.block.Block) BlockWater(cn.nukkit.block.BlockWater) PlayerBucketEmptyEvent(cn.nukkit.event.player.PlayerBucketEmptyEvent)

Example 20 with Block

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

the class EntityMinecartAbstract method getNextRail.

private Vector3 getNextRail(double dx, double dy, double dz) {
    int checkX = MathHelper.floor(dx);
    int checkY = MathHelper.floor(dy);
    int checkZ = MathHelper.floor(dz);
    if (Rail.isRailBlock(level.getBlockIdAt(checkX, checkY - 1, checkZ))) {
        --checkY;
    }
    Block block = level.getBlock(new Vector3(checkX, checkY, checkZ));
    if (Rail.isRailBlock(block)) {
        int[][] facing = matrix[((BlockRail) block).getRealMeta()];
        double rail;
        // Genisys mistake (Doesn't check surrounding more exactly)
        double nextOne = (double) checkX + 0.5D + (double) facing[0][0] * 0.5D;
        double nextTwo = (double) checkY + 0.5D + (double) facing[0][1] * 0.5D;
        double nextThree = (double) checkZ + 0.5D + (double) facing[0][2] * 0.5D;
        double nextFour = (double) checkX + 0.5D + (double) facing[1][0] * 0.5D;
        double nextFive = (double) checkY + 0.5D + (double) facing[1][1] * 0.5D;
        double nextSix = (double) checkZ + 0.5D + (double) facing[1][2] * 0.5D;
        double nextSeven = nextFour - nextOne;
        double nextEight = (nextFive - nextTwo) * 2;
        double nextMax = nextSix - nextThree;
        if (nextSeven == 0) {
            rail = dz - (double) checkZ;
        } else if (nextMax == 0) {
            rail = dx - (double) checkX;
        } else {
            double whatOne = dx - nextOne;
            double whatTwo = dz - nextThree;
            rail = (whatOne * nextSeven + whatTwo * nextMax) * 2;
        }
        dx = nextOne + nextSeven * rail;
        dy = nextTwo + nextEight * rail;
        dz = nextThree + nextMax * rail;
        if (nextEight < 0) {
            ++dy;
        }
        if (nextEight > 0) {
            dy += 0.5D;
        }
        return new Vector3(dx, dy, dz);
    } else {
        return null;
    }
}
Also used : Block(cn.nukkit.block.Block) Vector3(cn.nukkit.math.Vector3)

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