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());
}
Aggregations