Improve sector-related code.

This commit is contained in:
Major-
2014-08-09 17:36:14 +01:00
parent c162705cd4
commit 467bcfe39f
5 changed files with 41 additions and 52 deletions
+19 -21
View File
@@ -188,7 +188,7 @@ public final class World {
* @return The player. * @return The player.
*/ */
public Player getPlayer(String username) { 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}. * @return {@code true} if the player is online, otherwise {@code false}.
*/ */
public boolean isPlayerOnline(String username) { 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}. * Adds entities to sectors in the {@link SectorRepository}.
* *
* @param entities The entities. * @param entities The entities.
* @return {@code true} if all entities were added successfully, otherwise {@code false}.
*/ */
private boolean placeEntities(Entity... entities) { private void placeEntities(Entity... entities) {
boolean success = true;
for (Entity entity : entities) { for (Entity entity : entities) {
Sector sector = sectorRepository.fromPosition(entity.getPosition()); 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); boolean success = npcRepository.add(npc);
if (success) { if (success) {
Sector sector = sectorRepository.get(SectorCoordinates.fromPosition(npc.getPosition())); Sector sector = sectorRepository.fromPosition(npc.getPosition());
sector.addEntity(npc); sector.addEntity(npc);
} else { } else {
logger.warning("Failed to register npc, repository capacity reached: [count=" + npcRepository.size() + "]"); 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); boolean success = playerRepository.add(player);
if (success) { 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 sector = sectorRepository.get(SectorCoordinates.fromPosition(player.getPosition()));
sector.addEntity(player); sector.addEntity(player);
logger.info("Registered player: " + player + " [count=" + playerRepository.size() + "]");
return RegistrationStatus.OK; return RegistrationStatus.OK;
} }
@@ -364,8 +356,11 @@ public final class World {
*/ */
public void unregister(final Npc npc) { public void unregister(final Npc npc) {
if (npcRepository.remove(npc)) { if (npcRepository.remove(npc)) {
Sector sector = sectorRepository.get(SectorCoordinates.fromPosition(npc.getPosition())); Sector sector = sectorRepository.fromPosition(npc.getPosition());
sector.removeEntity(npc);
if (!sector.removeEntity(npc)) {
logger.warning("Could not remove npc from their sector.");
}
} else { } else {
logger.warning("Could not find npc " + npc + " to unregister!"); logger.warning("Could not find npc " + npc + " to unregister!");
} }
@@ -377,12 +372,15 @@ public final class World {
* @param player The player. * @param player The player.
*/ */
public void unregister(final Player player) { public void unregister(final Player player) {
if (playerRepository.remove(player) if (playerRepository.remove(player)) {
& players.remove(NameUtil.encodeBase37(player.getUsername().toLowerCase())) == player) { players.remove(NameUtil.encodeBase37(player.getUsername()));
logger.info("Unregistered player: " + player + " [count=" + playerRepository.size() + "]"); logger.info("Unregistered player: " + player + " [count=" + playerRepository.size() + "]");
Sector sector = sectorRepository.get(SectorCoordinates.fromPosition(player.getPosition())); Sector sector = sectorRepository.fromPosition(player.getPosition());
sector.removeEntity(player); if (!sector.removeEntity(player)) {
logger.warning("Could not remove player from their sector.");
}
logoutDispatcher.dispatch(player); logoutDispatcher.dispatch(player);
} else { } else {
logger.warning("Could not find player " + player + " to unregister!"); logger.warning("Could not find player " + player + " to unregister!");
+13 -22
View File
@@ -1,6 +1,7 @@
package org.apollo.game.model.area; package org.apollo.game.model.area;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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 * 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. * register it to this sector.
* returns {@code false}), the entity should <strong>not</strong> be registered in the world.
* *
* @param entity The entity. * @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(); Position position = entity.getPosition();
List<Entity> entities = this.entities.get(position); List<Entity> entities = this.entities.get(position);
if (entities == null) { if (entities == null) {
entities = new ArrayList<>(); entities = new ArrayList<>();
} }
if (entities.add(entity)) { entities.add(entity);
this.entities.put(position, entities); this.entities.put(position, entities);
notifyListeners(entity); notifyListeners(entity);
return true;
}
return false;
} }
/** /**
@@ -89,12 +85,7 @@ public final class Sector {
Position position = entity.getPosition(); Position position = entity.getPosition();
List<Entity> entities = this.entities.get(position); List<Entity> entities = this.entities.get(position);
if (entities == null) { return entities != null && entities.contains(entity);
this.entities.put(position, new ArrayList<>());
return false;
}
return this.entities.get(position).contains(entity);
} }
/** /**
@@ -115,8 +106,8 @@ public final class Sector {
public List<Entity> getEntities(Position position) { public List<Entity> getEntities(Position position) {
List<Entity> entities = this.entities.get(position); List<Entity> entities = this.entities.get(position);
if (entities == null) { if (entities == null) {
entities = new ArrayList<>(); this.entities.put(position, new ArrayList<>());
this.entities.put(position, entities); return Collections.emptyList();
} }
return new ArrayList<>(entities); return new ArrayList<>(entities);
@@ -133,11 +124,11 @@ public final class Sector {
public <T extends Entity> List<T> getEntities(Position position, EntityType type) { public <T extends Entity> List<T> getEntities(Position position, EntityType type) {
List<Entity> entities = this.entities.get(position); List<Entity> entities = this.entities.get(position);
if (entities == null) { if (entities == null) {
entities = new ArrayList<>(); this.entities.put(position, new ArrayList<>());
this.entities.put(position, entities); 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. * @param entity The entity that was updated.
*/ */
public void notifyListeners(Entity entity) { 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. * Executes the action for this listener.
* *
* @param The sector that was updated. * @param sector The sector that was updated.
* @param entity The affected entity. * @param entity The affected entity.
*/ */
public abstract void execute(Sector sector, Entity entity); public abstract void execute(Sector sector, Entity entity);
@@ -8,27 +8,27 @@ import java.util.Map;
import org.apollo.game.model.Position; import org.apollo.game.model.Position;
/** /**
* A repository of sectors, backed by a {@link HashMap} of {@link SectorCoordinates} that correspond to their * A repository of {@link Sector Sectors}, backed by a {@link HashMap} of {@link SectorCoordinates} that correspond to
* appropriate {@link Sector}s. * their appropriate sectors.
* *
* @author Major * @author Major
*/ */
public final class SectorRepository { 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; 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<>(); private final Map<SectorCoordinates, Sector> sectors = new HashMap<>();
/** /**
* Creates a new sector repository. * 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) { public SectorRepository(boolean permitRemoval) {
this.permitRemoval = 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 * Gets the {@link List} of {@link Sector Sectors}. This is a shallow copy (i.e. modifying the list will not change
* repository, but modifying the sectors in the list will change the sectors). * the repository, but modifying the sectors in the list will change the sectors in this repository).
* *
* @return The list. * @return The list.
*/ */
+1 -1
View File
@@ -69,7 +69,7 @@ public abstract class Entity {
* *
* @return The position. * @return The position.
*/ */
public Position getPosition() { public final Position getPosition() {
return position; return position;
} }