Search in sources :

Example 86 with EntityRef

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

the class InventoryUtils method checkForStacking.

private static boolean checkForStacking(EntityRef from, int slotFrom, EntityRef to, int slotTo) {
    EntityRef itemFrom = getItemAt(from, slotFrom);
    EntityRef itemTo = getItemAt(to, slotTo);
    if (itemFrom.exists() && itemTo.exists() && canStackInto(itemFrom, itemTo)) {
        int fromCount = itemFrom.getComponent(ItemComponent.class).stackCount;
        int toCount = itemTo.getComponent(ItemComponent.class).stackCount;
        putItemIntoSlot(from, EntityRef.NULL, slotFrom);
        adjustStackSize(to, slotTo, fromCount + toCount);
        return true;
    }
    return false;
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 87 with EntityRef

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

the class InventoryUtils method moveItemToSlots.

static boolean moveItemToSlots(EntityRef instigator, EntityRef from, int fromSlot, EntityRef to, List<Integer> toSlots) {
    EntityRef fromItem = InventoryUtils.getItemAt(from, fromSlot);
    BeforeItemRemovedFromInventory removeFrom = new BeforeItemRemovedFromInventory(instigator, fromItem, fromSlot);
    from.send(removeFrom);
    if (removeFrom.isConsumed()) {
        return false;
    }
    int stackCount = InventoryUtils.getStackCount(fromItem);
    int movedToStack = moveToExistingStacksInSlots(from, fromSlot, to, toSlots);
    boolean movedToFreeSlot = false;
    if (stackCount != movedToStack) {
        movedToFreeSlot = moveToFreeSlots(instigator, from, fromSlot, to, toSlots);
    }
    return movedToStack > 0 || movedToFreeSlot;
}
Also used : BeforeItemRemovedFromInventory(org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 88 with EntityRef

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

the class InventoryHud method initialise.

@Override
public void initialise() {
    for (InventoryCell cell : findAll(InventoryCell.class)) {
        cell.bindSelected(new SlotSelectedBinding(cell.getTargetSlot(), localPlayer));
        cell.bindTargetInventory(new ReadOnlyBinding<EntityRef>() {

            @Override
            public EntityRef get() {
                return localPlayer.getCharacterEntity();
            }
        });
    }
    crosshair = find("crosshair", UICrosshair.class);
}
Also used : InventoryCell(org.terasology.rendering.nui.layers.ingame.inventory.InventoryCell) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 89 with EntityRef

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

the class InventoryCell method takeAmount.

private void takeAmount(int amount) {
    EntityRef characterEntity = CoreRegistry.get(LocalPlayer.class).getCharacterEntity();
    InventoryManager inventoryManager = CoreRegistry.get(InventoryManager.class);
    inventoryManager.moveItem(getTransferEntity(), characterEntity, 0, getTargetInventory(), getTargetSlot(), amount);
}
Also used : InventoryManager(org.terasology.logic.inventory.InventoryManager) LocalPlayer(org.terasology.logic.players.LocalPlayer) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 90 with EntityRef

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

the class InventoryCell method moveItemSmartly.

private void moveItemSmartly() {
    EntityRef fromEntity = getTargetInventory();
    int fromSlot = getTargetSlot();
    EntityRef playerEntity = CoreRegistry.get(LocalPlayer.class).getCharacterEntity();
    InventoryComponent playerInventory = playerEntity.getComponent(InventoryComponent.class);
    if (playerInventory == null) {
        return;
    }
    CharacterComponent characterComponent = playerEntity.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        logger.error("Character entity of player had no character component");
        return;
    }
    int totalSlotCount = playerInventory.itemSlots.size();
    EntityRef interactionTarget = characterComponent.predictedInteractionTarget;
    InventoryComponent interactionTargetInventory = interactionTarget.getComponent(InventoryComponent.class);
    EntityRef targetEntity;
    List<Integer> toSlots = new ArrayList<>(totalSlotCount);
    if (fromEntity.equals(playerEntity)) {
        if (interactionTarget.exists() && interactionTargetInventory != null) {
            targetEntity = interactionTarget;
            toSlots = numbersBetween(0, interactionTargetInventory.itemSlots.size());
        } else {
            targetEntity = playerEntity;
            // TODO use a constant once there is one
            int hudSlotCount = 10;
            boolean fromHud = (fromSlot < hudSlotCount);
            boolean toHud = !fromHud;
            if (toHud) {
                toSlots = numbersBetween(0, hudSlotCount);
            } else {
                toSlots = numbersBetween(hudSlotCount, totalSlotCount);
            }
        }
    } else {
        targetEntity = playerEntity;
        toSlots = numbersBetween(0, totalSlotCount);
    }
    CoreRegistry.get(InventoryManager.class).moveItemToSlots(getTransferEntity(), fromEntity, fromSlot, targetEntity, toSlots);
}
Also used : InventoryManager(org.terasology.logic.inventory.InventoryManager) InventoryComponent(org.terasology.logic.inventory.InventoryComponent) LocalPlayer(org.terasology.logic.players.LocalPlayer) ArrayList(java.util.ArrayList) CharacterComponent(org.terasology.logic.characters.CharacterComponent) 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