Java Code Examples for cn.nukkit.item.Item#getCount()

The following examples show how to use cn.nukkit.item.Item#getCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockEntityBrewingStand.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: BaseInventory.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: BlockEntityBrewingStand.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example 4
Source File: BlockEntityDispenser.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example 5
Source File: BlockEntityHopper.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example 6
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean contains(Item item) {
    int count = Math.max(1, item.getCount());
    boolean checkDamage = item.hasMeta() && item.getDamage() >= 0;
    boolean checkTag = item.getCompoundTag() != null;
    for (Item i : this.getContents().values()) {
        if (item.equals(i, checkDamage, checkTag)) {
            count -= i.getCount();
            if (count <= 0) {
                return true;
            }
        }
    }

    return false;
}
 
Example 7
Source File: ContainerInventory.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
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);
    }
}
 
Example 8
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 9
Source File: SimpleInventoryTransaction.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
protected boolean matchItems(List<Item> needItems, List<Item> haveItems) {
    for (InventoryAction action : this.actions) {
        if (action.getTargetItem().getId() != Item.AIR) {
            needItems.add(action.getTargetItem());
        }

        if (!action.isValid(this.source)) {
            return false;
        }

        if (action.getSourceItem().getId() != Item.AIR) {
            haveItems.add(action.getSourceItem());
        }
    }

    for (Item needItem : new ArrayList<>(needItems)) {
        for (Item haveItem : new ArrayList<>(haveItems)) {
            if (needItem.equals(haveItem)) {
                int amount = Math.min(haveItem.getCount(), needItem.getCount());
                needItem.setCount(needItem.getCount() - amount);
                haveItem.setCount(haveItem.getCount() - amount);
                if (haveItem.getCount() == 0) {
                    haveItems.remove(haveItem);
                }
                if (needItem.getCount() == 0) {
                    needItems.remove(needItem);
                    break;
                }
            }
        }
    }

    return haveItems.isEmpty() && needItems.isEmpty();
}
 
Example 10
Source File: BlockItemFrame.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@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
        Item itemOnFrame = item.clone(); // So we clone the item
        itemOnFrame.setCount(1); // Change it to only one item (if we keep +1, visual glitches will happen)
        itemFrame.setItem(itemOnFrame); // And then we set it on the item frame
        // 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;
}
 
Example 11
Source File: ShapelessRecipe.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public ShapelessRecipe addIngredient(Item item) {
    if (this.ingredients.size() > 9) {
        throw new IllegalArgumentException("Shapeless recipes cannot have more than 9 ingredients");
    }

    Item it = item.clone();
    it.setCount(1);

    while (item.getCount() > 0) {
        this.ingredients.add(it.clone());
        item.setCount(item.getCount() - 1);
    }

    return this;
}
 
Example 12
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isFull() {
    if (this.slots.size() < this.getSize()) {
        return false;
    }

    for (Item item : this.slots.values()) {
        if (item == null || item.getId() == 0 || item.getCount() < item.getMaxStackSize() || item.getCount() < this.getMaxStackSize()) {
            return false;
        }
    }

    return true;
}
 
Example 13
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isEmpty() {
    if (this.getMaxStackSize() <= 0) {
        return false;
    }

    for (Item item : this.slots.values()) {
        if (item != null && item.getId() != 0 && item.getCount() > 0) {
            return false;
        }
    }

    return true;
}
 
Example 14
Source File: EntityArmorStand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public boolean onInteract(Player player, Item item) {
	Item change;
	if (item.getId() == 0) {
		this.sendHand(player);
		this.sendArmor(player);
		player.getInventory().sendContents(player);
		return false;
	} else if (item.isHelmet()) {
		change = this.getHelmet();
		this.setHelmet(item.clone());
	} else if (item.isChestplate()) {
		change = this.getChestPlate();
		this.setChestPlate(item.clone());
	} else if (item.isLeggings()) {
		change = this.getLeggins();
		this.setLeggins(item.clone());
	} else if (item.isBoots()) {
		change = this.getBoots();
		this.setBoots(item.clone());
	} else {
		if (item.getCount() > 1) {
			change = item.clone();
			change.setCount(change.getCount() - 1);
	    	player.getInventory().setItemInHand(change);
			player.getInventory().addItem(this.getHand());
			item.setCount(1);
			this.setHand(item.clone());
			return false;
		} else {
			change = this.getHand();
			this.setHand(item);
		}
	}
	player.getInventory().setItemInHand(change);
    return false;
}
 
Example 15
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void decreaseCount(int slot) {
    Item item = this.getItem(slot);

    if (item.getCount() > 0) {
        item.count--;
        this.setItem(slot, item);
    }
}
 
