Search in sources :

Example 6 with BlockComponent

use of org.terasology.engine.world.block.BlockComponent 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 7 with BlockComponent

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

the class ServerImpl method updateEntity.

private void updateEntity(NetData.UpdateEntityMessage updateEntity) {
    EntityRef currentEntity = networkSystem.getEntity(updateEntity.getNetId());
    if (currentEntity.exists()) {
        NetworkComponent netComp = currentEntity.getComponent(NetworkComponent.class);
        if (netComp == null) {
            logger.error("Updating entity with no network component: {}, expected netId {}", currentEntity, updateEntity.getNetId());
            return;
        }
        if (netComp.getNetworkId() != updateEntity.getNetId()) {
            logger.error("Network ID wrong before update");
        }
        boolean blockEntityBefore = currentEntity.hasComponent(BlockComponent.class);
        entitySerializer.deserializeOnto(currentEntity, updateEntity.getEntity());
        BlockComponent blockComponent = currentEntity.getComponent(BlockComponent.class);
        if (blockComponent != null && !blockEntityBefore) {
            if (!blockEntityRegistry.getExistingBlockEntityAt(blockComponent.getPosition()).equals(currentEntity)) {
                logger.error("Failed to associated new block entity");
            }
        }
        if (netComp.getNetworkId() != updateEntity.getNetId()) {
            logger.error("Network ID lost in update: {}, {} -> {}", currentEntity, updateEntity.getNetId(), netComp.getNetworkId());
        }
    } else {
        logger.warn("Received update for non-existent entity {}", updateEntity.getNetId());
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 8 with BlockComponent

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

the class NetworkEventSystemDecorator method broadcastEvent.

private <T extends Event> void broadcastEvent(EntityRef entity, T event, EventMetadata<T> metadata) {
    if (networkSystem.getMode().isServer()) {
        NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
        BlockComponent blockComp = entity.getComponent(BlockComponent.class);
        if (netComp != null || blockComp != null) {
            Client instigatorClient = null;
            if (metadata.isSkipInstigator() && event instanceof NetworkEvent) {
                instigatorClient = networkSystem.getOwner(((NetworkEvent) event).getInstigator());
            }
            for (Client client : networkSystem.getPlayers()) {
                if (!client.equals(instigatorClient)) {
                    client.send(event, entity);
                }
            }
        }
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent)

Example 9 with BlockComponent

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

the class EventSystemReplayImpl method broadcastEvent.

private void broadcastEvent(EntityRef entity, Event event, EventMetadata metadata) {
    if (networkSystem.getMode().isServer()) {
        NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
        BlockComponent blockComp = entity.getComponent(BlockComponent.class);
        if (netComp != null || blockComp != null) {
            Client instigatorClient = null;
            if (metadata.isSkipInstigator() && event instanceof NetworkEvent) {
                instigatorClient = networkSystem.getOwner(((NetworkEvent) event).getInstigator());
            }
            for (Client client : networkSystem.getPlayers()) {
                if (!client.equals(instigatorClient)) {
                    client.send(event, entity);
                }
            }
        }
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) NetworkEvent(org.terasology.engine.network.NetworkEvent) Client(org.terasology.engine.network.Client)

Example 10 with BlockComponent

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

the class LocalPlayerSystem method onTargetChanged.

@ReceiveEvent
public void onTargetChanged(PlayerTargetChangedEvent event, EntityRef entity) {
    EntityRef target = event.getNewTarget();
    hasTarget = target.exists();
    if (hasTarget) {
        LocationComponent location = target.getComponent(LocationComponent.class);
        if (location != null) {
            BlockComponent blockComp = target.getComponent(BlockComponent.class);
            BlockRegionComponent blockRegion = target.getComponent(BlockRegionComponent.class);
            if (blockComp != null || blockRegion != null) {
                Vector3f blockPos = location.getWorldPosition(new Vector3f());
                Block block = worldProvider.getBlock(blockPos);
                aabb.set(block.getBounds(blockPos));
            } else {
                MeshComponent mesh = target.getComponent(MeshComponent.class);
                if (mesh != null && mesh.mesh != null) {
                    aabb.set(mesh.mesh.getAABB());
                    aabb.transform(new Matrix4f().translationRotateScale(location.getWorldPosition(new Vector3f()), location.getWorldRotation(new Quaternionf()), location.getWorldScale()));
                }
            }
        }
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) MeshComponent(org.terasology.engine.rendering.logic.MeshComponent) Matrix4f(org.joml.Matrix4f) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) Vector3f(org.joml.Vector3f) Block(org.terasology.engine.world.block.Block) Quaternionf(org.joml.Quaternionf) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Aggregations

BlockComponent (org.terasology.engine.world.block.BlockComponent)15 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)8 NetworkComponent (org.terasology.engine.network.NetworkComponent)6 Vector3f (org.joml.Vector3f)4 Vector3i (org.joml.Vector3i)4 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)4 LocationComponent (org.terasology.engine.logic.location.LocationComponent)4 Block (org.terasology.engine.world.block.Block)4 Vector3ic (org.joml.Vector3ic)2 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)2 Client (org.terasology.engine.network.Client)2 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)2 BlockRegionComponent (org.terasology.engine.world.block.regions.BlockRegionComponent)2 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Matrix4f (org.joml.Matrix4f)1 Quaternionf (org.joml.Quaternionf)1 Vector2f (org.joml.Vector2f)1 PlaySoundEvent (org.terasology.engine.audio.events.PlaySoundEvent)1 BeforeDeactivateComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)1