use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class ComponentSerializerTest method testComponentTypeIdUsedWhenLookupTableEnabled.
@Test
public void testComponentTypeIdUsedWhenLookupTableEnabled() throws Exception {
componentSerializer.setIdMapping(ImmutableMap.<Class<? extends Component>, Integer>builder().put(StringComponent.class, 1).build());
Component stringComponent = new StringComponent("Test");
EntityData.Component compData = componentSerializer.serialize(stringComponent);
assertEquals(1, compData.getTypeIndex());
assertFalse(compData.hasType());
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class ComponentSerializerTest method testComponentTypeIdDeserializes.
@Test
public void testComponentTypeIdDeserializes() throws Exception {
componentSerializer.setIdMapping(ImmutableMap.<Class<? extends Component>, Integer>builder().put(StringComponent.class, 1).build());
EntityData.Component compData = EntityData.Component.newBuilder().setTypeIndex(1).addField(EntityData.NameValue.newBuilder().setName("value").setValue(EntityData.Value.newBuilder().addString("item"))).build();
Component comp = componentSerializer.deserialize(compData);
assertTrue(comp instanceof StringComponent);
assertEquals("item", ((StringComponent) comp).value);
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class BlockPrefabManager method updateBlock.
private void updateBlock(Block block) {
Optional<Prefab> prefab = block.getPrefab();
boolean keepActive = block.isKeepActive();
boolean requiresLifecycleEvents = false;
if (prefab.isPresent()) {
for (Component comp : prefab.get().iterateComponents()) {
ComponentMetadata<?> metadata = entityManager.getComponentLibrary().getMetadata(comp.getClass());
if (metadata.isForceBlockActive()) {
keepActive = true;
break;
}
if (metadata.isBlockLifecycleEventsRequired()) {
requiresLifecycleEvents = true;
}
}
}
block.setKeepActive(keepActive);
block.setLifecycleEventsRequired(requiresLifecycleEvents && !keepActive);
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class BlockItemFactory method newBuilder.
/**
* Use this method instead of {@link #newInstance(BlockFamily)} to modify entity properties like the persistence
* flag before it gets created.
*
* @param blockFamily must not be null
*/
public EntityBuilder newBuilder(BlockFamily blockFamily, int quantity) {
EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
builder.addComponent(new LightComponent());
}
// Copy the components from block prefab into the block item
Optional<Prefab> prefab = blockFamily.getArchetypeBlock().getPrefab();
if (prefab.isPresent()) {
for (Component component : prefab.get().iterateComponents()) {
if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
builder.addComponent(entityManager.getComponentLibrary().copy(component));
}
}
}
DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
displayNameComponent.name = blockFamily.getDisplayName();
ItemComponent item = builder.getComponent(ItemComponent.class);
if (blockFamily.getArchetypeBlock().isStackable()) {
item.stackId = "block:" + blockFamily.getURI().toString();
item.stackCount = (byte) quantity;
}
BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
blockItem.blockFamily = blockFamily;
return builder;
}
use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.
the class ComponentSerializer method serialize.
/**
* Serializes the differences between two components.
*
* @param base The base component to compare against.
* @param delta The component whose differences will be serialized
* @param check A check to use to see if each field should be serialized.
* @return The serialized component, or null if it could not be serialized
*/
public EntityData.Component serialize(Component base, Component delta, FieldSerializeCheck<Component> check) {
ComponentMetadata<?> componentMetadata = componentLibrary.getMetadata(base.getClass());
if (componentMetadata == null) {
logger.error("Unregistered component type: {}", base.getClass());
return null;
}
EntityData.Component.Builder componentMessage = EntityData.Component.newBuilder();
serializeComponentType(componentMetadata, componentMessage);
Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
boolean changed = false;
for (ReplicatedFieldMetadata field : componentMetadata.getFields()) {
if (check.shouldSerializeField(field, delta) && serializer.getHandlerFor(field) != null) {
Object origValue = field.getValue(base);
Object deltaValue = field.getValue(delta);
if (!Objects.equal(origValue, deltaValue)) {
PersistedData value = serializer.serializeValue(field, deltaValue, serializationContext);
if (!value.isNull()) {
EntityData.Value dataValue = ((ProtobufPersistedData) value).getValue();
if (usingFieldIds) {
componentMessage.addField(EntityData.NameValue.newBuilder().setNameIndex(field.getId()).setValue(dataValue).build());
} else {
componentMessage.addField(EntityData.NameValue.newBuilder().setName(field.getName()).setValue(dataValue).build());
}
changed = true;
}
}
}
}
if (changed) {
return componentMessage.build();
}
return null;
}
Aggregations