/**
   * Adds an ItemStack to the first available slot in the provided IInventory, continuing to
   * distribute the stack as necessary until the stacksize reaches 0, if possible
   *
   * @return the number of items remaining in the stack, zero if all were added
   */
  public static int addItemToInventory(ItemStack stack, IInventory inv) {
    int remaining = stack.stackSize;
    for (int i = 0; i < inv.getSizeInventory() && remaining > 0; ++i) {
      ItemStack slotstack = inv.getStackInSlot(i);
      if (slotstack == null && inv.isItemValidForSlot(i, stack)) {
        remaining -= inv.getInventoryStackLimit();
        stack.stackSize = (remaining > 0 ? inv.getInventoryStackLimit() : stack.stackSize);
        inv.setInventorySlotContents(i, stack);
        inv.markDirty();
      } else if (slotstack != null && stack.isStackable() && inv.isItemValidForSlot(i, stack)) {
        if (slotstack.getItem() == stack.getItem()
            && (!stack.getHasSubtypes() || stack.getItemDamage() == slotstack.getItemDamage())
            && ItemStack.areItemStackTagsEqual(stack, slotstack)) {
          int l = slotstack.stackSize + remaining;
          if (l <= stack.getMaxStackSize() && l <= inv.getInventoryStackLimit()) {
            remaining = 0;
            slotstack.stackSize = l;
            inv.markDirty();
          } else if (slotstack.stackSize < stack.getMaxStackSize()
              && stack.getMaxStackSize() <= inv.getInventoryStackLimit()) {
            remaining -= stack.getMaxStackSize() - slotstack.stackSize;
            slotstack.stackSize = stack.getMaxStackSize();
            inv.markDirty();
          }
        }
      }
    }

    return remaining;
  }
Esempio n. 2
0
 public static boolean canInsertStackIntoInventory(
     IInventory inventory, ItemStack stack, int side) {
   if (stack == null || inventory == null) return false;
   if (inventory instanceof ISidedInventory) {
     ISidedInventory sidedInv = (ISidedInventory) inventory;
     int slots[] = sidedInv.getAccessibleSlotsFromSide(side);
     if (slots == null) return false;
     for (int i = 0; i < slots.length && stack != null; i++) {
       if (sidedInv.canInsertItem(slots[i], stack, side)
           && sidedInv.isItemValidForSlot(slots[i], stack)) {
         ItemStack existingStack = inventory.getStackInSlot(slots[i]);
         if (existingStack == null) return true;
         else if (OreDictionary.itemMatches(existingStack, stack, true)
             && ItemStack.areItemStackTagsEqual(stack, existingStack))
           if (existingStack.stackSize + stack.stackSize < inventory.getInventoryStackLimit()
               && existingStack.stackSize + stack.stackSize < existingStack.getMaxStackSize())
             return true;
       }
     }
   } else {
     int invSize = inventory.getSizeInventory();
     for (int i = 0; i < invSize && stack != null; i++)
       if (inventory.isItemValidForSlot(i, stack)) {
         ItemStack existingStack = inventory.getStackInSlot(i);
         if (existingStack == null) return true;
         else if (OreDictionary.itemMatches(existingStack, stack, true)
             && ItemStack.areItemStackTagsEqual(stack, existingStack))
           if (existingStack.stackSize + stack.stackSize < inventory.getInventoryStackLimit()
               && existingStack.stackSize + stack.stackSize < existingStack.getMaxStackSize())
             return true;
       }
   }
   return false;
 }
Esempio n. 3
0
  public static final int getItemStackMaxQuantity(ItemStack itemStack, IInventory inventory) {
    Item item = itemStack.getItem();

    if (item instanceof IFood)
      return (int) ((IFood) itemStack.getItem()).getFoodMaxWeight(itemStack);

    if (inventory instanceof TEIngotPile) return inventory.getInventoryStackLimit();

    return Math.min(itemStack.getMaxStackSize(), inventory.getInventoryStackLimit());
  }
