Search in sources :

Example 1 with PlayerBucketFillEvent

use of cn.nukkit.event.player.PlayerBucketFillEvent 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 2 with PlayerBucketFillEvent

use of cn.nukkit.event.player.PlayerBucketFillEvent in project Nukkit by Nukkit.

the class BlockCauldron method onActivate.

@Override
public boolean onActivate(Item item, Player player) {
    BlockEntity be = this.level.getBlockEntity(this);
    if (!(be instanceof BlockEntityCauldron)) {
        return false;
    }
    BlockEntityCauldron cauldron = (BlockEntityCauldron) be;
    switch(item.getId()) {
        case Item.BUCKET:
            if (item.getDamage() == 0) {
                // empty bucket
                if (!isFull() || cauldron.isCustomColor() || cauldron.hasPotion()) {
                    break;
                }
                ItemBucket bucket = (ItemBucket) item.clone();
                // water bucket
                bucket.setDamage(8);
                PlayerBucketFillEvent ev = new PlayerBucketFillEvent(player, this, null, item, bucket);
                this.level.getServer().getPluginManager().callEvent(ev);
                if (!ev.isCancelled()) {
                    if (player.isSurvival()) {
                        player.getInventory().setItemInHand(ev.getItem());
                    }
                    // empty
                    this.setDamage(0);
                    this.level.setBlock(this, this, true);
                    cauldron.clearCustomColor();
                    this.getLevel().addSound(this.add(0.5, 1, 0.5), Sound.CAULDRON_TAKEWATER);
                }
            } else if (item.getDamage() == 8) {
                if (isFull() && !cauldron.isCustomColor() && !cauldron.hasPotion()) {
                    break;
                }
                ItemBucket bucket = (ItemBucket) item.clone();
                // empty bucket
                bucket.setDamage(0);
                PlayerBucketEmptyEvent ev = new PlayerBucketEmptyEvent(player, this, null, item, bucket);
                this.level.getServer().getPluginManager().callEvent(ev);
                if (!ev.isCancelled()) {
                    if (player.isSurvival()) {
                        player.getInventory().setItemInHand(ev.getItem());
                    }
                    if (cauldron.hasPotion()) {
                        // if has potion
                        // empty
                        this.setDamage(0);
                        // reset potion
                        cauldron.setPotionId(0xffff);
                        cauldron.setSplashPotion(false);
                        cauldron.clearCustomColor();
                        this.level.setBlock(this, this, true);
                        this.level.addSound(this.add(0.5, 0, 0.5), Sound.CAULDRON_EXPLODE);
                    } else {
                        // fill
                        this.setDamage(6);
                        cauldron.clearCustomColor();
                        this.level.setBlock(this, this, true);
                        this.level.addSound(this.add(0.5, 1, 0.5), Sound.BUCKET_FILL_WATER);
                    }
                // this.update();
                }
            }
            break;
        case // TODO
        Item.DYE:
            break;
        case Item.LEATHER_CAP:
        case Item.LEATHER_TUNIC:
        case Item.LEATHER_PANTS:
        case Item.LEATHER_BOOTS:
            break;
        case Item.POTION:
            if (isFull()) {
                break;
            }
            this.setDamage(this.getDamage() + 1);
            if (this.getDamage() > 0x06)
                this.setDamage(0x06);
            if (item.getCount() == 1) {
                player.getInventory().setItemInHand(new ItemBlock(new BlockAir()));
            } else if (item.getCount() > 1) {
                item.setCount(item.getCount() - 1);
                player.getInventory().setItemInHand(item);
                Item bottle = new ItemGlassBottle();
                if (player.getInventory().canAddItem(bottle)) {
                    player.getInventory().addItem(bottle);
                } else {
                    player.getLevel().dropItem(player.add(0, 1.3, 0), bottle, player.getDirectionVector().multiply(0.4));
                }
            }
            this.level.addSound(this.add(0.5, 0.5, 0.5), Sound.CAULDRON_FILLPOTION);
            break;
        case Item.GLASS_BOTTLE:
            if (isEmpty()) {
                break;
            }
            this.setDamage(this.getDamage() - 1);
            if (this.getDamage() < 0x00)
                this.setDamage(0x00);
            if (item.getCount() == 1) {
                player.getInventory().setItemInHand(new ItemPotion());
            } else if (item.getCount() > 1) {
                item.setCount(item.getCount() - 1);
                player.getInventory().setItemInHand(item);
                Item potion = new ItemPotion();
                if (player.getInventory().canAddItem(potion)) {
                    player.getInventory().addItem(potion);
                } else {
                    player.getLevel().dropItem(player.add(0, 1.3, 0), potion, player.getDirectionVector().multiply(0.4));
                }
            }
            this.level.addSound(this.add(0.5, 0.5, 0.5), Sound.CAULDRON_TAKEPOTION);
            break;
        default:
            return true;
    }
    this.level.updateComparatorOutputLevel(this);
    return true;
}
Also used : PlayerBucketFillEvent(cn.nukkit.event.player.PlayerBucketFillEvent) BlockEntityCauldron(cn.nukkit.blockentity.BlockEntityCauldron) PlayerBucketEmptyEvent(cn.nukkit.event.player.PlayerBucketEmptyEvent) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

PlayerBucketEmptyEvent (cn.nukkit.event.player.PlayerBucketEmptyEvent)2 PlayerBucketFillEvent (cn.nukkit.event.player.PlayerBucketFillEvent)2 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 BlockEntityCauldron (cn.nukkit.blockentity.BlockEntityCauldron)1 BlockFace (cn.nukkit.math.BlockFace)1