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;
  }
  @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;
  }
  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;
  }
Exemple #4
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 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;
  }
 @Override
 protected boolean canRemove(ItemStack stack, int slot) {
   return sidedInventory.canExtractItem(slot, stack, this.side.ordinal());
 }