Search in sources :

Example 11 with BlockComponent

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

the class EntityAwareWorldProvider method onActivateBlock.

@ReceiveEvent(components = { BlockComponent.class })
public void onActivateBlock(OnActivatedComponent event, EntityRef entity) {
    BlockComponent block = entity.getComponent(BlockComponent.class);
    EntityRef oldEntity = blockEntityLookup.put(new Vector3i(block.getPosition()), entity);
    // If this is a client, then an existing block entity may exist. Destroy it.
    if (oldEntity != null && !Objects.equal(oldEntity, entity)) {
        oldEntity.destroy();
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) Vector3i(org.terasology.math.geom.Vector3i) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 12 with BlockComponent

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

the class EntityAwareWorldProvider method onDeactivateBlock.

@ReceiveEvent(components = { BlockComponent.class })
public void onDeactivateBlock(BeforeDeactivateComponent event, EntityRef entity) {
    BlockComponent block = entity.getComponent(BlockComponent.class);
    Vector3i pos = new Vector3i(block.getPosition());
    if (blockEntityLookup.get(pos) == entity) {
        blockEntityLookup.remove(pos);
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) Vector3i(org.terasology.math.geom.Vector3i) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 13 with BlockComponent

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

the class NetClient method sendInitialEntities.

private void sendInitialEntities(NetData.NetMessage.Builder message) {
    int[] initial = netInitial.toArray();
    netInitial.clear();
    Arrays.sort(initial);
    for (int netId : initial) {
        netRelevant.add(netId);
        EntityRef entity = networkSystem.getEntity(netId);
        if (!entity.hasComponent(NetworkComponent.class)) {
            logger.error("Sending net entity with no network component: {} - {}", netId, entity);
            continue;
        }
        // Note: Send owner->server fields on initial create
        Client owner = networkSystem.getOwner(entity);
        EntityData.PackedEntity entityData = entitySerializer.serialize(entity, true, new ServerComponentFieldCheck(owner == this, true)).build();
        NetData.CreateEntityMessage.Builder createMessage = NetData.CreateEntityMessage.newBuilder().setEntity(entityData);
        BlockComponent blockComponent = entity.getComponent(BlockComponent.class);
        if (blockComponent != null) {
            createMessage.setBlockPos(NetMessageUtil.convert(blockComponent.getPosition()));
        }
        message.addCreateEntity(createMessage);
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) EntityData(org.terasology.protobuf.EntityData) ServerComponentFieldCheck(org.terasology.network.serialization.ServerComponentFieldCheck) Client(org.terasology.network.Client) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 14 with BlockComponent

use of org.terasology.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.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 15 with BlockComponent

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

the class EventSystemImpl 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.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) NetworkEvent(org.terasology.network.NetworkEvent) Client(org.terasology.network.Client)

Aggregations

BlockComponent (org.terasology.world.block.BlockComponent)20 EntityRef (org.terasology.entitySystem.entity.EntityRef)11 Vector3i (org.terasology.math.geom.Vector3i)10 LocationComponent (org.terasology.logic.location.LocationComponent)9 NetworkComponent (org.terasology.network.NetworkComponent)7 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)6 Component (org.terasology.entitySystem.Component)4 Vector3f (org.terasology.math.geom.Vector3f)4 MeshComponent (org.terasology.rendering.logic.MeshComponent)4 Block (org.terasology.world.block.Block)4 BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)4 FastRandom (org.terasology.utilities.random.FastRandom)3 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)2 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)2 PojoEntityManager (org.terasology.entitySystem.entity.internal.PojoEntityManager)2 Side (org.terasology.math.Side)2 Client (org.terasology.network.Client)2 PlaceBlocks (org.terasology.world.block.entity.placement.PlaceBlocks)2 BlockFamily (org.terasology.world.block.family.BlockFamily)2 HashMap (java.util.HashMap)1