use of org.terasology.engine.network.Client in project Terasology by MovingBlocks.
the class ReadWriteStorageManager method addPlayersToSaveTransaction.
private void addPlayersToSaveTransaction(SaveTransactionBuilder saveTransactionBuilder, NetworkSystem networkSystem) {
unloadedAndSavingPlayerMap.clear();
/*
* New entries might be added concurrently. By using putAll + clear to transfer entries we might loose new
* ones added in between putAll and clear. By iterating we can make sure that all entities removed
* from unloadedAndUnsavedPlayerMap get added to unloadedAndSavingPlayerMap.
*/
Iterator<Map.Entry<String, EntityData.PlayerStore>> unsavedEntryIterator = unloadedAndUnsavedPlayerMap.entrySet().iterator();
while (unsavedEntryIterator.hasNext()) {
Map.Entry<String, EntityData.PlayerStore> entry = unsavedEntryIterator.next();
unloadedAndSavingPlayerMap.put(entry.getKey(), entry.getValue());
unsavedEntryIterator.remove();
}
for (Client client : networkSystem.getPlayers()) {
// If there is a newer undisposed version of the player,we don't need to save the disposed version:
unloadedAndSavingPlayerMap.remove(client.getId());
EntityRef character = client.getEntity().getComponent(ClientComponent.class).character;
saveTransactionBuilder.addLoadedPlayer(client.getId(), createPlayerStore(client, character));
}
for (Map.Entry<String, EntityData.PlayerStore> entry : unloadedAndSavingPlayerMap.entrySet()) {
saveTransactionBuilder.addUnloadedPlayer(entry.getKey(), entry.getValue());
}
}
use of org.terasology.engine.network.Client in project Terasology by MovingBlocks.
the class PlayerSystem method spawnPlayer.
private void spawnPlayer(EntityRef clientEntity) {
ClientComponent client = clientEntity.getComponent(ClientComponent.class);
PlayerFactory playerFactory = new PlayerFactory(entityManager, worldProvider);
EntityRef playerCharacter = playerFactory.newInstance(clientEntity);
Client clientListener = networkSystem.getOwner(clientEntity);
Vector3ic distance = clientListener.getViewDistance().getChunkDistance();
updateRelevanceEntity(clientEntity, distance);
client.character = playerCharacter;
clientEntity.saveComponent(client);
playerCharacter.send(new OnPlayerSpawnedEvent());
}
use of org.terasology.engine.network.Client in project Terasology by MovingBlocks.
the class PlayerSystem method restoreCharacter.
private void restoreCharacter(EntityRef entity, EntityRef character) {
Client clientListener = networkSystem.getOwner(entity);
System.out.println(clientListener);
updateRelevanceEntity(entity, clientListener.getViewDistance().getChunkDistance());
ClientComponent client = entity.getComponent(ClientComponent.class);
client.character = character;
entity.saveComponent(client);
CharacterComponent characterComp = character.getComponent(CharacterComponent.class);
if (characterComp != null) {
characterComp.controller = entity;
character.saveComponent(characterComp);
character.setOwner(entity);
if (!character.hasComponent(AliveCharacterComponent.class)) {
respawnPlayer(entity);
}
Location.attachChild(character, entity, new Vector3f(), new Quaternionf());
} else {
character.destroy();
spawnPlayer(entity);
}
}
use of org.terasology.engine.network.Client in project Terasology by MovingBlocks.
the class PlayerSystem method respawnPlayer.
private void respawnPlayer(EntityRef clientEntity) {
ClientComponent client = clientEntity.getComponent(ClientComponent.class);
EntityRef playerCharacter = client.character;
LocationComponent location = playerCharacter.getComponent(LocationComponent.class);
PlayerFactory playerFactory = new PlayerFactory(entityManager, worldProvider);
Vector3f spawnPosition = playerFactory.findSpawnPositionFromLocationComponent(location);
playerCharacter.addComponent(new AliveCharacterComponent());
playerCharacter.send(new CharacterTeleportEvent(spawnPosition));
logger.debug("Re-spawing player at: {}", spawnPosition);
Client clientListener = networkSystem.getOwner(clientEntity);
Vector3ic distance = clientListener.getViewDistance().getChunkDistance();
updateRelevanceEntity(clientEntity, distance);
playerCharacter.send(new OnPlayerRespawnedEvent());
}
Aggregations