Search in sources :

Example 1 with NetworkComponent

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

the class NetworkSystemImpl method registerNetworkEntity.

public void registerNetworkEntity(EntityRef entity) {
    if (mode == NetworkMode.NONE) {
        return;
    }
    if (mode.isServer()) {
        NetworkComponent netComponent = entity.getComponent(NetworkComponent.class);
        netComponent.setNetworkId(nextNetId++);
        entity.saveComponent(netComponent);
        netIdToEntityId.put(netComponent.getNetworkId(), entity.getId());
        switch(netComponent.replicateMode) {
            case OWNER:
                NetClient clientPlayer = getNetOwner(entity);
                if (clientPlayer != null) {
                    clientPlayer.setNetInitial(netComponent.getNetworkId());
                }
                break;
            default:
                for (NetClient client : netClientList) {
                    // TODO: Relevance Check
                    client.setNetInitial(netComponent.getNetworkId());
                }
                break;
        }
        EntityRef owner = entity.getOwner();
        if (owner.exists()) {
            ownerLookup.put(entity, owner);
            ownedLookup.put(owner, entity);
        }
    }
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 2 with NetworkComponent

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

the class NetworkSystemImpl method onEntityComponentChange.

@Override
public void onEntityComponentChange(EntityRef entity, Class<? extends Component> component) {
    NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
    ComponentMetadata<? extends Component> metadata = componentLibrary.getMetadata(component);
    if (netComp != null && netComp.getNetworkId() != NULL_NET_ID) {
        switch(mode) {
            case LISTEN_SERVER:
            case DEDICATED_SERVER:
                if (metadata.isReplicated()) {
                    for (NetClient client : netClientList) {
                        client.setComponentDirty(netComp.getNetworkId(), component);
                    }
                }
                break;
            case CLIENT:
                if (server != null && metadata.isReplicatedFromOwner() && getOwnerEntity(entity).equals(server.getClientEntity())) {
                    server.setComponentDirty(netComp.getNetworkId(), component);
                }
                break;
            default:
                break;
        }
    }
    updatedOwnedEntities(entity, component, metadata);
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent)

Example 3 with NetworkComponent

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

the class NetworkSystemImpl method onEntityComponentRemoved.

@Override
public void onEntityComponentRemoved(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 {} removed from {}", component, entity);
                    client.setComponentRemoved(netComp.getNetworkId(), component);
                }
            }
        }
    }
    if (mode.isAuthority() && metadata.isReferenceOwner()) {
        ownershipHelper.listOwnedEntities(entity.getComponent(component)).forEach(EntityRef::destroy);
    }
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 4 with NetworkComponent

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

the class NetEntityRefTypeHandler method serializeCollection.

@Override
public PersistedData serializeCollection(Collection<EntityRef> value, SerializationContext context) {
    List<PersistedData> items = Lists.newArrayList();
    for (EntityRef ref : value) {
        BlockComponent blockComponent = ref.getComponent(BlockComponent.class);
        if (blockComponent != null) {
            Vector3i blockPos = blockComponent.getPosition();
            items.add(context.create(blockPos.x, blockPos.y, blockPos.z));
        } else {
            NetworkComponent netComponent = ref.getComponent(NetworkComponent.class);
            if (netComponent != null) {
                items.add(context.create(netComponent.getNetworkId()));
            } else {
                items.add(context.createNull());
            }
        }
    }
    return context.create(items);
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Vector3i(org.terasology.math.geom.Vector3i) PersistedData(org.terasology.persistence.typeHandling.PersistedData) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 5 with NetworkComponent

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

the class NetEntityRefTypeHandler method serialize.

@Override
public PersistedData serialize(EntityRef value, SerializationContext context) {
    BlockComponent blockComponent = value.getComponent(BlockComponent.class);
    if (blockComponent != null) {
        Vector3i pos = blockComponent.getPosition();
        return context.create(pos.x, pos.y, pos.z);
    }
    NetworkComponent netComponent = value.getComponent(NetworkComponent.class);
    if (netComponent != null) {
        return context.create(netComponent.getNetworkId());
    }
    return context.createNull();
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Vector3i(org.terasology.math.geom.Vector3i)

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