Search in sources :

Example 6 with EntityData

use of org.terasology.protobuf.EntityData in project Terasology by MovingBlocks.

the class EntitySerializerTest method testDeltaChangedComponent.

@Test
public void testDeltaChangedComponent() throws Exception {
    EntityRef entity = entityManager.create(prefab);
    StringComponent comp = entity.getComponent(StringComponent.class);
    comp.value = "Delta";
    entity.saveComponent(comp);
    EntityData.Entity entityData = entitySerializer.serialize(entity);
    assertEquals(entity.getId(), entityData.getId());
    assertEquals(prefab.getName(), entityData.getParentPrefab());
    assertEquals(1, entityData.getComponentCount());
    assertEquals(0, entityData.getRemovedComponentCount());
    EntityData.Component componentData = entityData.getComponent(0);
    assertEquals("test:string", componentData.getType());
    assertEquals(Lists.newArrayList(EntityData.NameValue.newBuilder().setName("value").setValue(EntityData.Value.newBuilder().addString("Delta").build()).build()), componentData.getFieldList());
}
Also used : StringComponent(org.terasology.entitySystem.stubs.StringComponent) EntityData(org.terasology.protobuf.EntityData) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 7 with EntityData

use of org.terasology.protobuf.EntityData in project Terasology by MovingBlocks.

the class EntitySerializerTest method testDeltaLoadChangedComponent.

@Test
public void testDeltaLoadChangedComponent() throws Exception {
    EntityRef entity = entityManager.create("test:Test");
    StringComponent comp = entity.getComponent(StringComponent.class);
    comp.value = "Delta";
    entity.saveComponent(comp);
    EntityData.Entity entityData = entitySerializer.serialize(entity);
    long nextId = entityManager.getNextId();
    entityManager.clear();
    entityManager.setNextId(nextId);
    EntityRef loadedEntity = entitySerializer.deserialize(entityData);
    assertTrue(loadedEntity.exists());
    assertTrue(loadedEntity.hasComponent(StringComponent.class));
    assertEquals("Delta", loadedEntity.getComponent(StringComponent.class).value);
}
Also used : StringComponent(org.terasology.entitySystem.stubs.StringComponent) EntityData(org.terasology.protobuf.EntityData) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 8 with EntityData

use of org.terasology.protobuf.EntityData in project Terasology by MovingBlocks.

the class EntitySerializerTest method testMappedTypeHandling.

@Test
public void testMappedTypeHandling() throws Exception {
    componentLibrary.register(new SimpleUri("test", "mappedtype"), MappedTypeComponent.class);
    EntityRef entity = entityManager.create();
    entity.addComponent(new MappedTypeComponent());
    EntityData.Entity entityData = entitySerializer.serialize(entity);
    long nextId = entityManager.getNextId();
    entityManager.clear();
    entityManager.setNextId(nextId);
    EntityRef loadedEntity = entitySerializer.deserialize(entityData);
    assertTrue(loadedEntity.exists());
    assertTrue(loadedEntity.hasComponent(MappedTypeComponent.class));
}
Also used : SimpleUri(org.terasology.engine.SimpleUri) EntityData(org.terasology.protobuf.EntityData) MappedTypeComponent(org.terasology.entitySystem.stubs.MappedTypeComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 9 with EntityData

use of org.terasology.protobuf.EntityData in project Terasology by MovingBlocks.

the class EntitySerializer method deserializeOntoComponents.

/**
 * Deserializes the components from an EntityData onto a map of components
 *
 * @param entityData
 * @param componentMap
 */
private void deserializeOntoComponents(EntityData.Entity entityData, Map<Class<? extends Component>, Component> componentMap) {
    EntityInfoComponent entityInfo = (EntityInfoComponent) componentMap.get(EntityInfoComponent.class);
    if (entityInfo == null) {
        entityInfo = new EntityInfoComponent();
        componentMap.put(EntityInfoComponent.class, entityInfo);
    }
    if (entityData.hasOwner()) {
        entityInfo.owner = entityManager.getEntity(entityData.getOwner());
    }
    if (entityData.hasAlwaysRelevant()) {
        entityInfo.alwaysRelevant = entityData.getAlwaysRelevant();
    }
    switch(entityData.getScope()) {
        case GLOBAL:
            entityInfo.scope = EntityScope.GLOBAL;
            break;
        case SECTOR:
            entityInfo.scope = EntityScope.SECTOR;
            break;
        case CHUNK:
            entityInfo.scope = EntityScope.CHUNK;
            break;
    }
    for (EntityData.Component componentData : entityData.getComponentList()) {
        ComponentMetadata<? extends Component> metadata = componentSerializer.getComponentMetadata(componentData);
        if (metadata == null || !componentSerializeCheck.serialize(metadata)) {
            continue;
        }
        Component existingComponent = componentMap.get(metadata.getType());
        if (existingComponent == null) {
            Component newComponent = componentSerializer.deserialize(componentData);
            componentMap.put(metadata.getType(), newComponent);
        } else {
            componentSerializer.deserializeOnto(existingComponent, componentData, FieldSerializeCheck.NullCheck.<Component>newInstance());
        }
    }
}
Also used : EntityData(org.terasology.protobuf.EntityData) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.entitySystem.Component) EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent)

