use of org.terasology.engine.logic.common.RetainComponentsComponent in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method cleanUpTemporaryEntity.
private void cleanUpTemporaryEntity(EntityRef entity) {
Prefab prefab = entity.getParentPrefab();
for (Component comp : entity.iterateComponents()) {
// TODO: should this also check for components listed in `RetainComponentsComponent`?
if (!COMMON_BLOCK_COMPONENTS.contains(comp.getClass()) && (prefab == null || !prefab.hasComponent(comp.getClass()))) {
entity.removeComponent(comp.getClass());
}
}
entity.removeComponent(NetworkComponent.class);
if (prefab != null) {
for (Component comp : prefab.iterateComponents()) {
Component currentComp = entity.getComponent(comp.getClass());
if (currentComp == null) {
entity.addComponent(entityManager.getComponentLibrary().copy(comp));
} else {
ComponentMetadata<?> metadata = entityManager.getComponentLibrary().getMetadata(comp.getClass());
boolean changed = false;
for (FieldMetadata field : metadata.getFields()) {
Object expected = field.getValue(comp);
if (!Objects.equal(expected, field.getValue(currentComp))) {
field.setValue(currentComp, expected);
changed = true;
}
}
if (changed) {
entity.saveComponent(currentComp);
}
}
}
}
entityManager.destroyEntityWithoutEvents(entity);
}
use of org.terasology.engine.logic.common.RetainComponentsComponent in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method setBlocks.
// SetBlocks, not SetBlock, is currently triggered by the engine whenever a player places a block.
// This allows for several useful features, such as quickly synchronizing placement across networks.
// However, this means that even if only one block is placed, this is the method being called.
// It must be overridden here to allow an OnChangedBlock event to be properly sent for placed blocks.
@Override
public Map<Vector3ic, Block> setBlocks(Map<? extends Vector3ic, Block> blocks) {
if (GameThread.isCurrentThread()) {
Map<Vector3ic, Block> oldBlocks = super.setBlocks(blocks);
for (Vector3ic vec : oldBlocks.keySet()) {
if (oldBlocks.get(vec) != null) {
EntityRef blockEntity = getBlockEntityAt(vec);
// check for components to be retained when updating the block entity
final Set<Class<? extends Component>> retainComponents = Optional.ofNullable(blockEntity.getComponent(RetainComponentsComponent.class)).map(retainComponentsComponent -> retainComponentsComponent.components).orElse(Collections.emptySet());
updateBlockEntity(blockEntity, vec, oldBlocks.get(vec), blocks.get(vec), false, retainComponents);
}
}
return oldBlocks;
}
return null;
}
use of org.terasology.engine.logic.common.RetainComponentsComponent in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method setBlock.
@Override
public Block setBlock(Vector3ic pos, Block type) {
if (GameThread.isCurrentThread()) {
EntityRef blockEntity = getBlockEntityAt(pos);
Block oldType = super.setBlock(pos, type);
final Set<Class<? extends Component>> retainComponents = Optional.ofNullable(blockEntity.getComponent(RetainComponentsComponent.class)).map(retainComponentsComponent -> retainComponentsComponent.components).orElse(Collections.emptySet());
if (oldType != null) {
updateBlockEntity(blockEntity, pos, oldType, type, false, retainComponents);
}
return oldType;
}
return null;
}
Aggregations