Skip to content

Commit

Permalink
Open first chest if no permission for extra chests (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Mar 8, 2021
1 parent e185b9b commit 2b0571b
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ public void loadEnderchests(int count) {
* @param block Block which has triggered the opening, can be null
*/
public void openHubMenuFor(Player viewer, Block block) {
new EnderChestHubMenu(this).open(viewer);
if (this.getAccessibleChestCount() > 1) {
new EnderChestHubMenu(this).open(viewer);
} else {
this.openEnderchestFor(viewer, 0);
}

if (block != null && Files.getConfiguration().isGlobalSound()) {
MiscUtil.playSound(block.getLocation(), "CHEST_OPEN", "BLOCK_CHEST_OPEN");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import fr.utarwyn.endercontainers.configuration.wrapper.YamlFileLoadException;
import fr.utarwyn.endercontainers.mock.ItemFactoryMock;
import fr.utarwyn.endercontainers.mock.ItemMetaMock;
import fr.utarwyn.endercontainers.mock.v1_12.ServerMock;
import fr.utarwyn.endercontainers.mock.v1_15.ServerMock;
import org.bukkit.*;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -189,6 +189,7 @@ public static Player getPlayer() {

World world = mock(World.class);
Player player = mock(Player.class);
Inventory enderChest = mock(Inventory.class);

lenient().when(world.getName()).thenReturn("world");
lenient().when(player.getWorld()).thenReturn(world);
Expand All @@ -197,6 +198,9 @@ public static Player getPlayer() {
lenient().when(player.getName()).thenReturn("Utarwyn");
lenient().when(player.getServer()).thenReturn(Bukkit.getServer());
lenient().when(player.canSee(any())).thenReturn(true);
lenient().when(enderChest.getContents()).thenReturn(new ItemStack[0]);
lenient().when(player.getEnderChest()).thenReturn(enderChest);

lenient().when(Bukkit.getServer().getPlayer(playerIdentifier)).thenReturn(player);
lenient().when(Bukkit.getServer().getPlayer("Utarwyn")).thenReturn(player);
lenient().doReturn(Collections.singletonList(player)).when(Bukkit.getServer()).getOnlinePlayers();
Expand Down Expand Up @@ -245,6 +249,7 @@ private static void mockInventoryObjects(Server server) {
)).thenAnswer(answer -> {
Inventory inventory = mock(Inventory.class);
lenient().when(inventory.getContents()).thenReturn(new ItemStack[0]);
lenient().when(inventory.getHolder()).thenReturn(answer.getArgument(0, InventoryHolder.class));
return inventory;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ public void setUpClass() {
@Test
public void getVersion() {
// Should retrieve the package of the Server class.
// In testing env, the fake server is in a package named v1_12.
assertThat(ServerVersion.getBukkitVersion()).isEqualTo("v1_12");
assertThat(ServerVersion.get()).isEqualTo(V1_12);
// In testing env, the fake server is in a package named v1_15.
assertThat(ServerVersion.getBukkitVersion()).isEqualTo("v1_15");
assertThat(ServerVersion.get()).isEqualTo(V1_15);
}

@Test
public void comparison() {
// Equality
assertThat(ServerVersion.is(V1_8)).isFalse();
assertThat(ServerVersion.is(V1_12)).isTrue();
assertThat(ServerVersion.is(V1_15)).isFalse();
assertThat(ServerVersion.is(V1_12)).isFalse();
assertThat(ServerVersion.is(V1_15)).isTrue();
assertThat(ServerVersion.is(V1_16)).isFalse();

// Older
assertThat(ServerVersion.isOlderThan(V1_8)).isFalse();
assertThat(ServerVersion.isOlderThan(V1_12)).isFalse();
assertThat(ServerVersion.isOlderThan(V1_15)).isTrue();
assertThat(ServerVersion.isOlderThan(V1_15)).isFalse();
assertThat(ServerVersion.isOlderThan(V1_16)).isTrue();

// Newer
assertThat(ServerVersion.isNewerThan(V1_8)).isTrue();
assertThat(ServerVersion.isNewerThan(V1_12)).isFalse();
assertThat(ServerVersion.isNewerThan(V1_12)).isTrue();
assertThat(ServerVersion.isNewerThan(V1_15)).isFalse();
assertThat(ServerVersion.isNewerThan(V1_16)).isFalse();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package fr.utarwyn.endercontainers.enderchest.context;

import com.google.common.collect.Maps;
import fr.utarwyn.endercontainers.TestHelper;
import fr.utarwyn.endercontainers.configuration.wrapper.YamlFileLoadException;
import fr.utarwyn.endercontainers.menu.enderchest.EnderChestHubMenu;
import fr.utarwyn.endercontainers.storage.StorageManager;
import fr.utarwyn.endercontainers.storage.player.PlayerData;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.IOException;
import java.util.Collections;
import java.util.UUID;

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

@RunWith(MockitoJUnitRunner.class)
public class PlayerContextTest {

private static final int ENDERCHEST_AMOUNT = 27;

private PlayerContext context;

private Player player;

@Mock
private StorageManager storageManager;

@Mock
private PlayerData playerData;

@BeforeClass
public static void setUpClass() throws ReflectiveOperationException, YamlFileLoadException,
InvalidConfigurationException, IOException {
TestHelper.setUpFiles();
}

@Before
public void setUp() throws ReflectiveOperationException {
this.player = TestHelper.getPlayer();

TestHelper.registerManagers(this.storageManager);
when(this.storageManager.createPlayerDataStorage(this.player.getUniqueId())).thenReturn(this.playerData);
when(this.playerData.getEnderchestContents(any())).thenReturn(Maps.newConcurrentMap());

this.context = new PlayerContext(this.player.getUniqueId());
this.context.loadEnderchests(ENDERCHEST_AMOUNT);
}

@Test
public void create() {
assertThat(this.context.getOwner()).isEqualTo(this.player.getUniqueId());
assertThat(this.context.getOwnerAsObject()).isEqualTo(this.player);
assertThat(this.context.getData()).isEqualTo(this.playerData);
}

@Test
public void offlineOwner() {
assertThat(new PlayerContext(UUID.randomUUID()).getOwnerAsObject()).isNull();
}

@Test
public void getChest() {
assertThat(this.context.getChest(0)).isPresent();
assertThat(this.context.getChest(ENDERCHEST_AMOUNT - 1)).isPresent();
assertThat(this.context.getChest(ENDERCHEST_AMOUNT)).isNotPresent();
}

@Test
public void getAccessibleChestCount() {
assertThat(this.context.getAccessibleChestCount()).isEqualTo(1);
when(this.player.isOp()).thenReturn(true);
assertThat(this.context.getAccessibleChestCount()).isEqualTo(ENDERCHEST_AMOUNT);
}

@Test
public void isChestsUnused() {
assertThat(this.context.isChestsUnused()).isTrue();

when(this.player.getEnderChest().getViewers()).thenReturn(Collections.singletonList(this.player));
assertThat(this.context.isChestsUnused()).isFalse();
}

@Test
public void openHubMenu() {
// With permission, main menu has to be opened
when(this.player.isOp()).thenReturn(true);
this.context.openHubMenuFor(this.player);

ArgumentCaptor<Inventory> captor = ArgumentCaptor.forClass(Inventory.class);
verify(this.player).openInventory(captor.capture());
assertThat(captor.getValue().getHolder()).isInstanceOf(EnderChestHubMenu.class);
}

@Test
public void openHubMenuWithOneChest() {
// If only 1 chest accessible, open it directly
this.context.openHubMenuFor(this.player);
verify(this.player).openInventory(this.player.getEnderChest());
}

@Test
public void openHubMenuSound() {
Block block = mock(Block.class);
Location location = mock(Location.class);
World world = mock(World.class);

when(block.getLocation()).thenReturn(location);
when(location.getWorld()).thenReturn(world);

this.context.openHubMenuFor(this.player, block);

verify(world).playSound(eq(location), eq(Sound.BLOCK_CHEST_OPEN), anyFloat(), anyFloat());
}

@Test
public void openEnderchest() {
assertThat(this.context.openEnderchestFor(this.player, 0)).isTrue();
assertThat(this.context.openEnderchestFor(this.player, 2)).isFalse();
when(this.player.isOp()).thenReturn(true);
assertThat(this.context.openEnderchestFor(this.player, 2)).isTrue();
assertThat(this.context.openEnderchestFor(this.player, ENDERCHEST_AMOUNT)).isFalse();
}

@Test
public void save() {
this.context.save();
verify(this.playerData).saveContext(any());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class EnchantmentMock extends Enchantment {
private final String name;

public EnchantmentMock(String name) {
super(NamespacedKey.randomKey());
super(NamespacedKey.minecraft(name.toLowerCase()));
this.name = name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ public class ItemFactoryMock implements ItemFactory {

@Override
public ItemMeta getItemMeta(Material material) {
return new ItemMetaMock();
if (material == Material.PLAYER_HEAD) {
return new SkullItemMetaMock();
} else {
return new ItemMetaMock();
}
}

@Override
Expand All @@ -40,7 +44,11 @@ public ItemMeta asMetaFor(ItemMeta meta, ItemStack stack) throws IllegalArgument

@Override
public ItemMeta asMetaFor(ItemMeta meta, Material material) throws IllegalArgumentException {
return new ItemMetaMock(meta);
if (material == Material.PLAYER_HEAD) {
return new SkullItemMetaMock(meta);
} else {
return new ItemMetaMock(meta);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package fr.utarwyn.endercontainers.mock;

import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;

public class SkullItemMetaMock extends ItemMetaMock implements SkullMeta {

public SkullItemMetaMock() {
super();
}

public SkullItemMetaMock(ItemMeta meta) {
super(meta);
}

@Override
public String getOwner() {
return null;
}

@Override
public boolean hasOwner() {
return false;
}

@Override
public boolean setOwner(String owner) {
return false;
}

@Override
public OfflinePlayer getOwningPlayer() {
return null;
}

@Override
public boolean setOwningPlayer(OfflinePlayer owner) {
return false;
}

@Override
public SkullItemMetaMock clone() {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.utarwyn.endercontainers.mock.v1_12;
package fr.utarwyn.endercontainers.mock.v1_15;

import org.bukkit.Server;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.utarwyn.endercontainers.TestHelper;
import fr.utarwyn.endercontainers.mock.EnchantmentMock;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
Expand All @@ -21,6 +22,8 @@ public class LegacyItemSerializerTest {

private ItemSerializer serializer;

private Enchantment enchantment;

@BeforeClass
public static void setUpClass() {
TestHelper.setUpServer();
Expand All @@ -29,6 +32,7 @@ public static void setUpClass() {
@Before
public void setUp() {
this.serializer = new LegacyItemSerializer();
this.enchantment = new EnchantmentMock("DAMAGE_ALL");
}

@Test
Expand All @@ -40,7 +44,7 @@ public void serialize() throws IOException {
if (logMeta != null) {
((Damageable) logMeta).setDamage(5);
logMeta.setDisplayName("AWESOME TEST LOG @#");
logMeta.addEnchant(new EnchantmentMock("DAMAGE_ALL"), 1, false);
logMeta.addEnchant(this.enchantment, 1, false);
logMeta.setLore(Arrays.asList("first line", "second line"));
log.setItemMeta(logMeta);
}
Expand All @@ -49,7 +53,7 @@ public void serialize() throws IOException {
map.put(17, new ItemStack(Material.GRASS, 20));

assertThat(this.serializer.serialize(map)).isNotNull()
.isEqualTo("2;1#t@OAK_LOG:d@5:a@10:e@DAMAGE_ALL@1:n@AWESOME=TEST=LOG=\\\\@\\\\#=" +
.isEqualTo("2;1#t@OAK_LOG:d@5:a@10:e@minecraft!damage_all@1:n@AWESOME=TEST=LOG=\\\\@\\\\#=" +
":l@first line=second line=;17#t@GRASS:a@20:l@;");
}

Expand All @@ -67,7 +71,7 @@ public void deserialize() throws IOException {
if (grassMeta != null) {
((Damageable) grassMeta).setDamage(2);
grassMeta.setDisplayName("AMAZING GRASS");
grassMeta.addEnchant(new EnchantmentMock("DAMAGE_ALL"), 1, false);
grassMeta.addEnchant(this.enchantment, 1, false);
grassMeta.setLore(Arrays.asList("first line", "second line"));
grass.setItemMeta(grassMeta);
}
Expand Down

0 comments on commit 2b0571b

Please sign in to comment.