Exemplo n.º 1
0
 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;
 }
Exemplo 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;
 }
Exemplo n.º 3
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;
            }
          }
        }
      }
    }
  }
  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;
  }
Exemplo n.º 5
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;
  }
Exemplo n.º 6
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;
  }
Exemplo n.º 7
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
 protected boolean canAdd(ItemStack stack, int slot) {
   return sidedInventory.canInsertItem(slot, stack, this.side.ordinal());
 }