示例#1
0
 /**
  * Attempts to add a specific slot.
  *
  * @param item The item.
  * @param slot The desired slot.
  * @return <code>true</code> if the item was added, <code>false</code> if not.
  */
 public boolean add(Item item, int slot) {
   if (item == null) return false;
   int newSlot = (slot > -1) ? slot : freeSlot();
   if ((item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK))
       && !type.equals(Type.NEVER_STACK)) {
     if (getCount(item.getId()) > 0) {
       newSlot = getSlotById(item.getId());
     }
   }
   if (newSlot == -1) {
     // the free slot is -1
     return false;
   }
   if (get(newSlot) != null) {
     newSlot = freeSlot();
   }
   if ((item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK))
       && !type.equals(Type.NEVER_STACK)) {
     for (int i = 0; i < items.length; i++) {
       if (items[i] != null && items[i].getId() == item.getId()) {
         int totalCount = item.getCount() + items[i].getCount();
         if (totalCount >= Constants.MAX_ITEMS || totalCount < 1) {
           return false;
         }
         set(i, new Item(items[i].getId(), items[i].getCount() + item.getCount()));
         return true;
       }
     }
     if (newSlot == -1) {
       return false;
     } else {
       set(slot > -1 ? newSlot : freeSlot(), item);
       return true;
     }
   } else {
     int slots = freeSlots();
     if (slots >= item.getCount()) {
       boolean b = firingEvents;
       firingEvents = false;
       try {
         for (int i = 0; i < item.getCount(); i++) {
           set(slot > -1 ? newSlot : freeSlot(), new Item(item.getId()));
         }
         if (b) {
           fireItemsChanged();
         }
         return true;
       } finally {
         firingEvents = b;
       }
     } else {
       return false;
     }
   }
 }
示例#2
0
 public void removelol(Item item, int amount) {
   int am = item.getCount() - amount;
   remove(item, -1);
   if (am > 0) {
     item.setCount(am);
     add(item);
   }
 }
示例#3
0
 /**
  * Removes an item.
  *
  * @param item The item to remove.
  * @param preferredSlot The preferred slot.
  * @param allowZero If a zero amount item should be allowed.
  * @return The number of items removed.
  */
 public int remove(Item item, int preferredSlot, boolean allowZero) {
   int removed = 0;
   if ((item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK))
       && !type.equals(Type.NEVER_STACK)) {
     int slot = getSlotById(item.getId());
     Item stack = get(slot);
     if (stack.getCount() > item.getCount()) {
       removed = item.getCount();
       set(slot, new Item(stack.getId(), stack.getCount() - item.getCount()));
     } else {
       removed = stack.getCount();
       set(slot, allowZero ? new Item(stack.getId(), 0) : null);
     }
   } else {
     for (int i = 0; i < item.getCount(); i++) {
       int slot = getSlotById(item.getId());
       if (i == 0 && preferredSlot != -1) {
         Item inSlot = get(preferredSlot);
         if (inSlot.getId() == item.getId()) {
           slot = preferredSlot;
         }
       }
       if (slot != -1) {
         removed++;
         set(slot, null);
       } else {
         break;
       }
     }
   }
   return removed;
 }
示例#4
0
 /**
  * Checks if there is room in the inventory for an item.
  *
  * @param item The item.
  * @return <code>true</code> if so, <code>false</code> if not.
  */
 public boolean hasRoomFor(Item item) {
   if ((item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK))
       && !type.equals(Type.NEVER_STACK)) {
     for (int i = 0; i < items.length; i++) {
       if (items[i] != null && items[i].getId() == item.getId()) {
         int totalCount = item.getCount() + items[i].getCount();
         if (totalCount >= Constants.MAX_ITEMS || totalCount < 1) {
           return false;
         }
         return true;
       }
     }
     int slot = freeSlot();
     return slot != -1;
   } else {
     int slots = freeSlots();
     return slots >= item.getCount();
   }
 }
示例#5
0
 public boolean contains(int id, int amount) {
   int count = 0;
   for (Item item : items) {
     if (item != null) {
       if (item.getId() == id) {
         count += item.getCount();
       }
     }
   }
   if (count >= amount) {
     return true;
   }
   return false;
 }