Search in sources :

Example 21 with Item

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

the class BaseInventory method getFreeSpace.

public int getFreeSpace(Item item) {
    int maxStackSize = Math.min(item.getMaxStackSize(), this.getMaxStackSize());
    int space = (this.getSize() - this.slots.size()) * maxStackSize;
    for (Item slot : this.getContents().values()) {
        if (slot == null || slot.getId() == 0) {
            space += maxStackSize;
            continue;
        }
        if (slot.equals(item, true, true)) {
            space += maxStackSize - slot.getCount();
        }
    }
    return space;
}
Also used : Item(cn.nukkit.item.Item)

Example 22 with Item

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

the class BaseInventory method removeItem.

@Override
public Item[] removeItem(Item... slots) {
    List<Item> itemSlots = new ArrayList<>();
    for (Item slot : slots) {
        if (slot.getId() != 0 && slot.getCount() > 0) {
            itemSlots.add(slot.clone());
        }
    }
    for (int i = 0; i < this.size; ++i) {
        Item item = this.getItem(i);
        if (item.getId() == Item.AIR || item.getCount() <= 0) {
            continue;
        }
        for (Item slot : new ArrayList<>(itemSlots)) {
            if (slot.equals(item, item.hasMeta(), item.getCompoundTag() != null)) {
                int amount = Math.min(item.getCount(), slot.getCount());
                slot.setCount(slot.getCount() - amount);
                item.setCount(item.getCount() - amount);
                this.setItem(i, item);
                if (slot.getCount() <= 0) {
                    itemSlots.remove(slot);
                }
            }
        }
        if (itemSlots.size() == 0) {
            break;
        }
    }
    return itemSlots.stream().toArray(Item[]::new);
}
Also used : Item(cn.nukkit.item.Item)

Example 23 with Item

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

the class BaseInventory method canAddItem.

@Override
public boolean canAddItem(Item item) {
    item = item.clone();
    boolean checkDamage = item.hasMeta();
    boolean checkTag = item.getCompoundTag() != null;
    for (int i = 0; i < this.getSize(); ++i) {
        Item slot = this.getItem(i);
        if (item.equals(slot, checkDamage, checkTag)) {
            int diff;
            if ((diff = slot.getMaxStackSize() - slot.getCount()) > 0) {
                item.setCount(item.getCount() - diff);
            }
        } else if (slot.getId() == Item.AIR) {
            item.setCount(item.getCount() - this.getMaxStackSize());
        }
        if (item.getCount() <= 0) {
            return true;
        }
    }
    return false;
}
Also used : Item(cn.nukkit.item.Item)

Example 24 with Item

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

Example 25 with Item

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

the class ContainerInventory method calculateRedstone.

public static int calculateRedstone(Inventory inv) {
    if (inv == null) {
        return 0;
    } else {
        int itemCount = 0;
        float averageCount = 0;
        for (int slot = 0; slot < inv.getSize(); ++slot) {
            Item item = inv.getItem(slot);
            if (item.getId() != 0) {
                averageCount += (float) item.getCount() / (float) Math.min(inv.getMaxStackSize(), item.getMaxStackSize());
                ++itemCount;
            }
        }
        averageCount = averageCount / (float) inv.getSize();
        return NukkitMath.floorFloat(averageCount * 14) + (itemCount > 0 ? 1 : 0);
    }
}
Also used : Item(cn.nukkit.item.Item)

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