Search in sources :

Example 11 with Client

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());
    }
}
Also used : EntityData(org.terasology.protobuf.EntityData) Client(org.terasology.engine.network.Client) Map(java.util.Map) ConcurrentMap(java.util.concurrent.ConcurrentMap) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ClientComponent(org.terasology.engine.network.ClientComponent)

Example 12 with Client

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());
}
Also used : OnPlayerSpawnedEvent(org.terasology.engine.logic.players.event.OnPlayerSpawnedEvent) Vector3ic(org.joml.Vector3ic) Client(org.terasology.engine.network.Client) ClientComponent(org.terasology.engine.network.ClientComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 13 with Client

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);
    }
}
Also used : Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) AliveCharacterComponent(org.terasology.engine.logic.characters.AliveCharacterComponent) CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) Client(org.terasology.engine.network.Client) ClientComponent(org.terasology.engine.network.ClientComponent) AliveCharacterComponent(org.terasology.engine.logic.characters.AliveCharacterComponent)

Example 14 with Client

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());
}
Also used : CharacterTeleportEvent(org.terasology.engine.logic.characters.CharacterTeleportEvent) Vector3ic(org.joml.Vector3ic) Vector3f(org.joml.Vector3f) Client(org.terasology.engine.network.Client) ClientComponent(org.terasology.engine.network.ClientComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) OnPlayerRespawnedEvent(org.terasology.engine.logic.players.event.OnPlayerRespawnedEvent) AliveCharacterComponent(org.terasology.engine.logic.characters.AliveCharacterComponent)

Aggregations

Client (org.terasology.engine.network.Client)14 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)7 ClientComponent (org.terasology.engine.network.ClientComponent)5 Vector3ic (org.joml.Vector3ic)3 Vector3f (org.joml.Vector3f)2 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)2 AliveCharacterComponent (org.terasology.engine.logic.characters.AliveCharacterComponent)2 LocationComponent (org.terasology.engine.logic.location.LocationComponent)2 NetworkComponent (org.terasology.engine.network.NetworkComponent)2 NetworkSystem (org.terasology.engine.network.NetworkSystem)2 BlockComponent (org.terasology.engine.world.block.BlockComponent)2 EntityData (org.terasology.protobuf.EntityData)2 Map (java.util.Map)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Quaternionf (org.joml.Quaternionf)1 Vector3fc (org.joml.Vector3fc)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 PlayerConfig (org.terasology.engine.config.PlayerConfig)1 ModuleManager (org.terasology.engine.core.module.ModuleManager)1 EngineEntityManager (org.terasology.engine.entitySystem.entity.internal.EngineEntityManager)1