Search in sources :

Example 1 with RetainComponentsComponent

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);
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) Component(org.terasology.gestalt.entitysystem.component.Component) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Example 2 with RetainComponentsComponent

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;
}
Also used : RoundingMode(org.joml.RoundingMode) Component(org.terasology.gestalt.entitysystem.component.Component) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) LoggerFactory(org.slf4j.LoggerFactory) UpdateSubscriberSystem(org.terasology.engine.entitySystem.systems.UpdateSubscriberSystem) Block(org.terasology.engine.world.block.Block) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) Lists(com.google.common.collect.Lists) Vector3ic(org.joml.Vector3ic) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) Vector3i(org.joml.Vector3i) Map(java.util.Map) Vector3f(org.joml.Vector3f) Objects(com.google.common.base.Objects) ComponentContainer(org.terasology.engine.entitySystem.ComponentContainer) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) ComponentMetadata(org.terasology.engine.entitySystem.metadata.ComponentMetadata) BlockRegion(org.terasology.engine.world.block.BlockRegion) ComponentSystemManager(org.terasology.engine.core.ComponentSystemManager) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) PerformanceMonitor(org.terasology.engine.monitoring.PerformanceMonitor) FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Collection(java.util.Collection) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) NetworkComponent(org.terasology.engine.network.NetworkComponent) Set(java.util.Set) GameThread(org.terasology.engine.core.GameThread) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) List(java.util.List) Context(org.terasology.engine.context.Context) Vector3fc(org.joml.Vector3fc) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent) Optional(java.util.Optional) BlockEntityRegistry(org.terasology.engine.world.BlockEntityRegistry) BeforeEntityCreated(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeEntityCreated) EntityChangeSubscriber(org.terasology.engine.entitySystem.entity.internal.EntityChangeSubscriber) Collections(java.util.Collections) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) Vector3ic(org.joml.Vector3ic) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) Block(org.terasology.engine.world.block.Block) Component(org.terasology.gestalt.entitysystem.component.Component) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 3 with RetainComponentsComponent

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;
}
Also used : RoundingMode(org.joml.RoundingMode) Component(org.terasology.gestalt.entitysystem.component.Component) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) LoggerFactory(org.slf4j.LoggerFactory) UpdateSubscriberSystem(org.terasology.engine.entitySystem.systems.UpdateSubscriberSystem) Block(org.terasology.engine.world.block.Block) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) Lists(com.google.common.collect.Lists) Vector3ic(org.joml.Vector3ic) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) Vector3i(org.joml.Vector3i) Map(java.util.Map) Vector3f(org.joml.Vector3f) Objects(com.google.common.base.Objects) ComponentContainer(org.terasology.engine.entitySystem.ComponentContainer) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) ComponentMetadata(org.terasology.engine.entitySystem.metadata.ComponentMetadata) BlockRegion(org.terasology.engine.world.block.BlockRegion) ComponentSystemManager(org.terasology.engine.core.ComponentSystemManager) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) PerformanceMonitor(org.terasology.engine.monitoring.PerformanceMonitor) FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Collection(java.util.Collection) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) NetworkComponent(org.terasology.engine.network.NetworkComponent) Set(java.util.Set) GameThread(org.terasology.engine.core.GameThread) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) List(java.util.List) Context(org.terasology.engine.context.Context) Vector3fc(org.joml.Vector3fc) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent) Optional(java.util.Optional) BlockEntityRegistry(org.terasology.engine.world.BlockEntityRegistry) BeforeEntityCreated(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeEntityCreated) EntityChangeSubscriber(org.terasology.engine.entitySystem.entity.internal.EntityChangeSubscriber) Collections(java.util.Collections) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) Block(org.terasology.engine.world.block.Block) Component(org.terasology.gestalt.entitysystem.component.Component) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Aggregations

BeforeDeactivateComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)3 OnActivatedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent)3 OnChangedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent)3 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)3 RetainComponentsComponent (org.terasology.engine.logic.common.RetainComponentsComponent)3 LocationComponent (org.terasology.engine.logic.location.LocationComponent)3 NetworkComponent (org.terasology.engine.network.NetworkComponent)3 Objects (com.google.common.base.Objects)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 Sets (com.google.common.collect.Sets)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 RoundingMode (org.joml.RoundingMode)2 Vector3f (org.joml.Vector3f)2