Search in sources :

Example 21 with BlockFace

use of cn.nukkit.math.BlockFace 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 22 with BlockFace

use of cn.nukkit.math.BlockFace in project Nukkit by Nukkit.

the class BlockLever method onUpdate.

@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        int face = this.isPowerOn() ? this.getDamage() ^ 0x08 : this.getDamage();
        BlockFace faces = LeverOrientation.byMetadata(face).getFacing().getOpposite();
        if (!this.getSide(faces).isSolid()) {
            this.level.useBreakOn(this);
        }
    }
    return 0;
}
Also used : BlockFace(cn.nukkit.math.BlockFace)

Example 23 with BlockFace

use of cn.nukkit.math.BlockFace in project Nukkit by Nukkit.

the class BlockLiquid method checkForHarden.

private void checkForHarden() {
    if (this instanceof BlockLava) {
        boolean colliding = false;
        for (BlockFace face : BlockFace.values()) {
            if (colliding = this.getSide(face) instanceof BlockWater) {
                break;
            }
        }
        if (colliding) {
            Block to;
            if (this.getDamage() == 0) {
                to = new BlockObsidian();
            } else if (this.getDamage() <= 4) {
                to = new BlockCobblestone();
            } else {
                return;
            }
            to.setComponents(this.getX(), this.getY(), this.getZ());
            to.setLevel(this.getLevel());
            BlockFromToEvent ev = new BlockFromToEvent(this, to);
            this.getLevel().getServer().getPluginManager().callEvent(ev);
            if (!ev.isCancelled()) {
                this.getLevel().setBlock(this, ev.getTo(), true);
                this.triggerLavaMixEffects(this);
            }
        }
    }
}
Also used : BlockFace(cn.nukkit.math.BlockFace) BlockFromToEvent(cn.nukkit.event.block.BlockFromToEvent)

Example 24 with BlockFace

use of cn.nukkit.math.BlockFace in project Nukkit by Nukkit.

the class BlockPistonBase method doMove.

private boolean doMove(boolean extending) {
    Vector3 pos = this.getLocation();
    BlockFace direction = getFacing();
    if (!extending) {
        this.level.setBlock(pos.getSide(direction), new BlockAir(), true, false);
    }
    BlocksCalculator calculator = new BlocksCalculator(this.level, this, direction, extending);
    if (!calculator.canMove()) {
        return false;
    } else {
        List<Block> blocks = calculator.getBlocksToMove();
        List<Block> newBlocks = new ArrayList<>();
        for (int i = 0; i < blocks.size(); ++i) {
            Block block = blocks.get(i);
            newBlocks.add(block);
        }
        List<Block> destroyBlocks = calculator.getBlocksToDestroy();
        BlockFace side = extending ? direction : direction.getOpposite();
        for (int i = destroyBlocks.size() - 1; i >= 0; --i) {
            Block block = destroyBlocks.get(i);
            this.level.useBreakOn(block);
        }
        for (int i = blocks.size() - 1; i >= 0; --i) {
            Block block = blocks.get(i);
            this.level.setBlock(block, new BlockAir());
            Vector3 newPos = block.getLocation().getSide(side);
            // TODO: change this to block entity
            this.level.setBlock(newPos, newBlocks.get(i));
        }
        Vector3 pistonHead = pos.getSide(direction);
        if (extending) {
            // extension block entity
            this.level.setBlock(pistonHead, new BlockPistonHead(this.getDamage()));
        }
        return true;
    }
}
Also used : BlockFace(cn.nukkit.math.BlockFace) ArrayList(java.util.ArrayList) Vector3(cn.nukkit.math.Vector3)

Example 25 with BlockFace

use of cn.nukkit.math.BlockFace in project Nukkit by Nukkit.

the class BlockPistonBase method checkState.

private void checkState() {
    BlockFace facing = getFacing();
    boolean isPowered = this.isPowered();
    if (isPowered && !isExtended()) {
        if ((new BlocksCalculator(this.level, this, facing, true)).canMove()) {
            if (!this.doMove(true)) {
                return;
            }
            this.level.addSound(this, Sound.TILE_PISTON_OUT);
        } else {
        }
    } else if (!isPowered && isExtended()) {
        if (this.sticky) {
            Vector3 pos = this.add(facing.getXOffset() * 2, facing.getYOffset() * 2, facing.getZOffset() * 2);
            Block block = this.level.getBlock(pos);
            if (block.getId() == AIR) {
                this.level.setBlock(this.getLocation().getSide(facing), new BlockAir(), true, true);
            }
            if (canPush(block, facing.getOpposite(), false) && (!(block instanceof BlockFlowable) || block.getId() == PISTON || block.getId() == STICKY_PISTON)) {
                this.doMove(false);
            }
        } else {
            this.level.setBlock(getLocation().getSide(facing), new BlockAir(), true, false);
        }
        this.level.addSound(this, Sound.TILE_PISTON_IN);
    }
}
Also used : BlockFace(cn.nukkit.math.BlockFace) Vector3(cn.nukkit.math.Vector3)

Aggregations

BlockFace (cn.nukkit.math.BlockFace)29 Vector3 (cn.nukkit.math.Vector3)16 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)3 BlockRedstoneEvent (cn.nukkit.event.block.BlockRedstoneEvent)2 Player (cn.nukkit.Player)1 Block (cn.nukkit.block.Block)1 BlockAir (cn.nukkit.block.BlockAir)1 BlockLiquid (cn.nukkit.block.BlockLiquid)1 BlockWater (cn.nukkit.block.BlockWater)1 BlockEntity (cn.nukkit.blockentity.BlockEntity)1 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)1 BlockEntityHopper (cn.nukkit.blockentity.BlockEntityHopper)1 Entity (cn.nukkit.entity.Entity)1 BlockFromToEvent (cn.nukkit.event.block.BlockFromToEvent)1 PlayerBucketEmptyEvent (cn.nukkit.event.player.PlayerBucketEmptyEvent)1 PlayerBucketFillEvent (cn.nukkit.event.player.PlayerBucketFillEvent)1 Item (cn.nukkit.item.Item)1 ItemTool (cn.nukkit.item.ItemTool)1 Level (cn.nukkit.level.Level)1 Position (cn.nukkit.level.Position)1