Search in sources :

Example 11 with Item

use of org.apollo.game.model.Item in project apollo by apollo-rsps.

the class ItemOnItemVerificationHandlerTests method terminateWithNoTargetItem.

@Test
public void terminateWithNoTargetItem() throws Exception {
    Player player = mock(Player.class);
    Inventory inventory = new Inventory(28);
    inventory.set(1, new Item(4151, 1));
    when(player.getInventory()).thenReturn(inventory);
    ItemOnItemMessage itemOnItemMessage = new ItemOnItemMessage(BankConstants.SIDEBAR_INVENTORY_ID, 4151, 1, BankConstants.SIDEBAR_INVENTORY_ID, 4152, 2);
    itemOnItemVerificationHandler.handle(player, itemOnItemMessage);
    assertTrue("ItemOnItemVerificationHandler: failed terminating message with invalid target item", itemOnItemMessage.terminated());
}
Also used : Item(org.apollo.game.model.Item) Player(org.apollo.game.model.entity.Player) ItemOnItemMessage(org.apollo.game.message.impl.ItemOnItemMessage) Inventory(org.apollo.game.model.inv.Inventory) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 12 with Item

use of org.apollo.game.model.Item in project apollo by apollo-rsps.

the class BankUtils method deposit.

/**
 * Deposits an item into the 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 deposit(Player player, int slot, int id, int amount) {
    if (amount == 0) {
        return true;
    }
    Inventory inventory = player.getInventory();
    Inventory bank = player.getBank();
    Item item = inventory.get(slot);
    int newId = ItemDefinition.noteToItem(item.getId());
    if (bank.freeSlots() == 0 && !bank.contains(item.getId())) {
        bank.forceCapacityExceeded();
        return true;
    }
    int removed;
    if (amount > 1) {
        inventory.stopFiringEvents();
    }
    try {
        removed = inventory.remove(item.getId(), amount);
    } finally {
        if (amount > 1) {
            inventory.startFiringEvents();
        }
    }
    if (amount > 1) {
        inventory.forceRefresh();
    }
    bank.add(newId, removed);
    return true;
}
Also used : Item(org.apollo.game.model.Item) Inventory(org.apollo.game.model.inv.Inventory)

Example 13 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(53, 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();
        if (amount > 254) {
            builder.put(DataType.BYTE, 255);
            builder.put(DataType.INT, DataOrder.INVERSED_MIDDLE, amount);
        } else {
            builder.put(DataType.BYTE, amount);
        }
        builder.put(DataType.SHORT, DataOrder.LITTLE, DataTransformation.ADD, id + 1);
    }
    return builder.toGamePacket();
}
Also used : Item(org.apollo.game.model.Item) GamePacketBuilder(org.apollo.net.codec.game.GamePacketBuilder)

Example 14 with Item

use of org.apollo.game.model.Item in project apollo by apollo-rsps.

the class ItemOnObjectVerificationHandlerTests method terminateIfObjectOutOfRange.

@Test
public void terminateIfObjectOutOfRange() throws Exception {
    Position playerPosition = new Position(3200, 3200);
    Position objectPosition = new Position(3200, 3200);
    World world = mock(World.class);
    Region region = mock(Region.class);
    RegionRepository regionRepository = mock(RegionRepository.class);
    Player player = mock(Player.class);
    Set<Entity> entitySet = new HashSet<>();
    entitySet.add(new StaticGameObject(world, 4151, objectPosition, 0, 0));
    Inventory inventory = new Inventory(28);
    inventory.set(1, new Item(4151, 1));
    when(player.getInventory()).thenReturn(inventory);
    when(world.getRegionRepository()).thenReturn(regionRepository);
    when(regionRepository.fromPosition(objectPosition)).thenReturn(region);
    when(player.getPosition()).thenReturn(playerPosition);
    when(region.getEntities(objectPosition, EntityType.STATIC_OBJECT, EntityType.DYNAMIC_OBJECT)).thenReturn(entitySet);
    ItemOnObjectMessage itemOnObjectMessage = new ItemOnObjectMessage(SynchronizationInventoryListener.INVENTORY_ID, 4151, 1, 1, objectPosition.getX(), objectPosition.getY());
    ItemOnObjectVerificationHandler itemOnObjectVerificationHandler = new ItemOnObjectVerificationHandler(world);
    itemOnObjectVerificationHandler.handle(player, itemOnObjectMessage);
    assertTrue("ObjectVerificationHandler: message not terminated when object out of range!", itemOnObjectMessage.terminated());
}
Also used : Entity(org.apollo.game.model.entity.Entity) Player(org.apollo.game.model.entity.Player) Position(org.apollo.game.model.Position) ItemOnObjectMessage(org.apollo.game.message.impl.ItemOnObjectMessage) StaticGameObject(org.apollo.game.model.entity.obj.StaticGameObject) World(org.apollo.game.model.World) Item(org.apollo.game.model.Item) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region) Inventory(org.apollo.game.model.inv.Inventory) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with Item

use of org.apollo.game.model.Item in project apollo by apollo-rsps.

the class Inventory method swap.

/**
 * Swaps the two items at the specified slots.
 *
 * @param insert If the swap should be done in insertion mode.
 * @param oldSlot The old slot.
 * @param newSlot The new slot.
 */
public void swap(boolean insert, int oldSlot, int newSlot) {
    checkBounds(oldSlot, newSlot);
    if (insert) {
        if (newSlot > oldSlot) {
            for (int slot = oldSlot; slot < newSlot; slot++) {
                swap(slot, slot + 1);
            }
        } else if (oldSlot > newSlot) {
            for (int slot = oldSlot; slot > newSlot; slot--) {
                swap(slot, slot - 1);
            }
        }
        forceRefresh();
    } else {
        Item item = items[oldSlot];
        items[oldSlot] = items[newSlot];
        items[newSlot] = item;
        notifyItemUpdated(oldSlot);
        notifyItemUpdated(newSlot);
    }
}
Also used : Item(org.apollo.game.model.Item)

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