Search in sources :

Example 1 with BlockChange

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

the class WorldProviderCoreImpl method setBlocks.

@Override
public Map<Vector3ic, Block> setBlocks(Map<? extends Vector3ic, Block> blocks) {
    /*
         * Hint: This method has a benchmark available in the BenchmarkScreen, The screen can be opened ingame via the
         * command "showSCreen BenchmarkScreen".
         */
    Set<BlockChange> changedBlocks = new HashSet<>();
    Map<Vector3ic, Block> result = new HashMap<>(blocks.size());
    Vector3i chunkPos = new Vector3i();
    Vector3i relativePos = new Vector3i();
    for (Map.Entry<? extends Vector3ic, Block> entry : blocks.entrySet()) {
        Vector3ic worldPos = entry.getKey();
        Chunks.toChunkPos(worldPos, chunkPos);
        Chunk chunk = chunkProvider.getChunk(chunkPos);
        if (chunk != null) {
            Block type = entry.getValue();
            Chunks.toRelative(worldPos, relativePos);
            Block oldBlockType = chunk.setBlock(relativePos, type);
            if (oldBlockType != type) {
                BlockChange oldChange = blockChanges.get(worldPos);
                if (oldChange == null) {
                    blockChanges.put(new Vector3i(worldPos), new BlockChange(worldPos, oldBlockType, type));
                } else {
                    oldChange.setTo(type);
                }
                setDirtyChunksNear(worldPos);
                changedBlocks.add(new BlockChange(worldPos, oldBlockType, type));
            }
            result.put(worldPos, oldBlockType);
        } else {
            result.put(worldPos, null);
        }
    }
    for (BlockChange change : changedBlocks) {
        notifyBlockChanged(change.getPosition(), change.getTo(), change.getFrom());
    }
    return result;
}
Also used : BlockChange(org.terasology.engine.world.propagation.BlockChange) Vector3ic(org.joml.Vector3ic) HashMap(java.util.HashMap) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) Chunk(org.terasology.engine.world.chunks.Chunk) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with BlockChange

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

the class WorldProviderCoreImpl method setBlock.

@Override
public Block setBlock(Vector3ic 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 = Chunks.toChunkPos(worldPos, new Vector3i());
    Chunk chunk = chunkProvider.getChunk(chunkPos);
    if (chunk != null) {
        Vector3i blockPos = Chunks.toRelative(worldPos, new Vector3i());
        Block oldBlockType = chunk.setBlock(blockPos, type);
        if (oldBlockType != type) {
            BlockChange oldChange = blockChanges.get(worldPos);
            if (oldChange == null) {
                blockChanges.put(new Vector3i(worldPos), new BlockChange(worldPos, oldBlockType, type));
            } else {
                oldChange.setTo(type);
            }
            setDirtyChunksNear(worldPos);
            notifyBlockChanged(worldPos, type, oldBlockType);
        }
        return oldBlockType;
    }
    return null;
}
Also used : BlockChange(org.terasology.engine.world.propagation.BlockChange) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) Chunk(org.terasology.engine.world.chunks.Chunk)

Aggregations

Vector3i (org.joml.Vector3i)2 Block (org.terasology.engine.world.block.Block)2 Chunk (org.terasology.engine.world.chunks.Chunk)2 BlockChange (org.terasology.engine.world.propagation.BlockChange)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Vector3ic (org.joml.Vector3ic)1