Esempio n. 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;
  }
  @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;
  }
Esempio n. 3
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;
  }
Esempio n. 4
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;
            }
          }
        }
      }
    }
  }
Esempio n. 5
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;
  }
Esempio n. 7
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;
  }
 @Override
 public IItemStack getStackInSlot(int slot) {
   return MCInterface.fromItemStack(inv.getStackInSlot(slots[slot]));
 }