Example #1
0
  public static InvStack takeTopStack(IInventory inventory, int side) {
    if (!(inventory instanceof ISidedInventory)) {
      inventory = checkChestInv(inventory);

      for (int i = inventory.getSizeInventory() - 1; i >= 0; i--) {
        if (inventory.getStackInSlot(i) != null) {
          ItemStack toSend = inventory.getStackInSlot(i).copy();
          return new InvStack(inventory, i, toSend);
        }
      }
    } else {
      ISidedInventory sidedInventory = (ISidedInventory) inventory;
      int[] slots =
          sidedInventory.getAccessibleSlotsFromSide(
              ForgeDirection.getOrientation(side).getOpposite().ordinal());

      if (slots != null && slots.length != 0) {
        for (int get = slots.length - 1; get >= 0; get--) {
          int slotID = slots[get];

          if (sidedInventory.getStackInSlot(slotID) != null) {
            ItemStack toSend = sidedInventory.getStackInSlot(slotID);

            if (sidedInventory.canExtractItem(
                slotID, toSend, ForgeDirection.getOrientation(side).getOpposite().ordinal())) {
              return new InvStack(inventory, slotID, toSend);
            }
          }
        }
      }
    }

    return null;
  }
  /** Inserts a stack into an inventory. Args: Inventory, stack, side. Returns leftover items. */
  public static ItemStack insertStack(
      IInventory par0IInventory, ItemStack par1ItemStack, int par2) {
    if (par0IInventory instanceof ISidedInventory && par2 > -1) {
      ISidedInventory isidedinventory = (ISidedInventory) par0IInventory;
      int[] aint = isidedinventory.getAccessibleSlotsFromSide(par2);

      for (int j = 0;
          j < aint.length && par1ItemStack != null && par1ItemStack.stackSize > 0;
          ++j) {
        par1ItemStack = func_102014_c(par0IInventory, par1ItemStack, aint[j], par2);
      }
    } else {
      int k = par0IInventory.getSizeInventory();

      for (int l = 0; l < k && par1ItemStack != null && par1ItemStack.stackSize > 0; ++l) {
        par1ItemStack = func_102014_c(par0IInventory, par1ItemStack, l, par2);
      }
    }

    if (par1ItemStack != null && par1ItemStack.stackSize == 0) {
      par1ItemStack = null;
    }

    return par1ItemStack;
  }
 public static ItemStack insertStackIntoInventory(
     IInventory inventory, ItemStack stack, int side) {
   if (stack == null || inventory == null) return null;
   int stackSize = stack.stackSize;
   if (inventory instanceof ISidedInventory) {
     ISidedInventory sidedInv = (ISidedInventory) inventory;
     int slots[] = sidedInv.getAccessibleSlotsFromSide(side);
     if (slots == null) return stack;
     for (int i = 0; i < slots.length && stack != null; i++) {
       if (sidedInv.canInsertItem(slots[i], stack, side)) {
         ItemStack existingStack = inventory.getStackInSlot(slots[i]);
         if (OreDictionary.itemMatches(existingStack, stack, true)
             && ItemStack.areItemStackTagsEqual(stack, existingStack))
           stack = addToOccupiedSlot(sidedInv, slots[i], stack, existingStack);
       }
     }
     for (int i = 0; i < slots.length && stack != null; i++)
       if (inventory.getStackInSlot(slots[i]) == null
           && sidedInv.canInsertItem(slots[i], stack, side))
         stack = addToEmptyInventorySlot(sidedInv, slots[i], stack);
   } else {
     int invSize = inventory.getSizeInventory();
     for (int i = 0; i < invSize && stack != null; i++) {
       ItemStack existingStack = inventory.getStackInSlot(i);
       if (OreDictionary.itemMatches(existingStack, stack, true)
           && ItemStack.areItemStackTagsEqual(stack, existingStack))
         stack = addToOccupiedSlot(inventory, i, stack, existingStack);
     }
     for (int i = 0; i < invSize && stack != null; i++)
       if (inventory.getStackInSlot(i) == null)
         stack = addToEmptyInventorySlot(inventory, i, stack);
   }
   if (stack == null || stack.stackSize != stackSize) inventory.markDirty();
   return stack;
 }
 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;
 }
 public ItemStack checkExtract(IInventory inventory, boolean doRemove, ForgeDirection from) {
   IInventory inv = Utils.getInventory(inventory);
   int first = 0;
   int last = inv.getSizeInventory() - 1;
   if (inventory instanceof ISidedInventory) {
     ISidedInventory sidedInv = (ISidedInventory) inventory;
     int[] accessibleSlots = sidedInv.getAccessibleSlotsFromSide(from.ordinal());
     ItemStack result = checkExtractGeneric(inv, doRemove, from, accessibleSlots);
     return result;
   }
   ItemStack result = checkExtractGeneric(inv, doRemove, from, first, last);
   return result;
 }
  /** Sucks one item into the given hopper from an inventory or EntityItem above it. */
  public static boolean suckItemsIntoHopper(Hopper par0Hopper) {
    IInventory iinventory = getInventoryAboveHopper(par0Hopper);

    if (iinventory != null) {
      byte b0 = 0;

      if (iinventory instanceof ISidedInventory && b0 > -1) {
        ISidedInventory isidedinventory = (ISidedInventory) iinventory;
        int[] aint = isidedinventory.getAccessibleSlotsFromSide(b0);

        for (int i = 0; i < aint.length; ++i) {
          if (insertStackFromInventory(par0Hopper, iinventory, aint[i], b0)) {
            return true;
          }
        }
      } else {
        int j = iinventory.getSizeInventory();

        for (int k = 0; k < j; ++k) {
          if (insertStackFromInventory(par0Hopper, iinventory, k, b0)) {
            return true;
          }
        }
      }
    } else {
      EntityItem entityitem =
          getEntityAbove(
              par0Hopper.getWorldObj(),
              par0Hopper.getXPos(),
              par0Hopper.getYPos() + 1.0D,
              par0Hopper.getZPos());

      if (entityitem != null) {
        return insertStackFromEntity(par0Hopper, entityitem);
      }
    }

    return false;
  }
  @Override
  public ItemStack pullItem(ForgeDirection side, boolean doPull) {
    int xo = xCoord + side.offsetX;
    int yo = yCoord + side.offsetY;
    int zo = zCoord + side.offsetZ;

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

    if (t instanceof IInventory) {
      if (t instanceof ISpecialInventory) {
        ISpecialInventory isi = (ISpecialInventory) t;
        ItemStack[] items = isi.extractItem(doPull, side.getOpposite(), 1);
        if (items != null && items.length > 0) return items[0];
      } else if (t instanceof ISidedInventory) {
        ISidedInventory isi = (ISidedInventory) t;
        int[] slots = isi.getAccessibleSlotsFromSide(side.getOpposite().ordinal());

        for (int i = 0; i < slots.length; i++) {
          ItemStack pulled = isi.getStackInSlot(slots[i]);
          if (pulled != null
              && isi.canExtractItem(slots[i], pulled, side.getOpposite().ordinal())) {
            ItemStack result = null; // pulled.copy().splitStack(1);
            // pulled.stackSize--;
            // isi.setInventorySlotContents(slots[i], pulled);
            // if(pulled.stackSize <= 0) isi.setInventorySlotContents(slots[i], null);
            if (doPull) {
              result = isi.decrStackSize(slots[i], 1);
              isi.onInventoryChanged();
            } else {
              result = pulled.copy().splitStack(1);
            }
            return result;
          }
        }
      } else {
        IInventory ii = (IInventory) t;

        for (int i = 0; i < ii.getSizeInventory(); i++) {
          ItemStack pulled = ii.getStackInSlot(i);
          if (pulled != null) {
            ItemStack result = null;
            // pulled.stackSize--;
            // ii.setInventorySlotContents(i, pulled);
            // if(pulled.stackSize <= 0)ii.setInventorySlotContents(i, null);
            if (doPull) {
              result = ii.decrStackSize(i, 1);
              ii.onInventoryChanged();
            } else {
              result = pulled.copy().splitStack(1);
            }
            return result;
          }
        }
      }
    }

    return null;
  }
