Search in sources :

Example 1 with BlockChange

use of org.terasology.world.propagation.BlockChange in project Terasology by MovingBlocks.

the class WorldProviderCoreImpl method setBlock.

@Override
public Block setBlock(Vector3i worldPos, Block type) {
    /*
         * Hint: This method has a benchmark available in the BenchmarkScreen, The screen can be opened ingame via the
         * command "showSCreen BenchmarkScreen".
         */
    Vector3i chunkPos = ChunkMath.calcChunkPos(worldPos);
    CoreChunk chunk = chunkProvider.getChunk(chunkPos);
    if (chunk != null) {
        Vector3i blockPos = ChunkMath.calcBlockPos(worldPos);
        Block oldBlockType = chunk.setBlock(blockPos, type);
        if (oldBlockType != type) {
            BlockChange oldChange = blockChanges.get(worldPos);
            if (oldChange == null) {
                blockChanges.put(worldPos, new BlockChange(worldPos, oldBlockType, type));
            } else {
                oldChange.setTo(type);
            }
            for (Vector3i pos : ChunkMath.getChunkRegionAroundWorldPos(worldPos, 1)) {
                RenderableChunk dirtiedChunk = chunkProvider.getChunk(pos);
                if (dirtiedChunk != null) {
                    dirtiedChunk.setDirty(true);
                }
            }
            notifyBlockChanged(worldPos, type, oldBlockType);
        }
        return oldBlockType;
    }
    return null;
}
Also used : CoreChunk(org.terasology.world.chunks.CoreChunk) BlockChange(org.terasology.world.propagation.BlockChange) Vector3i(org.terasology.math.geom.Vector3i) RenderableChunk(org.terasology.world.chunks.RenderableChunk) Block(org.terasology.world.block.Block)

Example 2 with BlockChange

use of org.terasology.world.propagation.BlockChange in project Terasology by MovingBlocks.

the class WorldProviderCoreImpl method setBlocks.

@Override
public Map<Vector3i, Block> setBlocks(Map<Vector3i, Block> blocks) {
    /*
         * Hint: This method has a benchmark available in the BenchmarkScreen, The screen can be opened ingame via the
         * command "showSCreen BenchmarkScreen".
         */
    Set<RenderableChunk> dirtiedChunks = new HashSet<>();
    Set<BlockChange> changedBlocks = new HashSet<>();
    Map<Vector3i, Block> result = new HashMap<>(blocks.size());
    for (Map.Entry<Vector3i, Block> entry : blocks.entrySet()) {
        Vector3i worldPos = entry.getKey();
        Vector3i chunkPos = ChunkMath.calcChunkPos(worldPos);
        CoreChunk chunk = chunkProvider.getChunk(chunkPos);
        if (chunk != null) {
            Block type = entry.getValue();
            Vector3i blockPos = ChunkMath.calcBlockPos(worldPos);
            Block oldBlockType = chunk.setBlock(blockPos, type);
            if (oldBlockType != type) {
                BlockChange oldChange = blockChanges.get(worldPos);
                if (oldChange == null) {
                    blockChanges.put(worldPos, new BlockChange(worldPos, oldBlockType, type));
                } else {
                    oldChange.setTo(type);
                }
                for (Vector3i pos : ChunkMath.getChunkRegionAroundWorldPos(worldPos, 1)) {
                    RenderableChunk dirtiedChunk = chunkProvider.getChunk(pos);
                    if (dirtiedChunk != null) {
                        dirtiedChunks.add(dirtiedChunk);
                    }
                }
                changedBlocks.add(new BlockChange(worldPos, oldBlockType, type));
            }
            result.put(worldPos, oldBlockType);
        } else {
            result.put(worldPos, null);
        }
    }
    for (RenderableChunk chunk : dirtiedChunks) {
        chunk.setDirty(true);
    }
    for (BlockChange change : changedBlocks) {
        notifyBlockChanged(change.getPosition(), change.getTo(), change.getFrom());
    }
    return result;
}
Also used : CoreChunk(org.terasology.world.chunks.CoreChunk) BlockChange(org.terasology.world.propagation.BlockChange) HashMap(java.util.HashMap) RenderableChunk(org.terasology.world.chunks.RenderableChunk) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

Vector3i (org.terasology.math.geom.Vector3i)2 Block (org.terasology.world.block.Block)2 CoreChunk (org.terasology.world.chunks.CoreChunk)2 RenderableChunk (org.terasology.world.chunks.RenderableChunk)2 BlockChange (org.terasology.world.propagation.BlockChange)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1