Search in sources :

Example 61 with EntityRef

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

the class NetworkSystemImpl method getOwnerEntity.

@Override
public EntityRef getOwnerEntity(EntityRef entity) {
    EntityRef owner = entity;
    // NetworkComponent ownerNetComp = entity.getComponent(NetworkComponent.class);
    int i = 0;
    while (i++ < OWNER_DEPTH_LIMIT) {
        EntityRef nextOwner = owner.getOwner();
        if (nextOwner.exists()) {
            owner = nextOwner;
        } else {
            break;
        }
    }
    return owner;
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 62 with EntityRef

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

the class ServerImpl method processEvent.

private void processEvent(NetData.EventMessage message) {
    try {
        Event event = eventSerializer.deserialize(message.getEvent());
        EntityRef target = EntityRef.NULL;
        if (message.hasTargetBlockPos()) {
            target = blockEntityRegistry.getBlockEntityAt(NetMessageUtil.convert(message.getTargetBlockPos()));
        } else if (message.hasTargetId()) {
            target = networkSystem.getEntity(message.getTargetId());
        }
        if (target.exists()) {
            target.send(event);
        } else {
            logger.info("Dropping event {} for unavailable entity {}", event.getClass().getSimpleName(), target);
        }
    } catch (DeserializationException e) {
        logger.error("Failed to deserialize event", e);
    }
}
Also used : Event(org.terasology.entitySystem.event.Event) EntityRef(org.terasology.entitySystem.entity.EntityRef) DeserializationException(org.terasology.persistence.typeHandling.DeserializationException)

Example 63 with EntityRef

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

the class NetEntityRefTypeHandler method serializeCollection.

@Override
public PersistedData serializeCollection(Collection<EntityRef> value, SerializationContext context) {
    List<PersistedData> items = Lists.newArrayList();
    for (EntityRef ref : value) {
        BlockComponent blockComponent = ref.getComponent(BlockComponent.class);
        if (blockComponent != null) {
            Vector3i blockPos = blockComponent.getPosition();
            items.add(context.create(blockPos.x, blockPos.y, blockPos.z));
        } else {
            NetworkComponent netComponent = ref.getComponent(NetworkComponent.class);
            if (netComponent != null) {
                items.add(context.create(netComponent.getNetworkId()));
            } else {
                items.add(context.createNull());
            }
        }
    }
    return context.create(items);
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Vector3i(org.terasology.math.geom.Vector3i) PersistedData(org.terasology.persistence.typeHandling.PersistedData) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 64 with EntityRef

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

the class AbstractStorageManager method getEntitiesOfChunk.

protected Collection<EntityRef> getEntitiesOfChunk(Chunk chunk) {
    List<EntityRef> entitiesToStore = Lists.newArrayList();
    AABB aabb = chunk.getAABB();
    for (EntityRef entity : getEntityManager().getEntitiesWith(LocationComponent.class)) {
        if (!entity.getOwner().exists() && !entity.isAlwaysRelevant() && !entity.hasComponent(ClientComponent.class)) {
            LocationComponent loc = entity.getComponent(LocationComponent.class);
            if (loc != null) {
                if (aabb.contains(loc.getWorldPosition())) {
                    entitiesToStore.add(entity);
                }
            }
        }
    }
    return entitiesToStore;
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) AABB(org.terasology.math.AABB)

Example 65 with EntityRef

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

the class EntityRestorer method restore.

public Map<String, EntityRef> restore(EntityData.EntityStore store) {
    EntitySerializer serializer = new EntitySerializer(entityManager);
    Map<Class<? extends Component>, Integer> idMap = Maps.newHashMap();
    for (int i = 0; i < store.getComponentClassCount(); ++i) {
        ComponentMetadata<?> metadata = entityManager.getComponentLibrary().resolve(store.getComponentClass(i));
        if (metadata != null) {
            idMap.put(metadata.getType(), i);
        }
    }
    serializer.setComponentIdMapping(idMap);
    store.getEntityList().forEach(serializer::deserialize);
    Map<String, EntityRef> namedEntities = Maps.newHashMap();
    for (int i = 0; i < store.getEntityNameCount() && i < store.getEntityNamedCount(); ++i) {
        namedEntities.put(store.getEntityName(i), entityManager.getEntity(store.getEntityNamed(i)));
    }
    return namedEntities;
}
Also used : Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntitySerializer(org.terasology.persistence.serializers.EntitySerializer)

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