Search in sources :

Example 1 with PlaceBlocks

use of org.terasology.engine.world.block.entity.placement.PlaceBlocks 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)

Aggregations

Vector2f (org.joml.Vector2f)1 Vector3i (org.joml.Vector3i)1 PlaySoundEvent (org.terasology.engine.audio.events.PlaySoundEvent)1 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)1 Side (org.terasology.engine.math.Side)1 Block (org.terasology.engine.world.block.Block)1 BlockComponent (org.terasology.engine.world.block.BlockComponent)1 PlaceBlocks (org.terasology.engine.world.block.entity.placement.PlaceBlocks)1 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)1 BlockPlacementData (org.terasology.engine.world.block.family.BlockPlacementData)1