Esempio n. 4
0
 public static ItemStack insertItem(
     IInventory inventory, ItemStack itemStack, boolean test, boolean force) {
   if (test) {
     itemStack = itemStack.copy();
   }
   int size = inventory.getSizeInventory();
   for (int i = 0; i < size; i++) {
     if (inventory.isItemValidForSlot(i, itemStack) || force) {
       ItemStack storedItem = inventory.getStackInSlot(i);
       if (storedItem != null) {
         if (equalItemAndNBT(storedItem, itemStack)) {
           int maxStackSize =
               Math.min(itemStack.getMaxStackSize(), inventory.getInventoryStackLimit());
           int add = Math.min(itemStack.stackSize, maxStackSize - storedItem.stackSize);
           if (!test) {
             storedItem.stackSize += add;
           }
           itemStack.stackSize -= add;
           inventory.setInventorySlotContents(i, storedItem);
         }
       } else {
         storedItem = itemStack.copy();
         storedItem.stackSize = Math.min(itemStack.stackSize, itemStack.getMaxStackSize());
         if (!test) {
           inventory.setInventorySlotContents(i, storedItem);
         }
         itemStack.stackSize -= storedItem.stackSize;
       }
       if (itemStack.stackSize <= 0) {
         return null;
       }
     }
   }
   return itemStack;
 }
Esempio n. 5
0
 public static ItemStack addToEmptyInventorySlot(IInventory inventory, int slot, ItemStack stack) {
   if (!inventory.isItemValidForSlot(slot, stack)) {
     return stack;
   }
   int stackLimit = inventory.getInventoryStackLimit();
   inventory.setInventorySlotContents(
       slot, copyStackWithAmount(stack, Math.min(stack.stackSize, stackLimit)));
   return stackLimit >= stack.stackSize ? null : stack.splitStack(stack.stackSize - stackLimit);
 }
  private static ItemStack func_102014_c(
      IInventory par0IInventory, ItemStack par1ItemStack, int par2, int par3) {
    ItemStack itemstack1 = par0IInventory.getStackInSlot(par2);

    if (canInsertItemToInventory(par0IInventory, par1ItemStack, par2, par3)) {
      boolean flag = false;

      if (itemstack1 == null) {
        int max =
            Math.min(par1ItemStack.getMaxStackSize(), par0IInventory.getInventoryStackLimit());
        if (max >= par1ItemStack.stackSize) {
          par0IInventory.setInventorySlotContents(par2, par1ItemStack);
          par1ItemStack = null;
        } else {
          par0IInventory.setInventorySlotContents(par2, par1ItemStack.splitStack(max));
        }
        flag = true;
      } else if (areItemStacksEqualItem(itemstack1, par1ItemStack)) {
        int max =
            Math.min(par1ItemStack.getMaxStackSize(), par0IInventory.getInventoryStackLimit());
        if (max > itemstack1.stackSize) {
          int l = Math.min(par1ItemStack.stackSize, max - itemstack1.stackSize);
          par1ItemStack.stackSize -= l;
          itemstack1.stackSize += l;
          flag = l > 0;
        }
      }

      if (flag) {
        if (par0IInventory instanceof TileEntityHopper) {
          ((TileEntityHopper) par0IInventory).setTransferCooldown(8);
          par0IInventory.onInventoryChanged();
        }

        par0IInventory.onInventoryChanged();
      }
    }

    return par1ItemStack;
  }