Example 16
Source File: ShapelessRecipe.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public ShapelessRecipe addIngredient(Item item) {
    if (this.ingredients.size() > 9) {
        throw new IllegalArgumentException("Shapeless recipes cannot have more than 9 ingredients");
    }

    Item it = item.clone();
    it.setCount(1);

    while (item.getCount() > 0) {
        this.ingredients.add(it.clone());
        item.setCount(item.getCount() - 1);
    }

    return this;
}
 
Example 17
Source File: ShapelessRecipe.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public ShapelessRecipe removeIngredient(Item item) {
    for (Item ingredient : this.ingredients) {
        if (item.getCount() <= 0) {
            break;
        }

        if (ingredient.equals(item, item.hasMeta(), item.getCompoundTag() != null)) {
            this.ingredients.remove(ingredient);
            item.setCount(item.getCount() - 1);
        }
    }

    return this;
}
 
Example 18
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isEmpty() {
    if (this.getMaxStackSize() <= 0) {
        return false;
    }

    for (Item item : this.slots.values()) {
        if (item != null && item.getId() != 0 && item.getCount() > 0) {
            return false;
        }
    }

    return true;
}
 
Example 19
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void dropItem(Vector3 source, Item item, Vector3 motion, boolean dropAround, int delay) {
    if (motion == null) {
        if (dropAround) {
            float f = ThreadLocalRandom.current().nextFloat() * 0.5f;
            float f1 = ThreadLocalRandom.current().nextFloat() * ((float) Math.PI * 2);

            motion = new Vector3(-MathHelper.sin(f1) * f, 0.20000000298023224, MathHelper.cos(f1) * f);
        } else {
            motion = new Vector3(new java.util.Random().nextDouble() * 0.2 - 0.1, 0.2,
                    new java.util.Random().nextDouble() * 0.2 - 0.1);
        }
    }

    CompoundTag itemTag = NBTIO.putItemHelper(item);
    itemTag.setName("Item");

    if (item.getId() > 0 && item.getCount() > 0) {
        EntityItem itemEntity = new EntityItem(
                this.getChunk((int) source.getX() >> 4, (int) source.getZ() >> 4, true),
                new CompoundTag().putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("", source.getX()))
                        .add(new DoubleTag("", source.getY())).add(new DoubleTag("", source.getZ())))

                        .putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("", motion.x))
                                .add(new DoubleTag("", motion.y)).add(new DoubleTag("", motion.z)))

                        .putList(new ListTag<FloatTag>("Rotation")
                                .add(new FloatTag("", new java.util.Random().nextFloat() * 360))
                                .add(new FloatTag("", 0)))

                        .putShort("Health", 5).putCompound("Item", itemTag).putShort("PickupDelay", delay));

        itemEntity.spawnToAll();
    }
}
 
Example 20
Source File: Binary.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static EntityMetadata readMetadata(byte[] payload) {
    BinaryStream stream = new BinaryStream();
    stream.setBuffer(payload);
    long count = stream.getUnsignedVarInt();
    EntityMetadata m = new EntityMetadata();
    for (int i = 0; i < count; i++) {
        int key = (int) stream.getUnsignedVarInt();
        int type = (int) stream.getUnsignedVarInt();
        EntityData value = null;
        switch (type) {
            case Entity.DATA_TYPE_BYTE:
                value = new ByteEntityData(key, stream.getByte());
                break;
            case Entity.DATA_TYPE_SHORT:
                value = new ShortEntityData(key, stream.getLShort());
                break;
            case Entity.DATA_TYPE_INT:
                value = new IntEntityData(key, stream.getVarInt());
                break;
            case Entity.DATA_TYPE_FLOAT:
                value = new FloatEntityData(key, stream.getLFloat());
                break;
            case Entity.DATA_TYPE_STRING:
                value = new StringEntityData(key, stream.getString());
                break;
            case Entity.DATA_TYPE_SLOT:
                Item item = stream.getSlot();
                value = new SlotEntityData(key, item.getId(), item.getDamage(), item.getCount());
                break;
            case Entity.DATA_TYPE_POS:
                BlockVector3 v3 = stream.getSignedBlockPosition();
                value = new IntPositionEntityData(key, v3.x, v3.y, v3.z);
                break;
            case Entity.DATA_TYPE_LONG:
                value = new LongEntityData(key, stream.getVarLong());
                break;
            case Entity.DATA_TYPE_VECTOR3F:
                value = new Vector3fEntityData(key, stream.getVector3f());
                break;
        }
        if (value != null) m.put(value);
    }
    return m;
}