Example #1
0
 @Override
 public void setContents(ItemStack[] items) {
   if (items.length != slots.length) {
     throw new IllegalArgumentException("Length of items must be " + slots.length);
   }
   for (int i = 0; i < slots.length; ++i) {
     slots[i] = ItemIds.sanitize(items[i]);
   }
 }
Example #2
0
  @Override
  public HashMap<Integer, ItemStack> addItem(ItemStack... items) {
    HashMap<Integer, ItemStack> result = new HashMap<>();

    for (int i = 0; i < items.length; ++i) {
      ItemStack item = ItemIds.sanitize(items[i]);
      if (item == null) continue; // invalid items fail silently
      int maxStackSize = item.getType() == null ? 64 : item.getType().getMaxStackSize();
      int toAdd = item.getAmount();

      for (int j = 0; toAdd > 0 && j < getSize(); ++j) {
        // Look for existing stacks to add to
        if (slots[j] != null && slots[j].isSimilar(item)) {
          int space = maxStackSize - slots[j].getAmount();
          if (space < 0) continue;
          if (space > toAdd) space = toAdd;

          slots[j].setAmount(slots[j].getAmount() + space);
          toAdd -= space;
        }
      }

      if (toAdd > 0) {
        // Look for empty slots to add to
        for (int j = 0; toAdd > 0 && j < getSize(); ++j) {
          if (slots[j] == null) {
            int num = toAdd > maxStackSize ? maxStackSize : toAdd;
            slots[j] = item.clone();
            slots[j].setAmount(num);
            toAdd -= num;
          }
        }
      }

      if (toAdd > 0) {
        // Still couldn't stash them all.
        ItemStack remaining = item.clone();
        remaining.setAmount(toAdd);
        result.put(i, remaining);
      }
    }

    return result;
  }
Example #3
0
 @Override
 public void setItem(int index, ItemStack item) {
   slots[index] = ItemIds.sanitize(item);
 }