Search in sources :

Example 6 with Inventory

use of org.apollo.game.model.inv.Inventory 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 7 with Inventory

use of org.apollo.game.model.inv.Inventory 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 8 with Inventory

use of org.apollo.game.model.inv.Inventory 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 9 with Inventory

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

the class ItemOnObjectVerificationHandlerTests method terminateIfInvalidItem.

@Test
public void terminateIfInvalidItem() throws Exception {
    Position playerPosition = new Position(3200, 3200);
    Position objectPosition = new Position(3200, 3216);
    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);
    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 valid item given!", itemOnObjectMessage.terminated());
}
Also used : Entity(org.apollo.game.model.entity.Entity) Player(org.apollo.game.model.entity.Player) Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) ItemOnObjectMessage(org.apollo.game.message.impl.ItemOnObjectMessage) Region(org.apollo.game.model.area.Region) StaticGameObject(org.apollo.game.model.entity.obj.StaticGameObject) World(org.apollo.game.model.World) Inventory(org.apollo.game.model.inv.Inventory) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with Inventory

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

the class ItemOnObjectVerificationHandlerTests method terminateIfInvalidSlot.

@Test
public void terminateIfInvalidSlot() 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);
    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, 30, 1, objectPosition.getX(), objectPosition.getY());
    ItemOnObjectVerificationHandler itemOnObjectVerificationHandler = new ItemOnObjectVerificationHandler(world);
    itemOnObjectVerificationHandler.handle(player, itemOnObjectMessage);
    assertTrue("ObjectVerificationHandler: message not terminated when no valid slot given!", itemOnObjectMessage.terminated());
}
Also used : Entity(org.apollo.game.model.entity.Entity) Player(org.apollo.game.model.entity.Player) Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) ItemOnObjectMessage(org.apollo.game.message.impl.ItemOnObjectMessage) Region(org.apollo.game.model.area.Region) StaticGameObject(org.apollo.game.model.entity.obj.StaticGameObject) World(org.apollo.game.model.World) Inventory(org.apollo.game.model.inv.Inventory) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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