mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 08:39:11 +00:00
Improve sector-related code.
This commit is contained in:
@@ -188,7 +188,7 @@ public final class World {
|
||||
* @return The player.
|
||||
*/
|
||||
public Player getPlayer(String username) {
|
||||
return players.get(NameUtil.encodeBase37(username.toLowerCase()));
|
||||
return players.get(NameUtil.encodeBase37(username));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -276,23 +276,19 @@ public final class World {
|
||||
* @return {@code true} if the player is online, otherwise {@code false}.
|
||||
*/
|
||||
public boolean isPlayerOnline(String username) {
|
||||
return players.get(NameUtil.encodeBase37(username.toLowerCase())) != null;
|
||||
return players.get(NameUtil.encodeBase37(username)) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds entities to sectors in the {@link SectorRepository}.
|
||||
*
|
||||
* @param entities The entities.
|
||||
* @return {@code true} if all entities were added successfully, otherwise {@code false}.
|
||||
*/
|
||||
private boolean placeEntities(Entity... entities) {
|
||||
boolean success = true;
|
||||
|
||||
private void placeEntities(Entity... entities) {
|
||||
for (Entity entity : entities) {
|
||||
Sector sector = sectorRepository.fromPosition(entity.getPosition());
|
||||
success &= sector.addEntity(entity);
|
||||
sector.addEntity(entity);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -312,7 +308,7 @@ public final class World {
|
||||
boolean success = npcRepository.add(npc);
|
||||
|
||||
if (success) {
|
||||
Sector sector = sectorRepository.get(SectorCoordinates.fromPosition(npc.getPosition()));
|
||||
Sector sector = sectorRepository.fromPosition(npc.getPosition());
|
||||
sector.addEntity(npc);
|
||||
} else {
|
||||
logger.warning("Failed to register npc, repository capacity reached: [count=" + npcRepository.size() + "]");
|
||||
@@ -333,14 +329,10 @@ public final class World {
|
||||
|
||||
boolean success = playerRepository.add(player);
|
||||
if (success) {
|
||||
if (players.put(NameUtil.encodeBase37(player.getUsername().toLowerCase()), player) != null) {
|
||||
logger.info("Error adding the player to the username map - someone with that name already exists.");
|
||||
return RegistrationStatus.ALREADY_ONLINE;
|
||||
}
|
||||
logger.info("Registered player: " + player + " [count=" + playerRepository.size() + "]");
|
||||
|
||||
Sector sector = sectorRepository.get(SectorCoordinates.fromPosition(player.getPosition()));
|
||||
sector.addEntity(player);
|
||||
|
||||
logger.info("Registered player: " + player + " [count=" + playerRepository.size() + "]");
|
||||
return RegistrationStatus.OK;
|
||||
}
|
||||
|
||||
@@ -364,8 +356,11 @@ public final class World {
|
||||
*/
|
||||
public void unregister(final Npc npc) {
|
||||
if (npcRepository.remove(npc)) {
|
||||
Sector sector = sectorRepository.get(SectorCoordinates.fromPosition(npc.getPosition()));
|
||||
sector.removeEntity(npc);
|
||||
Sector sector = sectorRepository.fromPosition(npc.getPosition());
|
||||
|
||||
if (!sector.removeEntity(npc)) {
|
||||
logger.warning("Could not remove npc from their sector.");
|
||||
}
|
||||
} else {
|
||||
logger.warning("Could not find npc " + npc + " to unregister!");
|
||||
}
|
||||
@@ -377,12 +372,15 @@ public final class World {
|
||||
* @param player The player.
|
||||
*/
|
||||
public void unregister(final Player player) {
|
||||
if (playerRepository.remove(player)
|
||||
& players.remove(NameUtil.encodeBase37(player.getUsername().toLowerCase())) == player) {
|
||||
if (playerRepository.remove(player)) {
|
||||
players.remove(NameUtil.encodeBase37(player.getUsername()));
|
||||
logger.info("Unregistered player: " + player + " [count=" + playerRepository.size() + "]");
|
||||
|
||||
Sector sector = sectorRepository.get(SectorCoordinates.fromPosition(player.getPosition()));
|
||||
sector.removeEntity(player);
|
||||
Sector sector = sectorRepository.fromPosition(player.getPosition());
|
||||
if (!sector.removeEntity(player)) {
|
||||
logger.warning("Could not remove player from their sector.");
|
||||
}
|
||||
|
||||
logoutDispatcher.dispatch(player);
|
||||
} else {
|
||||
logger.warning("Could not find player " + player + " to unregister!");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.apollo.game.model.area;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -58,25 +59,20 @@ public final class Sector {
|
||||
|
||||
/**
|
||||
* Adds a {@link Entity} from to sector. Note that this does not spawn the entity, or do any other action other than
|
||||
* register it to this sector. The return value of this method should also be monitored - if this method fails (i.e.
|
||||
* returns {@code false}), the entity should <strong>not</strong> be registered in the world.
|
||||
* register it to this sector.
|
||||
*
|
||||
* @param entity The entity.
|
||||
* @return {@code true} if the entity was added successfully, otherwise {@code false}.
|
||||
*/
|
||||
public boolean addEntity(Entity entity) {
|
||||
public void addEntity(Entity entity) {
|
||||
Position position = entity.getPosition();
|
||||
List<Entity> entities = this.entities.get(position);
|
||||
if (entities == null) {
|
||||
entities = new ArrayList<>();
|
||||
}
|
||||
|
||||
if (entities.add(entity)) {
|
||||
this.entities.put(position, entities);
|
||||
notifyListeners(entity);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
entities.add(entity);
|
||||
this.entities.put(position, entities);
|
||||
notifyListeners(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,12 +85,7 @@ public final class Sector {
|
||||
Position position = entity.getPosition();
|
||||
List<Entity> entities = this.entities.get(position);
|
||||
|
||||
if (entities == null) {
|
||||
this.entities.put(position, new ArrayList<>());
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.entities.get(position).contains(entity);
|
||||
return entities != null && entities.contains(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,8 +106,8 @@ public final class Sector {
|
||||
public List<Entity> getEntities(Position position) {
|
||||
List<Entity> entities = this.entities.get(position);
|
||||
if (entities == null) {
|
||||
entities = new ArrayList<>();
|
||||
this.entities.put(position, entities);
|
||||
this.entities.put(position, new ArrayList<>());
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return new ArrayList<>(entities);
|
||||
@@ -133,11 +124,11 @@ public final class Sector {
|
||||
public <T extends Entity> List<T> getEntities(Position position, EntityType type) {
|
||||
List<Entity> entities = this.entities.get(position);
|
||||
if (entities == null) {
|
||||
entities = new ArrayList<>();
|
||||
this.entities.put(position, entities);
|
||||
this.entities.put(position, new ArrayList<>());
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return (List<T>) entities.stream().filter((e) -> e.getEntityType() == type).collect(Collectors.toList());
|
||||
return (List<T>) entities.stream().filter(e -> e.getEntityType() == type).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +137,7 @@ public final class Sector {
|
||||
* @param entity The entity that was updated.
|
||||
*/
|
||||
public void notifyListeners(Entity entity) {
|
||||
listeners.forEach((l) -> l.execute(this, entity));
|
||||
listeners.forEach(l -> l.execute(this, entity));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,7 +13,7 @@ public interface SectorListener {
|
||||
/**
|
||||
* Executes the action for this listener.
|
||||
*
|
||||
* @param The sector that was updated.
|
||||
* @param sector The sector that was updated.
|
||||
* @param entity The affected entity.
|
||||
*/
|
||||
public abstract void execute(Sector sector, Entity entity);
|
||||
|
||||
@@ -8,27 +8,27 @@ import java.util.Map;
|
||||
import org.apollo.game.model.Position;
|
||||
|
||||
/**
|
||||
* A repository of sectors, backed by a {@link HashMap} of {@link SectorCoordinates} that correspond to their
|
||||
* appropriate {@link Sector}s.
|
||||
* A repository of {@link Sector Sectors}, backed by a {@link HashMap} of {@link SectorCoordinates} that correspond to
|
||||
* their appropriate sectors.
|
||||
*
|
||||
* @author Major
|
||||
*/
|
||||
public final class SectorRepository {
|
||||
|
||||
/**
|
||||
* Indicates whether sectors can be removed from this repository.
|
||||
* Whether or not sectors can be removed from this repository.
|
||||
*/
|
||||
private final boolean permitRemoval;
|
||||
|
||||
/**
|
||||
* A {@link Map} of {@link SectorCoordinates} that correspond to the appropriate {@link Sector}s.
|
||||
* The map of sector coordinates that correspond to the appropriate sectors.
|
||||
*/
|
||||
private final Map<SectorCoordinates, Sector> sectors = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Creates a new sector repository.
|
||||
*
|
||||
* @param permitRemoval If removal (of {@link Sector}s) from this repository should be permitted.
|
||||
* @param permitRemoval If removal (of {@link Sector Sectors}) from this repository should be permitted.
|
||||
*/
|
||||
public SectorRepository(boolean permitRemoval) {
|
||||
this.permitRemoval = permitRemoval;
|
||||
@@ -100,8 +100,8 @@ public final class SectorRepository {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link List} of {@link Sector}s. This is a shallow copy (i.e. modifying the list will not change the
|
||||
* repository, but modifying the sectors in the list will change the sectors).
|
||||
* Gets the {@link List} of {@link Sector Sectors}. This is a shallow copy (i.e. modifying the list will not change
|
||||
* the repository, but modifying the sectors in the list will change the sectors in this repository).
|
||||
*
|
||||
* @return The list.
|
||||
*/
|
||||
|
||||
@@ -69,7 +69,7 @@ public abstract class Entity {
|
||||
*
|
||||
* @return The position.
|
||||
*/
|
||||
public Position getPosition() {
|
||||
public final Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user