Example #8
0
  public static ItemStack takeTopItemFromInventory(IInventory inventory, int side) {
    if (!(inventory instanceof ISidedInventory)) {
      for (int i = inventory.getSizeInventory() - 1; i >= 0; i--) {
        if (inventory.getStackInSlot(i) != null) {
          ItemStack toSend = inventory.getStackInSlot(i).copy();
          toSend.stackSize = 1;

          inventory.decrStackSize(i, 1);

          return toSend;
        }
      }
    } else {
      ISidedInventory sidedInventory = (ISidedInventory) inventory;
      int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);

      if (slots != null) {
        for (int get = slots.length - 1; get >= 0; get--) {
          int slotID = slots[get];

          if (sidedInventory.getStackInSlot(slotID) != null) {
            ItemStack toSend = sidedInventory.getStackInSlot(slotID);
            toSend.stackSize = 1;

            if (sidedInventory.canExtractItem(slotID, toSend, side)) {
              sidedInventory.decrStackSize(slotID, 1);

              return toSend;
            }
          }
        }
      }
    }

    return null;
  }
Example #9
0
  private void pushToInventories() {
    int iSide = 0;

    for (int i = 0; i < 5; i++) {
      int x = this.xCoord;
      int y = this.yCoord;
      int z = this.zCoord;

      switch (i) {
        case 0:
          x++;
          iSide = 5;
          break;
        case 2:
          z++;
          iSide = 3;
          break;
        case 3:
          x--;
          iSide = 4;
          break;
        case 4:
          y--;
          iSide = 0;
          break;
        case 1:
          z--;
          iSide = 2;
          break;
      }

      TileEntity tile = this.worldObj.getTileEntity(x, y, z);

      if (tile == null) {
        continue;
      }

      if (tile != null && tile instanceof ISidedInventory) {
        ISidedInventory inv = (ISidedInventory) tile;

        if (inv != null) {
          int[] slots = inv.getAccessibleSlotsFromSide(Facing.oppositeSide[iSide]);

          if (slots.length > 0) {
            for (int j = outputStorage[0]; j < outputStorage[1]; j++) {
              ItemStack stack = inventory[j];

              if (stack == null) {
                continue;
              }

              for (int k : slots) {
                if (inv.canInsertItem(k, stack, Facing.oppositeSide[iSide])) {
                  ItemStack otherStack = inv.getStackInSlot(k);

                  if (otherStack == null) {
                    inv.setInventorySlotContents(k, stack);
                    inventory[j] = null;
                  } else if (Utils.areItemStacksEqual(stack, otherStack)) {
                    int remain = otherStack.getMaxStackSize() - otherStack.stackSize;

                    if (stack.stackSize <= remain) {
                      otherStack.stackSize += stack.stackSize;
                      inventory[j] = null;
                    } else {
                      otherStack.stackSize += remain;
                      inventory[j].stackSize -= remain;
                    }
                  }
                }
              }
            }
          }
        }
      } else if (tile instanceof IInventory) {
        for (int j = outputStorage[0]; j <= outputStorage[1]; j++) {
          ItemStack stack = inventory[j];

          if (stack != null) {
            ItemStack result = Utils.pushStackInInv((IInventory) tile, stack);

            if (result == null) {
              inventory[j] = null;
            } else {
              inventory[j].stackSize = result.stackSize;
            }
          }
        }
      }
    }
  }
