Search in sources :

Example 71 with EntityRef

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

the class InventoryAuthoritySystem method giveItemToSlots.

private boolean giveItemToSlots(EntityRef instigator, EntityRef entity, EntityRef item, List<Integer> slots) {
    int toConsume = InventoryUtils.getStackCount(item);
    Map<Integer, Integer> consumableCount = new LinkedHashMap<>();
    // First: check which slots we can merge into
    for (int slot : slots) {
        EntityRef itemAtEntity = InventoryUtils.getItemAt(entity, slot);
        ItemComponent itemAt = itemAtEntity.getComponent(ItemComponent.class);
        if (itemAt != null && InventoryUtils.isSameItem(item, itemAtEntity)) {
            int spaceInSlot = itemAt.maxStackSize - itemAt.stackCount;
            int toAdd = Math.min(toConsume, spaceInSlot);
            if (toAdd > 0) {
                consumableCount.put(slot, toAdd);
                toConsume -= toAdd;
                if (toConsume == 0) {
                    break;
                }
            }
        }
    }
    int emptySlotNo = -1;
    int emptySlotCount = toConsume;
    if (toConsume > 0) {
        // Next: check which slots are empty and figure out where to add
        for (int slot : slots) {
            EntityRef itemAtEntity = InventoryUtils.getItemAt(entity, slot);
            ItemComponent itemAt = itemAtEntity.getComponent(ItemComponent.class);
            if (itemAt == null && canPutItemIntoSlot(instigator, entity, item, slot)) {
                emptySlotNo = slot;
                emptySlotCount = toConsume;
                toConsume = 0;
                break;
            }
        }
    }
    if (toConsume > 0) {
        return false;
    }
    for (Map.Entry<Integer, Integer> slotCount : consumableCount.entrySet()) {
        int slot = slotCount.getKey();
        int count = slotCount.getValue();
        EntityRef itemAtEntity = InventoryUtils.getItemAt(entity, slot);
        ItemComponent itemAt = itemAtEntity.getComponent(ItemComponent.class);
        InventoryUtils.adjustStackSize(entity, slot, itemAt.stackCount + count);
    }
    if (emptySlotNo > -1) {
        ItemComponent sourceItem = item.getComponent(ItemComponent.class);
        sourceItem.stackCount = (byte) emptySlotCount;
        item.saveComponent(sourceItem);
        InventoryUtils.putItemIntoSlot(entity, item, emptySlotNo);
    } else {
        item.destroy();
    }
    return true;
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 72 with EntityRef

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

the class InventoryAuthoritySystem method removeItemInternal.

private EntityRef removeItemInternal(EntityRef inventory, EntityRef instigator, List<EntityRef> items, boolean destroyRemoved, Integer count) {
    final EntityRef firstItem = items.get(0);
    for (EntityRef item : items) {
        if (item != firstItem && !InventoryUtils.isSameItem(firstItem, item)) {
            return null;
        }
    }
    for (EntityRef item : items) {
        ItemComponent itemToRemove = item.getComponent(ItemComponent.class);
        if (itemToRemove == null) {
            return EntityRef.NULL;
        }
    }
    List<Integer> slotsWithItem = new LinkedList<>();
    for (EntityRef item : items) {
        int slotWithItem = InventoryUtils.getSlotWithItem(inventory, item);
        if (slotWithItem == -1) {
            return null;
        }
        slotsWithItem.add(slotWithItem);
    }
    Integer toRemove = count;
    if (toRemove == null) {
        toRemove = 0;
        for (EntityRef item : items) {
            toRemove += InventoryUtils.getStackCount(item);
        }
    }
    return removeItemFromSlots(instigator, destroyRemoved, inventory, slotsWithItem, toRemove);
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef) LinkedList(java.util.LinkedList)

Example 73 with EntityRef

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

the class InventoryClientSystem method moveItemFillClientTempEntities.

private boolean moveItemFillClientTempEntities(EntityRef fromInventory, EntityRef instigator, int slotFrom, EntityRef toInventory, int slotTo, Collection<EntityRef> clientTempEntities) {
    boolean slotFromEmpty = !InventoryUtils.getItemAt(fromInventory, slotFrom).exists();
    boolean slotToEmpty = !InventoryUtils.getItemAt(toInventory, slotTo).exists();
    if (!InventoryUtils.moveItem(instigator, fromInventory, slotFrom, toInventory, slotTo)) {
        return true;
    }
    if (slotFromEmpty) {
        EntityRef itemAt = InventoryUtils.getItemAt(fromInventory, slotFrom);
        if (itemAt.exists()) {
            clientTempEntities.add(itemAt);
        }
    }
    if (slotToEmpty) {
        EntityRef itemAt = InventoryUtils.getItemAt(toInventory, slotTo);
        if (itemAt.exists()) {
            clientTempEntities.add(itemAt);
        }
    }
    return false;
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 74 with EntityRef

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

the class InventoryClientSystem method switchItem.

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

Example 75 with EntityRef

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

the class InventoryClientSystem method moveItem.

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

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