Search in sources :

Example 96 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class InventoryAuthoritySystemTest method testMoveItemToSlotsWithToLessSpaceInTargetSlots.

@Test
public void testMoveItemToSlotsWithToLessSpaceInTargetSlots() {
    int stackSize = 10;
    EntityRef toInventory = inventory;
    InventoryComponent toInventoryComp = toInventory.getComponent(InventoryComponent.class);
    EntityRef itemA1 = createItem("A", 8, stackSize);
    EntityRef itemB1 = createItem("B", 8, stackSize);
    EntityRef itemA2 = createItem("A", 7, stackSize);
    toInventoryComp.itemSlots.set(0, itemA1);
    toInventoryComp.itemSlots.set(2, itemB1);
    toInventoryComp.itemSlots.set(3, itemA2);
    EntityRef fromInventory = Mockito.mock(EntityRef.class);
    InventoryComponent fromInventoryComp = new InventoryComponent(5);
    Mockito.when(fromInventory.getComponent(InventoryComponent.class)).thenReturn(fromInventoryComp);
    EntityRef itemA3 = createItem("A", 4, stackSize);
    int fromSlot = 1;
    fromInventoryComp.itemSlots.set(fromSlot, itemA3);
    List<Integer> toSlots = Arrays.asList(0, 2);
    // The method that gets tested:
    boolean result = inventoryAuthoritySystem.moveItemToSlots(instigator, fromInventory, fromSlot, toInventory, toSlots);
    assertTrue(result);
    assertEquals(10, itemA1.getComponent(ItemComponent.class).stackCount);
    assertEquals(7, itemA2.getComponent(ItemComponent.class).stackCount);
    assertEquals(2, itemA3.getComponent(ItemComponent.class).stackCount);
    assertEquals(itemA3, fromInventoryComp.itemSlots.get(fromSlot));
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 97 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class InventoryAuthoritySystemTest method testMoveItemToSlotsWithSplittingToMultipleStacks.

@Test
public void testMoveItemToSlotsWithSplittingToMultipleStacks() {
    int stackSize = 10;
    EntityRef toInventory = inventory;
    InventoryComponent toInventoryComp = toInventory.getComponent(InventoryComponent.class);
    EntityRef itemA1 = createItem("A", 8, stackSize);
    EntityRef itemB1 = createItem("B", 8, stackSize);
    EntityRef itemA2 = createItem("A", 7, stackSize);
    toInventoryComp.itemSlots.set(0, itemA1);
    toInventoryComp.itemSlots.set(2, itemB1);
    toInventoryComp.itemSlots.set(3, itemA2);
    EntityRef fromInventory = Mockito.mock(EntityRef.class);
    InventoryComponent fromInventoryComp = new InventoryComponent(5);
    Mockito.when(fromInventory.getComponent(InventoryComponent.class)).thenReturn(fromInventoryComp);
    EntityRef itemA3 = createItem("A", 4, stackSize);
    int fromSlot = 1;
    fromInventoryComp.itemSlots.set(fromSlot, itemA3);
    List<Integer> toSlots = Arrays.asList(0, 1, 2, 3, 4);
    // The method that gets tested:
    inventoryAuthoritySystem.moveItemToSlots(instigator, fromInventory, fromSlot, toInventory, toSlots);
    assertEquals(10, itemA1.getComponent(ItemComponent.class).stackCount);
    assertEquals(9, itemA2.getComponent(ItemComponent.class).stackCount);
    assertFalse(fromInventoryComp.itemSlots.get(fromSlot).exists());
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 98 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class InventoryAuthoritySystemTest method testMoveItemToSlotsWithSplittingToMultipleStacksAndEmptySlot.

@Test
public void testMoveItemToSlotsWithSplittingToMultipleStacksAndEmptySlot() {
    int stackSize = 10;
    EntityRef toInventory = inventory;
    InventoryComponent toInventoryComp = toInventory.getComponent(InventoryComponent.class);
    EntityRef itemA1 = createItem("A", 8, stackSize);
    EntityRef itemB1 = createItem("B", 8, stackSize);
    EntityRef itemA2 = createItem("A", 7, stackSize);
    toInventoryComp.itemSlots.set(0, itemA1);
    toInventoryComp.itemSlots.set(2, itemB1);
    toInventoryComp.itemSlots.set(3, itemA2);
    EntityRef fromInventory = Mockito.mock(EntityRef.class);
    InventoryComponent fromInventoryComp = new InventoryComponent(5);
    Mockito.when(fromInventory.getComponent(InventoryComponent.class)).thenReturn(fromInventoryComp);
    EntityRef itemA3 = createItem("A", 8, stackSize);
    int fromSlot = 1;
    fromInventoryComp.itemSlots.set(fromSlot, itemA3);
    List<Integer> toSlots = Arrays.asList(0, 1, 2, 3, 4);
    // The method that gets tested:
    inventoryAuthoritySystem.moveItemToSlots(instigator, fromInventory, fromSlot, toInventory, toSlots);
    assertEquals(10, itemA1.getComponent(ItemComponent.class).stackCount);
    assertEquals(10, itemA2.getComponent(ItemComponent.class).stackCount);
    assertEquals(3, itemA3.getComponent(ItemComponent.class).stackCount);
    assertEquals(itemA3, toInventoryComp.itemSlots.get(1));
    assertFalse(fromInventoryComp.itemSlots.get(fromSlot).exists());
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 99 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef 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));
}
Also used : BeforeItemRemovedFromInventory(org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 100 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class InventoryAuthoritySystemTest method testMoveItemToSlotsWithFullTargetInventorySlots.

@Test
public void testMoveItemToSlotsWithFullTargetInventorySlots() {
    int stackSize = 10;
    EntityRef toInventory = inventory;
    InventoryComponent toInventoryComp = toInventory.getComponent(InventoryComponent.class);
    EntityRef itemA1 = createItem("A", 10, stackSize);
    EntityRef itemB1 = createItem("B", 8, stackSize);
    EntityRef itemA2 = createItem("A", 10, stackSize);
    toInventoryComp.itemSlots.set(0, itemA1);
    toInventoryComp.itemSlots.set(1, itemB1);
    toInventoryComp.itemSlots.set(2, itemA2);
    EntityRef fromInventory = Mockito.mock(EntityRef.class);
    InventoryComponent fromInventoryComp = new InventoryComponent(5);
    Mockito.when(fromInventory.getComponent(InventoryComponent.class)).thenReturn(fromInventoryComp);
    EntityRef itemA3 = createItem("A", 4, stackSize);
    int fromSlot = 1;
    fromInventoryComp.itemSlots.set(fromSlot, itemA3);
    List<Integer> toSlots = Arrays.asList(0, 1, 2);
    // The method that gets tested:
    boolean result = inventoryAuthoritySystem.moveItemToSlots(instigator, fromInventory, fromSlot, toInventory, toSlots);
    assertFalse(result);
    assertEquals(10, itemA1.getComponent(ItemComponent.class).stackCount);
    assertEquals(10, itemA2.getComponent(ItemComponent.class).stackCount);
    assertEquals(4, itemA3.getComponent(ItemComponent.class).stackCount);
    assertEquals(itemA3, fromInventoryComp.itemSlots.get(fromSlot));
}
Also used : EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)337 Test (org.junit.Test)106 ClientComponent (org.terasology.network.ClientComponent)49 LocationComponent (org.terasology.logic.location.LocationComponent)45 Vector3f (org.terasology.math.geom.Vector3f)44 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)36 Vector3i (org.terasology.math.geom.Vector3i)34 Command (org.terasology.logic.console.commandSystem.annotations.Command)28 StringComponent (org.terasology.entitySystem.stubs.StringComponent)26 NetworkComponent (org.terasology.network.NetworkComponent)21 EntityData (org.terasology.protobuf.EntityData)21 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)17 Block (org.terasology.world.block.Block)16 Component (org.terasology.entitySystem.Component)15 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)15 CharacterComponent (org.terasology.logic.characters.CharacterComponent)14 Quat4f (org.terasology.math.geom.Quat4f)14 BlockComponent (org.terasology.world.block.BlockComponent)13 Map (java.util.Map)11 LocalPlayer (org.terasology.logic.players.LocalPlayer)11