Java Code Examples for net.minecraftforge.items.IItemHandler#extractItem()
The following examples show how to use
net.minecraftforge.items.IItemHandler#extractItem() .
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: InventoryUtils.java From enderutilities with GNU Lesser General Public License v3.0 | 6 votes |
/** * Returns an ItemStack of up to maxAmount items from the first slot possible to extract from. */ public static ItemStack getItemsFromFirstNonEmptySlot(IItemHandler inv, int maxAmount, boolean simulate) { ItemStack stack; for (int i = 0; i < inv.getSlots(); i++) { stack = inv.extractItem(i, maxAmount, simulate); if (stack.isEmpty() == false) { return stack; } } return ItemStack.EMPTY; }
Example 2
Source File: ItemMover.java From OpenModsLib with MIT License | 6 votes |
static int pushFromSlot(IItemHandler source, int sourceSlot, int maxSize, Iterable<IItemHandler> targets) { int transferedAmount = 0; MAIN: for (IItemHandler target : targets) { ItemStack stackToPush = source.getStackInSlot(sourceSlot); for (int targetSlot = 0; targetSlot < target.getSlots(); targetSlot++) { if (stackToPush.isEmpty()) break MAIN; final ItemStack leftover = target.insertItem(targetSlot, stackToPush, true); if (leftover.getCount() < stackToPush.getCount()) { final int leftoverAmount = leftover.getCount(); final int amountToExtract = Math.min(maxSize - transferedAmount, stackToPush.getCount() - leftoverAmount); final ItemStack extractedItem = source.extractItem(sourceSlot, amountToExtract, false); if (!extractedItem.isEmpty()) { target.insertItem(targetSlot, extractedItem, false); transferedAmount += extractedItem.getCount(); stackToPush = source.getStackInSlot(sourceSlot); } } } } return transferedAmount; }
Example 3
Source File: ItemMover.java From OpenModsLib with MIT License | 6 votes |
static int pullToSlot(IItemHandler target, int targetSlot, int maxSize, Iterable<IItemHandler> sources) { int transferedAmount = 0; MAIN: for (IItemHandler source : sources) { for (int sourceSlot = 0; sourceSlot < source.getSlots(); sourceSlot++) { final ItemStack stackToPull = source.getStackInSlot(sourceSlot); if (stackToPull.isEmpty()) continue; final ItemStack leftover = target.insertItem(targetSlot, stackToPull, true); if (leftover.getCount() < stackToPull.getCount()) { final int leftoverAmount = leftover.getCount(); final int amountToExtract = Math.min(maxSize - transferedAmount, stackToPull.getCount() - leftoverAmount); final ItemStack extractedItem = source.extractItem(sourceSlot, amountToExtract, false); if (!extractedItem.isEmpty()) { // don't care about results here, since target already declared space target.insertItem(targetSlot, extractedItem, false); transferedAmount += amountToExtract; } } final ItemStack targetContents = target.getStackInSlot(targetSlot); if (targetContents != null && targetContents.getCount() >= targetContents.getMaxStackSize()) break MAIN; } } return transferedAmount; }
Example 4
Source File: TileEntityInserter.java From enderutilities with GNU Lesser General Public License v3.0 | 6 votes |
@Nullable private ItemStack tryPullInItemsThatPassFilters(IItemHandler inv) { int slots = inv.getSlots(); for (int slot = 0; slot < slots; slot++) { ItemStack stack = inv.getStackInSlot(slot); if (stack.isEmpty() == false && this.itemAllowedByFilters(stack)) { return inv.extractItem(slot, this.itemHandlerBase.getInventoryStackLimit(), false); } } return ItemStack.EMPTY; }
Example 5
Source File: CartHopperBehaviourItems.java From Signals with GNU General Public License v3.0 | 6 votes |
@Override public boolean tryTransfer(IItemHandler from, IItemHandler to, List<Pair<TileEntity, EnumFacing>> filters){ int totalExtracted = 0; for(int i = 0; i < from.getSlots(); i++) { ItemStack extracted = from.extractItem(i, MAX_TRANSFER_RATE - totalExtracted, true); if(!extracted.isEmpty() && passesFilters(extracted, filters)) { ItemStack leftover = ItemHandlerHelper.insertItemStacked(to, extracted, false); int leftoverCount = !leftover.isEmpty() ? leftover.getCount() : 0; int actuallyExtracted = extracted.getCount() - leftoverCount; if(actuallyExtracted > 0) { from.extractItem(i, actuallyExtracted, false); totalExtracted += actuallyExtracted; if(totalExtracted >= MAX_TRANSFER_RATE) break; } } } return totalExtracted > 0; }
Example 6
Source File: IPearlStorageHolder.java From Wizardry with GNU Lesser General Public License v3.0 | 6 votes |
default void sortInv(IItemHandler handler) { if (handler == null) return; Deque<ItemStack> stacks = new ArrayDeque<>(); final int slots = handler.getSlots(); for (int i = 0; i < slots; i++) { ItemStack stack = handler.extractItem(i, 1, false); if (stack.isEmpty()) continue; stacks.add(stack); } for (int i = 0; i < slots; i++) { if (stacks.isEmpty()) break; handler.insertItem(i, stacks.pop(), false); } }
Example 7
Source File: ItemEnderTool.java From enderutilities with GNU Lesser General Public License v3.0 | 5 votes |
private boolean plantItemFromInventorySlot(World world, EntityPlayer player, EnumHand hand, IItemHandler inv, int slot, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ) { boolean ret = false; ItemStack plantStack = inv.getStackInSlot(slot); if (plantStack.isEmpty() == false && plantStack.getItem() instanceof IPlantable) { plantStack = inv.extractItem(slot, 1, false); if (plantStack.isEmpty()) { return false; } ItemStack stackHand = player.getHeldItem(hand); EntityUtils.setHeldItemWithoutEquipSound(player, hand, plantStack); if (plantStack.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ) == EnumActionResult.SUCCESS) { ret = true; } EntityUtils.setHeldItemWithoutEquipSound(player, hand, stackHand); if (plantStack.isEmpty() == false) { plantStack = InventoryUtils.tryInsertItemStackToInventory(inv, plantStack); if (plantStack.isEmpty() == false) { player.dropItem(plantStack, false, true); } } player.inventoryContainer.detectAndSendChanges(); } return ret; }
Example 8
Source File: ItemInventorySwapper.java From enderutilities with GNU Lesser General Public License v3.0 | 5 votes |
private void swapInventory(long slotMask, InventoryItemModular swapperInv, IItemHandler externalInv) { // Only swap up to 36 slots (which fit in the swapper's GUI, excluding armor slots) final int invSize = Math.min(36, externalInv.getSlots()); long bit = 0x1; for (int i = 0; i < invSize; i++) { // Only swap slots that have been enabled if ((slotMask & bit) != 0) { ItemStack stackSwapper = swapperInv.extractItem(i, 64, false); ItemStack stackExternal = externalInv.extractItem(i, 64, false); // Check that both stacks can be successfully inserted into the other inventory if (swapperInv.insertItem(i, stackExternal, true).isEmpty() && externalInv.insertItem(i, stackSwapper, true).isEmpty()) { swapperInv.insertItem(i, stackExternal, false); externalInv.insertItem(i, stackSwapper, false); } // Can't swap the stacks, return them to the original inventories else { swapperInv.insertItem(i, stackSwapper, false); externalInv.insertItem(i, stackExternal, false); } } bit <<= 1; } }
Example 9
Source File: ItemInventorySwapper.java From enderutilities with GNU Lesser General Public License v3.0 | 5 votes |
private void swapBaublesSlot(InventoryItemModular swapperInv, IItemHandler baublesInv, int slotSwapper, int slotBaubles) { ItemStack stackInSwapperInv = swapperInv.getStackInSlot(slotSwapper); // Check if the stack from the swapper can fit and is valid to be put into the baubles slot if (stackInSwapperInv.isEmpty() || stackInSwapperInv.getCount() == 1) { ItemStack stackInBaublesInv = baublesInv.getStackInSlot(slotBaubles); // Existing baubles item if (stackInBaublesInv.isEmpty() == false) { stackInBaublesInv = baublesInv.extractItem(slotBaubles, stackInBaublesInv.getCount(), false); // Successfully extracted the existing item if (stackInBaublesInv.isEmpty() == false) { // The item in the swapper was valid for the baubles slot if (baublesInv.insertItem(slotBaubles, stackInSwapperInv, false).isEmpty()) { swapperInv.setStackInSlot(slotSwapper, stackInBaublesInv); } // The item in the swapper was not a valid baubles item, put back the original baubles item else { baublesInv.insertItem(slotBaubles, stackInBaublesInv, false); } } } // Empty baubles slot and items in the swapper else if (stackInSwapperInv.isEmpty() == false) { // The item in the swapper was valid for the baubles slot if (baublesInv.insertItem(slotBaubles, stackInSwapperInv, false).isEmpty()) { swapperInv.setStackInSlot(slotSwapper, ItemStack.EMPTY); } } } }
Example 10
Source File: InventoryUtils.java From enderutilities with GNU Lesser General Public License v3.0 | 5 votes |
/** * Tries to empty out the given slot by repeatedly calling extractItem() on it and just ignoring the items */ public static boolean tryToEmptySlot(IItemHandler inv, int slot, int maxIterations) { for (int i = 0; i < maxIterations && inv.getStackInSlot(slot).isEmpty() == false; i++) { inv.extractItem(slot, 1048576, false); // 1M because why not :p } return inv.getStackInSlot(slot).isEmpty(); }
Example 11
Source File: IPearlStorageHolder.java From Wizardry with GNU Lesser General Public License v3.0 | 5 votes |
/** * @return The popped pearl. */ default ItemStack removePearl(ItemStack holder, int slot, boolean sort) { if (isDisabled(holder)) return ItemStack.EMPTY; IItemHandler handler = getPearls(holder); if (handler == null) return ItemStack.EMPTY; ItemStack output = handler.extractItem(slot, 1, false); if (sort) sortInv(handler); return output; }
Example 12
Source File: CoverConveyor.java From GregTech with GNU Lesser General Public License v3.0 | 5 votes |
protected int moveInventoryItems(IItemHandler sourceInventory, IItemHandler targetInventory, int maxTransferAmount) { int itemsLeftToTransfer = maxTransferAmount; for (int srcIndex = 0; srcIndex < sourceInventory.getSlots(); srcIndex++) { ItemStack sourceStack = sourceInventory.extractItem(srcIndex, itemsLeftToTransfer, true); if (sourceStack.isEmpty()) { continue; } if (!itemFilterContainer.testItemStack(sourceStack)) { continue; } ItemStack remainder = ItemHandlerHelper.insertItemStacked(targetInventory, sourceStack, true); int amountToInsert = sourceStack.getCount() - remainder.getCount(); if (amountToInsert > 0) { sourceStack = sourceInventory.extractItem(srcIndex, amountToInsert, false); if (!sourceStack.isEmpty()) { ItemHandlerHelper.insertItemStacked(targetInventory, sourceStack, false); itemsLeftToTransfer -= sourceStack.getCount(); if (itemsLeftToTransfer == 0) { break; } } } } return maxTransferAmount - itemsLeftToTransfer; }
Example 13
Source File: InventoryUtils.java From enderutilities with GNU Lesser General Public License v3.0 | 4 votes |
/** * Tries to move all items from the inventory invSrc into invDst within the provided slot range. */ public static InvResult tryMoveAllItemsWithinSlotRange(IItemHandler invSrc, IItemHandler invDst, SlotRange slotsSrc, SlotRange slotsDst) { boolean movedAll = true; boolean movedSome = false; final int lastSlot = Math.min(slotsSrc.lastInc, invSrc.getSlots() - 1); for (int slot = slotsSrc.first; slot <= lastSlot; slot++) { ItemStack stack; int limit = SLOT_ITER_LIMIT; while (limit-- > 0) { stack = invSrc.extractItem(slot, 64, false); if (stack.isEmpty()) { break; } int origSize = stack.getCount(); stack = tryInsertItemStackToInventoryWithinSlotRange(invDst, stack, slotsDst); if (stack.isEmpty() || stack.getCount() != origSize) { movedSome = true; } // Can't insert anymore items if (stack.isEmpty() == false) { // Put the rest of the items back to the source inventory invSrc.insertItem(slot, stack, false); movedAll = false; break; } } } return movedAll ? InvResult.MOVED_ALL : (movedSome ? InvResult.MOVED_SOME : InvResult.MOVED_NOTHING); }
Example 14
Source File: ItemHandlerList.java From GregTech with GNU Lesser General Public License v3.0 | 4 votes |
@Nonnull @Override public ItemStack extractItem(int slot, int amount, boolean simulate) { IItemHandler itemHandler = handlerBySlotIndex.get(slot); return itemHandler.extractItem(slot - baseIndexOffset.get(itemHandler), slot, simulate); }
Example 15
Source File: InventoryUtils.java From enderutilities with GNU Lesser General Public License v3.0 | 4 votes |
/** * Extracts up to <b>amount</b> of <b>item</b> from the first slot in the inventory that * has <b>item</b> in it and returns them. Does not try to fill the stack up to <b>amount</b>! */ public static ItemStack extractItems(IItemHandler inv, Item item, int amount) { int slot = getSlotOfFirstMatchingItem(inv, item); return slot >= 0 ? inv.extractItem(slot, amount, false) : ItemStack.EMPTY; }
Example 16
Source File: InventoryUtils.java From enderutilities with GNU Lesser General Public License v3.0 | 4 votes |
/** * Extracts up to <b>amount</b> items from the first found slot with items matching <b>templateStack</b>. * Does not try to fill the stack up to amount if the first found slot has less than <b>amount</b> items! */ public static ItemStack extractMatchingItems(IItemHandler inv, @Nonnull ItemStack templateStack, int amount, boolean simulate) { int slot = getSlotOfFirstMatchingItemStack(inv, templateStack); return slot >= 0 ? inv.extractItem(slot, amount, simulate) : ItemStack.EMPTY; }
Example 17
Source File: InventoryUtils.java From enderutilities with GNU Lesser General Public License v3.0 | 4 votes |
/** * Extract items from the given slot until the resulting stack's stackSize equals amount */ public static ItemStack extractItemsFromSlot(IItemHandler inv, int slot, int amount) { ItemStack stackExtract = inv.extractItem(slot, amount, false); if (stackExtract.isEmpty()) { return ItemStack.EMPTY; } if ((stackExtract.getMaxStackSize() * SLOT_ITER_LIMIT) < amount && inv instanceof IItemHandlerModifiable) { amount -= stackExtract.getCount(); ItemStack stackSlot = inv.getStackInSlot(slot); if (stackSlot.isEmpty() == false) { if (stackSlot.getCount() <= amount) { stackExtract.grow(stackSlot.getCount()); ((IItemHandlerModifiable) inv).setStackInSlot(slot, ItemStack.EMPTY); } else { stackExtract.grow(amount); stackSlot = stackSlot.copy(); stackSlot.shrink(amount); ((IItemHandlerModifiable) inv).setStackInSlot(slot, stackSlot); } } return stackExtract; } int loops = 0; while (stackExtract.getCount() < amount && loops < SLOT_ITER_LIMIT) { ItemStack stackTmp = inv.extractItem(slot, amount - stackExtract.getCount(), false); if (stackTmp.isEmpty()) { break; } stackExtract.grow(stackTmp.getCount()); loops++; } //System.out.printf("extractItemsFromSlot(): slot: %d, requested amount: %d, loops %d, extracted: %s\n", slot, amount, loops, stack); return stackExtract; }
Example 18
Source File: TileEntityEnderFurnace.java From enderutilities with GNU Lesser General Public License v3.0 | 4 votes |
public static int consumeFuelItem(IItemHandler fuelInv, int fuelSlot, boolean simulate) { ItemStack stack = fuelInv.getStackInSlot(fuelSlot); if (stack.isEmpty()) { return 0; } int burnTime = 0; if (itemContainsFluidFuel(stack)) { // Can't return the drained container if there is more than one item in the slot... if (stack.getCount() > 1) { return 0; } stack = fuelInv.extractItem(fuelSlot, 1, simulate); burnTime = consumeFluidFuelDosage(stack); } else { burnTime = getItemBurnTime(stack); if (burnTime == 0 || (stack.getCount() > 1 && stack.getItem().getContainerItem(stack).isEmpty() == false)) { return 0; } stack = fuelInv.extractItem(fuelSlot, 1, simulate); stack = stack.getItem().getContainerItem(stack); } // Put the fuel/fluid container item back if (simulate == false && stack.isEmpty() == false) { fuelInv.insertItem(fuelSlot, stack, false); } return burnTime; }
Example 19
Source File: TileEntityQuickStackerAdvanced.java From enderutilities with GNU Lesser General Public License v3.0 | 4 votes |
/** * Tries to move all items from enabled slots in the player's inventory to the given external inventory */ public static Result quickStackItems(IItemHandler playerInv, IItemHandler externalInv, long slotMask, boolean matchingOnly, FilterSettings filter) { Result ret = Result.MOVED_NONE; boolean movedAll = true; long bit = 0x1; for (int slotPlayer = 0; slotPlayer < playerInv.getSlots(); slotPlayer++) { ItemStack stack = playerInv.getStackInSlot(slotPlayer); // Only take from slots that have been enabled if ((slotMask & bit) != 0 && stack.isEmpty() == false && (filter == null || filter.itemAllowedByFilter(stack))) { stack = playerInv.extractItem(slotPlayer, 64, false); if (stack.isEmpty()) { continue; } if (matchingOnly == false || InventoryUtils.getSlotOfLastMatchingItemStack(externalInv, stack) != -1) { int sizeOrig = stack.getCount(); stack = InventoryUtils.tryInsertItemStackToInventory(externalInv, stack); if (ret == Result.MOVED_NONE && (stack.isEmpty() || stack.getCount() != sizeOrig)) { ret = Result.MOVED_SOME; } } // Return the items that were left over if (stack.isEmpty() == false) { playerInv.insertItem(slotPlayer, stack, false); movedAll = false; } } bit <<= 1; } if (movedAll && ret == Result.MOVED_SOME) { ret = Result.MOVED_ALL; } return ret; }
Example 20
Source File: CoverConveyor.java From GregTech with GNU Lesser General Public License v3.0 | 4 votes |
protected int moveInventoryItems(IItemHandler sourceInventory, IItemHandler targetInventory, Map<Object, GroupItemInfo> itemInfos, int maxTransferAmount) { int itemsLeftToTransfer = maxTransferAmount; for (int i = 0; i < sourceInventory.getSlots(); i++) { ItemStack itemStack = sourceInventory.getStackInSlot(i); if(itemStack.isEmpty()) { continue; } Object matchSlotIndex = itemFilterContainer.matchItemStack(itemStack); if (matchSlotIndex == null || !itemInfos.containsKey(matchSlotIndex)) { continue; } GroupItemInfo itemInfo = itemInfos.get(matchSlotIndex); ItemStack extractedStack = sourceInventory.extractItem(i, Math.min(itemInfo.totalCount, itemsLeftToTransfer), true); ItemStack remainderStack = ItemHandlerHelper.insertItemStacked(targetInventory, extractedStack, true); int amountToInsert = extractedStack.getCount() - remainderStack.getCount(); if (amountToInsert > 0) { extractedStack = sourceInventory.extractItem(i, amountToInsert, false); if(!extractedStack.isEmpty()) { ItemHandlerHelper.insertItemStacked(targetInventory, extractedStack, false); itemsLeftToTransfer -= extractedStack.getCount(); itemInfo.totalCount -= extractedStack.getCount(); if (itemInfo.totalCount == 0) { itemInfos.remove(matchSlotIndex); if(itemInfos.isEmpty()) { break; } } if(itemsLeftToTransfer == 0) { break; } } } } return maxTransferAmount - itemsLeftToTransfer; }