use of org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory 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;
}
use of org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory 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;
}
use of org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory in project Terasology by MovingBlocks.
the class InventoryAuthoritySystemTest method removeWholeStackWithVeto.
@Test
public void removeWholeStackWithVeto() {
ItemComponent itemComp = new ItemComponent();
EntityRef item = Mockito.mock(EntityRef.class);
setupItemRef(item, itemComp, 2, 10);
inventoryComp.itemSlots.set(0, item);
Mockito.when(inventory.send(any(BeforeItemRemovedFromInventory.class))).then(invocation -> {
BeforeItemRemovedFromInventory event = (BeforeItemRemovedFromInventory) invocation.getArguments()[0];
event.consume();
return null;
});
RemoveItemAction action = new RemoveItemAction(instigator, item, true, 2);
inventoryAuthoritySystem.removeItem(action, inventory);
assertFalse(action.isConsumed());
assertNull(action.getRemovedItem());
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).send(any(BeforeItemRemovedFromInventory.class));
Mockito.verifyNoMoreInteractions(instigator, inventory, entityManager, item);
}
use of org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory in project Terasology by MovingBlocks.
the class InventoryAuthoritySystemTest method testMoveItemToSlotsWithRemovalVeto.
/**
* A shift click isn't possible because the removal of the item gets blocked
*/
@Test
public void testMoveItemToSlotsWithRemovalVeto() {
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(fromInventory.send(any(BeforeItemRemovedFromInventory.class))).then(invocation -> {
Object arg = invocation.getArguments()[0];
if (arg instanceof BeforeItemRemovedFromInventory) {
BeforeItemRemovedFromInventory event = (BeforeItemRemovedFromInventory) 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);
assertFalse(result);
assertEquals(8, itemA1.getComponent(ItemComponent.class).stackCount);
assertEquals(5, itemA2.getComponent(ItemComponent.class).stackCount);
assertEquals(EntityRef.NULL, toInventoryComp.itemSlots.get(1));
assertEquals(itemA2, fromInventoryComp.itemSlots.get(fromSlot));
}
use of org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory in project Terasology by MovingBlocks.
the class InventoryAuthoritySystem method canRemoveItemFromSlot.
private boolean canRemoveItemFromSlot(EntityRef instigator, EntityRef entity, EntityRef item, int slot) {
if (!item.exists()) {
return true;
}
BeforeItemRemovedFromInventory itemRemoved = new BeforeItemRemovedFromInventory(instigator, item, slot);
entity.send(itemRemoved);
return !itemRemoved.isConsumed();
}
Aggregations