Example #10
0
  private void pullFromInventories() {
    TileEntity tile = this.worldObj.getTileEntity(this.xCoord, this.yCoord + 1, this.zCoord);

    if (tile instanceof ISidedInventory) {
      final int side = 0;
      ISidedInventory inv = (ISidedInventory) tile;

      int[] slots = inv.getAccessibleSlotsFromSide(side);

      if (slots.length > 0) {
        for (int i : slots) {
          ItemStack stack = inv.getStackInSlot(i);

          if (stack == null) {
            continue;
          }

          if (inv.canExtractItem(i, stack, side)) {
            if (TileEntityFurnace.isItemFuel(stack) || stack.getItem() == ObjHandler.kleinStars) {
              if (inventory[0] == null) {
                inventory[0] = stack;
                inv.setInventorySlotContents(i, null);
              } else if (Utils.areItemStacksEqual(stack, inventory[0])) {
                int remain = inventory[0].getMaxStackSize() - inventory[0].stackSize;

                if (stack.stackSize <= remain) {
                  inventory[0].stackSize += stack.stackSize;
                  inv.setInventorySlotContents(i, null);
                } else {
                  inventory[0].stackSize += remain;
                  stack.stackSize -= remain;
                }
              }

              continue;
            }

            for (int j = inputStorage[0]; j < inputStorage[1]; j++) {
              ItemStack otherStack = inventory[j];

              if (otherStack == null) {
                inventory[j] = stack;
                inv.setInventorySlotContents(i, null);
                break;
              } else if (Utils.areItemStacksEqual(stack, otherStack)) {
                int remain = otherStack.getMaxStackSize() - otherStack.stackSize;

                if (stack.stackSize <= remain) {
                  inventory[j].stackSize += stack.stackSize;
                  inv.setInventorySlotContents(i, null);
                  break;
                } else {
                  inventory[j].stackSize += remain;
                  stack.stackSize -= remain;
                }
              }
            }
          }
        }
      }
    } else if (tile instanceof IInventory) {
      IInventory inv = (IInventory) tile;

      for (int i = 0; i < inv.getSizeInventory(); i++) {
        ItemStack stack = inv.getStackInSlot(i);

        if (stack == null) {
          continue;
        }

        if (TileEntityFurnace.isItemFuel(stack) || stack.getItem() == ObjHandler.kleinStars) {
          ItemStack fuel = inventory[0];

          if (fuel == null) {
            inventory[0] = stack;
            inv.setInventorySlotContents(i, null);
          } else {
            int remain = fuel.getMaxStackSize() - fuel.stackSize;

            if (stack.stackSize <= remain) {
              inventory[0].stackSize += stack.stackSize;
              inv.setInventorySlotContents(i, null);
            } else {
              inventory[0].stackSize += remain;
              stack.stackSize -= remain;
            }
          }

          continue;
        } else if (FurnaceRecipes.smelting().getSmeltingResult(stack) == null) {
          continue;
        }

        for (int j = inputStorage[0]; j < inputStorage[1]; j++) {
          ItemStack otherStack = inventory[j];

          if (otherStack == null) {
            inventory[j] = stack;
            inv.setInventorySlotContents(i, null);
            break;
          } else if (Utils.areItemStacksEqual(stack, otherStack)) {
            int remain = otherStack.getMaxStackSize() - otherStack.stackSize;

            if (stack.stackSize <= remain) {
              inventory[j].stackSize += stack.stackSize;
              inv.setInventorySlotContents(i, null);
              break;
            } else {
              inventory[j].stackSize += remain;
              stack.stackSize -= remain;
            }
          }
        }
      }
    }
  }
  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;
  }
