Search in sources :

Example 1 with BlockLiquid

use of cn.nukkit.block.BlockLiquid 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)

Aggregations

Block (cn.nukkit.block.Block)1 BlockAir (cn.nukkit.block.BlockAir)1 BlockLiquid (cn.nukkit.block.BlockLiquid)1 BlockWater (cn.nukkit.block.BlockWater)1 PlayerBucketEmptyEvent (cn.nukkit.event.player.PlayerBucketEmptyEvent)1 PlayerBucketFillEvent (cn.nukkit.event.player.PlayerBucketFillEvent)1 BlockFace (cn.nukkit.math.BlockFace)1