Search in sources :

Example 1 with ItemBlock

use of cn.nukkit.item.ItemBlock in project Nukkit by Nukkit.

the class BlockItemFrame method onActivate.

@Override
public boolean onActivate(Item item, Player player) {
    BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
    BlockEntityItemFrame itemFrame = (BlockEntityItemFrame) blockEntity;
    if (itemFrame.getItem().getId() == Item.AIR) {
        // We can't use Item.get(item.getId(), item.getDamage(), 1) because
        // we need to keep the item's NBT tags
        // So we clone the item
        Item itemOnFrame = item.clone();
        // Change it to only one item (if we keep +1, visual glitches will happen)
        itemOnFrame.setCount(1);
        // And then we set it on the item frame
        itemFrame.setItem(itemOnFrame);
        // The item will be removed from the player's hand a few lines ahead
        this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_ADD_ITEM);
        if (player != null && player.isSurvival()) {
            int count = item.getCount();
            if (count-- <= 0) {
                player.getInventory().setItemInHand(new ItemBlock(new BlockAir(), 0, 0));
                return true;
            }
            item.setCount(count);
            player.getInventory().setItemInHand(item);
        }
    } else {
        int itemRot = itemFrame.getItemRotation();
        if (itemRot >= 7) {
            itemRot = 0;
        } else {
            itemRot++;
        }
        itemFrame.setItemRotation(itemRot);
        this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_ROTATE_ITEM);
    }
    return true;
}
Also used : Item(cn.nukkit.item.Item) BlockEntityItemFrame(cn.nukkit.blockentity.BlockEntityItemFrame) ItemBlock(cn.nukkit.item.ItemBlock) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 2 with ItemBlock

use of cn.nukkit.item.ItemBlock in project Nukkit by Nukkit.

the class BlockEntityFurnace method onUpdate.

@Override
public boolean onUpdate() {
    if (this.closed) {
        return false;
    }
    this.timing.startTiming();
    boolean ret = false;
    Item fuel = this.inventory.getFuel();
    Item raw = this.inventory.getSmelting();
    Item product = this.inventory.getResult();
    FurnaceRecipe smelt = this.server.getCraftingManager().matchFurnaceRecipe(raw);
    boolean canSmelt = (smelt != null && raw.getCount() > 0 && ((smelt.getResult().equals(product, true) && product.getCount() < product.getMaxStackSize()) || product.getId() == Item.AIR));
    if (burnTime <= 0 && canSmelt && fuel.getFuelTime() != null && fuel.getCount() > 0) {
        this.checkFuel(fuel);
    }
    if (burnTime > 0) {
        burnTime--;
        burnDuration = (int) Math.ceil(burnTime / maxTime * 200);
        if (smelt != null && canSmelt) {
            cookTime++;
            if (cookTime >= 200) {
                product = Item.get(smelt.getResult().getId(), smelt.getResult().getDamage(), product.getCount() + 1);
                FurnaceSmeltEvent ev = new FurnaceSmeltEvent(this, raw, product);
                this.server.getPluginManager().callEvent(ev);
                if (!ev.isCancelled()) {
                    this.inventory.setResult(ev.getResult());
                    raw.setCount(raw.getCount() - 1);
                    if (raw.getCount() == 0) {
                        raw = new ItemBlock(new BlockAir(), 0, 0);
                    }
                    this.inventory.setSmelting(raw);
                }
                cookTime -= 200;
            }
        } else if (burnTime <= 0) {
            burnTime = 0;
            cookTime = 0;
            burnDuration = 0;
        } else {
            cookTime = 0;
        }
        ret = true;
    } else {
        if (this.getBlock().getId() == Item.BURNING_FURNACE) {
            this.getLevel().setBlock(this, new BlockFurnace(this.getBlock().getDamage()), true);
        }
        burnTime = 0;
        cookTime = 0;
        burnDuration = 0;
    }
    for (Player player : this.getInventory().getViewers()) {
        int windowId = player.getWindowId(this.getInventory());
        if (windowId > 0) {
            ContainerSetDataPacket pk = new ContainerSetDataPacket();
            pk.windowId = windowId;
            pk.property = ContainerSetDataPacket.PROPERTY_FURNACE_TICK_COUNT;
            ;
            pk.value = cookTime;
            player.dataPacket(pk);
            pk = new ContainerSetDataPacket();
            pk.windowId = windowId;
            pk.property = ContainerSetDataPacket.PROPERTY_FURNACE_LIT_TIME;
            pk.value = burnDuration;
            player.dataPacket(pk);
        }
    }
    this.lastUpdate = System.currentTimeMillis();
    this.timing.stopTiming();
    return ret;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Item(cn.nukkit.item.Item) BlockFurnace(cn.nukkit.block.BlockFurnace) Player(cn.nukkit.Player) ContainerSetDataPacket(cn.nukkit.network.protocol.ContainerSetDataPacket) FurnaceRecipe(cn.nukkit.inventory.FurnaceRecipe) ItemBlock(cn.nukkit.item.ItemBlock) FurnaceSmeltEvent(cn.nukkit.event.inventory.FurnaceSmeltEvent)

Example 3 with ItemBlock

use of cn.nukkit.item.ItemBlock in project Nukkit by Nukkit.

the class BlockEntityItemFrame method initBlockEntity.

@Override
protected void initBlockEntity() {
    if (!namedTag.contains("Item")) {
        namedTag.putCompound("Item", NBTIO.putItemHelper(new ItemBlock(new BlockAir())));
    }
    if (!namedTag.contains("ItemRotation")) {
        namedTag.putByte("ItemRotation", 0);
    }
    if (!namedTag.contains("ItemDropChance")) {
        namedTag.putFloat("ItemDropChance", 1.0f);
    }
    this.level.updateComparatorOutputLevel(this);
    super.initBlockEntity();
}
Also used : BlockAir(cn.nukkit.block.BlockAir) ItemBlock(cn.nukkit.item.ItemBlock)

Example 4 with ItemBlock

use of cn.nukkit.item.ItemBlock in project Nukkit by Nukkit.

the class BlockEntityItemFrame method getSpawnCompound.

@Override
public CompoundTag getSpawnCompound() {
    if (!this.namedTag.contains("Item")) {
        this.setItem(new ItemBlock(new BlockAir()), false);
    }
    CompoundTag NBTItem = namedTag.getCompound("Item").copy();
    NBTItem.setName("Item");
    boolean item = NBTItem.getShort("id") == Item.AIR;
    return new CompoundTag().putString("id", BlockEntity.ITEM_FRAME).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z).putCompound("Item", item ? NBTIO.putItemHelper(new ItemBlock(new BlockAir())) : NBTItem).putByte("ItemRotation", item ? 0 : this.getItemRotation());
// TODO: This crashes the client, why?
// .putFloat("ItemDropChance", this.getItemDropChance());
}
Also used : BlockAir(cn.nukkit.block.BlockAir) ItemBlock(cn.nukkit.item.ItemBlock) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 5 with ItemBlock