Example #12
0
  public static ItemStack putStackInInventory(
      IInventory inventory, ItemStack itemStack, int side, boolean force) {
    if (force && inventory instanceof TileEntityLogisticalSorter) {
      return ((TileEntityLogisticalSorter) inventory).sendHome(itemStack.copy());
    }

    ItemStack toInsert = itemStack.copy();

    if (!(inventory instanceof ISidedInventory)) {
      inventory = checkChestInv(inventory);

      for (int i = 0; i <= inventory.getSizeInventory() - 1; i++) {
        if (!force) {
          if (!inventory.isItemValidForSlot(i, toInsert)) {
            continue;
          }
        }

        ItemStack inSlot = inventory.getStackInSlot(i);

        if (inSlot == null) {
          inventory.setInventorySlotContents(i, toInsert);
          return null;
        } else if (inSlot.isItemEqual(toInsert) && inSlot.stackSize < inSlot.getMaxStackSize()) {
          if (inSlot.stackSize + toInsert.stackSize <= inSlot.getMaxStackSize()) {
            ItemStack toSet = toInsert.copy();
            toSet.stackSize += inSlot.stackSize;

            inventory.setInventorySlotContents(i, toSet);
            return null;
          } else {
            int rejects = (inSlot.stackSize + toInsert.stackSize) - inSlot.getMaxStackSize();

            ItemStack toSet = toInsert.copy();
            toSet.stackSize = inSlot.getMaxStackSize();

            ItemStack remains = toInsert.copy();
            remains.stackSize = rejects;

            inventory.setInventorySlotContents(i, toSet);

            toInsert = remains;
          }
        }
      }
    } else {
      ISidedInventory sidedInventory = (ISidedInventory) inventory;
      int[] slots =
          sidedInventory.getAccessibleSlotsFromSide(
              ForgeDirection.getOrientation(side).getOpposite().ordinal());

      if (slots != null && slots.length != 0) {
        if (force
            && sidedInventory instanceof TileEntityBin
            && ForgeDirection.getOrientation(side).getOpposite().ordinal() == 0) {
          slots = sidedInventory.getAccessibleSlotsFromSide(1);
        }

        for (int get = 0; get <= slots.length - 1; get++) {
          int slotID = slots[get];

          if (!force) {
            if (!sidedInventory.isItemValidForSlot(slotID, toInsert)
                && !sidedInventory.canInsertItem(
                    slotID,
                    toInsert,
                    ForgeDirection.getOrientation(side).getOpposite().ordinal())) {
              continue;
            }
          }

          ItemStack inSlot = inventory.getStackInSlot(slotID);

          if (inSlot == null) {
            inventory.setInventorySlotContents(slotID, toInsert);
            return null;
          } else if (inSlot.isItemEqual(toInsert) && inSlot.stackSize < inSlot.getMaxStackSize()) {
            if (inSlot.stackSize + toInsert.stackSize <= inSlot.getMaxStackSize()) {
              ItemStack toSet = toInsert.copy();
              toSet.stackSize += inSlot.stackSize;

              inventory.setInventorySlotContents(slotID, toSet);
              return null;
            } else {
              int rejects = (inSlot.stackSize + toInsert.stackSize) - inSlot.getMaxStackSize();

              ItemStack toSet = toInsert.copy();
              toSet.stackSize = inSlot.getMaxStackSize();

              ItemStack remains = toInsert.copy();
              remains.stackSize = rejects;

              inventory.setInventorySlotContents(slotID, toSet);

              toInsert = remains;
            }
          }
        }
      }
    }

    return toInsert;
  }
 @Override
 protected boolean canAdd(ItemStack stack, int slot) {
   return sidedInventory.canInsertItem(slot, stack, this.side.ordinal());
 }
