use of org.apollo.game.model.area.RegionRepository in project apollo by apollo-rsps.
the class PrePlayerSynchronizationTask method run.
@Override
public void run() {
Position old = player.getPosition();
player.getWalkingQueue().pulse();
boolean local = true;
if (player.isTeleporting()) {
player.resetViewingDistance();
local = false;
}
Position position = player.getPosition();
if (!player.hasLastKnownRegion() || isRegionUpdateRequired()) {
player.setRegionChanged(true);
local = false;
player.setLastKnownRegion(position);
player.send(new RegionChangeMessage(position));
}
RegionRepository repository = player.getWorld().getRegionRepository();
Set<RegionCoordinates> oldViewable = repository.fromPosition(old).getSurrounding();
Set<RegionCoordinates> newViewable = repository.fromPosition(position).getSurrounding();
Set<RegionCoordinates> differences = new HashSet<>(newViewable);
differences.retainAll(oldViewable);
Set<RegionCoordinates> full = new HashSet<>(newViewable);
if (local) {
full.removeAll(oldViewable);
}
sendUpdates(player.getLastKnownRegion(), differences, full);
}
use of org.apollo.game.model.area.RegionRepository 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());
}
use of org.apollo.game.model.area.RegionRepository 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);
}
use of org.apollo.game.model.area.RegionRepository in project apollo by apollo-rsps.
the class Mob method setPosition.
/**
* Sets the {@link Position} of this mob.
* <p>
* This method may be intercepted using a {@link MobPositionUpdateEvent}, which can be terminated like any
* other. Plugins that intercept this Event <strong>must</strong> be cautious, because movement will not be
* possible (even through mechanisms such as teleporting) if the Event is terminated.
*
* @param position The Position.
*/
public final void setPosition(Position position) {
if (!position.equals(this.position) && world.submit(new MobPositionUpdateEvent(this, position))) {
Position old = this.position;
RegionRepository repository = world.getRegionRepository();
Region current = repository.fromPosition(old), next = repository.fromPosition(position);
current.removeEntity(this);
// addEntity relies on the position being updated, so do that first.
this.position = position;
next.addEntity(this);
}
}
use of org.apollo.game.model.area.RegionRepository 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());
}
Aggregations