Search in sources :

Example 11 with Region

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

Example 12 with Region

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

the class GameService method finalizePlayerRegistration.

/**
 * Finalizes the registration of a player.
 *
 * @param player The player.
 */
public synchronized void finalizePlayerRegistration(Player player) {
    world.register(player);
    Region region = world.getRegionRepository().fromPosition(player.getPosition());
    region.addEntity(player);
    if (!player.getSession().isReconnecting()) {
        player.sendInitialMessages();
    }
}
Also used : Region(org.apollo.game.model.area.Region)

Example 13 with Region

use of org.apollo.game.model.area.Region 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 Region

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

the class World method registerNpcs.

/**
 * Registers all of the {@link Npc}s in the {@link #queuedNpcs queue}.
 */
private void registerNpcs() {
    while (!queuedNpcs.isEmpty()) {
        Npc npc = queuedNpcs.poll();
        boolean success = npcRepository.add(npc);
        if (success) {
            Region region = regions.fromPosition(npc.getPosition());
            region.addEntity(npc);
            if (npc.hasBoundaries()) {
                npcMovement.addNpc(npc);
            }
        } else {
            logger.warning("Failed to register npc (capacity reached): [count=" + npcRepository.size() + "]");
        }
    }
}
Also used : Npc(org.apollo.game.model.entity.Npc) Region(org.apollo.game.model.area.Region)

Example 15 with Region

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

the class World method unregister.

/**
 * Unregisters the specified player.
 *
 * @param player The player.
 */
public void unregister(final Player player) {
    Preconditions.checkNotNull(player, "Player may not be null.");
    players.remove(NameUtil.encodeBase37(player.getUsername()));
    Region region = regions.fromPosition(player.getPosition());
    region.removeEntity(player);
    playerRepository.remove(player);
    logger.info("Unregistered player: " + player + " [count=" + playerRepository.size() + "]");
}
Also used : Region(org.apollo.game.model.area.Region)

Aggregations

Region (org.apollo.game.model.area.Region)19 Position (org.apollo.game.model.Position)14 RegionRepository (org.apollo.game.model.area.RegionRepository)9 World (org.apollo.game.model.World)6 Player (org.apollo.game.model.entity.Player)6 HashSet (java.util.HashSet)5 Entity (org.apollo.game.model.entity.Entity)5 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Inventory (org.apollo.game.model.inv.Inventory)4 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 Npc (org.apollo.game.model.entity.Npc)3 GameObject (org.apollo.game.model.entity.obj.GameObject)3 ArrayList (java.util.ArrayList)2 ObjectActionMessage (org.apollo.game.message.impl.ObjectActionMessage)2 Direction (org.apollo.game.model.Direction)2 Item (org.apollo.game.model.Item)2 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)2 MovementSegment (org.apollo.game.sync.seg.MovementSegment)2