use of org.apollo.game.model.inv.Inventory 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.inv.Inventory in project apollo by apollo-rsps.
the class ItemVerificationHandler method handle.
@Override
public void handle(Player player, InventoryItemMessage message) {
InventorySupplier supplier = inventories.get(message.getInterfaceId());
if (supplier == null) {
message.terminate();
return;
}
Inventory inventory = supplier.getInventory(player);
int slot = message.getSlot();
if (inventory == null || slot < 0 || slot >= inventory.capacity()) {
message.terminate();
return;
}
Item item = inventory.get(slot);
if (item == null || item.getId() != message.getId()) {
message.terminate();
}
}
use of org.apollo.game.model.inv.Inventory in project apollo by apollo-rsps.
the class ItemOnObjectVerificationHandler method handle.
@Override
public void handle(Player player, ItemOnObjectMessage message) {
if (message.getInterfaceId() != SynchronizationInventoryListener.INVENTORY_ID && message.getInterfaceId() != BankConstants.SIDEBAR_INVENTORY_ID) {
message.terminate();
return;
}
Inventory inventory = player.getInventory();
int slot = message.getSlot();
if (slot < 0 || slot >= inventory.capacity()) {
message.terminate();
return;
}
Item item = inventory.get(slot);
if (item == null || item.getId() != message.getId()) {
message.terminate();
return;
}
int objectId = message.getObjectId();
if (objectId < 0 || objectId >= ObjectDefinition.count()) {
message.terminate();
return;
}
Position position = message.getPosition();
Region region = world.getRegionRepository().fromPosition(position);
Set<GameObject> objects = region.getEntities(position, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT);
if (!player.getPosition().isWithinDistance(position, 15) || !ObjectActionVerificationHandler.containsObject(objectId, objects)) {
message.terminate();
return;
}
}
use of org.apollo.game.model.inv.Inventory in project apollo by apollo-rsps.
the class SwitchItemMessageHandler method handle.
@Override
public void handle(Player player, SwitchItemMessage message) {
Inventory inventory;
boolean insertPermitted = false;
switch(message.getInterfaceId()) {
case SynchronizationInventoryListener.INVENTORY_ID:
case BankConstants.SIDEBAR_INVENTORY_ID:
inventory = player.getInventory();
break;
case SynchronizationInventoryListener.EQUIPMENT_ID:
inventory = player.getEquipment();
break;
case BankConstants.BANK_INVENTORY_ID:
inventory = player.getBank();
insertPermitted = true;
break;
default:
// not a known inventory, ignore
return;
}
int old = message.getOldSlot(), next = message.getNewSlot();
if (old >= 0 && next >= 0 && old < inventory.capacity() && next < inventory.capacity()) {
// events must be fired for it to work if a sidebar inventory overlay is used
inventory.swap(insertPermitted && message.isInserting(), old, next);
}
}
Aggregations