Java Code Examples for cn.nukkit.item.Item#setCount()
The following examples show how to use
cn.nukkit.item.Item#setCount() .
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: BlockItemFrame.java From Nukkit with GNU General Public License v3.0 | 6 votes |
@Override public boolean onActivate(Item item, Player player) { BlockEntity blockEntity = this.getLevel().getBlockEntity(this); BlockEntityItemFrame itemFrame = (BlockEntityItemFrame) blockEntity; if (itemFrame.getItem().getId() == Item.AIR) { Item itemOnFrame = item.clone(); if (player != null && player.isSurvival()) { itemOnFrame.setCount(itemOnFrame.getCount() - 1); player.getInventory().setItemInHand(itemOnFrame); } itemOnFrame.setCount(1); itemFrame.setItem(itemOnFrame); this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_ADD_ITEM); } else { itemFrame.setItemRotation((itemFrame.getItemRotation() + 1) % 8); this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_ROTATE_ITEM); } return true; }
Example 2
Source File: NBTIO.java From Nukkit with GNU General Public License v3.0 | 6 votes |
public static Item getItemHelper(CompoundTag tag) { if (!tag.contains("id") || !tag.contains("Count")) { return Item.get(0); } Item item; try { item = Item.get(tag.getShort("id"), !tag.contains("Damage") ? 0 : tag.getShort("Damage"), tag.getByte("Count")); } catch (Exception e) { item = Item.fromString(tag.getString("id")); item.setDamage(!tag.contains("Damage") ? 0 : tag.getShort("Damage")); item.setCount(tag.getByte("Count")); } if (tag.contains("tag") && tag.get("tag") instanceof CompoundTag) { item.setNamedTag(tag.getCompound("tag")); } return item; }
Example 3
Source File: BaseInventory.java From Nukkit with GNU General Public License v3.0 | 6 votes |
@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; }
Example 4
Source File: ShapedRecipe.java From Nukkit with GNU General Public License v3.0 | 6 votes |
private boolean matchItemList(List<Item> haveItems, List<Item> needItems) { for (Item needItem : new ArrayList<>(needItems)) { for (Item haveItem : new ArrayList<>(haveItems)) { if (needItem.equals(haveItem, needItem.hasMeta(), needItem.hasCompoundTag())) { 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 5
Source File: NBTIO.java From Nukkit with GNU General Public License v3.0 | 6 votes |
public static Item getItemHelper(CompoundTag tag) { if (!tag.contains("id") || !tag.contains("Count")) { return Item.get(0); } Item item; try { item = Item.get(tag.getShort("id"), !tag.contains("Damage") ? 0 : tag.getShort("Damage"), tag.getByte("Count")); } catch (Exception e) { item = Item.fromString(tag.getString("id")); item.setDamage(!tag.contains("Damage") ? 0 : tag.getShort("Damage")); item.setCount(tag.getByte("Count")); } Tag tagTag = tag.get("tag"); if (tagTag instanceof CompoundTag) { item.setNamedTag((CompoundTag) tagTag); } return item; }
Example 6
Source File: BaseInventory.java From Jupiter with GNU General Public License v3.0 | 6 votes |
@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; }
Example 7
Source File: ShapelessRecipe.java From Jupiter with GNU General Public License v3.0 | 5 votes |
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 8
Source File: AnvilTransaction.java From Jupiter with GNU General Public License v3.0 | 5 votes |
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 9
Source File: BlockFlowerPot.java From Nukkit with GNU General Public License v3.0 | 5 votes |
@Override public boolean onActivate(Item item, Player player) { BlockEntity blockEntity = getLevel().getBlockEntity(this); if (!(blockEntity instanceof BlockEntityFlowerPot)) return false; if (blockEntity.namedTag.getShort("item") != 0 || blockEntity.namedTag.getInt("mData") != 0) return false; int itemID; int itemMeta; if (!canPlaceIntoFlowerPot(item.getId())) { if (!canPlaceIntoFlowerPot(item.getBlock().getId())) { return true; } itemID = item.getBlock().getId(); itemMeta = item.getDamage(); } else { itemID = item.getId(); itemMeta = item.getDamage(); } blockEntity.namedTag.putShort("item", itemID); blockEntity.namedTag.putInt("data", itemMeta); this.setDamage(1); this.getLevel().setBlock(this, this, true); ((BlockEntityFlowerPot) blockEntity).spawnToAll(); if (player.isSurvival()) { item.setCount(item.getCount() - 1); player.getInventory().setItemInHand(item.getCount() > 0 ? item : Item.get(Item.AIR)); } return true; }
Example 10
Source File: EntityArmorStand.java From Jupiter with GNU General Public License v3.0 | 5 votes |
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 11
Source File: CraftingTransaction.java From Nukkit with GNU General Public License v3.0 | 5 votes |
public void setInput(Item item) { if (inputs.size() < gridSize * gridSize) { for (Item existingInput : this.inputs) { if (existingInput.equals(item, item.hasMeta(), item.hasCompoundTag())) { existingInput.setCount(existingInput.getCount() + item.getCount()); return; } } inputs.add(item.clone()); } else { throw new RuntimeException("Input list is full can't add " + item); } }
Example 12
Source File: InventoryTransaction.java From Nukkit with GNU General Public License v3.0 | 5 votes |
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 13
Source File: SimpleInventoryTransaction.java From Jupiter with GNU General Public License v3.0 | 5 votes |
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 14
Source File: ShapelessRecipe.java From Nukkit with GNU General Public License v3.0 | 5 votes |
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 15
Source File: ShapelessRecipe.java From Nukkit with GNU General Public License v3.0 | 5 votes |
public ShapelessRecipe(String recipeId, int priority, Item result, Collection<Item> ingredients) { this.recipeId = recipeId; this.priority = priority; this.output = result.clone(); if (ingredients.size() > 9) { throw new IllegalArgumentException("Shapeless recipes cannot have more than 9 ingredients"); } this.ingredients = new ArrayList<>(); this.ingredientsAggregate = new ArrayList<>(); for (Item item : ingredients) { if (item.getCount() < 1) { throw new IllegalArgumentException("Recipe '" + recipeId + "' Ingredient amount was not 1 (value: " + item.getCount() + ")"); } boolean found = false; for (Item existingIngredient : this.ingredientsAggregate) { if (existingIngredient.equals(item, item.hasMeta(), item.hasCompoundTag())) { existingIngredient.setCount(existingIngredient.getCount() + item.getCount()); found = true; break; } } if (!found) this.ingredientsAggregate.add(item.clone()); this.ingredients.add(item.clone()); } this.ingredientsAggregate.sort(CraftingManager.recipeComparator); }
Example 16
Source File: ShapelessRecipe.java From Nukkit with GNU General Public License v3.0 | 5 votes |
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 17
Source File: BlockItemFrame.java From Nukkit with GNU General Public License v3.0 | 5 votes |
@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 18
Source File: BlockFlowerPot.java From Nukkit with GNU General Public License v3.0 | 5 votes |
@Override public boolean onActivate(Item item, Player player) { BlockEntity blockEntity = getLevel().getBlockEntity(this); if (!(blockEntity instanceof BlockEntityFlowerPot)) return false; if (blockEntity.namedTag.getShort("item") != 0 || blockEntity.namedTag.getInt("mData") != 0) return false; int itemID; int itemMeta; if (!canPlaceIntoFlowerPot(item.getId())) { if (!canPlaceIntoFlowerPot(item.getBlock().getId())) { return true; } itemID = item.getBlock().getId(); itemMeta = item.getDamage(); } else { itemID = item.getId(); itemMeta = item.getDamage(); } blockEntity.namedTag.putShort("item", itemID); blockEntity.namedTag.putInt("data", itemMeta); this.setDamage(1); this.getLevel().setBlock(this, this, true); ((BlockEntityFlowerPot) blockEntity).spawnToAll(); if (player.isSurvival()) { item.setCount(item.getCount() - 1); player.getInventory().setItemInHand(item.getCount() > 0 ? item : Item.get(Item.AIR)); } return true; }
Example 19
Source File: NukkitPlayerBlockBag.java From FastAsyncWorldedit with GNU General Public License v3.0 | 4 votes |
@Override public void storeItem(BaseItem item) throws BlockBagException { final int id = item.getType(); final int damage = item.getData(); int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1; assert(amount <= 64); boolean usesDamageValue = ItemType.usesDamageValue(id); if (id == BlockID.AIR) { throw new IllegalArgumentException("Can't store air block"); } loadInventory(); int freeSlot = -1; for (int slot = 0; slot < size; ++slot) { Item nukkitItem = items.get(slot); if (nukkitItem == null) { // Delay using up a free slot until we know there are no stacks // of this item to merge into if (freeSlot == -1) { freeSlot = slot; } continue; } if (nukkitItem.getId() != id) { // Type id doesn't fit continue; } if (usesDamageValue && nukkitItem.getDamage() != damage) { // Damage value doesn't fit. continue; } int currentCount = nukkitItem.getCount(); if (currentCount < 0) { // Unlimited return; } if (currentCount >= 64) { // Full stack continue; } int spaceLeft = 64 - currentCount; if (spaceLeft >= amount) { nukkitItem.setCount(currentCount + amount); return; } nukkitItem.setCount(64); amount -= spaceLeft; } if (freeSlot > -1) { items.put(freeSlot, new Item(id, 0, amount)); return; } throw new OutOfSpaceException(id); }
Example 20
Source File: BlockEntityHopper.java From Nukkit with GNU General Public License v3.0 | 4 votes |
public boolean pickupItems() { if (this.inventory.isFull()) { return false; } boolean pickedUpItem = false; for (Entity entity : this.level.getCollidingEntities(this.pickupArea)) { if (entity.isClosed() || !(entity instanceof EntityItem)) { continue; } EntityItem itemEntity = (EntityItem) entity; Item item = itemEntity.getItem(); if (item.isNull()) { continue; } int originalCount = item.getCount(); if (!this.inventory.canAddItem(item)) { continue; } InventoryMoveItemEvent ev = new InventoryMoveItemEvent(null, this.inventory, this, item, InventoryMoveItemEvent.Action.PICKUP); this.server.getPluginManager().callEvent(ev); if (ev.isCancelled()) { continue; } Item[] items = this.inventory.addItem(item); if (items.length == 0) { entity.close(); pickedUpItem = true; continue; } if (items[0].getCount() != originalCount) { pickedUpItem = true; item.setCount(items[0].getCount()); } } //TODO: check for minecart return pickedUpItem; }