Search in sources :

Example 16 with NetworkComponent

use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.

the class NetworkSystemImpl method processNewClient.

private void processNewClient(NetClient client) {
    client.connected(entityManager, entitySerializer, eventSerializer, eventLibrary);
    // log after connect so that the name has been set:
    logger.info("New client connected: {}", client.getName());
    client.send(NetData.NetMessage.newBuilder().setJoinComplete(NetData.JoinCompleteMessage.newBuilder().setClientId(client.getEntity().getComponent(NetworkComponent.class).getNetworkId())).build());
    clientList.add(client);
    netClientList.add(client);
    clientPlayerLookup.put(client.getEntity(), client);
    connectClient(client);
    logger.info("New client entity: {}", client.getEntity());
    for (EntityRef netEntity : entityManager.getEntitiesWith(NetworkComponent.class)) {
        NetworkComponent netComp = netEntity.getComponent(NetworkComponent.class);
        if (netComp.getNetworkId() != NULL_NET_ID) {
            switch(netComp.replicateMode) {
                case OWNER:
                    if (client.equals(getOwner(netEntity))) {
                        client.setNetInitial(netComp.getNetworkId());
                    }
                    break;
                default:
                    // TODO: Relevance Check
                    client.setNetInitial(netComp.getNetworkId());
                    break;
            }
        }
    }
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 17 with NetworkComponent

use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.

the class NetworkSystemImpl method shutdown.

@Override
public void shutdown() {
    allChannels.close().awaitUninterruptibly();
    // Factory may be null if a local game session has happened, yet be initialized if networking has been used
    if (factory != null) {
        factory.releaseExternalResources();
    }
    processPendingDisconnects();
    clientList.forEach(this::processRemovedClient);
    server = null;
    nextNetId = 1;
    netIdToEntityId.clear();
    if (mode != NetworkMode.CLIENT) {
        if (this.entityManager != null) {
            for (EntityRef entity : entityManager.getEntitiesWith(NetworkComponent.class)) {
                NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
                netComp.setNetworkId(0);
                entity.saveComponent(netComp);
            }
            this.entityManager.unsubscribe(this);
        }
    }
    mode = NetworkMode.NONE;
    entityManager = null;
    eventLibrary = null;
    componentLibrary = null;
    eventSerializer = null;
    entitySerializer = null;
    clientList.clear();
    netClientList.clear();
    blockManager = null;
    biomeManager = null;
    ownerLookup.clear();
    ownedLookup.clear();
    ownershipHelper = null;
    storageManager = null;
    logger.info("Network shutdown");
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 18 with NetworkComponent

use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.

the class NetworkSystemImpl method unregisterNetworkEntity.

public void unregisterNetworkEntity(EntityRef entity) {
    if (mode != NetworkMode.CLIENT) {
        NetworkComponent netComponent = entity.getComponent(NetworkComponent.class);
        if (netComponent != null) {
            logger.debug("Unregistering network entity: {} with netId {}", entity, netComponent.getNetworkId());
            netIdToEntityId.remove(netComponent.getNetworkId());
            if (mode.isServer()) {
                for (NetClient client : netClientList) {
                    client.setNetRemoved(netComponent.getNetworkId());
                }
            }
            netComponent.setNetworkId(NULL_NET_ID);
            entity.saveComponent(netComponent);
        }
    }
    ownerLookup.remove(entity);
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent)

Example 19 with NetworkComponent

use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.

the class NetworkSystemImpl method onEntityComponentAdded.

@Override
public void onEntityComponentAdded(EntityRef entity, Class<? extends Component> component) {
    ComponentMetadata<? extends Component> metadata = componentLibrary.getMetadata(component);
    NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
    if (netComp != null && netComp.getNetworkId() != NULL_NET_ID) {
        if (mode.isServer()) {
            if (metadata.isReplicated()) {
                for (NetClient client : netClientList) {
                    logger.info("Component {} added to {}", component, entity);
                    client.setComponentAdded(netComp.getNetworkId(), component);
                }
            }
        }
    }
    updatedOwnedEntities(entity, component, metadata);
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent)

Example 20 with NetworkComponent

use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.

the class NetworkSystemImpl method updateOwnership.

public void updateOwnership(EntityRef entity) {
    NetworkComponent netComponent = entity.getComponent(NetworkComponent.class);
    if (netComponent == null || netComponent.getNetworkId() == NULL_NET_ID) {
        return;
    }
    EntityRef lastOwnerEntity = ownerLookup.get(entity);
    if (lastOwnerEntity == null) {
        lastOwnerEntity = EntityRef.NULL;
    }
    EntityRef newOwnerEntity = entity.getOwner();
    if (!Objects.equal(lastOwnerEntity, newOwnerEntity)) {
        NetClient lastOwner = getNetOwner(lastOwnerEntity);
        NetClient newOwner = getNetOwner(newOwnerEntity);
        if (!Objects.equal(lastOwner, newOwner)) {
            recursiveUpdateOwnership(entity, lastOwner, newOwner);
            if (newOwner != null) {
                int id = netComponent.getNetworkId();
                for (Component component : entity.iterateComponents()) {
                    if (componentLibrary.getMetadata(component.getClass()).isReplicated()) {
                        newOwner.setComponentDirty(id, component.getClass());
                    }
                }
            }
        }
        if (lastOwnerEntity.exists()) {
            ownedLookup.remove(lastOwnerEntity, entity);
        }
        if (newOwnerEntity.exists()) {
            ownerLookup.put(entity, newOwnerEntity);
            ownedLookup.put(newOwnerEntity, entity);
        } else {
            ownerLookup.remove(entity);
        }
    }
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) NetworkComponent(org.terasology.network.NetworkComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

NetworkComponent (org.terasology.network.NetworkComponent)27 EntityRef (org.terasology.entitySystem.entity.EntityRef)18 Test (org.junit.Test)7 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)6 BlockComponent (org.terasology.world.block.BlockComponent)5 Component (org.terasology.entitySystem.Component)3 Prefab (org.terasology.entitySystem.prefab.Prefab)3 Vector3i (org.terasology.math.geom.Vector3i)3 Command (org.terasology.logic.console.commandSystem.annotations.Command)2 Map (java.util.Map)1 Before (org.junit.Before)1 Config (org.terasology.config.Config)1 ComponentSystemManager (org.terasology.engine.ComponentSystemManager)1 EngineTime (org.terasology.engine.EngineTime)1 SimpleUri (org.terasology.engine.SimpleUri)1 ModuleManager (org.terasology.engine.module.ModuleManager)1 EntityManager (org.terasology.entitySystem.entity.EntityManager)1 EventLibrary (org.terasology.entitySystem.metadata.EventLibrary)1 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)1 LocationComponent (org.terasology.logic.location.LocationComponent)1