Search in sources :

Example 1 with BeforeEntityCreated

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

the class EntityAwareWorldProvider method updateBlockEntityComponents.

/**
 * Transforms a block entity with the change of block type. This is driven from the delta between the old and new
 * block type prefabs, but takes into account changes made to the block entity.
 *
 * @param blockEntity The entity to update
 * @param oldType     The previous type of the block
 * @param type        The new type of the block
 */
private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, Block type, Set<Class<? extends Component>> retainComponents) {
    BlockComponent blockComponent = blockEntity.getComponent(BlockComponent.class);
    Optional<Prefab> oldPrefab = oldType.getPrefab();
    EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab.orElse(null));
    oldEntityBuilder.addComponent(new BlockComponent(oldType, new Vector3i(blockComponent.getPosition())));
    BeforeEntityCreated oldEntityEvent = new BeforeEntityCreated(oldPrefab.orElse(null), oldEntityBuilder.iterateComponents());
    blockEntity.send(oldEntityEvent);
    for (Component comp : oldEntityEvent.getResultComponents()) {
        oldEntityBuilder.addComponent(comp);
    }
    Optional<Prefab> newPrefab = type.getPrefab();
    EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab.orElse(null));
    newEntityBuilder.addComponent(new BlockComponent(type, new Vector3i(blockComponent.getPosition())));
    BeforeEntityCreated newEntityEvent = new BeforeEntityCreated(newPrefab.orElse(null), newEntityBuilder.iterateComponents());
    blockEntity.send(newEntityEvent);
    for (Component comp : newEntityEvent.getResultComponents()) {
        newEntityBuilder.addComponent(comp);
    }
    for (Component component : blockEntity.iterateComponents()) {
        if (!COMMON_BLOCK_COMPONENTS.contains(component.getClass()) && !entityManager.getComponentLibrary().getMetadata(component.getClass()).isRetainUnalteredOnBlockChange() && !newEntityBuilder.hasComponent(component.getClass()) && !retainComponents.contains(component.getClass())) {
            blockEntity.removeComponent(component.getClass());
        }
    }
    blockComponent.setBlock(type);
    blockEntity.saveComponent(blockComponent);
    for (Component comp : newEntityBuilder.iterateComponents()) {
        copyIntoPrefab(blockEntity, comp, retainComponents);
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) Vector3i(org.terasology.math.geom.Vector3i) BeforeEntityCreated(org.terasology.entitySystem.entity.lifecycleEvents.BeforeEntityCreated) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent) NetworkComponent(org.terasology.network.NetworkComponent) BlockComponent(org.terasology.world.block.BlockComponent) BeforeDeactivateComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Component(org.terasology.entitySystem.Component) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Aggregations

Component (org.terasology.entitySystem.Component)1 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)1 BeforeDeactivateComponent (org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)1 BeforeEntityCreated (org.terasology.entitySystem.entity.lifecycleEvents.BeforeEntityCreated)1 OnActivatedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent)1 OnChangedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent)1 Prefab (org.terasology.entitySystem.prefab.Prefab)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 Vector3i (org.terasology.math.geom.Vector3i)1 NetworkComponent (org.terasology.network.NetworkComponent)1 BlockComponent (org.terasology.world.block.BlockComponent)1 BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)1