Search in sources :

Example 1 with GiveItemAction

use of org.terasology.logic.inventory.action.GiveItemAction in project Terasology by MovingBlocks.

the class InventoryAuthoritySystemTest method addItemToPartialAndOverflow.

@Test
public void addItemToPartialAndOverflow() {
    ItemComponent itemComp = new ItemComponent();
    EntityRef item = Mockito.mock(EntityRef.class);
    setupItemRef(item, itemComp, 2, 10);
    ItemComponent partialItemComp = new ItemComponent();
    EntityRef partialItem = Mockito.mock(EntityRef.class);
    setupItemRef(partialItem, partialItemComp, 9, 10);
    inventoryComp.itemSlots.set(0, partialItem);
    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(item, atLeast(0)).iterateComponents();
    Mockito.verify(item).saveComponent(itemComp);
    Mockito.verify(partialItem, atLeast(0)).getComponent(ItemComponent.class);
    Mockito.verify(partialItem, atLeast(0)).exists();
    Mockito.verify(partialItem, atLeast(0)).iterateComponents();
    Mockito.verify(partialItem).saveComponent(partialItemComp);
    Mockito.verify(inventory, atLeast(0)).getComponent(InventoryComponent.class);
    Mockito.verify(inventory).saveComponent(inventoryComp);
    Mockito.verify(inventory, times(1)).send(any(InventorySlotStackSizeChangedEvent.class));
    Mockito.verify(inventory, times(1)).send(any(InventorySlotChangedEvent.class));
    Mockito.verify(inventory, times(1)).send(any(BeforeItemPutInInventory.class));
    Mockito.verifyNoMoreInteractions(instigator, inventory, entityManager, item, partialItem);
    assertEquals(partialItem, inventoryComp.itemSlots.get(0));
    assertEquals(item, inventoryComp.itemSlots.get(1));
    assertEquals(10, partialItemComp.stackCount);
    assertEquals(1, itemComp.stackCount);
    assertTrue(action.isConsumed());
}
Also used : InventorySlotStackSizeChangedEvent(org.terasology.logic.inventory.events.InventorySlotStackSizeChangedEvent) BeforeItemPutInInventory(org.terasology.logic.inventory.events.BeforeItemPutInInventory) GiveItemAction(org.terasology.logic.inventory.action.GiveItemAction) EntityRef(org.terasology.entitySystem.entity.EntityRef) InventorySlotChangedEvent(org.terasology.logic.inventory.events.InventorySlotChangedEvent) Test(org.junit.Test)

Example 2 with GiveItemAction

use of org.terasology.logic.inventory.action.GiveItemAction 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)

Example 3 with GiveItemAction

use of org.terasology.logic.inventory.action.GiveItemAction in project Terasology by MovingBlocks.

the class InventoryAuthoritySystemTest method addItemToPartial.

@Test
public void addItemToPartial() {
    ItemComponent itemComp = new ItemComponent();
    EntityRef item = Mockito.mock(EntityRef.class);
    setupItemRef(item, itemComp, 2, 10);
    ItemComponent partialItemComp = new ItemComponent();
    EntityRef partialItem = Mockito.mock(EntityRef.class);
    setupItemRef(partialItem, partialItemComp, 2, 10);
    inventoryComp.itemSlots.set(0, partialItem);
    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(item, atLeast(0)).iterateComponents();
    Mockito.verify(item).destroy();
    Mockito.verify(partialItem, atLeast(0)).getComponent(ItemComponent.class);
    Mockito.verify(partialItem, atLeast(0)).exists();
    Mockito.verify(partialItem, atLeast(0)).iterateComponents();
    Mockito.verify(partialItem).saveComponent(partialItemComp);
    Mockito.verify(inventory, atLeast(0)).getComponent(InventoryComponent.class);
    Mockito.verify(inventory).send(any(InventorySlotStackSizeChangedEvent.class));
    Mockito.verifyNoMoreInteractions(instigator, inventory, entityManager, item, partialItem);
    assertEquals(partialItem, inventoryComp.itemSlots.get(0));
    assertEquals(4, partialItemComp.stackCount);
    assertTrue(action.isConsumed());
}
Also used : InventorySlotStackSizeChangedEvent(org.terasology.logic.inventory.events.InventorySlotStackSizeChangedEvent) GiveItemAction(org.terasology.logic.inventory.action.GiveItemAction) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 4 with GiveItemAction

use of org.terasology.logic.inventory.action.GiveItemAction in project Terasology by MovingBlocks.

the class InventoryAuthoritySystemTest method addItemToEmpty.

@Test
public void addItemToEmpty() {
    ItemComponent itemComp = new ItemComponent();
    EntityRef item = Mockito.mock(EntityRef.class);
    setupItemRef(item, itemComp, 2, 10);
    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(item).saveComponent(itemComp);
    Mockito.verify(inventory, atLeast(0)).getComponent(InventoryComponent.class);
    Mockito.verify(inventory).saveComponent(inventoryComp);
    Mockito.verify(inventory, times(1)).send(any(BeforeItemPutInInventory.class));
    Mockito.verify(inventory, times(1)).send(any(InventorySlotChangedEvent.class));
    Mockito.verifyNoMoreInteractions(instigator, inventory, entityManager, item);
    assertEquals(item, inventoryComp.itemSlots.get(0));
    assertTrue(action.isConsumed());
}
Also used : BeforeItemPutInInventory(org.terasology.logic.inventory.events.BeforeItemPutInInventory) GiveItemAction(org.terasology.logic.inventory.action.GiveItemAction) EntityRef(org.terasology.entitySystem.entity.EntityRef) InventorySlotChangedEvent(org.terasology.logic.inventory.events.InventorySlotChangedEvent) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 EntityRef (org.terasology.entitySystem.entity.EntityRef)4 GiveItemAction (org.terasology.logic.inventory.action.GiveItemAction)4 BeforeItemPutInInventory (org.terasology.logic.inventory.events.BeforeItemPutInInventory)3 InventorySlotChangedEvent (org.terasology.logic.inventory.events.InventorySlotChangedEvent)2 InventorySlotStackSizeChangedEvent (org.terasology.logic.inventory.events.InventorySlotStackSizeChangedEvent)2