Search in sources :

Example 21 with NetworkComponent

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

the class ServerImpl method updateEntity.

private void updateEntity(NetData.UpdateEntityMessage updateEntity) {
    EntityRef currentEntity = networkSystem.getEntity(updateEntity.getNetId());
    if (currentEntity.exists()) {
        NetworkComponent netComp = currentEntity.getComponent(NetworkComponent.class);
        if (netComp == null) {
            logger.error("Updating entity with no network component: {}, expected netId {}", currentEntity, updateEntity.getNetId());
            return;
        }
        if (netComp.getNetworkId() != updateEntity.getNetId()) {
            logger.error("Network ID wrong before update");
        }
        boolean blockEntityBefore = currentEntity.hasComponent(BlockComponent.class);
        entitySerializer.deserializeOnto(currentEntity, updateEntity.getEntity());
        BlockComponent blockComponent = currentEntity.getComponent(BlockComponent.class);
        if (blockComponent != null && !blockEntityBefore) {
            if (!blockEntityRegistry.getExistingBlockEntityAt(blockComponent.getPosition()).equals(currentEntity)) {
                logger.error("Failed to associated new block entity");
            }
        }
        if (netComp.getNetworkId() != updateEntity.getNetId()) {
            logger.error("Network ID lost in update: {}, {} -> {}", currentEntity, updateEntity.getNetId(), netComp.getNetworkId());
        }
    } else {
        logger.warn("Received update for non-existent entity {}", updateEntity.getNetId());
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 22 with NetworkComponent

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

the class PojoEntityRef method toString.

@Override
public String toString() {
    Prefab parent = getParentPrefab();
    StringBuilder builder = new StringBuilder();
    builder.append("EntityRef{id = ");
    builder.append(id);
    NetworkComponent networkComponent = getComponent(NetworkComponent.class);
    if (networkComponent != null) {
        builder.append(", netId = ");
        builder.append(networkComponent.getNetworkId());
    }
    if (parent != null) {
        builder.append(", prefab = '");
        builder.append(parent.getUrn());
        builder.append("'");
    }
    builder.append("}");
    return builder.toString();
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 23 with NetworkComponent

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

the class EventSystemImpl method broadcastEvent.

private void broadcastEvent(EntityRef entity, Event event, EventMetadata metadata) {
    if (networkSystem.getMode().isServer()) {
        NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
        BlockComponent blockComp = entity.getComponent(BlockComponent.class);
        if (netComp != null || blockComp != null) {
            Client instigatorClient = null;
            if (metadata.isSkipInstigator() && event instanceof NetworkEvent) {
                instigatorClient = networkSystem.getOwner(((NetworkEvent) event).getInstigator());
            }
            for (Client client : networkSystem.getPlayers()) {
                if (!client.equals(instigatorClient)) {
                    client.send(event, entity);
                }
            }
        }
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) NetworkEvent(org.terasology.network.NetworkEvent) Client(org.terasology.network.Client)

Example 24 with NetworkComponent

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

the class NetworkOwnershipTest method testClientNoInitialEntityIfNotOwnedAndReplicateToOwner.

@Test
public void testClientNoInitialEntityIfNotOwnedAndReplicateToOwner() {
    connectClient();
    NetworkComponent netComp = new NetworkComponent();
    netComp.replicateMode = NetworkComponent.ReplicateMode.OWNER;
    EntityRef entity = entityManager.create(netComp);
    networkSystem.registerNetworkEntity(entity);
    assertTrue(entity.getComponent(NetworkComponent.class).getNetworkId() != 0);
    verify(client, times(0)).setNetInitial(entity.getComponent(NetworkComponent.class).getNetworkId());
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 25 with NetworkComponent

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

the class NetworkOwnershipTest method testClientSentInitialOnlyOnce.

@Test
public void testClientSentInitialOnlyOnce() {
    EntityBuilder builder = entityManager.newBuilder();
    NetworkComponent netComp = builder.addComponent(new NetworkComponent());
    netComp.replicateMode = NetworkComponent.ReplicateMode.OWNER;
    builder.setOwner(clientEntity);
    EntityRef entity = builder.build();
    networkSystem.registerNetworkEntity(entity);
    connectClient();
    networkSystem.updateOwnership(entity);
    verify(client, times(1)).setNetInitial(entity.getComponent(NetworkComponent.class).getNetworkId());
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

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