Java Code Examples for net.minecraft.item.ItemStack#getCount()
The following examples show how to use
net.minecraft.item.ItemStack#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: ContainerExtended.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 6 votes |
@Nonnull @Override public ItemStack transferStackInSlot(@Nonnull PlayerEntity par1EntityPlayer, int slotIndex) { ItemStack transferredStack = ItemStack.EMPTY; Slot slot = inventorySlots.get(slotIndex); if (slot != null && slot.getHasStack()) { ItemStack stack = slot.getStack(); transferredStack = stack.copy(); if (!doMergeStackAreas(slotIndex, stack)) { return ItemStack.EMPTY; } if (stack.getCount() == 0) { slot.putStack(ItemStack.EMPTY); } else { slot.onSlotChanged(); } } return transferredStack; }
Example 2
Source File: InventoryUtils.java From OpenModsLib with MIT License | 6 votes |
public static boolean tryMergeStacks(@Nonnull ItemStack stackToMerge, @Nonnull ItemStack stackInSlot) { if (stackInSlot.isEmpty() || !stackInSlot.isItemEqual(stackToMerge) || !ItemStack.areItemStackTagsEqual(stackToMerge, stackInSlot)) return false; int newStackSize = stackInSlot.getCount() + stackToMerge.getCount(); final int maxStackSize = stackToMerge.getMaxStackSize(); if (newStackSize <= maxStackSize) { stackToMerge.setCount(0); stackInSlot.setCount(newStackSize); return true; } else if (stackInSlot.getCount() < maxStackSize) { stackToMerge.shrink(maxStackSize - stackInSlot.getCount()); stackInSlot.setCount(maxStackSize); return true; } return false; }
Example 3
Source File: QuestBase.java From ToroQuest with GNU General Public License v3.0 | 6 votes |
protected static List<ItemStack> removeItems(List<ItemStack> requiredIn, List<ItemStack> itemsIn) throws InsufficientItems { List<ItemStack> givenItems = copyItems(itemsIn); List<ItemStack> requiredItems = copyItems(requiredIn); for (ItemStack givenItem : givenItems) { for (ItemStack requiredItem : requiredItems) { handleStackDecrement(requiredItem, givenItem); } } for (ItemStack remainingRequired : requiredItems) { if (remainingRequired.getCount() > 0) { throw new InsufficientItems(remainingRequired.getCount() + " " + remainingRequired.getDisplayName()); } } return givenItems; }
Example 4
Source File: ToolUtility.java From GregTech with GNU Lesser General Public License v3.0 | 6 votes |
public static void applyHammerDrops(Random random, IBlockState blockState, List<ItemStack> drops, int fortuneLevel, EntityPlayer player) { ItemStack itemStack = new ItemStack(blockState.getBlock(), 1, blockState.getBlock().getMetaFromState(blockState)); Recipe recipe = RecipeMaps.FORGE_HAMMER_RECIPES.findRecipe(Long.MAX_VALUE, Collections.singletonList(itemStack), Collections.emptyList(), 0); if (recipe != null && !recipe.getOutputs().isEmpty()) { drops.clear(); for (ItemStack outputStack : recipe.getResultItemOutputs(Integer.MAX_VALUE, random, 0)) { outputStack = outputStack.copy(); if (!(player instanceof FakePlayer) && OreDictUnifier.getPrefix(outputStack) == OrePrefix.crushed) { int growAmount = Math.round(outputStack.getCount() * random.nextFloat()); if (fortuneLevel > 0) { int i = Math.max(0, random.nextInt(fortuneLevel + 2) - 1); growAmount += outputStack.getCount() * i; } outputStack.grow(growAmount); } drops.add(outputStack); } } }
Example 5
Source File: TileEntityInserter.java From enderutilities with GNU Lesser General Public License v3.0 | 5 votes |
private boolean tryPushOutItemsToSide(World world, BlockPos posSelf, EnumFacing side) { TileEntity te = world.getTileEntity(posSelf.offset(side)); if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite())) { IItemHandler inv = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite()); if (inv != null) { // This is used to prevent scheduling a new update because of an adjacent inventory changing // while we push out items, and our own inventory changing due to this extract. // The update will be scheduled, if needed, after the push is complete. this.disableUpdateScheduling = true; ItemStack stack = this.itemHandlerBase.extractItem(0, 64, false); int sizeOrig = stack.getCount(); boolean movedSome = false; stack = InventoryUtils.tryInsertItemStackToInventory(inv, stack); // Return the items that couldn't be moved if (stack.isEmpty() == false) { movedSome = stack.getCount() != sizeOrig; this.itemHandlerBase.insertItem(0, stack, false); } this.disableUpdateScheduling = false; return stack.isEmpty() || movedSome; } } return false; }
Example 6
Source File: TileEntitySaltFurnace.java From TofuCraftReload with MIT License | 5 votes |
@Override public void setInventorySlotContents(int index, ItemStack stack) { boolean flag = stack.isItemEqual(this.furnaceItemStacks.get(index)) && ItemStack.areItemStackTagsEqual(stack, this.furnaceItemStacks.get(index)); this.furnaceItemStacks.set(index, stack); if (stack.getCount() > this.getInventoryStackLimit()) { stack.setCount(this.getInventoryStackLimit()); } if (index == 0 && !flag) { this.markDirty(); } }
Example 7
Source File: GenericInventory.java From OpenModsLib with MIT License | 5 votes |
@Override public void setInventorySlotContents(int index, ItemStack itemstack) { this.inventoryContents.set(index, itemstack); if (itemstack.getCount() > getInventoryStackLimit()) { itemstack.setCount(getInventoryStackLimit()); } onInventoryChanged(index); }
Example 8
Source File: TileDataBus.java From AdvancedRocketry with MIT License | 5 votes |
@Override public void storeData(int id) { ItemStack itemStack = inventory.getStackInSlot(0); if(itemStack != null && itemStack.getItem() instanceof ItemData && itemStack.getCount() == 1) { ItemData itemData = (ItemData)itemStack.getItem(); this.data.removeData(itemData.addData(itemStack, this.data.getData(), this.data.getDataType()), true); } if(world.isRemote) { PacketHandler.sendToServer(new PacketMachine(this, (byte)-1)); } }
Example 9
Source File: CraftInventoryCustom.java From Kettle with GNU General Public License v3.0 | 5 votes |
@Override public ItemStack decrStackSize(int i, int j) { ItemStack stack = this.getStackInSlot(i); ItemStack result; if (stack == ItemStack.EMPTY) return stack; if (stack.getCount() <= j) { this.setInventorySlotContents(i, ItemStack.EMPTY); result = stack; } else { result = CraftItemStack.copyNMSStack(stack, j); stack.shrink(j); } this.markDirty(); return result; }
Example 10
Source File: TileChemicalReactor.java From AdvancedRocketry with MIT License | 5 votes |
public void consumeItemsSpecial(IRecipe recipe) { List<List<ItemStack>> ingredients = recipe.getIngredients(); for(int ingredientNum = 0;ingredientNum < ingredients.size(); ingredientNum++) { List<ItemStack> ingredient = ingredients.get(ingredientNum); ingredientCheck: for(IInventory hatch : itemInPorts) { for(int i = 0; i < hatch.getSizeInventory(); i++) { ItemStack stackInSlot = hatch.getStackInSlot(i); for (ItemStack stack : ingredient) { if(stackInSlot != null && stackInSlot.getCount() >= stack.getCount() && stackInSlot.isItemEqual(stack)) { ItemStack stack2 = hatch.decrStackSize(i, stack.getCount()); if(stack2.getItem() instanceof ItemArmor) { stack2.addEnchantment(AdvancedRocketryAPI.enchantmentSpaceProtection, 1); List<ItemStack> list = new LinkedList<ItemStack>(); list.add(stack2); setOutputs(list); } hatch.markDirty(); world.notifyBlockUpdate(pos, world.getBlockState(((TileEntity)hatch).getPos()), world.getBlockState(((TileEntity)hatch).getPos()), 6); break ingredientCheck; } } } } } }
Example 11
Source File: EntityInfo.java From fabric-carpet with MIT License | 5 votes |
private static String display_item(ItemStack item) { if (item == null) { return null; } if (item.isEmpty()) // func_190926_b() { return null; } // func_190916_E() String stackname = item.getCount()>1?String.format("%dx%s",item.getCount(), item.getName().getString()):item.getName().getString(); if (item.isDamaged()) { stackname += String.format(" %d/%d", item.getMaxUseTime()-item.getDamage(), item.getMaxUseTime()); } if (item.hasEnchantments()) { stackname += " ( "; Map<Enchantment, Integer> enchants = EnchantmentHelper.getEnchantments(item); for (Enchantment e: enchants.keySet()) { int level = enchants.get(e); String enstring = e.getName(level).getString(); stackname += enstring+" "; } stackname += ")"; } return stackname; }
Example 12
Source File: InventoryUtils.java From enderutilities with GNU Lesser General Public License v3.0 | 5 votes |
public static int calcRedstoneFromInventory(IItemHandler inv) { final int slots = inv.getSlots(); int items = 0; int capacity = 0; for (int slot = 0; slot < slots; slot++) { ItemStack stack = inv.getStackInSlot(slot); if ((inv instanceof IItemHandlerSize) && stack.isEmpty() == false) { capacity += ((IItemHandlerSize)inv).getItemStackLimit(slot, stack); } else { capacity += inv.getSlotLimit(slot); } if (stack.isEmpty() == false) { items += stack.getCount(); } } if (capacity > 0) { int strength = (14 * items) / capacity; // Emit a signal strength of 1 as soon as there is one item in the inventory if (items > 0) { strength += 1; } return strength; } return 0; }
Example 13
Source File: ElectricCompressorBlockEntity.java From Galacticraft-Rewoven with MIT License | 5 votes |
protected void craftItem(ItemStack craftingResult) { boolean canCraftTwo = true; for (int i = 0; i < 9; i++) { ItemStack item = getInventory().getStack(i); // If slot is not empty ( must be an ingredient if we've made it this far ), and there is less than 2 items in the slot, we cannot craft two. if (!item.isEmpty() && item.getCount() < 2) { canCraftTwo = false; break; } } if (canCraftTwo) { if (getInventory().getStack(OUTPUT_SLOT).getCount() >= craftingResult.getMaxCount() || getInventory().getStack(SECOND_OUTPUT_SLOT).getCount() >= craftingResult.getMaxCount()) { // There would be too many items in the output slot. Just craft one. canCraftTwo = false; } } for (int i = 0; i < 9; i++) { decrement(i, canCraftTwo ? 2 : 1); } // <= because otherwise it loops only once and puts in only one slot for (int i = OUTPUT_SLOT; i <= SECOND_OUTPUT_SLOT; i++) { insert(i, craftingResult); } }
Example 14
Source File: InfusionPillarBlockEntity.java From the-hallow with MIT License | 4 votes |
public ItemStack putStack(ItemStack insertStack) { if (storedStack.isEmpty() && insertStack.getCount() >= 1) { storedStack = insertStack.split(1); } return insertStack; }
Example 15
Source File: ContainerEnderUtilities.java From enderutilities with GNU Lesser General Public License v3.0 | 4 votes |
protected boolean transferStackToSlotRange(EntityPlayer player, int slotNum, MergeSlotRange slotRange, boolean reverse) { SlotItemHandlerGeneric slot = this.getSlotItemHandler(slotNum); if (slot == null || slot.getHasStack() == false || slot.canTakeStack(player) == false) { return false; } ItemStack stack = slot.getStack().copy(); int amount = Math.min(stack.getCount(), stack.getMaxStackSize()); stack.setCount(amount); // Simulate the merge stack = this.mergeItemStack(stack, slotRange, reverse, true); if (stack.isEmpty() == false) { // If the item can't be put back to the slot, then we need to make sure that the whole // stack can be merged elsewhere before trying to (partially) merge it. Important for crafting slots! // Or if nothing could be merged, then also abort. if (slot.isItemValid(stack) == false || stack.getCount() == amount) { return false; } // Can merge at least some of the items, get the amount that can be merged amount -= stack.getCount(); } // Get the actual stack for non-simulated merging stack = slot.decrStackSize(amount); slot.onTake(player, stack); // Actually merge the items stack = this.mergeItemStack(stack, slotRange, reverse, false); // If they couldn't fit after all, then return them. // This shouldn't happen, and will cause some issues like gaining XP from nothing in furnaces. if (stack.isEmpty() == false) { slot.insertItem(stack, false); EnderUtilities.logger.warn("Failed to merge all items in '{}'. This shouldn't happen and should be reported.", this.getClass().getSimpleName()); } return true; }
Example 16
Source File: ItemNullifier.java From enderutilities with GNU Lesser General Public License v3.0 | 4 votes |
/** * Tries to first fill the matching stacks in the player's inventory, * and then depending on the bag's mode, tries to add the remaining items * to the bag's inventory. * @param event * @return true if all items were handled and further processing of the event should not occur */ public static boolean onEntityItemPickupEvent(EntityItemPickupEvent event) { EntityItem entityItem = event.getItem(); ItemStack stack = entityItem.getItem(); EntityPlayer player = event.getEntityPlayer(); if (player.getEntityWorld().isRemote || entityItem.isDead || stack.isEmpty()) { return true; } ItemStack origStack = ItemStack.EMPTY; final int origStackSize = stack.getCount(); int stackSizeLast = origStackSize; boolean ret = false; IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); // Not all the items could fit into existing stacks in the player's inventory, move them directly to the nullifier List<Integer> slots = InventoryUtils.getSlotNumbersOfMatchingItems(playerInv, EnderUtilitiesItems.NULLIFIER); for (int slot : slots) { ItemStack nullifierStack = playerInv.getStackInSlot(slot); // Nullifier is not disabled if (nullifierStack.isEmpty() == false && isNullifierEnabled(nullifierStack)) { // Delayed the stack copying until we know if there is a valid bag, // so check if the stack was copied already or not. if (origStack == ItemStack.EMPTY) { origStack = stack.copy(); } stack = handleItems(stack, nullifierStack, player); if (stack.isEmpty() || stack.getCount() != stackSizeLast) { if (stack.isEmpty()) { entityItem.setDead(); event.setCanceled(true); ret = true; break; } ItemStack pickedUpStack = origStack.copy(); pickedUpStack.setCount(stackSizeLast - stack.getCount()); FMLCommonHandler.instance().firePlayerItemPickupEvent(player, entityItem, pickedUpStack); player.onItemPickup(entityItem, origStackSize); } stackSizeLast = stack.getCount(); } } // Not everything was handled, update the stack if (entityItem.isDead == false && stack.getCount() != origStackSize) { entityItem.setItem(stack); } // At least some items were picked up if (entityItem.isSilent() == false && (entityItem.isDead || stack.getCount() != origStackSize)) { player.getEntityWorld().playSound(null, player.getPosition(), SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.MASTER, 0.2F, ((itemRand.nextFloat() - itemRand.nextFloat()) * 0.7F + 1.0F) * 2.0F); } return ret; }
Example 17
Source File: ContainerCustomSlotClick.java From enderutilities with GNU Lesser General Public License v3.0 | 4 votes |
protected void rightClickSlot(int slotNum, EntityPlayer player) { SlotItemHandlerGeneric slot = this.getSlotItemHandler(slotNum); if (slot == null) { return; } ItemStack stackCursor = this.inventoryPlayer.getItemStack(); ItemStack stackSlot = slot.getStack(); // Items in cursor if (stackCursor.isEmpty() == false) { // Empty slot or identical items: try to add to slot if (stackSlot.isEmpty() || InventoryUtils.areItemStacksEqual(stackCursor, stackSlot)) { // Can put items into the slot if (slot.isItemValid(stackCursor)) { if (slot.insertItem(stackCursor.splitStack(1), false).isEmpty() == false) { stackCursor.grow(1); } this.inventoryPlayer.setItemStack(stackCursor.isEmpty() ? ItemStack.EMPTY : stackCursor); } // Can't put items into the slot (for example a furnace output slot or a crafting output slot); take items instead else if (stackSlot.isEmpty() == false) { this.takeItemsFromSlotToCursor(slot, Math.min(stackSlot.getMaxStackSize() / 2, stackSlot.getCount())); } } // Different items, try to swap the stacks else if (slot.canTakeStack(this.player) && slot.canTakeAll() && slot.isItemValid(stackCursor) && stackSlot.getCount() <= stackSlot.getMaxStackSize() && slot.getItemStackLimit(stackCursor) >= stackCursor.getCount()) { this.inventoryPlayer.setItemStack(slot.decrStackSize(stackSlot.getCount())); slot.onTake(this.player, stackSlot); slot.insertItem(stackCursor, false); } } // Empty cursor, trying to take items from the slot into the cursor else if (stackSlot.isEmpty() == false) { // only allow taking the whole stack from crafting slots int amount = stackSlot.getCount(); if ((slot instanceof SlotItemHandlerCraftResult) == false) { amount = Math.min((int) Math.ceil((double) stackSlot.getCount() / 2.0d), (int) Math.ceil((double) stackSlot.getMaxStackSize() / 2.0d)); } this.takeItemsFromSlotToCursor(slot, amount); } }
Example 18
Source File: ProcessorTileEntity.java From EmergingTechnology with MIT License | 4 votes |
public void doProcessing() { ItemStack inputStack = getInputStack(); // Nothing in input stack if (inputStack.getCount() == 0) { this.setProgress(0); return; } // Can't process this item if (!ProcessorRecipes.isValidInput(inputStack)) { this.setProgress(0); return; } ItemStack outputStack = getOutputStack(); IMachineRecipe recipe = ProcessorRecipes.getRecipeByInputItemStack(inputStack); // This is probably unneccessary if (recipe == null) { return; } // Output stack is full if (outputStack.getCount() == 64) { return; } // Output stack incompatible/non-empty if (!StackHelper.compareItemStacks(outputStack, recipe.getOutput()) && !StackHelper.isItemStackEmpty(outputStack)) { return; } // Not enough room in output stack if (outputStack.getCount() + recipe.getOutput().getCount() > recipe.getOutput().getMaxStackSize()) { return; } // Not enough items in input stack if (inputStack.getCount() < recipe.getInput().getCount()) { return; } // Not enough water if (this.getWater() < getPacket().calculateFluidUse(EmergingTechnologyConfig.POLYMERS_MODULE.PROCESSOR.processorWaterBaseUsage)) { return; } // Not enough energy if (this.getEnergy() < getPacket().calculateEnergyUse(EmergingTechnologyConfig.POLYMERS_MODULE.PROCESSOR.processorEnergyBaseUsage)) { return; } this.energyHandler.extractEnergy(getPacket().calculateEnergyUse(EmergingTechnologyConfig.POLYMERS_MODULE.PROCESSOR.processorEnergyBaseUsage), false); this.fluidHandler.drain(getPacket().calculateFluidUse(EmergingTechnologyConfig.POLYMERS_MODULE.PROCESSOR.processorWaterBaseUsage), true); this.setEnergy(this.energyHandler.getEnergyStored()); this.setWater(this.fluidHandler.getFluidAmount()); // Not enough operations performed if (this.getProgress() < getPacket().calculateProgress(EmergingTechnologyConfig.POLYMERS_MODULE.PROCESSOR.processorBaseTimeTaken)) { this.setProgress(this.getProgress() + 1); return; } itemHandler.insertItem(1, recipe.getOutput().copy(), false); itemHandler.extractItem(0, recipe.getInputCount(), false); this.setProgress(0); }
Example 19
Source File: GuiContainerLargeStacks.java From enderutilities with GNU Lesser General Public License v3.0 | 4 votes |
@Override public void drawSlot(Slot slotIn) { int slotPosX = slotIn.xPos; int slotPosY = slotIn.yPos; ItemStack stack = slotIn.getStack(); boolean flag = false; boolean flag1 = slotIn == this.clickedSlot && this.draggedStack.isEmpty() == false && this.isRightMouseClick == false; ItemStack stackCursor = this.mc.player.inventory.getItemStack(); String str = null; if (slotIn == this.clickedSlot && this.draggedStack.isEmpty() == false && this.isRightMouseClick && stack.isEmpty() == false) { stack = stack.copy(); stack.setCount(stack.getCount() / 2); } else if (this.dragSplitting && this.dragSplittingSlots.contains(slotIn) && stackCursor.isEmpty() == false) { if (this.dragSplittingSlots.size() == 1) { return; } if (Container.canAddItemToSlot(slotIn, stackCursor, true) && this.inventorySlots.canDragIntoSlot(slotIn)) { stack = stackCursor.copy(); flag = true; Container.computeStackSize(this.dragSplittingSlots, this.dragSplittingLimit, stack, slotIn.getStack().getCount()); if (stack.getCount() > stack.getMaxStackSize()) { str = TextFormatting.YELLOW + "" + stack.getMaxStackSize(); stack.setCount(stack.getMaxStackSize()); } if (stack.getCount() > slotIn.getItemStackLimit(stack)) { str = TextFormatting.YELLOW + "" + slotIn.getItemStackLimit(stack); stack.setCount(slotIn.getItemStackLimit(stack)); } } else { this.dragSplittingSlots.remove(slotIn); this.updateDragSplitting(); } } this.zLevel = 100.0F; this.itemRender.zLevel = 100.0F; if (stack.isEmpty()) { TextureAtlasSprite textureatlassprite = slotIn.getBackgroundSprite(); if (textureatlassprite != null) { GlStateManager.disableLighting(); this.mc.getTextureManager().bindTexture(slotIn.getBackgroundLocation()); this.drawTexturedModalRect(slotPosX, slotPosY, textureatlassprite, 16, 16); GlStateManager.enableLighting(); flag1 = true; } } if (flag1 == false) { if (flag) { drawRect(slotPosX, slotPosY, slotPosX + 16, slotPosY + 16, -2130706433); } GlStateManager.enableDepth(); this.itemRender.renderItemAndEffectIntoGUI(stack, slotPosX, slotPosY); // This slot belongs to a "large stacks" type inventory, render the stack size text scaled to 0.5x if (slotIn instanceof SlotItemHandler && this.scaledStackSizeTextInventories.contains(((SlotItemHandler) slotIn).getItemHandler())) { this.renderLargeStackItemOverlayIntoGUI(this.fontRenderer, stack, slotPosX, slotPosY); } else { this.itemRender.renderItemOverlayIntoGUI(this.fontRenderer, stack, slotPosX, slotPosY, str); } } this.itemRender.zLevel = 0.0F; this.zLevel = 0.0F; }
Example 20
Source File: ImplementedInventory.java From LibGui with MIT License | 3 votes |
/** * Replaces the current stack in the {@code slot} with the provided stack. * * <p>If the stack is too big for this inventory ({@link Inventory#getMaxCountPerStack()} ()}), * it gets resized to this inventory's maximum amount. * * @param slot the slot * @param stack the stack */ @Override default void setStack(int slot, ItemStack stack) { getItems().set(slot, stack); if (stack.getCount() > getMaxCountPerStack()) { stack.setCount(getMaxCountPerStack()); } }