Search in sources :

Example 11 with Inventory

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;
}
Also used : Item(org.apollo.game.model.Item) Inventory(org.apollo.game.model.inv.Inventory)

Example 12 with Inventory

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();
    }
}
Also used : Item(org.apollo.game.model.Item) Inventory(org.apollo.game.model.inv.Inventory)

Example 13 with Inventory

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;
    }
}
Also used : Item(org.apollo.game.model.Item) Position(org.apollo.game.model.Position) GameObject(org.apollo.game.model.entity.obj.GameObject) Region(org.apollo.game.model.area.Region) Inventory(org.apollo.game.model.inv.Inventory)

Example 14 with Inventory

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);
    }
}
Also used : Inventory(org.apollo.game.model.inv.Inventory)

Aggregations

Inventory (org.apollo.game.model.inv.Inventory)14 Item (org.apollo.game.model.Item)11 Position (org.apollo.game.model.Position)4 Region (org.apollo.game.model.area.Region)4 Player (org.apollo.game.model.entity.Player)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 HashSet (java.util.HashSet)3 EquipmentDefinition (org.apollo.cache.def.EquipmentDefinition)3 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 World (org.apollo.game.model.World)3 RegionRepository (org.apollo.game.model.area.RegionRepository)3 Entity (org.apollo.game.model.entity.Entity)3 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)3 Appearance (org.apollo.game.model.Appearance)2 GamePacketBuilder (org.apollo.net.codec.game.GamePacketBuilder)2 ItemOnItemMessage (org.apollo.game.message.impl.ItemOnItemMessage)1 GameObject (org.apollo.game.model.entity.obj.GameObject)1