Search in sources :

Example 21 with NetworkComponent

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

the class ServerCommands method listUsers.

@Command(shortDescription = "List users", requiredPermission = PermissionManager.USER_MANAGEMENT_PERMISSION)
public String listUsers() {
    StringBuilder stringBuilder = new StringBuilder();
    for (EntityRef clientInfo : entityManager.getEntitiesWith(ClientInfoComponent.class)) {
        DisplayNameComponent dnc = clientInfo.getComponent(DisplayNameComponent.class);
        NetworkComponent nc = clientInfo.getComponent(NetworkComponent.class);
        String playerText = PlayerUtil.getColoredPlayerName(clientInfo);
        String line = String.format("%s - %s (%d)", playerText, dnc.description, nc.getNetworkId());
        stringBuilder.append(line);
        stringBuilder.append(Console.NEW_LINE);
    }
    return stringBuilder.toString();
}
Also used : DisplayNameComponent(org.terasology.engine.logic.common.DisplayNameComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command)

Example 22 with NetworkComponent

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

Example 23 with NetworkComponent

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

Example 24 with NetworkComponent

use of org.terasology.engine.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.engine.network.NetworkComponent) Component(org.terasology.gestalt.entitysystem.component.Component) NetworkComponent(org.terasology.engine.network.NetworkComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 25 with NetworkComponent

use of org.terasology.engine.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.debug("Component {} added to {}", component, entity);
                    client.setComponentAdded(netComp.getNetworkId(), component);
                }
            }
        }
    }
    updatedOwnedEntities(entity, component, metadata);
}
Also used : NetworkComponent(org.terasology.engine.network.NetworkComponent)

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