Skip to content

Commit

Permalink
Do not use custom container for vanilla chests (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Dec 5, 2021
1 parent 647615a commit 9feb770
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public EnderChest(PlayerContext context, int num) {
this.context = context;
this.num = num;
this.updateRowCount();
this.container = new EnderChestInventory(this);
this.updateContainer();
}

/**
Expand Down Expand Up @@ -208,7 +208,7 @@ public void updateRowCount() {
}

/**
* Updates container of the chest if needed.
* Updates or creates custom container of the chest.
*/
public void updateContainer() {
if (this.container != null) {
Expand All @@ -225,6 +225,8 @@ public void updateContainer() {
if (needReload) {
this.container.reloadInventory();
}
} else {
this.container = new EnderChestInventory(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public void updateRowCount() {
this.rows = 3;
}

/**
* {@inheritDoc}
*/
@Override
public void updateContainer() {
// No custom container attached
}

/**
* Retrieves the offline player profile from server data.
* MUST be called in a synchronous way.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void setUp() throws ReflectiveOperationException, YamlFileLoadException,

@Test
public void initialState() {
assertThat(this.chest.container).isNotNull();
assertThat(this.chest.getNum()).isEqualTo(1);
assertThat(this.chest.getRows()).isEqualTo(3);
assertThat(this.chest.getMaxSize()).isEqualTo(27);
Expand Down Expand Up @@ -99,7 +100,7 @@ public void contents() {
verify(this.storage, times(2)).getEnderchestContents(any());

// With an existing and loaded container
ConcurrentMap<Integer, ItemStack> fakeContents = new ConcurrentHashMap<Integer, ItemStack>() {{
ConcurrentMap<Integer, ItemStack> fakeContents = new ConcurrentHashMap<>() {{
put(10, new ItemStack(Material.STONE));
put(15, new ItemStack(Material.FIREWORK_ROCKET));
}};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package fr.utarwyn.endercontainers.enderchest;

import fr.utarwyn.endercontainers.TestHelper;
import fr.utarwyn.endercontainers.enderchest.context.PlayerContext;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Arrays;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class VanillaEnderChestTest {

@Mock
private PlayerContext context;

@Mock
private Inventory chestInventory;

private VanillaEnderChest chest;

@Before
public void setUp() {
Player player = TestHelper.getPlayer();
UUID uuid = player.getUniqueId();

when(this.context.getOwner()).thenReturn(uuid);
when(this.context.getOwnerAsObject()).thenReturn(player);
when(player.getEnderChest()).thenReturn(this.chestInventory);

this.chest = new VanillaEnderChest(this.context);
}

@Test
public void initialize() {
assertThat(this.chest.container).isNull();
assertThat(this.chest.getNum()).isEqualTo(0);
assertThat(this.chest.getRows()).isEqualTo(3);
assertThat(this.chest.getMaxSize()).isEqualTo(27);
assertThat(this.chest.getOwner()).isEqualTo(TestHelper.getPlayer().getUniqueId());
}

@Test
public void updateRowCount() {
this.chest.updateRowCount();
assertThat(this.chest.getRows()).isEqualTo(3);
}

@Test
public void doNotUpdateContainer() {
this.chest.updateContainer();
assertThat(this.chest.container).isNull();
}

@Test
public void getSize() {
// Inventory without owner
when(this.context.getOwnerAsObject()).thenReturn(null);
assertThat((new VanillaEnderChest(this.context)).getSize()).isZero();

// Inventory without content
when(this.chestInventory.getContents()).thenReturn(new ItemStack[0]);
assertThat(this.chest.getSize()).isZero();

// Inventory with few items
ItemStack itemStack = mock(ItemStack.class);
when(this.chestInventory.getContents()).thenReturn(
Arrays.asList(itemStack, itemStack, null, itemStack).toArray(new ItemStack[0])
);
assertThat(this.chest.getSize()).isEqualTo(3);
}

@Test
public void openContainer() {
Player viewer = mock(Player.class);
this.chest.openContainerFor(viewer);
verify(viewer).openInventory(this.chestInventory);
}

}

0 comments on commit 9feb770

Please sign in to comment.