Search in sources :

Example 56 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class AbstractClient method findNamesOfOtherPlayers.

private Set<String> findNamesOfOtherPlayers(EntityManager entityManager, EntityRef player) {
    Set<String> otherNames = new HashSet<>();
    for (EntityRef clientInfo : entityManager.getEntitiesWith(ClientInfoComponent.class)) {
        if (!clientInfo.equals(player)) {
            DisplayNameComponent displayInfo = clientInfo.getComponent(DisplayNameComponent.class);
            String usedName = displayInfo.name;
            otherNames.add(usedName);
        }
    }
    return otherNames;
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) HashSet(java.util.HashSet)

Example 57 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class NetClient method sendDirtyEntities.

private void sendDirtyEntities(NetData.NetMessage.Builder message) {
    TIntIterator dirtyIterator = netDirty.iterator();
    while (dirtyIterator.hasNext()) {
        int netId = dirtyIterator.next();
        EntityRef entity = networkSystem.getEntity(netId);
        if (!entity.exists()) {
            logger.error("Sending non-existent entity update for netId {}", netId);
        }
        boolean isOwner = networkSystem.getOwner(entity) == this;
        EntityData.PackedEntity entityData = entitySerializer.serialize(entity, addedComponents.get(netId), dirtyComponents.get(netId), removedComponents.get(netId), new ServerComponentFieldCheck(isOwner, false));
        if (entityData != null) {
            message.addUpdateEntity(NetData.UpdateEntityMessage.newBuilder().setEntity(entityData).setNetId(netId));
        }
    }
    netDirty.clear();
    addedComponents.clear();
    removedComponents.clear();
    dirtyComponents.clear();
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) EntityData(org.terasology.protobuf.EntityData) ServerComponentFieldCheck(org.terasology.network.serialization.ServerComponentFieldCheck) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 58 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class NetClient method processEvents.

private void processEvents(NetData.NetMessage message) {
    boolean lagCompensated = false;
    PredictionSystem predictionSystem = CoreRegistry.get(PredictionSystem.class);
    for (NetData.EventMessage eventMessage : message.getEventList()) {
        try {
            Event event = eventSerializer.deserialize(eventMessage.getEvent());
            EventMetadata<?> metadata = eventLibrary.getMetadata(event.getClass());
            if (metadata.getNetworkEventType() != NetworkEventType.SERVER) {
                logger.warn("Received non-server event '{}' from client '{}'", metadata, getName());
                continue;
            }
            if (!lagCompensated && metadata.isLagCompensated()) {
                if (predictionSystem != null) {
                    predictionSystem.lagCompensate(getEntity(), lastReceivedTime);
                }
                lagCompensated = true;
            }
            EntityRef target = EntityRef.NULL;
            if (eventMessage.hasTargetId()) {
                target = networkSystem.getEntity(eventMessage.getTargetId());
            }
            if (target.exists()) {
                if (Objects.equal(networkSystem.getOwner(target), this)) {
                    target.send(event);
                } else {
                    logger.warn("Received event {} for non-owned entity {} from {}", event, target, this);
                }
            }
        } catch (DeserializationException e) {
            logger.error("Failed to deserialize event", e);
        } catch (RuntimeException e) {
            logger.error("Error processing event", e);
        }
    }
    if (lagCompensated && predictionSystem != null) {
        predictionSystem.restoreToPresent();
    }
}
Also used : NetData(org.terasology.protobuf.NetData) Event(org.terasology.entitySystem.event.Event) EntityRef(org.terasology.entitySystem.entity.EntityRef) DeserializationException(org.terasology.persistence.typeHandling.DeserializationException) PredictionSystem(org.terasology.logic.characters.PredictionSystem)

Example 59 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef 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 60 with EntityRef

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

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)337 Test (org.junit.Test)106 ClientComponent (org.terasology.network.ClientComponent)49 LocationComponent (org.terasology.logic.location.LocationComponent)45 Vector3f (org.terasology.math.geom.Vector3f)44 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)36 Vector3i (org.terasology.math.geom.Vector3i)34 Command (org.terasology.logic.console.commandSystem.annotations.Command)28 StringComponent (org.terasology.entitySystem.stubs.StringComponent)26 NetworkComponent (org.terasology.network.NetworkComponent)21 EntityData (org.terasology.protobuf.EntityData)21 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)17 Block (org.terasology.world.block.Block)16 Component (org.terasology.entitySystem.Component)15 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)15 CharacterComponent (org.terasology.logic.characters.CharacterComponent)14 Quat4f (org.terasology.math.geom.Quat4f)14 BlockComponent (org.terasology.world.block.BlockComponent)13 Map (java.util.Map)11 LocalPlayer (org.terasology.logic.players.LocalPlayer)11