Search in sources :

Example 76 with EntityRef

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

the class InventoryClientSystem method moveItemToSlots.

@Override
public boolean moveItemToSlots(EntityRef instigator, EntityRef fromInventory, int slotFrom, EntityRef toInventory, List<Integer> toSlots) {
    Collection<EntityRef> clientTempEntities = new HashSet<>();
    if (moveItemToSlotsFillClientTempEntities(instigator, fromInventory, slotFrom, toInventory, toSlots, clientTempEntities)) {
        return false;
    }
    MoveItemToSlotsRequest request = new MoveItemToSlotsRequest(instigator, fromInventory, slotFrom, toInventory, toSlots, changeId++, clientTempEntities);
    pendingMoves.put(request.getChangeId(), request);
    localPlayer.getClientEntity().send(request);
    return true;
}
Also used : MoveItemToSlotsRequest(org.terasology.logic.inventory.events.MoveItemToSlotsRequest) EntityRef(org.terasology.entitySystem.entity.EntityRef) HashSet(java.util.HashSet)

Example 77 with EntityRef

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

the class InventoryUtils method moveToFreeSlots.

/**
 * @param instigator used to verify if the action is allowed
 * @param to         has to provide {@link InventoryComponent} for a successful transfer
 * @param slotFrom   slot number to take the items from.
 * @param from       has to provide {@link InventoryComponent} for a successful transfer
 * @param toSlots    slots that will be checked if they are free
 * @return true if at least 1 item got moved from the specified location.
 */
private static boolean moveToFreeSlots(EntityRef instigator, EntityRef from, int slotFrom, EntityRef to, List<Integer> toSlots) {
    EntityRef fromItem = getItemAt(from, slotFrom);
    ItemComponent fromItemComp = fromItem.getComponent(ItemComponent.class);
    if (fromItemComp == null) {
        return false;
    }
    for (int toSlot : toSlots) {
        EntityRef toItem = getItemAt(to, toSlot);
        if (!toItem.exists()) {
            BeforeItemPutInInventory putTo = new BeforeItemPutInInventory(instigator, fromItem, toSlot);
            to.send(putTo);
            boolean allowed = !putTo.isConsumed();
            if (allowed) {
                putItemIntoSlot(from, EntityRef.NULL, slotFrom);
                putItemIntoSlot(to, fromItem, toSlot);
                return true;
            }
        }
    }
    return false;
}
Also used : BeforeItemPutInInventory(org.terasology.logic.inventory.events.BeforeItemPutInInventory) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 78 with EntityRef

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

the class InventoryUtils method putItemIntoSlot.

static void putItemIntoSlot(EntityRef entity, EntityRef item, int slot) {
    InventoryComponent inventory = entity.getComponent(InventoryComponent.class);
    EntityRef oldItem = inventory.itemSlots.get(slot);
    inventory.itemSlots.set(slot, item);
    entity.saveComponent(inventory);
    entity.send(new InventorySlotChangedEvent(slot, oldItem, item));
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef) InventorySlotChangedEvent(org.terasology.logic.inventory.events.InventorySlotChangedEvent)

Example 79 with EntityRef

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

the class EntityAwareWorldProvider method updateBlockEntity.

private void updateBlockEntity(EntityRef blockEntity, Vector3i pos, Block oldType, Block type, boolean forceEntityUpdate, Set<Class<? extends Component>> retainComponents) {
    if (type.isKeepActive()) {
        temporaryBlockEntities.remove(blockEntity);
    } else if (oldType.isKeepActive() && isTemporaryBlock(blockEntity, type)) {
        temporaryBlockEntities.add(blockEntity);
    }
    if (forceEntityUpdate || !(Objects.equal(oldType.getBlockFamily(), type.getBlockFamily()) && Objects.equal(oldType.getPrefab(), type.getPrefab()))) {
        updateBlockEntityComponents(blockEntity, oldType, type, retainComponents);
    }
    EntityRef regionEntity = blockRegionLookup.get(pos);
    if (regionEntity != null) {
        regionEntity.send(new OnChangedBlock(pos, type, oldType));
    }
    blockEntity.send(new OnChangedBlock(new Vector3i(pos), type, oldType));
}
Also used : OnChangedBlock(org.terasology.world.OnChangedBlock) Vector3i(org.terasology.math.geom.Vector3i) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 80 with EntityRef

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

the class EntityAwareWorldProvider method createBlockEntity.

private EntityRef createBlockEntity(Vector3i blockPosition, Block block) {
    EntityBuilder builder = entityManager.newBuilder(block.getPrefab().orElse(null));
    builder.addComponent(new LocationComponent(blockPosition.toVector3f()));
    builder.addComponent(new BlockComponent(block, blockPosition));
    boolean isTemporary = isTemporaryBlock(builder, block);
    if (!isTemporary && !builder.hasComponent(NetworkComponent.class)) {
        builder.addComponent(new NetworkComponent());
    }
    EntityRef blockEntity;
    if (isTemporary) {
        blockEntity = builder.buildWithoutLifecycleEvents();
        temporaryBlockEntities.add(blockEntity);
    } else {
        blockEntity = builder.build();
    }
    blockEntityLookup.put(new Vector3i(blockPosition), blockEntity);
    return blockEntity;
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Vector3i(org.terasology.math.geom.Vector3i) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)337 Test (org.junit.Test)106 ClientComponent (org.terasology.network.ClientComponent)49 LocationComponent (org.terasology.logic.location.LocationComponent)45 Vector3f (org.terasology.math.geom.Vector3f)44 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)36 Vector3i (org.terasology.math.geom.Vector3i)34 Command (org.terasology.logic.console.commandSystem.annotations.Command)28 StringComponent (org.terasology.entitySystem.stubs.StringComponent)26 NetworkComponent (org.terasology.network.NetworkComponent)21 EntityData (org.terasology.protobuf.EntityData)21 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)17 Block (org.terasology.world.block.Block)16 Component (org.terasology.entitySystem.Component)15 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)15 CharacterComponent (org.terasology.logic.characters.CharacterComponent)14 Quat4f (org.terasology.math.geom.Quat4f)14 BlockComponent (org.terasology.world.block.BlockComponent)13 Map (java.util.Map)11 LocalPlayer (org.terasology.logic.players.LocalPlayer)11