Example #14
0
  public static InvStack takeDefinedItem(
      IInventory inventory, int side, ItemStack type, int min, int max) {
    InvStack ret = new InvStack(inventory);

    if (!(inventory instanceof ISidedInventory)) {
      inventory = checkChestInv(inventory);

      for (int i = inventory.getSizeInventory() - 1; i >= 0; i--) {
        if (inventory.getStackInSlot(i) != null && inventory.getStackInSlot(i).isItemEqual(type)) {
          ItemStack stack = inventory.getStackInSlot(i);
          int current = ret.getStack() != null ? ret.getStack().stackSize : 0;

          if (current + stack.stackSize <= max) {
            ret.appendStack(i, stack.copy());
          } else {
            ItemStack copy = stack.copy();
            copy.stackSize = max - current;
            ret.appendStack(i, copy);
          }

          if (ret.getStack() != null && ret.getStack().stackSize == max) {
            return ret;
          }
        }
      }
    } else {
      ISidedInventory sidedInventory = (ISidedInventory) inventory;
      int[] slots =
          sidedInventory.getAccessibleSlotsFromSide(
              ForgeDirection.getOrientation(side).getOpposite().ordinal());

      if (slots != null && slots.length != 0) {
        for (int get = slots.length - 1; get >= 0; get--) {
          int slotID = slots[get];

          if (sidedInventory.getStackInSlot(slotID) != null
              && inventory.getStackInSlot(slotID).isItemEqual(type)) {
            ItemStack stack = sidedInventory.getStackInSlot(slotID);
            int current = ret.getStack() != null ? ret.getStack().stackSize : 0;

            if (current + stack.stackSize <= max) {
              ItemStack copy = stack.copy();

              if (sidedInventory.canExtractItem(
                  slotID, copy, ForgeDirection.getOrientation(side).getOpposite().ordinal())) {
                ret.appendStack(slotID, copy);
              }
            } else {
              ItemStack copy = stack.copy();

              if (sidedInventory.canExtractItem(
                  slotID, copy, ForgeDirection.getOrientation(side).getOpposite().ordinal())) {
                copy.stackSize = max - current;
                ret.appendStack(slotID, copy);
              }
            }

            if (ret.getStack() != null && ret.getStack().stackSize == max) {
              return ret;
            }
          }
        }
      }
    }

    if (ret != null && ret.getStack() != null && ret.getStack().stackSize >= min) {
      return ret;
    }

    return null;
  }
