Search in sources :

Example 1 with Entity

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

the class ObjectActionVerificationHandlerTests method terminateIfNoObject.

@Test
public void terminateIfNoObject() throws Exception {
    Position playerPosition = new Position(3200, 3200);
    Position objectPosition = new Position(3200, 3201);
    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<>();
    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);
    ObjectActionMessage objectActionMessage = new ObjectActionMessage(1, 4151, objectPosition);
    ObjectActionVerificationHandler objectActionVerificationHandler = new ObjectActionVerificationHandler(world);
    objectActionVerificationHandler.handle(player, objectActionMessage);
    assertTrue("ObjectVerificationHandler: message not terminated when no object exists!", objectActionMessage.terminated());
}
Also used : Entity(org.apollo.game.model.entity.Entity) Player(org.apollo.game.model.entity.Player) ObjectActionMessage(org.apollo.game.message.impl.ObjectActionMessage) Position(org.apollo.game.model.Position) RegionRepository(org.apollo.game.model.area.RegionRepository) Region(org.apollo.game.model.area.Region) World(org.apollo.game.model.World) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with Entity

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

the class Region method addEntity.

/**
 * Adds a {@link Entity} to the Region. Note that this does not spawn the Entity, or do any other action other than
 * register it to this Region.
 *
 * @param entity The Entity.
 * @param notify Whether or not the {@link RegionListener}s for this Region should be notified.
 * @throws IllegalArgumentException If the Entity does not belong in this Region.
 */
public void addEntity(Entity entity, boolean notify) {
    EntityType type = entity.getEntityType();
    Position position = entity.getPosition();
    checkPosition(position);
    if (!type.isTransient()) {
        Set<Entity> local = entities.computeIfAbsent(position, key -> new HashSet<>(DEFAULT_LIST_SIZE));
        local.add(entity);
    }
    if (notify) {
        notifyListeners(entity, EntityUpdateType.ADD);
    }
}
Also used : EntityType(org.apollo.game.model.entity.EntityType) Entity(org.apollo.game.model.entity.Entity) GroupableEntity(org.apollo.game.model.area.update.GroupableEntity) Position(org.apollo.game.model.Position)

Example 3 with Entity

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

the class Region method removeEntity.

/**
 * Removes an {@link Entity} from this Region.
 *
 * @param entity The Entity.
 * @throws IllegalArgumentException If the Entity does not belong in this Region, or if it was never added.
 */
public void removeEntity(Entity entity) {
    EntityType type = entity.getEntityType();
    if (type.isTransient()) {
        throw new IllegalArgumentException("Tried to remove a transient Entity (" + entity + ") from " + "(" + this + ").");
    }
    Position position = entity.getPosition();
    checkPosition(position);
    Set<Entity> local = entities.get(position);
    if (local == null || !local.remove(entity)) {
        throw new IllegalArgumentException("Entity (" + entity + ") belongs in (" + this + ") but does not exist.");
    }
    notifyListeners(entity, EntityUpdateType.REMOVE);
}
Also used : EntityType(org.apollo.game.model.entity.EntityType) Entity(org.apollo.game.model.entity.Entity) GroupableEntity(org.apollo.game.model.area.update.GroupableEntity) Position(org.apollo.game.model.Position)

Example 4 with Entity

use of org.apollo.game.model.entity.Entity 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 5 with Entity

use of org.apollo.game.model.entity.Entity 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)

Aggregations

Position (org.apollo.game.model.Position)8 Entity (org.apollo.game.model.entity.Entity)8 HashSet (java.util.HashSet)5 World (org.apollo.game.model.World)5 Region (org.apollo.game.model.area.Region)5 RegionRepository (org.apollo.game.model.area.RegionRepository)5 Player (org.apollo.game.model.entity.Player)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)4 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 GroupableEntity (org.apollo.game.model.area.update.GroupableEntity)3 Inventory (org.apollo.game.model.inv.Inventory)3 ObjectActionMessage (org.apollo.game.message.impl.ObjectActionMessage)2 EntityType (org.apollo.game.model.entity.EntityType)2 Item (org.apollo.game.model.Item)1