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;
}
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);
}
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;
}
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;
}
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);
}
}
Aggregations