Search in sources :

Example 1 with NetworkComponent

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

the class NetworkOwnershipTest method testClientSentInitialForOwnershipChain.

@Test
public void testClientSentInitialForOwnershipChain() {
    NetworkComponent netCompA = new NetworkComponent();
    netCompA.replicateMode = NetworkComponent.ReplicateMode.OWNER;
    EntityRef entityA = entityManager.create(netCompA);
    EntityBuilder builder = entityManager.newBuilder();
    NetworkComponent netCompB = builder.addComponent(new NetworkComponent());
    netCompB.replicateMode = NetworkComponent.ReplicateMode.OWNER;
    builder.setOwner(entityA);
    EntityRef entityB = builder.build();
    networkSystem.registerNetworkEntity(entityA);
    networkSystem.registerNetworkEntity(entityB);
    connectClient();
    verify(client, times(0)).setNetInitial(entityA.getComponent(NetworkComponent.class).getNetworkId());
    verify(client, times(0)).setNetInitial(entityB.getComponent(NetworkComponent.class).getNetworkId());
    entityA.setOwner(clientEntity);
    networkSystem.updateOwnership(entityA);
    verify(client, times(1)).setNetInitial(entityA.getComponent(NetworkComponent.class).getNetworkId());
    verify(client, times(1)).setNetInitial(entityB.getComponent(NetworkComponent.class).getNetworkId());
}
Also used : NetworkComponent(org.terasology.engine.network.NetworkComponent) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Test(org.junit.jupiter.api.Test)

Example 2 with NetworkComponent

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

the class EntityAwareWorldProvider method updateBlockEntityComponents.

/**
 * Transforms a block entity with the change of block type. This is driven from the delta between the old and new
 * block type prefabs, but takes into account changes made to the block entity.
 * Components contained in `blockEntity` that
 * <ul>
 *     <li>are not "common block components" (e.g. `NetworkComponent`)</li>
 *     <li>don't have `reatinUnalteredOnBlockChange` metadata</li>
 *     <li>are not listed in the block prefab</li>
 *     <li>are not listed in the set of components to be retained</li>
 * </ul>
 * will be removed.
 *
 * @param blockEntity      The entity to update
 * @param oldType          The previous type of the block
 * @param type             The new type of the block
 * @param retainComponents List of components to be retained
 */
private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, Block type, Set<Class<? extends Component>> retainComponents) {
    BlockComponent blockComponent = blockEntity.getComponent(BlockComponent.class);
    Optional<Prefab> oldPrefab = oldType.getPrefab();
    EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab.orElse(null));
    oldEntityBuilder.addComponent(new BlockComponent(oldType, blockComponent.getPosition()));
    BeforeEntityCreated oldEntityEvent = new BeforeEntityCreated(oldPrefab.orElse(null), oldEntityBuilder.iterateComponents());
    blockEntity.send(oldEntityEvent);
    for (Component comp : oldEntityEvent.getResultComponents()) {
        oldEntityBuilder.addComponent(comp);
    }
    Optional<Prefab> newPrefab = type.getPrefab();
    EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab.orElse(null));
    newEntityBuilder.addComponent(new BlockComponent(type, blockComponent.getPosition()));
    BeforeEntityCreated newEntityEvent = new BeforeEntityCreated(newPrefab.orElse(null), newEntityBuilder.iterateComponents());
    blockEntity.send(newEntityEvent);
    for (Component comp : newEntityEvent.getResultComponents()) {
        newEntityBuilder.addComponent(comp);
    }
    for (Component component : blockEntity.iterateComponents()) {
        if (!COMMON_BLOCK_COMPONENTS.contains(component.getClass()) && !entityManager.getComponentLibrary().getMetadata(component.getClass()).isRetainUnalteredOnBlockChange() && !newEntityBuilder.hasComponent(component.getClass()) && !retainComponents.contains(component.getClass())) {
            blockEntity.removeComponent(component.getClass());
        }
    }
    BlockComponent newBlockComponent = new BlockComponent(type, blockComponent.getPosition());
    blockEntity.saveComponent(newBlockComponent);
    for (Component comp : newEntityBuilder.iterateComponents()) {
        copyIntoPrefab(blockEntity, comp, retainComponents);
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) BeforeEntityCreated(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeEntityCreated) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) Component(org.terasology.gestalt.entitysystem.component.Component) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Example 3 with NetworkComponent

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

the class EntityAwareWorldProvider method createBlockEntity.

private EntityRef createBlockEntity(Vector3ic blockPosition, Block block) {
    EntityBuilder builder = entityManager.newBuilder(block.getPrefab().orElse(null));
    builder.addComponent(new LocationComponent(new Vector3f(blockPosition)));
    builder.addComponent(new BlockComponent(block, blockPosition));
    boolean isTemporary = isTemporaryBlock(builder, block);
    if (!isTemporary && !builder.hasComponent(NetworkComponent.class)) {
        builder.addComponent(new NetworkComponent());
    }
    EntityRef blockEntity;
    if (isTemporary) {
        blockEntity = builder.buildWithoutLifecycleEvents();
        temporaryBlockEntities.add(blockEntity);
    } else {
        blockEntity = builder.build();
    }
    blockEntityLookup.put(new Vector3i(blockPosition), blockEntity);
    return blockEntity;
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.engine.logic.location.LocationComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 4 with NetworkComponent

use of org.terasology.engine.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.engine.network.NetworkComponent)

Example 5 with NetworkComponent

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

the class NetworkSystemImpl method processNewClient.

private void processNewClient(NetClient client) {
    ServerConnectListManager serverConnectListManager = context.get(ServerConnectListManager.class);
    if (!serverConnectListManager.isClientAllowedToConnect(client.getId())) {
        String errorMessage = serverConnectListManager.getErrorMessage(client.getId());
        client.send(NetData.NetMessage.newBuilder().setServerInfo(getServerInfoMessage(errorMessage)).build());
        forceDisconnect(client);
        // reset kicked status so the next connection is set correctly
        kicked = false;
        return;
    }
    client.connected(entityManager, entitySerializer, eventSerializer, eventLibrary);
    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);
    // log after connect so that the name has been set:
    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.engine.network.NetworkComponent) ByteString(com.google.protobuf.ByteString) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Aggregations

NetworkComponent (org.terasology.engine.network.NetworkComponent)27 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)17 Test (org.junit.jupiter.api.Test)7 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)7 BlockComponent (org.terasology.engine.world.block.BlockComponent)5 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)4 Component (org.terasology.gestalt.entitysystem.component.Component)3 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)2 LocationComponent (org.terasology.engine.logic.location.LocationComponent)2 ByteString (com.google.protobuf.ByteString)1 Vector3f (org.joml.Vector3f)1 Vector3i (org.joml.Vector3i)1 Vector3ic (org.joml.Vector3ic)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ComponentSystemManager (org.terasology.engine.core.ComponentSystemManager)1 EngineTime (org.terasology.engine.core.EngineTime)1 ModuleManager (org.terasology.engine.core.module.ModuleManager)1 BeforeDeactivateComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)1 BeforeEntityCreated (org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeEntityCreated)1 OnActivatedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent)1