Search in sources :

Example 6 with NetworkComponent

use of org.terasology.engine.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.debug("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.engine.network.NetworkComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 7 with NetworkComponent

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

the class CreateWorldEntity method createWorldPoolsAndEntity.

private EntityRef createWorldPoolsAndEntity() {
    entityManager.createWorldPools(gameManifest);
    EntityRef worldEntity = entityManager.create();
    worldEntity.addComponent(new WorldComponent());
    NetworkComponent networkComponent = new NetworkComponent();
    networkComponent.replicateMode = NetworkComponent.ReplicateMode.ALWAYS;
    worldEntity.addComponent(networkComponent);
    return worldEntity;
}
Also used : NetworkComponent(org.terasology.engine.network.NetworkComponent) WorldComponent(org.terasology.engine.world.WorldComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 8 with NetworkComponent

use of org.terasology.engine.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.engine.world.block.BlockComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 9 with NetworkComponent

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

the class BlockTypeEntityGenerator method generateBlockTypeEntity.

private void generateBlockTypeEntity(Block block) {
    EntityBuilder builder = entityManager.newBuilder(blockTypePrefab);
    builder.getComponent(BlockTypeComponent.class).block = block;
    // TODO: Copy across settings as necessary
    Optional<Prefab> prefab = block.getPrefab();
    if (prefab.isPresent()) {
        for (Component comp : prefab.get().iterateComponents()) {
            if (!(comp instanceof NetworkComponent)) {
                builder.addComponent(entityManager.getComponentLibrary().copy(comp));
            }
        }
    }
    block.setEntity(builder.build());
}
Also used : NetworkComponent(org.terasology.engine.network.NetworkComponent) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) Component(org.terasology.gestalt.entitysystem.component.Component) NetworkComponent(org.terasology.engine.network.NetworkComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Example 10 with NetworkComponent

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

the class EventSystemReplayImpl 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.engine.world.block.BlockComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) NetworkEvent(org.terasology.engine.network.NetworkEvent) Client(org.terasology.engine.network.Client)

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