mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 16:49:11 +00:00
Refactor add method in Inventory
This commit is contained in:
@@ -139,36 +139,7 @@ public final class Inventory {
|
||||
boolean stackable = isStackable(item.getDefinition());
|
||||
|
||||
if (stackable) {
|
||||
int slot = slotOf(id);
|
||||
|
||||
if (slot != -1) {
|
||||
Item other = items[slot];
|
||||
|
||||
long total = (long) item.getAmount() + other.getAmount();
|
||||
int amount, remaining;
|
||||
|
||||
if (total > Integer.MAX_VALUE) {
|
||||
amount = (int) (total - Integer.MAX_VALUE);
|
||||
remaining = (int) (total - amount);
|
||||
notifyCapacityExceeded();
|
||||
} else {
|
||||
amount = (int) total;
|
||||
remaining = 0;
|
||||
}
|
||||
|
||||
set(slot, new Item(id, amount));
|
||||
return remaining > 0 ? Optional.of(new Item(id, remaining)) : Optional.empty();
|
||||
}
|
||||
|
||||
for (slot = 0; slot < capacity; slot++) {
|
||||
if (items[slot] == null) {
|
||||
set(slot, item);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
notifyCapacityExceeded();
|
||||
return Optional.of(item);
|
||||
return addStackable(item, id);
|
||||
}
|
||||
|
||||
int remaining = item.getAmount();
|
||||
@@ -203,6 +174,46 @@ public final class Inventory {
|
||||
return Optional.of(new Item(item.getId(), remaining));
|
||||
}
|
||||
|
||||
/**
|
||||
* This method adds as much of the specified stackable {@code item} to this inventory as possible
|
||||
*
|
||||
* @param item The item to add to this inventory.
|
||||
* @param id The item's id
|
||||
* @return The item that may remain, if nothing remains, {@link Optional#empty an empty Optional} is returned.
|
||||
*/
|
||||
private Optional<Item> addStackable(Item item, int id) {
|
||||
int slot = slotOf(id);
|
||||
|
||||
if (slot != -1) {
|
||||
Item other = items[slot];
|
||||
|
||||
long total = (long) item.getAmount() + other.getAmount();
|
||||
int amount, remaining;
|
||||
|
||||
if (total > Integer.MAX_VALUE) {
|
||||
amount = (int) (total - Integer.MAX_VALUE);
|
||||
remaining = (int) (total - amount);
|
||||
notifyCapacityExceeded();
|
||||
} else {
|
||||
amount = (int) total;
|
||||
remaining = 0;
|
||||
}
|
||||
|
||||
set(slot, new Item(id, amount));
|
||||
return remaining > 0 ? Optional.of(new Item(id, remaining)) : Optional.empty();
|
||||
}
|
||||
|
||||
for (slot = 0; slot < capacity; slot++) {
|
||||
if (items[slot] == null) {
|
||||
set(slot, item);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
notifyCapacityExceeded();
|
||||
return Optional.of(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an {@link InventoryListener}.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user