Esempio n. 7
0
 public static ItemStack addToOccupiedSlot(
     IInventory inventory, int slot, ItemStack stack, ItemStack existingStack) {
   int stackLimit = Math.min(inventory.getInventoryStackLimit(), stack.getMaxStackSize());
   if (stack.stackSize + existingStack.stackSize > stackLimit) {
     int stackDiff = stackLimit - existingStack.stackSize;
     existingStack.stackSize = stackLimit;
     stack.stackSize -= stackDiff;
     inventory.setInventorySlotContents(slot, existingStack);
     return stack;
   }
   existingStack.stackSize += Math.min(stack.stackSize, stackLimit);
   inventory.setInventorySlotContents(slot, existingStack);
   return stackLimit >= stack.stackSize ? null : stack.splitStack(stack.stackSize - stackLimit);
 }
  @Override
  public ItemStack addItem(ItemStack stack) {

    if (stack == null) {
      return null;
    }

    int quantitytoadd = stack.stackSize;
    ItemStack remaining = stack.copy();
    int[] slots = getSlots();
    if (slots == null) {
      return remaining;
    }

    for (int i : slots) {
      int maxStackSize = Math.min(_inv.getInventoryStackLimit(), stack.getMaxStackSize());
      ItemStack s = getSlotContents(i);
      if (s == null) {
        ItemStack add = stack.copy();
        add.stackSize = Math.min(quantitytoadd, maxStackSize);

        if (canAddItem(add, i)) {
          quantitytoadd -= add.stackSize;
          _inv.setInventorySlotContents(i, add);
          _inv.markDirty();
        }
      } else if (ItemHelper.itemsEqualWithMetadata(s, stack)) {
        ItemStack add = stack.copy();
        add.stackSize = Math.min(quantitytoadd, maxStackSize - s.stackSize);

        if (add.stackSize > 0 && canAddItem(add, i)) {
          s.stackSize += add.stackSize;
          quantitytoadd -= add.stackSize;
          _inv.setInventorySlotContents(i, s);
          _inv.markDirty();
        }
      }
      if (quantitytoadd == 0) {
        break;
      }
    }

    remaining.stackSize = quantitytoadd;
    if (remaining.stackSize == 0) {
      return null;
    } else {
      return remaining;
    }
  }
 @Override
 public int getInventoryStackLimit() {
   return stackSizeLimit > 0 ? stackSizeLimit : inv.getInventoryStackLimit();
 }
  public boolean tryInsertItem(ItemStack stack, ForgeDirection side) {
    int xo = xCoord + side.offsetX;
    int yo = yCoord + side.offsetY;
    int zo = zCoord + side.offsetZ;

    TileEntity t = worldObj.getBlockTileEntity(xo, yo, zo);

    if (stack == null) return false;

    if (t instanceof IInventory) {
      if (t instanceof ISpecialInventory) {
        ISpecialInventory isi = (ISpecialInventory) t;
        ItemStack ghost = stack.copy().splitStack(1);
        int used = isi.addItem(ghost, true, side.getOpposite());
        if (used > 0) return true;

      } else if (t instanceof ISidedInventory) {
        ISidedInventory isi = (ISidedInventory) t;
        ItemStack ghost = stack.copy().splitStack(1);
        int[] slots = isi.getAccessibleSlotsFromSide(side.getOpposite().ordinal());

        for (int i = 0; i < slots.length; i++) {
          if (isi.canInsertItem(slots[i], ghost, side.getOpposite().ordinal())) {
            ItemStack inSlot = isi.getStackInSlot(slots[i]);
            if (inSlot != null
                && inSlot.isItemEqual(ghost)
                && inSlot.stackSize < inSlot.getMaxStackSize()
                && inSlot.stackSize < isi.getInventoryStackLimit()) {
              inSlot.stackSize++;
              isi.onInventoryChanged();
              return true;
            }
          }
        }

        for (int i = 0; i < slots.length; i++) {
          if (isi.canInsertItem(slots[i], ghost, side.getOpposite().ordinal())) {
            ItemStack inSlot = isi.getStackInSlot(slots[i]);
            if (inSlot == null) {
              isi.setInventorySlotContents(slots[i], ghost);
              isi.onInventoryChanged();
              return true;
            }
          }
        }

        return false;
      } else {
        IInventory ii = (IInventory) t;
        ItemStack ghost = stack.copy().splitStack(1);

        for (int i = 0; i < ii.getSizeInventory(); i++) {
          if (ii.isItemValidForSlot(i, ghost)) {
            ItemStack inSlot = ii.getStackInSlot(i);
            if (inSlot != null
                && inSlot.isItemEqual(ghost)
                && inSlot.stackSize < inSlot.getMaxStackSize()
                && inSlot.stackSize < ii.getInventoryStackLimit()) {
              inSlot.stackSize++;
              ii.onInventoryChanged();
              return true;
            }
          }
        }

        for (int i = 0; i < ii.getSizeInventory(); i++) {
          if (ii.isItemValidForSlot(i, ghost)) {
            ItemStack inSlot = ii.getStackInSlot(i);
            if (inSlot == null) {
              ii.setInventorySlotContents(i, ghost);
              ii.onInventoryChanged();
              return true;
            }
          }
        }

        return false;
      }
    }

    if (Loader.isModLoaded("BuildCraft|Core") && t != null && stack != null) {
      if (t instanceof IPipeTile) {
        IPipeTile p = (IPipeTile) t;

        if (p.getPipeType() == PipeType.ITEM && p.isPipeConnected(side.getOpposite())) {
          int res = p.injectItem(stack, false, side.getOpposite());
          if (res == stack.stackSize) {
            p.injectItem(stack, true, side.getOpposite());
            stack.stackSize = 0;
            return true;
          }
        }
      }
    }

    return false;
  }