Search in sources :

Example 21 with Block

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

the class BlockItemSystem method onPlaceBlock.

@ReceiveEvent(components = { BlockItemComponent.class, ItemComponent.class })
public void onPlaceBlock(ActivateEvent event, EntityRef item) {
    if (!event.getTarget().exists()) {
        event.consume();
        return;
    }
    BlockItemComponent blockItem = item.getComponent(BlockItemComponent.class);
    BlockFamily blockFamily = blockItem.blockFamily;
    Side surfaceSide = Side.inDirection(event.getHitNormal());
    BlockComponent blockComponent = event.getTarget().getComponent(BlockComponent.class);
    if (blockComponent == null) {
        // If there is no block there (i.e. it's a BlockGroup, we don't allow placing block, try somewhere else)
        event.consume();
        return;
    }
    Vector3i targetBlock = new Vector3i(blockComponent.getPosition());
    Vector3i placementPos = new Vector3i(targetBlock);
    placementPos.add(surfaceSide.direction());
    Vector2f relativeAttachmentPosition = getRelativeAttachmentPosition(event);
    Block block = blockFamily.getBlockForPlacement(new BlockPlacementData(placementPos, surfaceSide, event.getDirection(), relativeAttachmentPosition));
    if (canPlaceBlock(block, targetBlock, placementPos)) {
        // TODO: Fix this for changes.
        if (networkSystem.getMode().isAuthority()) {
            PlaceBlocks placeBlocks = new PlaceBlocks(placementPos, block, event.getInstigator());
            worldProvider.getWorldEntity().send(placeBlocks);
            if (!placeBlocks.isConsumed()) {
                item.send(new OnBlockItemPlaced(placementPos, blockEntityRegistry.getBlockEntityAt(placementPos), event.getInstigator()));
            } else {
                event.consume();
            }
        }
        recordBlockPlaced(event, blockFamily);
        event.getInstigator().send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
    } else {
        event.consume();
    }
}
Also used : Side(org.terasology.engine.math.Side) BlockComponent(org.terasology.engine.world.block.BlockComponent) BlockPlacementData(org.terasology.engine.world.block.family.BlockPlacementData) Vector2f(org.joml.Vector2f) PlaySoundEvent(org.terasology.engine.audio.events.PlaySoundEvent) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) PlaceBlocks(org.terasology.engine.world.block.entity.placement.PlaceBlocks) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 22 with Block

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

the class AttachSupportRequired method getEntity.

private EntityRef getEntity(Vector3ic location, Map<? extends Vector3ic, Block> blockOverrides) {
    final Block overwrittenBlock = blockOverrides.get(location);
    if (overwrittenBlock != null) {
        return overwrittenBlock.getEntity();
    }
    EntityRef blockEntity = getBlockEntityRegistry().getExistingBlockEntityAt(location);
    if (blockEntity.exists()) {
        return blockEntity;
    } else {
        return getWorldProvider().getBlock(location).getEntity();
    }
}
Also used : Block(org.terasology.engine.world.block.Block) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 23 with Block

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

the class MapWorldProvider method getBlock.

@Override
public Block getBlock(int x, int y, int z) {
    Vector3i pos = new Vector3i(x, y, z);
    Block block = blocks.get(pos);
    if (block != null) {
        return block;
    }
    // TODO block manager
    Vector3i chunkPos = Chunks.toChunkPos(pos, new Vector3i());
    Chunk chunk = chunks.get(chunkPos);
    if (chunk == null && worldGenerator != null) {
        chunk = new ChunkImpl(chunkPos, blockManager, extraDataManager);
        worldGenerator.createChunk(chunk, entityBuffer);
        chunks.put(chunkPos, chunk);
    }
    if (chunk != null) {
        return chunk.getBlock(Chunks.toRelative(pos, pos));
    }
    return null;
}
Also used : ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 24 with Block

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

the class NetClient method sendRegisteredBlocks.

private void sendRegisteredBlocks(NetData.NetMessage.Builder message) {
    synchronized (newlyRegisteredFamilies) {
        for (BlockFamily family : newlyRegisteredFamilies) {
            NetData.BlockFamilyRegisteredMessage.Builder blockRegMessage = NetData.BlockFamilyRegisteredMessage.newBuilder();
            for (Block block : family.getBlocks()) {
                blockRegMessage.addBlockUri(block.getURI().toString());
                blockRegMessage.addBlockId(block.getId());
            }
            message.addBlockFamilyRegistered(blockRegMessage);
        }
        newlyRegisteredFamilies.clear();
    }
}
Also used : Block(org.terasology.engine.world.block.Block) BlockFamily(org.terasology.engine.world.block.family.BlockFamily)

Example 25 with Block

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

the class ServerImpl method processBlockChanges.

/**
 * Apply the block changes from the message to the local world.
 */
private void processBlockChanges(NetData.NetMessage message) {
    for (NetData.BlockChangeMessage blockChange : message.getBlockChangeList()) {
        Block newBlock = blockManager.getBlock((short) blockChange.getNewBlock());
        logger.debug("Received block change to {}", newBlock);
        // TODO: Store changes to blocks that aren't ready to be modified (the surrounding chunks aren't available)
        WorldProvider worldProvider = CoreRegistry.get(WorldProvider.class);
        Vector3i pos = NetMessageUtil.convert(blockChange.getPos());
        if (worldProvider.isBlockRelevant(pos)) {
            worldProvider.setBlock(pos, newBlock);
        } else {
            awaitingChunkReadyBlockUpdates.put(Chunks.toChunkPos(pos), blockChange);
        }
    }
}
Also used : NetData(org.terasology.protobuf.NetData) WorldProvider(org.terasology.engine.world.WorldProvider) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block)

Aggregations

Block (org.terasology.engine.world.block.Block)54 Vector3i (org.joml.Vector3i)23 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)13 Vector3ic (org.joml.Vector3ic)12 Vector3f (org.joml.Vector3f)9 OnChangedBlock (org.terasology.engine.world.OnChangedBlock)9 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)7 Side (org.terasology.engine.math.Side)7 Map (java.util.Map)6 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)6 EntityManager (org.terasology.engine.entitySystem.entity.EntityManager)5 BlockComponent (org.terasology.engine.world.block.BlockComponent)5 Chunk (org.terasology.engine.world.chunks.Chunk)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 EngineEntityManager (org.terasology.engine.entitySystem.entity.internal.EngineEntityManager)4 Maps (com.google.common.collect.Maps)3 Optional (java.util.Optional)3 ComponentSystemManager (org.terasology.engine.core.ComponentSystemManager)3 LocationComponent (org.terasology.engine.logic.location.LocationComponent)3 BlockEntityRegistry (org.terasology.engine.world.BlockEntityRegistry)3