Search in sources :

Example 1 with BlockEntityCauldron

use of cn.nukkit.blockentity.BlockEntityCauldron 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)

Example 2 with BlockEntityCauldron

use of cn.nukkit.blockentity.BlockEntityCauldron in project Nukkit by Nukkit.

the class BlockCauldron method place.

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    CompoundTag nbt = new CompoundTag("").putString("id", BlockEntity.CHEST).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z).putShort("PotionId", 0xffff).putByte("SplashPotion", 0);
    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }
    new BlockEntityCauldron(this.level.getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
    this.getLevel().setBlock(block, this, true, true);
    return true;
}
Also used : BlockEntityCauldron(cn.nukkit.blockentity.BlockEntityCauldron) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) Map(java.util.Map) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Aggregations

BlockEntityCauldron (cn.nukkit.blockentity.BlockEntityCauldron)2 BlockEntity (cn.nukkit.blockentity.BlockEntity)1 PlayerBucketEmptyEvent (cn.nukkit.event.player.PlayerBucketEmptyEvent)1 PlayerBucketFillEvent (cn.nukkit.event.player.PlayerBucketFillEvent)1 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)1 Tag (cn.nukkit.nbt.tag.Tag)1 Map (java.util.Map)1