Search in sources :

Example 6 with Player

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

the class PlayerSynchronizationTask method run.

@Override
public void run() {
    Position lastKnownRegion = player.getLastKnownRegion();
    boolean regionChanged = player.hasRegionChanged();
    int[] appearanceTickets = player.getAppearanceTickets();
    SynchronizationBlockSet blockSet = player.getBlockSet();
    if (blockSet.contains(ChatBlock.class)) {
        blockSet = blockSet.clone();
        blockSet.remove(ChatBlock.class);
    }
    Position position = player.getPosition();
    SynchronizationSegment segment = (player.isTeleporting() || player.hasRegionChanged()) ? new TeleportSegment(blockSet, position) : new MovementSegment(blockSet, player.getDirections());
    List<Player> localPlayers = player.getLocalPlayerList();
    int oldCount = localPlayers.size();
    List<SynchronizationSegment> segments = new ArrayList<>();
    int distance = player.getViewingDistance();
    for (Iterator<Player> iterator = localPlayers.iterator(); iterator.hasNext(); ) {
        Player other = iterator.next();
        if (removeable(position, distance, other)) {
            iterator.remove();
            segments.add(new RemoveMobSegment());
        } else {
            segments.add(new MovementSegment(other.getBlockSet(), other.getDirections()));
        }
    }
    int added = 0, count = localPlayers.size();
    RegionRepository repository = player.getWorld().getRegionRepository();
    Region current = repository.fromPosition(position);
    Set<RegionCoordinates> regions = current.getSurrounding();
    regions.add(current.getCoordinates());
    Stream<Player> players = regions.stream().map(repository::get).flatMap(region -> region.getEntities(EntityType.PLAYER));
    Iterator<Player> iterator = players.iterator();
    while (iterator.hasNext()) {
        if (count >= MAXIMUM_LOCAL_PLAYERS) {
            player.flagExcessivePlayers();
            break;
        } else if (added >= NEW_PLAYERS_PER_CYCLE) {
            break;
        }
        Player other = iterator.next();
        Position local = other.getPosition();
        if (other != player && local.isWithinDistance(position, distance) && !localPlayers.contains(other)) {
            localPlayers.add(other);
            count++;
            added++;
            blockSet = other.getBlockSet();
            int index = other.getIndex();
            if (!blockSet.contains(AppearanceBlock.class) && !hasCachedAppearance(appearanceTickets, index - 1, other.getAppearanceTicket())) {
                blockSet = blockSet.clone();
                blockSet.add(SynchronizationBlock.createAppearanceBlock(other));
            }
            segments.add(new AddPlayerSegment(blockSet, index, local));
        }
    }
    PlayerSynchronizationMessage message = new PlayerSynchronizationMessage(lastKnownRegion, position, regionChanged, segment, oldCount, segments);
    player.send(message);
}
Also used : Player(org.apollo.game.model.entity.Player) Position(org.apollo.game.model.Position) ArrayList(java.util.ArrayList) TeleportSegment(org.apollo.game.sync.seg.TeleportSegment) SynchronizationBlockSet(org.apollo.game.sync.block.SynchronizationBlockSet) MovementSegment(org.apollo.game.sync.seg.MovementSegment) RemoveMobSegment(org.apollo.game.sync.seg.RemoveMobSegment) RegionCoordinates(org.apollo.game.model.area.RegionCoordinates) RegionRepository(org.apollo.game.model.area.RegionRepository) AddPlayerSegment(org.apollo.game.sync.seg.AddPlayerSegment) Region(org.apollo.game.model.area.Region) SynchronizationSegment(org.apollo.game.sync.seg.SynchronizationSegment) PlayerSynchronizationMessage(org.apollo.game.message.impl.PlayerSynchronizationMessage)

Example 7 with Player

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

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

the class PublicChatMessageHandlerTests method terminateIfMuted.

@Test
public void terminateIfMuted() throws Exception {
    Player player = PowerMockito.mock(Player.class);
    when(player.isMuted()).thenReturn(true);
    PublicChatMessage publicChatMessage = new PublicChatMessage("Test", "Test".getBytes(), 0, 0);
    handler.handle(player, publicChatMessage);
    assertTrue("PublicChatMessageHandler: player can send messages when muted", publicChatMessage.terminated());
}
Also used : Player(org.apollo.game.model.entity.Player) PublicChatMessage(org.apollo.game.message.impl.PublicChatMessage) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with Player

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

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

Player (org.apollo.game.model.entity.Player)17 Position (org.apollo.game.model.Position)7 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Region (org.apollo.game.model.area.Region)6 RegionRepository (org.apollo.game.model.area.RegionRepository)6 HashSet (java.util.HashSet)5 World (org.apollo.game.model.World)5 Entity (org.apollo.game.model.entity.Entity)5 StaticGameObject (org.apollo.game.model.entity.obj.StaticGameObject)4 Inventory (org.apollo.game.model.inv.Inventory)4 ItemOnObjectMessage (org.apollo.game.message.impl.ItemOnObjectMessage)3 RegionCoordinates (org.apollo.game.model.area.RegionCoordinates)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 ObjectActionMessage (org.apollo.game.message.impl.ObjectActionMessage)2 Item (org.apollo.game.model.Item)2 Npc (org.apollo.game.model.entity.Npc)2 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1