Search in sources :

Example 21 with BlockEntity

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

the class BlockItemFrame method getDrops.

@Override
public Item[] getDrops(Item item) {
    BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
    BlockEntityItemFrame itemFrame = (BlockEntityItemFrame) blockEntity;
    int chance = new Random().nextInt(100) + 1;
    if (itemFrame != null && chance <= (itemFrame.getItemDropChance() * 100)) {
        return new Item[] { toItem(), Item.get(itemFrame.getItem().getId(), itemFrame.getItem().getDamage(), 1) };
    } else {
        return new Item[] { toItem() };
    }
}
Also used : Item(cn.nukkit.item.Item) BlockEntityItemFrame(cn.nukkit.blockentity.BlockEntityItemFrame) Random(java.util.Random) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 22 with BlockEntity

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

the class BlockEnderChest method onActivate.

@Override
public boolean onActivate(Item item, Player player) {
    if (player != null) {
        Block top = this.up();
        if (!top.isTransparent()) {
            return true;
        }
        BlockEntity t = this.getLevel().getBlockEntity(this);
        BlockEntityEnderChest chest;
        if (t instanceof BlockEntityEnderChest) {
            chest = (BlockEntityEnderChest) t;
        } else {
            CompoundTag nbt = new CompoundTag("").putString("id", BlockEntity.ENDER_CHEST).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
            chest = new BlockEntityEnderChest(this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
        }
        if (chest.namedTag.contains("Lock") && chest.namedTag.get("Lock") instanceof StringTag) {
            if (!chest.namedTag.getString("Lock").equals(item.getCustomName())) {
                return true;
            }
        }
        player.setViewingEnderChest(this);
        player.addWindow(player.getEnderChestInventory());
    }
    return true;
}
Also used : StringTag(cn.nukkit.nbt.tag.StringTag) BlockEntityEnderChest(cn.nukkit.blockentity.BlockEntityEnderChest) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 23 with BlockEntity

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

the class BlockFlowerPot method onActivate.

@Override
public boolean onActivate(Item item, Player player) {
    BlockEntity blockEntity = getLevel().getBlockEntity(this);
    if (!(blockEntity instanceof BlockEntityFlowerPot))
        return false;
    if (blockEntity.namedTag.getShort("item") != 0 || blockEntity.namedTag.getInt("mData") != 0)
        return false;
    int itemID;
    int itemMeta;
    if (!canPlaceIntoFlowerPot(item.getId())) {
        if (!canPlaceIntoFlowerPot(item.getBlock().getId())) {
            return true;
        }
        itemID = item.getBlock().getId();
        itemMeta = item.getDamage();
    } else {
        itemID = item.getId();
        itemMeta = item.getDamage();
    }
    blockEntity.namedTag.putShort("item", itemID);
    blockEntity.namedTag.putInt("data", itemMeta);
    this.setDamage(1);
    this.getLevel().setBlock(this, this, true);
    ((BlockEntityFlowerPot) blockEntity).spawnToAll();
    if (player.isSurvival()) {
        item.setCount(item.getCount() - 1);
        player.getInventory().setItemInHand(item.getCount() > 0 ? item : Item.get(Item.AIR));
    }
    return true;
}
Also used : BlockEntity(cn.nukkit.blockentity.BlockEntity) BlockEntityFlowerPot(cn.nukkit.blockentity.BlockEntityFlowerPot)

Example 24 with BlockEntity

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

the class BlockRedstoneComparator method updateState.

@Override
public void updateState() {
    if (!this.level.isBlockTickPending(this, this)) {
        int output = this.calculateOutput();
        BlockEntity blockEntity = this.level.getBlockEntity(this);
        int power = blockEntity instanceof BlockEntityComparator ? ((BlockEntityComparator) blockEntity).getOutputSignal() : 0;
        if (output != power || this.isPowered() != this.shouldBePowered()) {
            /*if(isFacingTowardsRepeater()) {
                    this.level.scheduleUpdate(this, this, 2, -1);
                } else {
                    this.level.scheduleUpdate(this, this, 2, 0);
                }*/
            // System.out.println("schedule update 0");
            this.level.scheduleUpdate(this, this, 2);
        }
    }
}
Also used : BlockEntityComparator(cn.nukkit.blockentity.BlockEntityComparator) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 25 with BlockEntity

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

BlockEntity (cn.nukkit.blockentity.BlockEntity)32 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)13 Entity (cn.nukkit.entity.Entity)7 Player (cn.nukkit.Player)6 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)6 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)6 StringTag (cn.nukkit.nbt.tag.StringTag)6 IOException (java.io.IOException)6 Item (cn.nukkit.item.Item)5 FullChunk (cn.nukkit.level.format.FullChunk)5 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)4 ListTag (cn.nukkit.nbt.tag.ListTag)4 BinaryStream (cn.nukkit.utils.BinaryStream)4 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)3 ArrayList (java.util.ArrayList)3 BlockEntityComparator (cn.nukkit.blockentity.BlockEntityComparator)2 BlockEntityFlowerPot (cn.nukkit.blockentity.BlockEntityFlowerPot)2 Tag (cn.nukkit.nbt.tag.Tag)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 Long2ObjectMap (it.unimi.dsi.fastutil.longs.Long2ObjectMap)2