use of org.apollo.game.model.Item in project apollo by apollo-rsps.
the class Inventory method set.
/**
* Sets the item that is in the specified slot.
*
* @param slot The slot.
* @param item The item, or {@code null} to remove the item that is in the slot.
* @return The item that was in the slot.
*/
public Item set(int slot, Item item) {
if (item == null) {
return reset(slot);
}
checkBounds(slot);
Item old = items[slot];
if (old == null) {
size++;
}
items[slot] = item;
notifyItemUpdated(slot);
return old;
}
use of org.apollo.game.model.Item in project apollo by apollo-rsps.
the class Inventory method remove.
/**
* Removes {@code amount} of the item with the specified {@code id}. If the item is stackable, it will remove it
* from the stack. If not, it'll remove {@code amount} items.
*
* @param id The id.
* @param amount The amount.
* @return The amount that was removed.
*/
public int remove(int id, int amount) {
ItemDefinition def = ItemDefinition.lookup(id);
boolean stackable = isStackable(def);
if (stackable) {
int slot = slotOf(id);
if (slot != -1) {
Item item = items[slot];
if (amount >= item.getAmount()) {
set(slot, null);
return item.getAmount();
}
set(slot, new Item(item.getId(), item.getAmount() - amount));
return amount;
}
return 0;
}
int removed = 0;
stopFiringEvents();
try {
for (int slot = 0; slot < capacity; slot++) {
Item item = items[slot];
if (item != null && item.getId() == id) {
set(slot, null);
if (++removed >= amount) {
break;
}
}
}
} finally {
startFiringEvents();
if (removed > 0) {
notifyItemsUpdated();
}
}
return removed;
}
use of org.apollo.game.model.Item in project apollo by apollo-rsps.
the class Inventory method removeSlot.
/**
* Removes {@code amount} of the item at the specified {@code slot}. If the item is not stacked, it will only remove
* the single item at the slot (meaning it will ignore any amount higher than 1). This means that this method will
* under no circumstances make any changes to other slots.
*
* @param slot The slot.
* @param amount The amount to remove.
* @return The amount that was removed (0 if nothing was removed).
*/
public int removeSlot(int slot, int amount) {
if (amount != 0) {
Item item = items[slot];
if (item != null) {
int itemAmount = item.getAmount();
int removed = Math.min(amount, itemAmount);
int remainder = itemAmount - removed;
set(slot, remainder > 0 ? new Item(item.getId(), remainder) : null);
return removed;
}
}
return 0;
}
use of org.apollo.game.model.Item in project apollo by apollo-rsps.
the class BankUtils method withdraw.
/**
* Withdraws an item from a player's bank.
*
* @param player The player.
* @param slot The slot.
* @param id The id.
* @param amount The amount.
* @return {@code false} if the chain should be broken.
*/
public static boolean withdraw(Player player, int slot, int id, int amount) {
if (amount == 0) {
return true;
}
Inventory inventory = player.getInventory();
Inventory bank = player.getBank();
Item item = bank.get(slot);
if (amount >= item.getAmount()) {
amount = item.getAmount();
}
int newId = player.isWithdrawingNotes() ? ItemDefinition.itemToNote(item.getId()) : item.getId();
if (inventory.freeSlots() == 0 && !(inventory.contains(newId) && ItemDefinition.lookup(newId).isStackable())) {
inventory.forceCapacityExceeded();
return true;
}
int remaining = inventory.add(newId, amount);
bank.stopFiringEvents();
try {
bank.remove(item.getId(), amount - remaining);
bank.shift();
} finally {
bank.startFiringEvents();
}
bank.forceRefresh();
return true;
}
use of org.apollo.game.model.Item in project apollo by apollo-rsps.
the class UpdateItemsMessageEncoder method encode.
@Override
public GamePacket encode(UpdateItemsMessage message) {
GamePacketBuilder builder = new GamePacketBuilder(206, PacketType.VARIABLE_SHORT);
Item[] items = message.getItems();
builder.put(DataType.SHORT, message.getInterfaceId());
builder.put(DataType.SHORT, items.length);
for (Item item : items) {
int id = item == null ? -1 : item.getId();
int amount = item == null ? 0 : item.getAmount();
builder.put(DataType.SHORT, DataOrder.LITTLE, DataTransformation.ADD, id + 1);
if (amount > 254) {
builder.put(DataType.BYTE, DataTransformation.NEGATE, 255);
builder.put(DataType.INT, DataOrder.LITTLE, amount);
} else {
builder.put(DataType.BYTE, DataTransformation.NEGATE, amount);
}
}
return builder.toGamePacket();
}
Aggregations