use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class NetworkSystemImpl method host.
@Override
public void host(int port, boolean dedicatedServer) throws HostingFailedException {
if (mode == NetworkMode.NONE) {
try {
if (hibernationSettings.isPresent()) {
hibernationSettings.get().setHibernationAllowed(false);
}
mode = dedicatedServer ? NetworkMode.DEDICATED_SERVER : NetworkMode.LISTEN_SERVER;
for (EntityRef entity : entityManager.getEntitiesWith(NetworkComponent.class)) {
registerNetworkEntity(entity);
}
generateSerializationTables();
// Configure the server.
bossGroup = new NioEventLoopGroup();
workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).localAddress(port).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new TerasologyServerPipelineFactory(this));
// Start the server.
serverChannelFuture = b.bind();
logger.info("Started server on port {}", port);
if (config.getServerMOTD() != null) {
logger.info("Server MOTD is \"{}\"", config.getServerMOTD());
} else {
logger.info("No server MOTD is defined");
}
if (serverChannelFuture.isSuccess()) {
logger.info("Server started");
}
serverChannelFuture.sync();
nextNetworkTick = time.getRealTimeInMs();
} catch (ChannelException e) {
if (e.getCause() instanceof BindException) {
throw new HostingFailedException("Port already in use (are you already hosting a game?)", e.getCause());
} else {
throw new HostingFailedException("Failed to host game", e.getCause());
}
} catch (InterruptedException e) {
shutdown();
throw new HostingFailedException("Server has been interrupted", e.getCause());
}
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class SaveTransaction method preparePlayerStores.
/**
* @param unsavedEntities currently loaded persistent entities without owner that have not been saved yet.
* This method removes entities it saves.
*/
private void preparePlayerStores(Set<EntityRef> unsavedEntities) {
allPlayers = Maps.newHashMap();
allPlayers.putAll(unloadedPlayers);
for (Map.Entry<String, PlayerStoreBuilder> playerEntry : loadedPlayers.entrySet()) {
PlayerStoreBuilder playerStoreBuilder = playerEntry.getValue();
EntityData.PlayerStore playerStore = playerStoreBuilder.build(privateEntityManager);
unsavedEntities.removeAll(playerStoreBuilder.getStoredEntities());
Long characterEntityId = playerStoreBuilder.getCharacterEntityId();
if (characterEntityId != null) {
EntityRef character = privateEntityManager.getEntity(characterEntityId);
unsavedEntities.remove(character);
}
allPlayers.put(playerEntry.getKey(), playerStore);
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class PlayerStoreInternal method restoreEntities.
@Override
public void restoreEntities() {
if (entityStore != null) {
EntityRestorer restorer = new EntityRestorer(entityManager);
Map<String, EntityRef> refMap = restorer.restore(entityStore);
EntityRef loadedCharacter = refMap.get(CHARACTER);
if (loadedCharacter != null) {
setCharacter(loadedCharacter);
}
entityStore = null;
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class AbstractStorageManager method getEntitiesOfChunk.
protected Collection<EntityRef> getEntitiesOfChunk(Chunk chunk) {
List<EntityRef> entitiesToStore = Lists.newArrayList();
AABBfc aabb = chunk.getAABB();
for (EntityRef entity : getEntityManager().getEntitiesWith(LocationComponent.class)) {
if (!entity.getOwner().exists() && !entity.isAlwaysRelevant() && !entity.hasComponent(ClientComponent.class)) {
LocationComponent loc = entity.getComponent(LocationComponent.class);
if (loc == null) {
continue;
}
Vector3f pos = loc.getWorldPosition(new Vector3f());
if (pos.isFinite()) {
if (aabb.containsPoint(loc.getWorldPosition(new Vector3f()))) {
entitiesToStore.add(entity);
}
}
}
}
return entitiesToStore;
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class EntityRestorer method restore.
public Map<String, EntityRef> restore(EntityData.EntityStore store) {
EntitySerializer serializer = new EntitySerializer(entityManager);
serializer.setComponentSerializeCheck(new PersistenceComponentSerializeCheck());
Map<Class<? extends Component>, Integer> idMap = Maps.newHashMap();
for (int i = 0; i < store.getComponentClassCount(); ++i) {
ComponentMetadata<?> metadata = entityManager.getComponentLibrary().resolve(store.getComponentClass(i));
if (metadata != null) {
idMap.put(metadata.getType(), i);
}
}
serializer.setComponentIdMapping(idMap);
store.getEntityList().forEach(serializer::deserialize);
Map<String, EntityRef> namedEntities = Maps.newHashMap();
for (int i = 0; i < store.getEntityNameCount() && i < store.getEntityNamedCount(); ++i) {
namedEntities.put(store.getEntityName(i), entityManager.getEntity(store.getEntityNamed(i)));
}
return namedEntities;
}
Aggregations