mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 00:38:21 +00:00
Add utility methods to Inventory.
This commit is contained in:
@@ -251,13 +251,7 @@ public final class Inventory implements Cloneable {
|
||||
* @return {@code true} if so, {@code false} if not.
|
||||
*/
|
||||
public boolean contains(int id) {
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
Item item = items[i];
|
||||
if (item != null && item.getId() == id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return slotOf(id) != -1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -303,6 +297,34 @@ public final class Inventory implements Cloneable {
|
||||
return items[slot];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the amount of items with the specified id in this inventory.
|
||||
*
|
||||
* @param id The id.
|
||||
* @return The number of matching items, or {@code 0} if none were found.
|
||||
*/
|
||||
public int getAmount(int id) {
|
||||
if (isStackable(ItemDefinition.lookup(id))) {
|
||||
int slot = slotOf(id);
|
||||
if (slot != -1) {
|
||||
return items[slot].getAmount();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int amount = 0, usedSlots = 0;
|
||||
for (int i = 0; i < capacity && usedSlots <= size; i++) {
|
||||
Item item = items[i];
|
||||
if (item != null) {
|
||||
if (item.getId() == id) {
|
||||
amount++;
|
||||
}
|
||||
usedSlots++;
|
||||
}
|
||||
}
|
||||
return amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a clone of the items array.
|
||||
*
|
||||
@@ -382,8 +404,8 @@ public final class Inventory implements Cloneable {
|
||||
* @return The amount that was removed.
|
||||
*/
|
||||
public int remove(int id, int amount) {
|
||||
ItemDefinition def = ItemDefinition.lookup(id);
|
||||
boolean stackable = isStackable(def);
|
||||
ItemDefinition definition = ItemDefinition.lookup(id);
|
||||
boolean stackable = isStackable(definition);
|
||||
if (stackable) {
|
||||
for (int slot = 0; slot < capacity; slot++) {
|
||||
Item item = items[slot];
|
||||
@@ -505,6 +527,26 @@ public final class Inventory implements Cloneable {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the inventory slot for the specified id.
|
||||
*
|
||||
* @param id The id.
|
||||
* @return The first slot containing the specified item, or {@code -1} if none of the slots matched the conditions.
|
||||
*/
|
||||
public int slotOf(int id) {
|
||||
int usedSlots = 0;
|
||||
for (int slot = 0; slot < capacity && usedSlots <= size; slot++) {
|
||||
Item item = items[slot];
|
||||
if (item != null) {
|
||||
if (item.getId() == id) {
|
||||
return slot;
|
||||
}
|
||||
usedSlots++;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the firing of events.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user