use of cn.nukkit.item.ItemBlock in project Nukkit by Nukkit.

the class BaseInventory method clear.

@Override
public boolean clear(int index, boolean send) {
    if (this.slots.containsKey(index)) {
        Item item = new ItemBlock(new BlockAir(), null, 0);
        Item old = this.slots.get(index);
        InventoryHolder holder = this.getHolder();
        if (holder instanceof Entity) {
            EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent((Entity) holder, old, item, index);
            Server.getInstance().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                this.sendSlot(index, this.getViewers());
                return false;
            }
            item = ev.getNewItem();
        }
        if (item.getId() != Item.AIR) {
            this.slots.put(index, item.clone());
        } else {
            this.slots.remove(index);
        }
        this.onSlotChange(index, old, send);
    }
    return true;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Item(cn.nukkit.item.Item) Entity(cn.nukkit.entity.Entity) EntityInventoryChangeEvent(cn.nukkit.event.entity.EntityInventoryChangeEvent) ItemBlock(cn.nukkit.item.ItemBlock)

Aggregations

ItemBlock (cn.nukkit.item.ItemBlock)12 BlockAir (cn.nukkit.block.BlockAir)11 Item (cn.nukkit.item.Item)9 Player (cn.nukkit.Player)3 Block (cn.nukkit.block.Block)3 BlockEntity (cn.nukkit.blockentity.BlockEntity)3 Entity (cn.nukkit.entity.Entity)3 EntityItem (cn.nukkit.entity.item.EntityItem)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 EntityInventoryChangeEvent (cn.nukkit.event.entity.EntityInventoryChangeEvent)2 Enchantment (cn.nukkit.item.enchantment.Enchantment)2 BlockFurnace (cn.nukkit.block.BlockFurnace)1 BlockFurnaceBurning (cn.nukkit.block.BlockFurnaceBurning)1 BlockTNT (cn.nukkit.block.BlockTNT)1 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)1 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)1 EntityArrow (cn.nukkit.entity.projectile.EntityArrow)1 BlockBreakEvent (cn.nukkit.event.block.BlockBreakEvent)1 BlockPlaceEvent (cn.nukkit.event.block.BlockPlaceEvent)1 BlockUpdateEvent (cn.nukkit.event.block.BlockUpdateEvent)1