Example 10 with EntityData

use of org.terasology.protobuf.EntityData in project Terasology by MovingBlocks.

the class NetClient method sendInitialEntities.

private void sendInitialEntities(NetData.NetMessage.Builder message) {
    int[] initial = netInitial.toArray();
    netInitial.clear();
    Arrays.sort(initial);
    for (int netId : initial) {
        netRelevant.add(netId);
        EntityRef entity = networkSystem.getEntity(netId);
        if (!entity.hasComponent(NetworkComponent.class)) {
            logger.error("Sending net entity with no network component: {} - {}", netId, entity);
            continue;
        }
        // Note: Send owner->server fields on initial create
        Client owner = networkSystem.getOwner(entity);
        EntityData.PackedEntity entityData = entitySerializer.serialize(entity, true, new ServerComponentFieldCheck(owner == this, true)).build();
        NetData.CreateEntityMessage.Builder createMessage = NetData.CreateEntityMessage.newBuilder().setEntity(entityData);
        BlockComponent blockComponent = entity.getComponent(BlockComponent.class);
        if (blockComponent != null) {
            createMessage.setBlockPos(NetMessageUtil.convert(blockComponent.getPosition()));
        }
        message.addCreateEntity(createMessage);
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) EntityData(org.terasology.protobuf.EntityData) ServerComponentFieldCheck(org.terasology.network.serialization.ServerComponentFieldCheck) Client(org.terasology.network.Client) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

EntityData (org.terasology.protobuf.EntityData)16 EntityRef (org.terasology.entitySystem.entity.EntityRef)15 Test (org.junit.Test)11 StringComponent (org.terasology.entitySystem.stubs.StringComponent)5 TIntIterator (gnu.trove.iterator.TIntIterator)2 Component (org.terasology.entitySystem.Component)2 EntityInfoComponent (org.terasology.entitySystem.entity.internal.EntityInfoComponent)2 IntegerComponent (org.terasology.entitySystem.stubs.IntegerComponent)2 NetworkComponent (org.terasology.network.NetworkComponent)2 ServerComponentFieldCheck (org.terasology.network.serialization.ServerComponentFieldCheck)2 BlockComponent (org.terasology.world.block.BlockComponent)2 SimpleUri (org.terasology.engine.SimpleUri)1 EntityScope (org.terasology.entitySystem.entity.internal.EntityScope)1 MappedTypeComponent (org.terasology.entitySystem.stubs.MappedTypeComponent)1 Client (org.terasology.network.Client)1 ClientComponentFieldCheck (org.terasology.network.serialization.ClientComponentFieldCheck)1