Search in sources :

Example 1 with BlockComponent

use of org.terasology.engine.world.block.BlockComponent 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.
 * Components contained in `blockEntity` that
 * <ul>
 *     <li>are not "common block components" (e.g. `NetworkComponent`)</li>
 *     <li>don't have `reatinUnalteredOnBlockChange` metadata</li>
 *     <li>are not listed in the block prefab</li>
 *     <li>are not listed in the set of components to be retained</li>
 * </ul>
 * will be removed.
 *
 * @param blockEntity      The entity to update
 * @param oldType          The previous type of the block
 * @param type             The new type of the block
 * @param retainComponents List of components to be retained
 */
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, 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, 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 newBlockComponent = new BlockComponent(type, blockComponent.getPosition());
    blockEntity.saveComponent(newBlockComponent);
    for (Component comp : newEntityBuilder.iterateComponents()) {
        copyIntoPrefab(blockEntity, comp, retainComponents);
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) BeforeEntityCreated(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeEntityCreated) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) 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 BlockComponent

use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method onEntityComponentRemoved.

@Override
public void onEntityComponentRemoved(EntityRef entity, Class<? extends Component> component) {
    if (entityManager.getComponentLibrary().getMetadata(component).isForceBlockActive()) {
        BlockComponent blockComp = entity.getComponent(BlockComponent.class);
        if (blockComp != null) {
            Vector3ic blockPosition = blockComp.getPosition();
            Block block = getBlock(blockPosition.x(), blockPosition.y(), blockPosition.z());
            if (isTemporaryBlock(entity, block, component)) {
                temporaryBlockEntities.add(entity);
            }
        }
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) Vector3ic(org.joml.Vector3ic) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) Block(org.terasology.engine.world.block.Block)

Example 3 with BlockComponent

use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method onActivateBlock.

@ReceiveEvent(components = BlockComponent.class)
public void onActivateBlock(OnActivatedComponent event, EntityRef entity) {
    BlockComponent block = entity.getComponent(BlockComponent.class);
    EntityRef oldEntity = blockEntityLookup.put(block.getPosition(new Vector3i()), entity);
    // If this is a client, then an existing block entity may exist. Destroy it.
    if (oldEntity != null && !Objects.equal(oldEntity, entity)) {
        oldEntity.destroy();
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) Vector3i(org.joml.Vector3i) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 4 with BlockComponent

use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method createBlockEntity.

private EntityRef createBlockEntity(Vector3ic blockPosition, Block block) {
    EntityBuilder builder = entityManager.newBuilder(block.getPrefab().orElse(null));
    builder.addComponent(new LocationComponent(new Vector3f(blockPosition)));
    builder.addComponent(new BlockComponent(block, blockPosition));
    boolean isTemporary = isTemporaryBlock(builder, block);
    if (!isTemporary && !builder.hasComponent(NetworkComponent.class)) {
        builder.addComponent(new NetworkComponent());
    }
    EntityRef blockEntity;
    if (isTemporary) {
        blockEntity = builder.buildWithoutLifecycleEvents();
        temporaryBlockEntities.add(blockEntity);
    } else {
        blockEntity = builder.build();
    }
    blockEntityLookup.put(new Vector3i(blockPosition), blockEntity);
    return blockEntity;
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.engine.logic.location.LocationComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 5 with BlockComponent

use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.

the class BlockCommands method replaceBlock.

@Command(shortDescription = "Replaces a block in front of user", helpText = "Replaces a block in front of the user at the specified max distance", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public void replaceBlock(@Sender EntityRef sender, @CommandParam("blockName") String uri, @CommandParam(value = "maxDistance", required = false) Integer maxDistanceParam) {
    int maxDistance = maxDistanceParam != null ? maxDistanceParam : 12;
    EntityRef playerEntity = sender.getComponent(ClientComponent.class).character;
    EntityRef gazeEntity = GazeAuthoritySystem.getGazeEntityForCharacter(playerEntity);
    LocationComponent gazeLocation = gazeEntity.getComponent(LocationComponent.class);
    Set<ResourceUrn> matchingUris = Assets.resolveAssetUri(uri, BlockFamilyDefinition.class);
    targetSystem.updateTarget(gazeLocation.getWorldPosition(new Vector3f()), gazeLocation.getWorldDirection(new Vector3f()), maxDistance);
    EntityRef target = targetSystem.getTarget();
    BlockComponent targetLocation = target.getComponent(BlockComponent.class);
    if (matchingUris.size() == 1) {
        Optional<BlockFamilyDefinition> def = Assets.get(matchingUris.iterator().next(), BlockFamilyDefinition.class);
        if (def.isPresent()) {
            BlockFamily blockFamily = blockManager.getBlockFamily(uri);
            Block block = blockManager.getBlock(blockFamily.getURI());
            world.setBlock(targetLocation.getPosition(), block);
        } else if (matchingUris.size() > 1) {
            StringBuilder builder = new StringBuilder();
            builder.append("Non-unique shape name, possible matches: ");
            Iterator<ResourceUrn> shapeUris = sortItems(matchingUris).iterator();
            while (shapeUris.hasNext()) {
                builder.append(shapeUris.next().toString());
                if (shapeUris.hasNext()) {
                    builder.append(", ");
                }
            }
        }
    }
}
Also used : ClientComponent(org.terasology.engine.network.ClientComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent) Vector3f(org.joml.Vector3f) Iterator(java.util.Iterator) Block(org.terasology.engine.world.block.Block) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) BlockFamilyDefinition(org.terasology.engine.world.block.loader.BlockFamilyDefinition) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command)

Aggregations

BlockComponent (org.terasology.engine.world.block.BlockComponent)15 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)8 NetworkComponent (org.terasology.engine.network.NetworkComponent)6 Vector3f (org.joml.Vector3f)4 Vector3i (org.joml.Vector3i)4 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)4 LocationComponent (org.terasology.engine.logic.location.LocationComponent)4 Block (org.terasology.engine.world.block.Block)4 Vector3ic (org.joml.Vector3ic)2 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)2 Client (org.terasology.engine.network.Client)2 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)2 BlockRegionComponent (org.terasology.engine.world.block.regions.BlockRegionComponent)2 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Matrix4f (org.joml.Matrix4f)1 Quaternionf (org.joml.Quaternionf)1 Vector2f (org.joml.Vector2f)1 PlaySoundEvent (org.terasology.engine.audio.events.PlaySoundEvent)1 BeforeDeactivateComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)1