Search in sources :

Example 16 with Item

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

Example 17 with Item

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

Example 18 with Item

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

Example 19 with Item

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

Example 20 with Item

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

Aggregations

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