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());
}
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);
}
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));
}
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());
}
}
}
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);
}
}
Aggregations