Example #15
0
  private static int mergeItemStackInternal(
      IInventory inventory,
      ISidedInventory sidedInventory,
      EnumFacing side,
      ItemStack result,
      int start,
      int stop,
      Map<Integer, ItemStack> undo) {
    int k = start;

    ItemStack itemstack1;
    int itemsToPlace = result.stackSize;

    if (result.isStackable()) {
      while (itemsToPlace > 0 && (k < stop)) {
        itemstack1 = inventory.getStackInSlot(k);

        if (isItemStackConsideredEqual(result, itemstack1)
            && (sidedInventory == null || sidedInventory.canInsertItem(k, result, side))) {
          int l = itemstack1.stackSize + itemsToPlace;

          if (l <= result.getMaxStackSize()) {
            if (undo != null) {
              // Only put on undo map if the key is not already present.
              if (!undo.containsKey(k)) {
                undo.put(k, itemstack1.copy());
              }
            }
            itemsToPlace = 0;
            itemstack1.stackSize = l;
            inventory.markDirty();
          } else if (itemstack1.stackSize < result.getMaxStackSize()) {
            if (undo != null) {
              if (!undo.containsKey(k)) {
                undo.put(k, itemstack1.copy());
              }
            }
            itemsToPlace -= result.getMaxStackSize() - itemstack1.stackSize;
            itemstack1.stackSize = result.getMaxStackSize();
            inventory.markDirty();
          }
        }

        ++k;
      }
    }

    if (itemsToPlace > 0) {
      k = start;

      while (k < stop) {
        itemstack1 = inventory.getStackInSlot(k);

        if (itemstack1 == null
            && (sidedInventory == null || sidedInventory.canInsertItem(k, result, side))) {
          if (undo != null) {
            if (!undo.containsKey(k)) {
              undo.put(k, null);
            }
          }
          ItemStack copy = result.copy();
          copy.stackSize = itemsToPlace;
          inventory.setInventorySlotContents(k, copy);
          inventory.markDirty();
          itemsToPlace = 0;
          break;
        }

        ++k;
      }
    }

    return itemsToPlace;
  }
 @Override
 public int[] getSlots() {
   return sidedInventory.getAccessibleSlotsFromSide(this.side.ordinal());
 }
 @Override
 protected boolean canRemove(ItemStack stack, int slot) {
   return sidedInventory.canExtractItem(slot, stack, this.side.ordinal());
 }
 @Override
 public IItemStack getStackInSlot(int slot) {
   return MCInterface.fromItemStack(inv.getStackInSlot(slots[slot]));
 }
 public void setStackInSlot(int slot, IItemStack stack) {
   inv.setInventorySlotContents(slots[slot], MCInterface.toItemStack(stack));
 }
