Search in sources :

Example 6 with Item

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

the class BlockOreCoal method getDrops.

@Override
public Item[] getDrops(Item item) {
    if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_WOODEN) {
        int count = 1;
        Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
        if (fortune != null && fortune.getLevel() >= 1) {
            int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;
            if (i < 0) {
                i = 0;
            }
            count = i + 1;
        }
        return new Item[] { new ItemCoal(0, count) };
    } else {
        return new Item[0];
    }
}
Also used : Item(cn.nukkit.item.Item) ItemCoal(cn.nukkit.item.ItemCoal) Enchantment(cn.nukkit.item.enchantment.Enchantment)

Example 7 with Item

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

the class BlockGlowstone method getDrops.

@Override
public Item[] getDrops(Item item) {
    Random random = new Random();
    int count = 2 + random.nextInt(3);
    Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
    if (fortune != null && fortune.getLevel() >= 1) {
        count += random.nextInt(fortune.getLevel() + 1);
    }
    return new Item[] { new ItemGlowstoneDust(0, MathHelper.clamp(count, 1, 4)) };
}
Also used : Item(cn.nukkit.item.Item) Random(java.util.Random) ItemGlowstoneDust(cn.nukkit.item.ItemGlowstoneDust) Enchantment(cn.nukkit.item.enchantment.Enchantment)

Example 8 with Item

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

the class BlockEntityBrewingStand method updateBlock.

public void updateBlock() {
    Block block = this.getLevelBlock();
    if (!(block instanceof BlockBrewingStand)) {
        return;
    }
    int meta = 0;
    for (int i = 1; i <= 3; ++i) {
        Item potion = this.inventory.getItem(i);
        if (potion.getId() == Item.POTION && potion.getCount() > 0) {
            meta |= 1 << i;
        }
    }
    block.setDamage(meta);
    this.level.setBlock(block, block, false, false);
}
Also used : Item(cn.nukkit.item.Item) BlockBrewingStand(cn.nukkit.block.BlockBrewingStand) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block)

Example 9 with Item

use of cn.nukkit.item.Item 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 10 with Item

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

the class BlockEntityHopper method pickupDroppedItems.

public boolean pickupDroppedItems() {
    if (this.inventory.isFull()) {
        return false;
    }
    boolean update = false;
    for (Entity entity : this.level.getCollidingEntities(this.pickupArea)) {
        if (!(entity instanceof EntityItem)) {
            continue;
        }
        EntityItem itemEntity = (EntityItem) entity;
        Item item = itemEntity.getItem();
        if (item.getId() == 0 || item.getCount() < 1) {
            continue;
        }
        int originalCount = item.getCount();
        Item[] items = this.inventory.addItem(item);
        if (items.length == 0) {
            entity.close();
            update = true;
            continue;
        }
        if (items[0].getCount() != originalCount) {
            update = true;
        }
    }
    BlockEntity blockEntity = this.level.getBlockEntity(this.up());
    if (blockEntity instanceof InventoryHolder) {
        Inventory inv = ((InventoryHolder) blockEntity).getInventory();
        for (int i = 0; i < inv.getSize(); i++) {
            Item item = inv.getItem(i);
            if (item.getId() != 0 && item.getCount() > 0) {
                Item itemToAdd = item.clone();
                itemToAdd.count = 1;
                Item[] items = this.inventory.addItem(itemToAdd);
                if (items.length >= 1) {
                    continue;
                }
                item.count--;
                if (item.count <= 0) {
                    item = Item.get(0);
                }
                inv.setItem(i, item);
                update = true;
                break;
            }
        }
    }
    // TODO: check for minecart
    return update;
}
Also used : Entity(cn.nukkit.entity.Entity) EntityItem(cn.nukkit.entity.item.EntityItem) Item(cn.nukkit.item.Item) InventoryHolder(cn.nukkit.inventory.InventoryHolder) EntityItem(cn.nukkit.entity.item.EntityItem) HopperInventory(cn.nukkit.inventory.HopperInventory) Inventory(cn.nukkit.inventory.Inventory)

Aggregations

Item (cn.nukkit.item.Item)64 Enchantment (cn.nukkit.item.enchantment.Enchantment)13 Player (cn.nukkit.Player)10 ItemBlock (cn.nukkit.item.ItemBlock)10 BlockAir (cn.nukkit.block.BlockAir)8 Random (java.util.Random)7 BlockEntity (cn.nukkit.blockentity.BlockEntity)6 Entity (cn.nukkit.entity.Entity)5 Block (cn.nukkit.block.Block)4 EntityItem (cn.nukkit.entity.item.EntityItem)4 EntityInventoryChangeEvent (cn.nukkit.event.entity.EntityInventoryChangeEvent)4 Inventory (cn.nukkit.inventory.Inventory)3 InventoryHolder (cn.nukkit.inventory.InventoryHolder)3 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)2 EntityArmorChangeEvent (cn.nukkit.event.entity.EntityArmorChangeEvent)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 EntityDamageEvent (cn.nukkit.event.entity.EntityDamageEvent)2 HopperInventory (cn.nukkit.inventory.HopperInventory)2 InventoryAction (cn.nukkit.inventory.transaction.action.InventoryAction)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2