Search in sources :

Example 66 with EntityRef

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());
        }
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) HostingFailedException(org.terasology.engine.network.exceptions.HostingFailedException) TerasologyServerPipelineFactory(org.terasology.engine.network.internal.pipelineFactory.TerasologyServerPipelineFactory) BindException(java.net.BindException) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelException(io.netty.channel.ChannelException)

Example 67 with EntityRef

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);
    }
}
Also used : EntityData(org.terasology.protobuf.EntityData) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 68 with EntityRef

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;
    }
}
Also used : EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 69 with EntityRef

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;
}
Also used : AABBfc(org.terasology.joml.geom.AABBfc) Vector3f(org.joml.Vector3f) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 70 with EntityRef

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;
}
Also used : PersistenceComponentSerializeCheck(org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck) Component(org.terasology.gestalt.entitysystem.component.Component) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) EntitySerializer(org.terasology.engine.persistence.serializers.EntitySerializer)

Aggregations

EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)298 Test (org.junit.jupiter.api.Test)88 ClientComponent (org.terasology.engine.network.ClientComponent)55 Vector3f (org.joml.Vector3f)51 LocationComponent (org.terasology.engine.logic.location.LocationComponent)44 Vector3i (org.joml.Vector3i)36 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)34 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)29 StringComponent (org.terasology.unittest.stubs.StringComponent)26 NetworkComponent (org.terasology.engine.network.NetworkComponent)23 EntityData (org.terasology.protobuf.EntityData)23 Quaternionf (org.joml.Quaternionf)19 DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)19 Component (org.terasology.gestalt.entitysystem.component.Component)19 CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)15 Map (java.util.Map)14 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)13 BlockComponent (org.terasology.engine.world.block.BlockComponent)13 Block (org.terasology.engine.world.block.Block)11 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)10