Skip to content

Commit

Permalink
Merge pull request #72 from TheBusyBiscuit/feature/storage-contents
Browse files Browse the repository at this point in the history
Implemented Inventory#getStorageContents()
  • Loading branch information
seeseemelk committed May 24, 2020
2 parents 742f0ba + d5b7039 commit c79a603
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,13 @@ public HashMap<Integer, ItemStack> removeItem(ItemStack... items) throws Illegal
@Override
public ItemStack[] getStorageContents()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return getContents();
}

@Override
public void setStorageContents(ItemStack[] items) throws IllegalArgumentException
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
setContents(items);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import be.seeseemelk.mockbukkit.UnimplementedOperationException;

public class PlayerInventoryMock extends InventoryMock implements PlayerInventory
{
protected static final int HOTBAR = 0;
Expand All @@ -33,6 +35,19 @@ public HumanEntity getHolder()
return (HumanEntity) super.getHolder();
}

@Override
public ItemStack[] getStorageContents()
{
return Arrays.copyOfRange(getContents(), 0, 36);
}

@Override
public void setStorageContents(ItemStack[] items) throws IllegalArgumentException
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException("setStorageContests has not been implemented for Player Inventories");
}

@Override
public ItemStack[] getArmorContents()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertArrayEquals;

import java.util.HashMap;
import java.util.ListIterator;
Expand Down Expand Up @@ -42,11 +43,11 @@ public void constructor_SetsSize()
assertEquals(9, new SimpleInventoryMock(null, 9, InventoryType.CHEST).getSize());
assertEquals(18, new SimpleInventoryMock(null, 18, InventoryType.CHEST).getSize());
}

@Test
public void constructor_SetsType()
{
assertEquals(InventoryType.CHEST, new SimpleInventoryMock(null, 9, InventoryType.CHEST).getType());
assertEquals(InventoryType.CHEST, new SimpleInventoryMock(null, 9, InventoryType.CHEST).getType());
assertEquals(InventoryType.DROPPER, new SimpleInventoryMock(null, 9, InventoryType.DROPPER).getType());
}

Expand All @@ -61,46 +62,46 @@ public void getItem_Default_AllAir()
}
}

@Test
public void testClearInventory()
{
for (int i = 0; i < inventory.getSize(); i++)
{
inventory.addItem(new ItemStack(Material.DIRT, 64));
}
inventory.clear();
for (int i = 0; i < inventory.getSize(); i++)
{
ItemStack item = inventory.getItem(i);
assertNotNull(item);
assertEquals(Material.AIR, item.getType());
}
}

@Test
public void testClearSlot()
{
inventory.setItem(0, new ItemStack(Material.DIAMOND));
assertEquals(Material.DIAMOND, inventory.getItem(0).getType());

inventory.clear(0);
assertEquals(Material.AIR, inventory.getItem(0).getType());
}

@Test
public void testFirstEmpty()
{
for (int i = 0; i < inventory.getSize(); i++)
{
inventory.addItem(new ItemStack(Material.DIRT, 64));
}
assertEquals(-1, inventory.firstEmpty());
inventory.clear();
assertEquals(0, inventory.firstEmpty());
}
@Test
public void testClearInventory()
{
for (int i = 0; i < inventory.getSize(); i++)
{
inventory.addItem(new ItemStack(Material.DIRT, 64));
}

inventory.clear();

for (int i = 0; i < inventory.getSize(); i++)
{
ItemStack item = inventory.getItem(i);
assertNotNull(item);
assertEquals(Material.AIR, item.getType());
}
}

@Test
public void testClearSlot()
{
inventory.setItem(0, new ItemStack(Material.DIAMOND));
assertEquals(Material.DIAMOND, inventory.getItem(0).getType());

inventory.clear(0);
assertEquals(Material.AIR, inventory.getItem(0).getType());
}

@Test
public void testFirstEmpty()
{
for (int i = 0; i < inventory.getSize(); i++)
{
inventory.addItem(new ItemStack(Material.DIRT, 64));
}

assertEquals(-1, inventory.firstEmpty());
inventory.clear();
assertEquals(0, inventory.firstEmpty());
}

@Test
public void addItem_EmptyInventoryAddsOneStack_OneStackUsed()
Expand Down Expand Up @@ -186,7 +187,8 @@ public void setContents_OneItemAndOneNull_SetAndRestCleared()

ItemStack item = new ItemStack(Material.DIRT, 32);

inventory.setContents(new ItemStack[] { item });
inventory.setContents(new ItemStack[]
{ item });

assertTrue(item.isSimilar(inventory.getItem(0)));
for (int i = 1; i < inventory.getSize(); i++)
Expand All @@ -200,7 +202,8 @@ public void setContents_OneItemAndOneNull_SetAndRestCleared()
@Test
public void setContents_ArrayWithNulls_NullsIgnores()
{
inventory.setContents(new ItemStack[] { null });
inventory.setContents(new ItemStack[]
{ null });
}

@Test
Expand Down Expand Up @@ -234,8 +237,7 @@ public void assertTrueForNonNulls_NumberOfExecutionsOnInventoryOneItem_EqualToOn
{
inventory.addItem(new ItemStack(Material.DIRT, 1));
AtomicInteger calls = new AtomicInteger(0);
inventory.assertTrueForNonNulls(itemstack ->
{
inventory.assertTrueForNonNulls(itemstack -> {
calls.incrementAndGet();
return true;
});
Expand Down Expand Up @@ -290,4 +292,10 @@ public void assertContainsAtLeast_DoesNotContainEnough_Asserts()
inventory.addItem(new ItemStack(Material.GRASS, 3));
inventory.assertContainsAtLeast(new ItemStack(Material.DIRT), 4);
}

@Test
public void testContentsAndStorageContentsEqual()
{
assertArrayEquals(inventory.getContents(), inventory.getStorageContents());
}
}

0 comments on commit c79a603

Please sign in to comment.