Example #20
0
  public static boolean canInsert(
      TileEntity tileEntity, EnumColor color, ItemStack itemStack, int side, boolean force) {
    if (!(tileEntity instanceof IInventory)) {
      return false;
    }

    if (force && tileEntity instanceof TileEntityLogisticalSorter) {
      return ((TileEntityLogisticalSorter) tileEntity).canSendHome(itemStack);
    }

    if (!force && tileEntity instanceof IInvConfiguration) {
      IInvConfiguration config = (IInvConfiguration) tileEntity;
      int tileSide = config.getOrientation();
      EnumColor configColor =
          config
              .getEjector()
              .getInputColor(
                  ForgeDirection.getOrientation(MekanismUtils.getBaseOrientation(side, tileSide))
                      .getOpposite());

      if (config.getEjector().hasStrictInput() && configColor != null && configColor != color) {
        return false;
      }
    }

    IInventory inventory = (IInventory) tileEntity;

    if (!(inventory instanceof ISidedInventory)) {
      inventory = InventoryUtils.checkChestInv(inventory);

      for (int i = 0; i <= inventory.getSizeInventory() - 1; i++) {
        if (!force) {
          if (!inventory.isItemValidForSlot(i, itemStack)) {
            continue;
          }
        }

        ItemStack inSlot = inventory.getStackInSlot(i);

        if (inSlot == null) {
          return true;
        } else if (inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize()) {
          if (inSlot.stackSize + itemStack.stackSize <= inSlot.getMaxStackSize()) {
            return true;
          } else {
            int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize();

            if (rejects < itemStack.stackSize) {
              return true;
            }
          }
        }
      }
    } else {
      ISidedInventory sidedInventory = (ISidedInventory) inventory;
      int[] slots =
          sidedInventory.getAccessibleSlotsFromSide(
              ForgeDirection.getOrientation(side).getOpposite().ordinal());

      if (slots != null && slots.length != 0) {
        if (force
            && sidedInventory instanceof TileEntityBin
            && ForgeDirection.getOrientation(side).getOpposite().ordinal() == 0) {
          slots = sidedInventory.getAccessibleSlotsFromSide(1);
        }

        for (int get = 0; get <= slots.length - 1; get++) {
          int slotID = slots[get];

          if (!force) {
            if (!sidedInventory.isItemValidForSlot(slotID, itemStack)
                || !sidedInventory.canInsertItem(
                    slotID,
                    itemStack,
                    ForgeDirection.getOrientation(side).getOpposite().ordinal())) {
              continue;
            }
          }

          ItemStack inSlot = inventory.getStackInSlot(slotID);

          if (inSlot == null) {
            return true;
          } else if (inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize()) {
            if (inSlot.stackSize + itemStack.stackSize <= inSlot.getMaxStackSize()) {
              return true;
            } else {
              int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize();

              if (rejects < itemStack.stackSize) {
                return true;
              }
            }
          }
        }
      }
    }

    return false;
  }
 public SidedInventoryWrapper(EnumFacing side, ISidedInventory inv) {
   this.side = side;
   this.inv = inv;
   slots = inv.getSlotsForFace(side);
 }