private boolean compare(Inventory inv, Inventory inv2) {
   if (inv == null || inv2 == null) {
     return false;
   }
   if (!inv.getTitle().equals(inv2.getTitle())) {
     return false;
   }
   if (inv.getMaxStackSize() != inv2.getMaxStackSize()) {
     return false;
   }
   if (!inv.getViewers().equals(inv2.getViewers())) {
     return false;
   }
   return true;
 }
Ejemplo n.º 2
0
  /**
   * Checks whether the given isItemsToGive can be added to the inventory as a whole
   *
   * @param isItemsToGive Items to attempt to add to the inventory
   * @return True if there is sufficient space
   */
  public boolean canTakeItems(ItemStack[] isItemsToGive) {
    Map<Integer, StackWithAmount> lookaside = getAmountsMapping();
    for (int i = 0; i < isItemsToGive.length; i++) {
      ItemStack item = isItemsToGive[i];
      if (item == null) continue;
      int amountToAdd = item.getAmount();

      while (amountToAdd > 0) {
        int partialSpaceIndex = findSpace(item, lookaside, false);

        if (partialSpaceIndex == -1) {
          int freeSpaceIndex = findSpace(item, lookaside, true);

          if (freeSpaceIndex == -1) {
            return false;
          } else {
            int toSet = amountToAdd;

            if (amountToAdd > inventory.getMaxStackSize()) {
              amountToAdd -= inventory.getMaxStackSize();
              toSet = inventory.getMaxStackSize();
            } else {
              amountToAdd = 0;
            }

            lookaside.put(freeSpaceIndex, new StackWithAmount(toSet, item));
          }
        } else {
          StackWithAmount stackWithAmount = lookaside.get(partialSpaceIndex);
          int partialAmount = stackWithAmount.getAmount();
          int maxAmount = stackWithAmount.getStack().getMaxStackSize();

          int toSet;
          if (amountToAdd + partialAmount <= maxAmount) {
            toSet = amountToAdd + partialAmount;
            amountToAdd = 0;
          } else {
            toSet = maxAmount;
            amountToAdd = amountToAdd + partialAmount - maxAmount;
          }

          lookaside.get(partialSpaceIndex).setAmount(toSet);
        }
      }
    }

    return true;
  }