Search in sources :

Example 1 with BeforeItemPutInInventory

use of org.terasology.logic.inventory.events.BeforeItemPutInInventory in project Terasology by MovingBlocks.

the class InventoryUtils method validateMoveAmount.

private static boolean validateMoveAmount(EntityRef instigator, EntityRef from, int slotFrom, EntityRef to, int slotTo, int amount) {
    ItemComponent itemFrom = InventoryUtils.getItemAt(from, slotFrom).getComponent(ItemComponent.class);
    ItemComponent itemTo = InventoryUtils.getItemAt(to, slotTo).getComponent(ItemComponent.class);
    if (itemFrom == null || (itemTo != null && !InventoryUtils.isSameStackId(itemFrom, itemTo))) {
        return false;
    }
    int countOnFrom = itemFrom.stackCount;
    if (amount > countOnFrom) {
        return false;
    }
    int countOnTo = 0;
    if (itemTo != null) {
        countOnTo = itemTo.stackCount;
    }
    if (countOnTo + amount > itemFrom.maxStackSize) {
        return false;
    }
    BeforeItemRemovedFromInventory removeFrom = new BeforeItemRemovedFromInventory(instigator, InventoryUtils.getItemAt(from, slotFrom), slotFrom);
    from.send(removeFrom);
    if (removeFrom.isConsumed()) {
        return false;
    }
    if (itemTo == null) {
        BeforeItemPutInInventory putTo = new BeforeItemPutInInventory(instigator, InventoryUtils.getItemAt(from, slotFrom), slotTo);
        to.send(putTo);
        if (putTo.isConsumed()) {
            return false;
        }
    }
    return true;
}
Also used : BeforeItemRemovedFromInventory(org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory) BeforeItemPutInInventory(org.terasology.logic.inventory.events.BeforeItemPutInInventory)

Example 2 with BeforeItemPutInInventory

use of org.terasology.logic.inventory.events.BeforeItemPutInInventory 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 3 with BeforeItemPutInInventory

use of org.terasology.logic.inventory.events.BeforeItemPutInInventory in project Terasology by MovingBlocks.

the class InventoryAuthoritySystem method canPutItemIntoSlot.

private boolean canPutItemIntoSlot(EntityRef instigator, EntityRef entity, EntityRef item, int slot) {
    if (!item.exists()) {
        return true;
    }
    BeforeItemPutInInventory itemPut = new BeforeItemPutInInventory(instigator, item, slot);
    entity.send(itemPut);
    return !itemPut.isConsumed();
}
Also used : BeforeItemPutInInventory(org.terasology.logic.inventory.events.BeforeItemPutInInventory)

Example 4 with BeforeItemPutInInventory

use of org.terasology.logic.inventory.events.BeforeItemPutInInventory in project Terasology by MovingBlocks.

the class InventoryUtils method validateMove.

private static boolean validateMove(EntityRef instigator, EntityRef from, int slotFrom, EntityRef to, int slotTo) {
    // Validate the move
    EntityRef itemFrom = InventoryUtils.getItemAt(from, slotFrom);
    EntityRef itemTo = InventoryUtils.getItemAt(to, slotTo);
    if (itemFrom.exists()) {
        BeforeItemRemovedFromInventory removeFrom = new BeforeItemRemovedFromInventory(instigator, itemFrom, slotFrom);
        from.send(removeFrom);
        if (removeFrom.isConsumed()) {
            return false;
        }
    }
    if (itemTo.exists()) {
        BeforeItemRemovedFromInventory removeTo = new BeforeItemRemovedFromInventory(instigator, itemTo, slotTo);
        to.send(removeTo);
        if (removeTo.isConsumed()) {
            return false;
        }
    }
    if (itemTo.exists()) {
        BeforeItemPutInInventory putFrom = new BeforeItemPutInInventory(instigator, itemTo, slotFrom);
        from.send(putFrom);
        if (putFrom.isConsumed()) {
            return false;
        }
    }
    if (itemFrom.exists()) {
        BeforeItemPutInInventory putTo = new BeforeItemPutInInventory(instigator, itemFrom, slotTo);
        to.send(putTo);
        if (putTo.isConsumed()) {
            return false;
        }
    }
    return true;
}
Also used : BeforeItemRemovedFromInventory(org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory) BeforeItemPutInInventory(org.terasology.logic.inventory.events.BeforeItemPutInInventory) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 5 with BeforeItemPutInInventory

use of org.terasology.logic.inventory.events.BeforeItemPutInInventory in project Terasology by MovingBlocks.

the class InventoryAuthoritySystemTest method addItemToEmptyWithVeto.

@Test
public void addItemToEmptyWithVeto() {
    ItemComponent itemComp = new ItemComponent();
    EntityRef item = Mockito.mock(EntityRef.class);
    setupItemRef(item, itemComp, 2, 10);
    Mockito.when(inventory.send(any(BeforeItemPutInInventory.class))).then(invocation -> {
        BeforeItemPutInInventory event = (BeforeItemPutInInventory) invocation.getArguments()[0];
        event.consume();
        return null;
    });
    GiveItemAction action = new GiveItemAction(instigator, item);
    inventoryAuthoritySystem.giveItem(action, inventory);
    Mockito.verify(item, atLeast(0)).getComponent(ItemComponent.class);
    Mockito.verify(item, atLeast(0)).exists();
    Mockito.verify(inventory, atLeast(0)).getComponent(InventoryComponent.class);
    Mockito.verify(inventory, times(5)).send(any(BeforeItemPutInInventory.class));
    Mockito.verifyNoMoreInteractions(instigator, inventory, entityManager, item);
    assertFalse(action.isConsumed());
}
Also used : BeforeItemPutInInventory(org.terasology.logic.inventory.events.BeforeItemPutInInventory) GiveItemAction(org.terasology.logic.inventory.action.GiveItemAction) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Aggregations

BeforeItemPutInInventory (org.terasology.logic.inventory.events.BeforeItemPutInInventory)6 EntityRef (org.terasology.entitySystem.entity.EntityRef)4 Test (org.junit.Test)2 BeforeItemRemovedFromInventory (org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory)2 GiveItemAction (org.terasology.logic.inventory.action.GiveItemAction)1