Java Code Examples for net.minecraftforge.items.IItemHandler#insertItem()
The following examples show how to use
net.minecraftforge.items.IItemHandler#insertItem() .
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: 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 2
Source File: InventoryUtils.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 6 votes |
public static ItemStack insertItem(IItemHandler handler, ItemStack insert, boolean simulate) { insert = insert.copy(); for (int pass = 0; pass < 2; pass++) { for (int slot = 0; slot < handler.getSlots(); slot++) { ItemStack stack = handler.getStackInSlot(slot); if (pass == 0 && stack.isEmpty()) { continue; } if (insert.isEmpty()) { return ItemStack.EMPTY; } insert = handler.insertItem(slot, insert, simulate); } } return insert; }
Example 3
Source File: InventoryUtils.java From enderutilities with GNU Lesser General Public License v3.0 | 6 votes |
/** * Try insert the items in <b>stackIn</b> into existing stacks with identical items in the inventory <b>inv</b>. * @return null if all items were successfully inserted, otherwise the stack containing the remaining items */ public static ItemStack tryInsertItemStackToExistingStacksInInventory(IItemHandler inv, @Nonnull ItemStack stackIn) { List<Integer> slots = getSlotNumbersOfMatchingStacks(inv, stackIn); for (int slot : slots) { stackIn = inv.insertItem(slot, stackIn, false); // If the entire (remaining) stack was inserted to the current slot, then we are done if (stackIn.isEmpty()) { return ItemStack.EMPTY; } } return stackIn; }
Example 4
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 5
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 6
Source File: InventoryUtils.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 5 votes |
public static int getInsertableQuantity(IItemHandler handler, ItemStack stack) { ItemStack copy = ItemUtils.copyStack(stack, Integer.MAX_VALUE); int quantity = 0; for (int slot = 0; slot < handler.getSlots(); slot++) { if (canInsertStack(handler, slot, copy)) { ItemStack left = handler.insertItem(slot, copy, true); if (left.isEmpty()) { quantity += copy.getCount(); } else { quantity += copy.getCount() - left.getCount(); } } } return quantity; }
Example 7
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 8
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 9
Source File: InventoryUtils.java From enderutilities with GNU Lesser General Public License v3.0 | 5 votes |
/** * Tries to insert the given ItemStack stack to the target inventory, inside the given slot range. * The return value is a stack of the remaining items that couldn't be inserted. * If all items were successfully inserted, then null is returned. */ public static ItemStack tryInsertItemStackToInventoryWithinSlotRange(IItemHandler inv, @Nonnull ItemStack stackIn, SlotRange slotRange) { final int lastSlot = Math.min(slotRange.lastInc, inv.getSlots() - 1); // First try to add to existing stacks for (int slot = slotRange.first; slot <= lastSlot; slot++) { if (inv.getStackInSlot(slot).isEmpty() == false) { stackIn = inv.insertItem(slot, stackIn, false); if (stackIn.isEmpty()) { return ItemStack.EMPTY; } } } // Second round, try to add to any slot for (int slot = slotRange.first; slot <= lastSlot; slot++) { stackIn = inv.insertItem(slot, stackIn, false); if (stackIn.isEmpty()) { return ItemStack.EMPTY; } } return stackIn; }
Example 10
Source File: ItemHandlerList.java From GregTech with GNU Lesser General Public License v3.0 | 4 votes |
@Nonnull @Override public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) { IItemHandler itemHandler = handlerBySlotIndex.get(slot); return itemHandler.insertItem(slot - baseIndexOffset.get(itemHandler), stack, simulate); }
Example 11
Source File: InventoryUtils.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 4 votes |
public static boolean canInsertStack(IItemHandler handler, int slot, ItemStack stack) { return handler.insertItem(slot, stack, true) != stack; }
Example 12
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 13
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 14
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; }