Search in sources :

Example 76 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class NetClient method sendDirtyEntities.

private void sendDirtyEntities(NetData.NetMessage.Builder message) {
    TIntIterator dirtyIterator = netDirty.iterator();
    while (dirtyIterator.hasNext()) {
        int netId = dirtyIterator.next();
        EntityRef entity = networkSystem.getEntity(netId);
        if (!entity.exists()) {
            logger.error("Sending non-existent entity update for netId {}", netId);
        }
        boolean isOwner = networkSystem.getOwner(entity) == this;
        EntityData.PackedEntity entityData = entitySerializer.serialize(entity, addedComponents.get(netId), dirtyComponents.get(netId), removedComponents.get(netId), new ServerComponentFieldCheck(isOwner, false));
        if (entityData != null) {
            message.addUpdateEntity(NetData.UpdateEntityMessage.newBuilder().setEntity(entityData).setNetId(netId));
        }
    }
    netDirty.clear();
    addedComponents.clear();
    removedComponents.clear();
    dirtyComponents.clear();
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) EntityData(org.terasology.protobuf.EntityData) ServerComponentFieldCheck(org.terasology.engine.network.serialization.ServerComponentFieldCheck) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 77 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef 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 78 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class AbstractClient method findNamesOfOtherPlayers.

/**
 * Creates a {@code HashSet<String>} of all connected player names.
 * @param entityManager
 * @param player Client name to make sure it doesn't put its own name in the list.
 * @return Returns all connected player names.
 */
private Set<String> findNamesOfOtherPlayers(EntityManager entityManager, EntityRef player) {
    Set<String> otherNames = new HashSet<>();
    for (EntityRef clientInfo : entityManager.getEntitiesWith(ClientInfoComponent.class)) {
        if (!clientInfo.equals(player)) {
            DisplayNameComponent displayInfo = clientInfo.getComponent(DisplayNameComponent.class);
            String usedName = displayInfo.name;
            otherNames.add(usedName);
        }
    }
    return otherNames;
}
Also used : DisplayNameComponent(org.terasology.engine.logic.common.DisplayNameComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) HashSet(java.util.HashSet)

Example 79 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class LocalPlayerBlockSelectionByItemSystem method onPlaced.

@ReceiveEvent(components = OnItemActivateSelectionComponent.class)
public void onPlaced(ActivateEvent event, EntityRef itemEntity) {
    if (event.getTargetLocation() == null) {
        return;
    }
    EntityRef targetLocationEntity = event.getTarget();
    this.blockSelectionComponentEntity = itemEntity;
    BlockSelectionComponent blockSelectionComponent = itemEntity.getComponent(BlockSelectionComponent.class);
    if (null == blockSelectionComponent.startPosition) {
        // on the first item click, we start selecting blocks
        targetLocationEntity.send(new SetBlockSelectionStartingPointEvent(itemEntity));
        blockSelectionComponent.shouldRender = true;
    } else {
        // on the second item click, we will set the ending selection point and send an ApplyBlockSelectionEvent
        targetLocationEntity.send(new SetBlockSelectionEndingPointEvent(itemEntity));
        localPlayer.getCharacterEntity().send(new ApplyBlockSelectionEvent(itemEntity, blockSelectionComponent.currentSelection));
        blockSelectionComponent.shouldRender = false;
        blockSelectionComponent.currentSelection = null;
        blockSelectionComponent.startPosition = null;
    }
}
Also used : SetBlockSelectionEndingPointEvent(org.terasology.engine.world.selection.event.SetBlockSelectionEndingPointEvent) BlockSelectionComponent(org.terasology.engine.world.selection.BlockSelectionComponent) SetBlockSelectionStartingPointEvent(org.terasology.engine.world.selection.event.SetBlockSelectionStartingPointEvent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 80 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class CreateRemoteWorldEntity method step.

@Override
public boolean step() {
    EntityRef worldEntity = entityManager.create();
    worldEntity.addComponent(new WorldComponent());
    chunkProvider.setWorldEntity(worldEntity);
    return true;
}
Also used : WorldComponent(org.terasology.engine.world.WorldComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Aggregations

EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)298 Test (org.junit.jupiter.api.Test)88 ClientComponent (org.terasology.engine.network.ClientComponent)55 Vector3f (org.joml.Vector3f)51 LocationComponent (org.terasology.engine.logic.location.LocationComponent)44 Vector3i (org.joml.Vector3i)36 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)34 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)29 StringComponent (org.terasology.unittest.stubs.StringComponent)26 NetworkComponent (org.terasology.engine.network.NetworkComponent)23 EntityData (org.terasology.protobuf.EntityData)23 Quaternionf (org.joml.Quaternionf)19 DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)19 Component (org.terasology.gestalt.entitysystem.component.Component)19 CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)15 Map (java.util.Map)14 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)13 BlockComponent (org.terasology.engine.world.block.BlockComponent)13 Block (org.terasology.engine.world.block.Block)11 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)10