Search in sources :

Example 56 with Vector3i

use of org.terasology.math.geom.Vector3i 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)

Example 57 with Vector3i

use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method createBlockEntity.

private EntityRef createBlockEntity(Vector3i blockPosition, Block block) {
    EntityBuilder builder = entityManager.newBuilder(block.getPrefab().orElse(null));
    builder.addComponent(new LocationComponent(blockPosition.toVector3f()));
    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.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Vector3i(org.terasology.math.geom.Vector3i) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 58 with Vector3i

use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.

the class WorldProviderCore method setBlocks.

/**
 * Places all given blocks of specific types at their corresponding positions
 * </p>
 * Chunks are
 *
 * @param blocks A mapping from world position to change to the type of block to set
 * @return A mapping from world position to previous block type.
 * The value of a map entry is Null if the change failed (because the necessary chunk was not loaded)
 */
default Map<Vector3i, Block> setBlocks(Map<Vector3i, Block> blocks) {
    Map<Vector3i, Block> resultMap = Maps.newHashMap();
    for (Map.Entry<Vector3i, Block> entry : blocks.entrySet()) {
        Block oldBlock = setBlock(entry.getKey(), entry.getValue());
        resultMap.put(entry.getKey(), oldBlock);
    }
    return resultMap;
}
Also used : Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) Map(java.util.Map)

Example 59 with Vector3i

use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.

the class WorldProviderCoreImpl method getSunlight.

@Override
public byte getSunlight(int x, int y, int z) {
    Vector3i chunkPos = ChunkMath.calcChunkPos(x, y, z);
    LitChunk chunk = chunkProvider.getChunk(chunkPos);
    if (chunk != null) {
        Vector3i blockPos = ChunkMath.calcBlockPos(x, y, z);
        return chunk.getSunlight(blockPos);
    }
    return 0;
}
Also used : Vector3i(org.terasology.math.geom.Vector3i) LitChunk(org.terasology.world.chunks.LitChunk)

Example 60 with Vector3i

use of org.terasology.math.geom.Vector3i in project Terasology by MovingBlocks.

the class WorldProviderCoreImpl method getLiquid.

@Override
public LiquidData getLiquid(int x, int y, int z) {
    Vector3i chunkPos = ChunkMath.calcChunkPos(x, y, z);
    CoreChunk chunk = chunkProvider.getChunk(chunkPos);
    if (chunk != null) {
        Vector3i blockPos = ChunkMath.calcBlockPos(x, y, z);
        return chunk.getLiquid(blockPos);
    }
    return new LiquidData();
}
Also used : CoreChunk(org.terasology.world.chunks.CoreChunk) Vector3i(org.terasology.math.geom.Vector3i) LiquidData(org.terasology.world.liquid.LiquidData)

Aggregations

Vector3i (org.terasology.math.geom.Vector3i)246 Test (org.junit.Test)91 EntityRef (org.terasology.entitySystem.entity.EntityRef)34 Block (org.terasology.world.block.Block)32 Chunk (org.terasology.world.chunks.Chunk)30 Vector3f (org.terasology.math.geom.Vector3f)21 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)17 ChunkImpl (org.terasology.world.chunks.internal.ChunkImpl)17 Region3i (org.terasology.math.Region3i)15 BaseVector3i (org.terasology.math.geom.BaseVector3i)15 LocationComponent (org.terasology.logic.location.LocationComponent)14 BlockComponent (org.terasology.world.block.BlockComponent)10 Side (org.terasology.math.Side)9 ChunkViewCoreImpl (org.terasology.world.internal.ChunkViewCoreImpl)8 Before (org.junit.Before)7 Biome (org.terasology.world.biomes.Biome)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 CoreChunk (org.terasology.world.chunks.CoreChunk)6 RenderableChunk (org.terasology.world.chunks.RenderableChunk)6