Search in sources :

Example 6 with BeforeItemPutInInventory

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

the class InventoryAuthoritySystemTest method testMoveItemToSlotsWithTargetVetos.

@Test
public void testMoveItemToSlotsWithTargetVetos() {
    int stackSize = 10;
    EntityRef toInventory = inventory;
    InventoryComponent toInventoryComp = toInventory.getComponent(InventoryComponent.class);
    EntityRef itemA1 = createItem("A", 8, stackSize);
    toInventoryComp.itemSlots.set(0, itemA1);
    EntityRef fromInventory = Mockito.mock(EntityRef.class);
    InventoryComponent fromInventoryComp = new InventoryComponent(5);
    Mockito.when(fromInventory.getComponent(InventoryComponent.class)).thenReturn(fromInventoryComp);
    EntityRef itemA2 = createItem("A", 5, stackSize);
    int fromSlot = 1;
    fromInventoryComp.itemSlots.set(fromSlot, itemA2);
    // Placement to slots 1 gets blocked by veto
    Mockito.when(inventory.send(any(BeforeItemPutInInventory.class))).then(invocation -> {
        Object arg = invocation.getArguments()[0];
        if (arg instanceof BeforeItemPutInInventory) {
            BeforeItemPutInInventory event = (BeforeItemPutInInventory) arg;
            if (event.getSlot() == 1) {
                event.consume();
            }
        }
        return null;
    });
    List<Integer> toSlots = Arrays.asList(0, 1, 2, 3, 4);
    // The method that gets tested:
    boolean result = inventoryAuthoritySystem.moveItemToSlots(instigator, fromInventory, fromSlot, toInventory, toSlots);
    assertTrue(result);
    /*
         * The free slot 1 can't be used since it's blocked:
         * => A1 gets still filled up and the rest of the items gets placed at slot 2
         */
    assertEquals(10, itemA1.getComponent(ItemComponent.class).stackCount);
    assertEquals(3, itemA2.getComponent(ItemComponent.class).stackCount);
    assertEquals(EntityRef.NULL, toInventoryComp.itemSlots.get(1));
    assertEquals(itemA2, toInventoryComp.itemSlots.get(2));
    assertFalse(fromInventoryComp.itemSlots.get(fromSlot).exists());
}
Also used : BeforeItemPutInInventory(org.terasology.logic.inventory.events.BeforeItemPutInInventory) 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