From 3544c3b79e34dc9551f63d9412a158585d9ce9ee Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 6 Jul 2021 14:47:56 -0700 Subject: [PATCH 01/39] Version 1.11.1 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 41a680c..01f06cd 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ -LOCAL - 1.11.0 + 1.11.1 BentoBoxWorld_addon-invSwitcher bentobox-world From 4e9c944d047bfaa98b7d9752c627a17805a40391 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 31 Jul 2021 14:51:41 -0700 Subject: [PATCH 02/39] Remove unused location setting --- .../com/wasteofplastic/invswitcher/Settings.java | 16 +--------------- .../com/wasteofplastic/invswitcher/Store.java | 9 --------- src/main/resources/config.yml | 1 - 3 files changed, 1 insertion(+), 25 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Settings.java b/src/main/java/com/wasteofplastic/invswitcher/Settings.java index b808437..247e802 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Settings.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Settings.java @@ -30,13 +30,11 @@ public class Settings implements ConfigObject { private boolean gamemode = true; @ConfigEntry(path = "options.experience") private boolean experience = true; - @ConfigEntry(path = "options.location") - private boolean location = true; @ConfigEntry(path = "options.ender-chest") private boolean enderChest = true; @ConfigEntry(path = "options.statistics") private boolean statistics = true; - + /** * @return the worlds */ @@ -121,18 +119,6 @@ public boolean isExperience() { public void setExperience(boolean experience) { this.experience = experience; } - /** - * @return the location - */ - public boolean isLocation() { - return location; - } - /** - * @param location the location to set - */ - public void setLocation(boolean location) { - this.location = location; - } /** * @return the enderChest */ diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 76370a1..0edae64 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -86,8 +86,6 @@ public void getInventory(Player player, World world) { InventoryStorage store = getInv(player); // Do not differentiate between world environments. Only the location is different - String worldName = world.getName(); - String overworldName = (world.getName().replace("_the_end", "")).replace("_nether", ""); // Inventory @@ -137,10 +135,6 @@ public void getInventory(Player player, World world) { } }); } - if (addon.getSettings().isLocation()) { - // Get Spawn Point - store.getLocation(worldName); - } if (addon.getSettings().isEnderChest()) { player.getEnderChest().setContents(store.getEnderChest(overworldName).toArray(new ItemStack[0])); } @@ -211,9 +205,6 @@ public void storeAndSave(Player player, World world) { if (addon.getSettings().isExperience()) { store.setExp(overworldName, getTotalExperience(player)); } - if (addon.getSettings().isLocation()) { - store.setLocation(worldName, player.getLocation()); - } if (addon.getSettings().isGamemode()) { store.setGameMode(overworldName, player.getGameMode()); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index d180f1c..c8f9085 100755 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -16,6 +16,5 @@ options: advancements: true gamemode: true experience: true - location: true ender-chest: true statistics: true From bb50aed1903e100cf3203e56af181c1b2b61e092 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 31 Jul 2021 15:02:56 -0700 Subject: [PATCH 03/39] Clean up code. --- .../invswitcher/InvSwitcher.java | 1 - .../com/wasteofplastic/invswitcher/Store.java | 86 ++++++++++++------- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java b/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java index ecb6300..722b0fb 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java +++ b/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java @@ -75,7 +75,6 @@ public void onEnable() { if (this.getPlugin().getSettings().getDatabaseType().equals(DatabaseType.YAML)) { this.setState(State.DISABLED); this.logError("This addon is incompatible with YAML database. Please use another type, like JSON."); - return; } } diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 0edae64..e0f0762 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -46,6 +46,7 @@ import com.wasteofplastic.invswitcher.dataObjects.InventoryStorage; import world.bentobox.bentobox.database.Database; +import world.bentobox.bentobox.util.Util; /** * Enables inventory switching between games. Handles food, experience and spawn points. @@ -53,6 +54,8 @@ * */ public class Store { + private static final CharSequence THE_END = "_the_end"; + private static final CharSequence NETHER = "_nether"; private final Database database; private final Map cache; private final InvSwitcher addon; @@ -72,7 +75,7 @@ public Store(InvSwitcher addon) { public boolean isWorldStored(Player player, World world) { // Get the store InventoryStorage store = getInv(player); - String overworldName = (world.getName().replace("_the_end", "")).replace("_nether", ""); + String overworldName = (world.getName().replace(THE_END, "")).replace(NETHER, ""); return store.isInventory(overworldName); } @@ -85,34 +88,18 @@ public void getInventory(Player player, World world) { // Get the store InventoryStorage store = getInv(player); - // Do not differentiate between world environments. Only the location is different - String overworldName = (world.getName().replace("_the_end", "")).replace("_nether", ""); + // Do not differentiate between world environments. + String overworldName = Util.getWorld(world).getName(); // Inventory if (addon.getSettings().isInventory()) { player.getInventory().setContents(store.getInventory(overworldName).toArray(new ItemStack[0])); } if (addon.getSettings().isHealth()) { - // Health - double health = store.getHealth().getOrDefault(overworldName, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()); - - if (health > player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()) { - health = player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); - } - if (health < 0D) { - health = 0D; - } - player.setHealth(health); + setHeath(store, player, overworldName); } if (addon.getSettings().isFood()) { - // Food - int food = store.getFood().getOrDefault(overworldName, 20); - if (food > 20) { - food = 20; - } else if (food < 0) { - food = 0; - } - player.setFoodLevel(food); + setFood(store, player, overworldName); } if (addon.getSettings().isExperience()) { // Experience @@ -123,17 +110,7 @@ public void getInventory(Player player, World world) { player.setGameMode(store.getGameMode(overworldName)); } if (addon.getSettings().isAdvancements()) { - // Advancements - store.getAdvancements(overworldName).forEach((k, v) -> { - Iterator it = Bukkit.advancementIterator(); - while (it.hasNext()) { - Advancement a = it.next(); - if (a.getKey().toString().equals(k)) { - // Award - v.forEach(player.getAdvancementProgress(a)::awardCriteria); - } - } - }); + setAdvancements(store, player, overworldName); } if (addon.getSettings().isEnderChest()) { player.getEnderChest().setContents(store.getEnderChest(overworldName).toArray(new ItemStack[0])); @@ -143,6 +120,47 @@ public void getInventory(Player player, World world) { } } + private void setHeath(InventoryStorage store, Player player, String overworldName) { + // Health + double health = store.getHealth().getOrDefault(overworldName, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()); + + if (health > player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()) { + health = player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); + } + if (health < 0D) { + health = 0D; + } + player.setHealth(health); + + } + + private void setFood(InventoryStorage store, Player player, String overworldName) { + // Food + int food = store.getFood().getOrDefault(overworldName, 20); + if (food > 20) { + food = 20; + } else if (food < 0) { + food = 0; + } + player.setFoodLevel(food); + + } + + private void setAdvancements(InventoryStorage store, Player player, String overworldName) { + // Advancements + store.getAdvancements(overworldName).forEach((k, v) -> { + Iterator it = Bukkit.advancementIterator(); + while (it.hasNext()) { + Advancement a = it.next(); + if (a.getKey().toString().equals(k)) { + // Award + v.forEach(player.getAdvancementProgress(a)::awardCriteria); + } + } + }); + + } + public void removeFromCache(Player player) { cache.remove(player.getUniqueId()); } @@ -190,7 +208,7 @@ public void storeAndSave(Player player, World world) { InventoryStorage store = getInv(player); // Do not differentiate between world environments String worldName = world.getName(); - String overworldName = (world.getName().replace("_the_end", "")).replace("_nether", ""); + String overworldName = (world.getName().replace(THE_END, "")).replace(NETHER, ""); if (addon.getSettings().isInventory()) { // Copy the player's items to the store List contents = Arrays.asList(player.getInventory().getContents()); @@ -247,6 +265,7 @@ private void saveStats(InventoryStorage store, Player player, String worldName) if (!map.isEmpty()) { store.getBlockStats(worldName).put(s, map); } + break; case ITEM: map = Arrays.stream(Material.values()).filter(Material::isItem) .filter(m -> !m.isLegacy()) @@ -343,6 +362,7 @@ private void clearPlayer(Player player) { player.setStatistic(s, m, 0); } } + break; case ITEM: for (Material m: Material.values()) { if (m.isItem() && !m.isLegacy()) { From c5cff4d56193422caee210cbb3189ed51a2f0940 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 31 Jul 2021 15:08:49 -0700 Subject: [PATCH 04/39] Fixed test. --- .../java/com/wasteofplastic/invswitcher/StoreTest.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java index e341afc..2b43920 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java @@ -1,6 +1,7 @@ package com.wasteofplastic.invswitcher; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -25,6 +26,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; @@ -32,13 +34,14 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType; +import world.bentobox.bentobox.util.Util; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class}) +@PrepareForTest({Bukkit.class, Util.class}) public class StoreTest { @Mock @@ -82,6 +85,10 @@ public void setUp() { sets = new com.wasteofplastic.invswitcher.Settings(); when(addon.getSettings()).thenReturn(sets); when(addon.getWorlds()).thenReturn(Collections.singleton(world)); + + PowerMockito.mockStatic(Util.class); + when(Util.getWorld(eq(world))).thenReturn(world); + when(Util.getWorld(eq(fromWorld))).thenReturn(fromWorld); } @After From 9af70f533ce6bdc624d31fe2a776a452b55cf35e Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 1 Oct 2021 17:47:16 -0700 Subject: [PATCH 05/39] Build against BentoBox 1.18 API --- pom.xml | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index 01f06cd..fd85961 100644 --- a/pom.xml +++ b/pom.xml @@ -60,7 +60,7 @@ 2.0.9 1.16.5-R0.1-SNAPSHOT - 1.16.2 + 1.18.0-SNAPSHOT ${build.version}-SNAPSHOT @@ -113,30 +113,6 @@ - - sonar - - https://sonarcloud.io - bentobox-world - - - - - org.sonarsource.scanner.maven - sonar-maven-plugin - 3.6.0.1398 - - - verify - - sonar - - - - - - - From 87131159558b9ed8a5dfb1912015fa1acfd38592 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 20 Mar 2022 14:50:00 +0000 Subject: [PATCH 06/39] Not allow null. --- src/main/java/com/wasteofplastic/invswitcher/Store.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index e0f0762..bcb2128 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.UUID; import java.util.stream.Collectors; @@ -89,7 +90,7 @@ public void getInventory(Player player, World world) { InventoryStorage store = getInv(player); // Do not differentiate between world environments. - String overworldName = Util.getWorld(world).getName(); + String overworldName = Objects.requireNonNull(Util.getWorld(world)).getName(); // Inventory if (addon.getSettings().isInventory()) { From 737e493bdf04592b28a2ec975047380cfbaca066 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 20 Mar 2022 14:53:13 +0000 Subject: [PATCH 07/39] Remove useless eq invocation --- src/test/java/com/wasteofplastic/invswitcher/StoreTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java index 2b43920..c8905cd 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java @@ -1,7 +1,6 @@ package com.wasteofplastic.invswitcher; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -87,8 +86,8 @@ public void setUp() { when(addon.getWorlds()).thenReturn(Collections.singleton(world)); PowerMockito.mockStatic(Util.class); - when(Util.getWorld(eq(world))).thenReturn(world); - when(Util.getWorld(eq(fromWorld))).thenReturn(fromWorld); + when(Util.getWorld(world)).thenReturn(world); + when(Util.getWorld(fromWorld)).thenReturn(fromWorld); } @After From 1c31ebdbb44319d011c9acdd8d0748f7e4ba16c2 Mon Sep 17 00:00:00 2001 From: BONNe Date: Fri, 6 May 2022 19:27:31 +0300 Subject: [PATCH 08/39] Fixes major bug in InvSwitcher addon (#22) There was a huge bug that always cleared a separated value, even if that is disabled in the config file. The most important bug: if `inventory` or `ender-chest` separation is disabled, player inventories and ender chests would still be cleared without the ability to restore them. --- .../com/wasteofplastic/invswitcher/Store.java | 109 +++++++++++------- 1 file changed, 69 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index bcb2128..8104200 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -344,49 +344,78 @@ private void getStats(InventoryStorage store, Player player, String worldName) { @SuppressWarnings("deprecation") private void clearPlayer(Player player) { - // Clear the player's inventory - player.getInventory().clear(); - setTotalExperience(player, 0); - Iterator it = Bukkit.advancementIterator(); - while (it.hasNext()) { - Advancement a = it.next(); - AdvancementProgress p = player.getAdvancementProgress(a); - p.getAwardedCriteria().forEach(p::revokeCriteria); - } - player.getEnderChest().clear(); - // Statistics - Arrays.stream(Statistic.values()).forEach(s -> { - switch(s.getType()) { - case BLOCK: - for (Material m: Material.values()) { - if (m.isBlock() && !m.isLegacy()) { - player.setStatistic(s, m, 0); - } - } - break; - case ITEM: - for (Material m: Material.values()) { - if (m.isItem() && !m.isLegacy()) { - player.setStatistic(s, m, 0); - } - } - break; - case ENTITY: - for (EntityType en: EntityType.values()) { - if (en.isAlive()) { - player.setStatistic(s, en, 0); - } - } - break; - case UNTYPED: - player.setStatistic(s, 0); - break; - default: - break; + if (this.addon.getSettings().isInventory()) + { + // Clear the player's inventory + player.getInventory().clear(); + } + + if (this.addon.getSettings().isExperience()) + { + // Reset experience + setTotalExperience(player, 0); + } + if (this.addon.getSettings().isAdvancements()) + { + // Reset advancements + Iterator it = Bukkit.advancementIterator(); + while (it.hasNext()) + { + Advancement a = it.next(); + AdvancementProgress p = player.getAdvancementProgress(a); + p.getAwardedCriteria().forEach(p::revokeCriteria); } + } - }); + if (this.addon.getSettings().isEnderChest()) + { + // Reset enderchest + player.getEnderChest().clear(); + } + + if (this.addon.getSettings().isStatistics()) + { + // Reset Statistics + Arrays.stream(Statistic.values()).forEach(s -> + { + switch (s.getType()) + { + case BLOCK: + for (Material m : Material.values()) + { + if (m.isBlock() && !m.isLegacy()) + { + player.setStatistic(s, m, 0); + } + } + break; + case ITEM: + for (Material m : Material.values()) + { + if (m.isItem() && !m.isLegacy()) + { + player.setStatistic(s, m, 0); + } + } + break; + case ENTITY: + for (EntityType en : EntityType.values()) + { + if (en.isAlive()) + { + player.setStatistic(s, en, 0); + } + } + break; + case UNTYPED: + player.setStatistic(s, 0); + break; + default: + break; + } + }); + } } //new Exp Math from 1.8 From aeaeb84176546e91613a4f0f79671e5f41e0c9e6 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 20:21:23 -0800 Subject: [PATCH 09/39] Remove unused method --- .../invswitcher/dataObjects/InventoryStorage.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java b/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java index ef0575c..5c30a34 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java +++ b/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java @@ -174,12 +174,6 @@ public boolean isInventory(String overworldName) { return inventory != null && inventory.containsKey(overworldName); } - public void getLocation(String worldName) { - if (location != null) { - location.get(worldName); - } - } - public void setGameMode(String worldName, GameMode gameMode) { this.gameMode.put(worldName, gameMode); } From 79c1dc2f8c9d00ee005e83272e5f2b4b7654694b Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 21:08:50 -0800 Subject: [PATCH 10/39] Version up Switch to Java 17 --- pom.xml | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index fd85961..923c754 100644 --- a/pom.xml +++ b/pom.xml @@ -55,7 +55,7 @@ UTF-8 UTF-8 - 8 + 17 2.0.9 @@ -66,7 +66,7 @@ -LOCAL - 1.11.1 + 1.12.0 BentoBoxWorld_addon-invSwitcher bentobox-world @@ -213,7 +213,34 @@ 3.0.0-M5 - --illegal-access=permit + ${argLine} + --add-opens java.base/java.lang=ALL-UNNAMED + --add-opens java.base/java.math=ALL-UNNAMED + --add-opens java.base/java.io=ALL-UNNAMED + --add-opens java.base/java.util=ALL-UNNAMED + --add-opens + java.base/java.util.stream=ALL-UNNAMED + --add-opens java.base/java.text=ALL-UNNAMED + --add-opens + java.base/java.util.regex=ALL-UNNAMED + --add-opens + java.base/java.nio.channels.spi=ALL-UNNAMED + --add-opens java.base/sun.nio.ch=ALL-UNNAMED + --add-opens java.base/java.net=ALL-UNNAMED + --add-opens + java.base/java.util.concurrent=ALL-UNNAMED + --add-opens java.base/sun.nio.fs=ALL-UNNAMED + --add-opens java.base/sun.nio.cs=ALL-UNNAMED + --add-opens java.base/java.nio.file=ALL-UNNAMED + --add-opens + java.base/java.nio.charset=ALL-UNNAMED + --add-opens + java.base/java.lang.reflect=ALL-UNNAMED + --add-opens + java.logging/java.util.logging=ALL-UNNAMED + --add-opens java.base/java.lang.ref=ALL-UNNAMED + --add-opens java.base/java.util.jar=ALL-UNNAMED + --add-opens java.base/java.util.zip=ALL-UNNAMED @@ -256,7 +283,7 @@ org.jacoco jacoco-maven-plugin - 0.8.3 + 0.8.7 true @@ -267,16 +294,21 @@ - pre-unit-test + prepare-agent prepare-agent - post-unit-test + report report + + + XML + + From cd66b14889bb4889efbc0ea6c210cfb1f0eef292 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 21:11:29 -0800 Subject: [PATCH 11/39] Added InventoryStorage test class. --- .../dataObjects/InventoryStorageTest.java | 381 ++++++++++++++++++ 1 file changed, 381 insertions(+) create mode 100644 src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java diff --git a/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java b/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java new file mode 100644 index 0000000..ab588b4 --- /dev/null +++ b/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java @@ -0,0 +1,381 @@ +package com.wasteofplastic.invswitcher.dataObjects; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Statistic; +import org.bukkit.entity.EntityType; +import org.bukkit.inventory.ItemStack; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +public class InventoryStorageTest { + + private InventoryStorage is; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + is = new InventoryStorage(); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getUniqueId()}. + */ + @Test + public void testGetUniqueId() { + assertNull(is.getUniqueId()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setUniqueId(java.lang.String)}. + */ + @Test + public void testSetUniqueId() { + String uuid = UUID.randomUUID().toString(); + is.setUniqueId(uuid); + assertEquals(uuid, is.getUniqueId()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getInventory()}. + */ + @Test + public void testGetInventory() { + assertTrue(is.getInventory().isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getHealth()}. + */ + @Test + public void testGetHealth() { + assertTrue(is.getHealth().isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getFood()}. + */ + @Test + public void testGetFood() { + assertTrue(is.getFood().isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getExp()}. + */ + @Test + public void testGetExp() { + assertTrue(is.getExp().isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getLocation()}. + */ + @Test + public void testGetLocation() { + assertTrue(is.getLocation().isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setInventory(java.util.Map)}. + */ + @Test + public void testSetInventoryMapOfStringListOfItemStack() { + ItemStack item = new ItemStack(Material.ACACIA_BOAT); + is.setInventory(Map.of("test", List.of(item))); + Map> map = is.getInventory(); + assertEquals(item, map.get("test").get(0)); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setInventory(java.lang.String, java.util.List)}. + */ + @Test + public void testSetInventoryStringListOfItemStack() { + ItemStack item = new ItemStack(Material.ACACIA_BOAT); + is.setInventory("worldName", List.of(item)); + Map> map = is.getInventory(); + assertEquals(item, map.get("worldName").get(0)); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setHealth(java.util.Map)}. + */ + @Test + public void testSetHealthMapOfStringDouble() { + Map map = Map.of("test", 234D); + is.setHealth(map); + assertEquals(234D, is.getHealth().get("test").doubleValue(), 0D); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setFood(java.util.Map)}. + */ + @Test + public void testSetFoodMapOfStringInteger() { + Map map = Map.of("test", 234); + is.setFood(map); + assertEquals(234, is.getFood().get("test").intValue()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setExp(java.util.Map)}. + */ + @Test + public void testSetExpMapOfStringInteger() { + Map map = Map.of("test", 234); + is.setExp(map); + assertEquals(234, is.getExp().get("test").intValue()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setLocation(java.util.Map)}. + */ + @Test + public void testSetLocationMapOfStringLocation() { + Location loc = mock(Location.class); + Map map = Map.of("test", loc); + is.setLocation(map); + assertEquals(loc, is.getLocation().get("test")); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setHealth(java.lang.String, double)}. + */ + @Test + public void testSetHealthStringDouble() { + is.setHealth("test", 10D); + assertEquals(10D, is.getHealth().get("test").doubleValue(), 0D); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setFood(java.lang.String, int)}. + */ + @Test + public void testSetFoodStringInt() { + is.setFood("test", 234); + assertEquals(234, is.getFood().get("test").intValue()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setExp(java.lang.String, int)}. + */ + @Test + public void testSetExpStringInt() { + is.setExp("test", 234); + assertEquals(234, is.getExp().get("test").intValue()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setLocation(java.lang.String, org.bukkit.Location)}. + */ + @Test + public void testSetLocationStringLocation() { + Location loc = mock(Location.class); + is.setLocation("test", loc); + assertEquals(loc, is.getLocation().get("test")); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getInventory(java.lang.String)}. + */ + @Test + public void testGetInventoryString() { + assertTrue(is.getInventory("test").isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#isInventory(java.lang.String)}. + */ + @Test + public void testIsInventory() { + assertFalse(is.isInventory("test")); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setGameMode(java.lang.String, org.bukkit.GameMode)}. + */ + @Test + public void testSetGameMode() { + is.setGameMode("test", GameMode.ADVENTURE); + assertEquals(GameMode.ADVENTURE, is.getGameMode("test")); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getGameMode(java.lang.String)}. + */ + @Test + public void testGetGameMode() { + is.setGameMode("test2", GameMode.ADVENTURE); + assertEquals(GameMode.SURVIVAL, is.getGameMode("test")); + assertEquals(GameMode.ADVENTURE, is.getGameMode("test2")); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setAdvancement(java.lang.String, java.lang.String, java.util.List)}. + */ + @Test + public void testSetAdvancement() { + is.setAdvancement("word", "key", List.of("criteria", "cirt")); + assertTrue(is.getAdvancements("test").isEmpty()); + Map> r = is.getAdvancements("word"); + assertEquals("cirt", r.get("key").get(1)); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#clearAdvancement(java.lang.String)}. + */ + @Test + public void testClearAdvancement() { + is.setAdvancement("word", "key", List.of("criteria", "cirt")); + assertFalse(is.getAdvancements("word").isEmpty()); + is.clearAdvancement("wowoow"); + assertFalse(is.getAdvancements("word").isEmpty()); + is.clearAdvancement("word"); + assertTrue(is.getAdvancements("word").isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getAdvancements(java.lang.String)}. + */ + @Test + public void testGetAdvancements() { + assertTrue(is.getAdvancements("test").isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getEnderChest(java.lang.String)}. + */ + @Test + public void testGetEnderChestString() { + assertTrue(is.getEnderChest("test").isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setEnderChest(java.lang.String, java.util.List)}. + */ + @Test + public void testSetEnderChestStringListOfItemStack() { + ItemStack item = new ItemStack(Material.ACACIA_BOAT); + is.setEnderChest("worldName", List.of(item)); + Map> map = is.getEnderChest(); + assertEquals(item, map.get("worldName").get(0)); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getEnderChest()}. + */ + @Test + public void testGetEnderChest() { + assertTrue(is.getEnderChest().isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setEnderChest(java.util.Map)}. + */ + @Test + public void testSetEnderChestMapOfStringListOfItemStack() { + ItemStack item = new ItemStack(Material.ACACIA_BOAT); + Map> map = Map.of("worldName", List.of(item)); + is.setEnderChest(map); + Map> map2 = is.getEnderChest(); + assertEquals(item, map2.get("worldName").get(0)); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#clearStats(java.lang.String)}. + */ + @Test + public void testClearStats() { + is.setBlockStats("test", Map.of(Statistic.MINE_BLOCK, Map.of(Material.STONE, 3000))); + is.setEntityStats("test", Map.of(Statistic.ENTITY_KILLED_BY, Map.of(EntityType.BEE, 4000))); + is.setItemStats("test", Map.of(Statistic.USE_ITEM, Map.of(Material.DIAMOND_AXE, 5000))); + is.setUntypedStats("test", Map.of(Statistic.ARMOR_CLEANED, 30)); + is.clearStats("test2"); + assertFalse(is.getBlockStats("test").isEmpty()); + assertFalse(is.getEntityStats("test").isEmpty()); + assertFalse(is.getItemStats("test").isEmpty()); + assertFalse(is.getUntypedStats("test").isEmpty()); + is.clearStats("test"); + assertTrue(is.getBlockStats("test").isEmpty()); + assertTrue(is.getEntityStats("test").isEmpty()); + assertTrue(is.getItemStats("test").isEmpty()); + assertTrue(is.getUntypedStats("test").isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getUntypedStats(java.lang.String)}. + */ + @Test + public void testGetUntypedStats() { + assertTrue(is.getUntypedStats("test").isEmpty()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setUntypedStats(java.lang.String, java.util.Map)}. + */ + @Test + public void testSetUntypedStats() { + assertTrue(is.getUntypedStats("test").isEmpty()); + is.setUntypedStats("test", Map.of(Statistic.ARMOR_CLEANED, 30)); + assertEquals(30, is.getUntypedStats("test").get(Statistic.ARMOR_CLEANED).intValue()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getBlockStats(java.lang.String)}. + */ + @Test + public void testGetBlockStats() { + assertTrue(is.getBlockStats("test").isEmpty()); + is.setBlockStats("test", Map.of(Statistic.MINE_BLOCK, Map.of(Material.STONE, 3000))); + assertEquals(3000, is.getBlockStats("test").get(Statistic.MINE_BLOCK).get(Material.STONE).intValue()); + + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getItemStats(java.lang.String)}. + */ + @Test + public void testGetItemStats() { + assertTrue(is.getItemStats("test").isEmpty()); + is.setItemStats("test", Map.of(Statistic.MINE_BLOCK, Map.of(Material.STONE, 3000))); + assertEquals(3000, is.getItemStats("test").get(Statistic.MINE_BLOCK).get(Material.STONE).intValue()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getEntityStats(java.lang.String)}. + */ + @Test + public void testGetEntityStats() { + assertTrue(is.getEntityStats("test").isEmpty()); + is.setEntityStats("test", Map.of(Statistic.MINE_BLOCK, Map.of(EntityType.BLAZE, 3000))); + assertEquals(3000, is.getEntityStats("test").get(Statistic.MINE_BLOCK).get(EntityType.BLAZE).intValue()); + + } + +} From a5e70749313ca554746dc252958f3b3c40f1965d Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 21:15:22 -0800 Subject: [PATCH 12/39] Update github script --- .github/workflows/build.yml | 4 ++-- .../invswitcher/dataObjects/InventoryStorageTest.java | 7 ------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b9cf60c..c771fd5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,10 +14,10 @@ jobs: - uses: actions/checkout@v2 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - - name: Set up JDK 16 + - name: Set up JDK 17 uses: actions/setup-java@v1 with: - java-version: 16 + java-version: 17 - name: Cache SonarCloud packages uses: actions/cache@v1 with: diff --git a/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java b/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java index ab588b4..04bad93 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java @@ -36,13 +36,6 @@ public void setUp() throws Exception { is = new InventoryStorage(); } - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - } - /** * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getUniqueId()}. */ From 236995a818c312fd686d891b8c90efbf05c1b812 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 21:31:46 -0800 Subject: [PATCH 13/39] Added PlayerListener test class --- .../dataObjects/InventoryStorageTest.java | 6 +- .../listeners/PlayerListenerTest.java | 155 ++++++++++++++++++ 2 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java diff --git a/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java b/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java index 04bad93..d8dd5fa 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java @@ -1,6 +1,9 @@ package com.wasteofplastic.invswitcher.dataObjects; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import java.util.List; @@ -13,7 +16,6 @@ import org.bukkit.Statistic; import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java b/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java new file mode 100644 index 0000000..fa8471f --- /dev/null +++ b/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java @@ -0,0 +1,155 @@ +package com.wasteofplastic.invswitcher.listeners; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Set; + +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerChangedWorldEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.wasteofplastic.invswitcher.InvSwitcher; +import com.wasteofplastic.invswitcher.Store; + +import world.bentobox.bentobox.util.Util; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest(Util.class) +public class PlayerListenerTest { + + @Mock + private InvSwitcher addon; + private PlayerListener pl; + @Mock + private Store store; + @Mock + private Player player; + @Mock + private World world; + @Mock + private World notWorld; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Util + PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); + when(Util.sameWorld(world, world)).thenReturn(true); + // Player + when(player.getWorld()).thenReturn(world); + // Addon + when(addon.getStore()).thenReturn(store); + when(addon.getWorlds()).thenReturn(Set.of(world)); + pl = new PlayerListener(addon); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.listeners.PlayerListener#PlayerListener(com.wasteofplastic.invswitcher.InvSwitcher)}. + */ + @Test + public void testPlayerListener() { + assertNotNull(pl); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.listeners.PlayerListener#onWorldEnter(org.bukkit.event.player.PlayerChangedWorldEvent)}. + */ + @Test + public void testOnWorldEnterSameWorld() { + PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(player, world); + pl.onWorldEnter(event); + verify(store, never()).storeInventory(any(), any()); + verify(store, never()).getInventory(any(), any()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.listeners.PlayerListener#onWorldEnter(org.bukkit.event.player.PlayerChangedWorldEvent)}. + */ + @Test + public void testOnWorldEnterDifferentWorld() { + PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(player, notWorld); + pl.onWorldEnter(event); + verify(store).storeInventory(any(), any()); + verify(store).getInventory(any(), any()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.listeners.PlayerListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + */ + @Test + public void testOnPlayerJoin() { + PlayerJoinEvent event = new PlayerJoinEvent(player, ""); + pl.onPlayerJoin(event); + // No storage yet + verify(store, never()).getInventory(any(), any()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.listeners.PlayerListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + */ + @Test + public void testOnPlayerJoinNonHandledWorld() { + when(player.getWorld()).thenReturn(notWorld); + PlayerJoinEvent event = new PlayerJoinEvent(player, ""); + pl.onPlayerJoin(event); + // No storage yet + verify(store, never()).getInventory(any(), any()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.listeners.PlayerListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + */ + @Test + public void testOnPlayerJoinWithStorage() { + testOnWorldEnterDifferentWorld(); + when(player.getWorld()).thenReturn(notWorld); + PlayerJoinEvent event = new PlayerJoinEvent(player, ""); + pl.onPlayerJoin(event); + // No storage yet + verify(store).getInventory(any(), any()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.listeners.PlayerListener#onPlayerQuit(org.bukkit.event.player.PlayerQuitEvent)}. + */ + @Test + public void testOnPlayerQuit() { + PlayerQuitEvent event = new PlayerQuitEvent(player, ""); + pl.onPlayerQuit(event); + verify(store).storeAndSave(player, world); + verify(store).removeFromCache(player); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.listeners.PlayerListener#onPlayerQuit(org.bukkit.event.player.PlayerQuitEvent)}. + */ + @Test + public void testOnPlayerQuitNotCoveredWorld() { + when(player.getWorld()).thenReturn(notWorld); + PlayerQuitEvent event = new PlayerQuitEvent(player, ""); + pl.onPlayerQuit(event); + verify(store, never()).storeAndSave(player, world); + verify(store).removeFromCache(player); + } + +} From fc1df949dfc3dce8b71f0629e67f7ab106c5db30 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 22:13:47 -0800 Subject: [PATCH 14/39] Added InvSwitcher test --- .../invswitcher/InvSwitcher.java | 7 + .../invswitcher/InvSwitcherTest.java | 328 ++++++++++++++++++ 2 files changed, 335 insertions(+) create mode 100644 src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java diff --git a/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java b/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java index 722b0fb..c1fa469 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java +++ b/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java @@ -15,7 +15,14 @@ import world.bentobox.bentobox.api.configuration.Config; import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType; +/** + * Inventory switcher for worlds. Switches advancements too. + * + * @author tastybento + * + */ public class InvSwitcher extends Addon { + private Store store; private Settings settings; diff --git a/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java b/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java new file mode 100644 index 0000000..9c85a27 --- /dev/null +++ b/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java @@ -0,0 +1,328 @@ +/** + * + */ +package com.wasteofplastic.invswitcher; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.Comparator; +import java.util.Set; +import java.util.UUID; +import java.util.jar.JarEntry; +import java.util.jar.JarOutputStream; +import java.util.logging.Logger; + +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.UnsafeValues; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemFactory; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.plugin.PluginManager; +import org.bukkit.scheduler.BukkitScheduler; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import com.wasteofplastic.invswitcher.listeners.PlayerListener; + +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.api.addons.Addon.State; +import world.bentobox.bentobox.api.addons.AddonDescription; +import world.bentobox.bentobox.api.addons.GameModeAddon; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.managers.AddonsManager; +import world.bentobox.bentobox.managers.FlagsManager; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.bentobox.managers.PlaceholdersManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class, User.class}) +public class InvSwitcherTest { + + private static File jFile; + @Mock + private User user; + @Mock + private IslandsManager im; + @Mock + private Island island; + @Mock + private BentoBox plugin; + @Mock + private FlagsManager fm; + @Mock + private GameModeAddon gameMode; + @Mock + private AddonsManager am; + @Mock + private BukkitScheduler scheduler; + + @Mock + private Settings pluginSettings; + + private InvSwitcher addon; + + @Mock + private Logger logger; + @Mock + private PlaceholdersManager phm; + @Mock + private World world; + private UUID uuid; + + @Mock + private PluginManager pim; + + @BeforeClass + public static void beforeClass() throws IOException { + // Make the addon jar + jFile = new File("addon.jar"); + // Copy over config file from src folder + Path fromPath = Paths.get("src/main/resources/config.yml"); + Path path = Paths.get("config.yml"); + Files.copy(fromPath, path); + try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) { + //Added the new files to the jar. + try (FileInputStream fis = new FileInputStream(path.toFile())) { + byte[] buffer = new byte[1024]; + int bytesRead = 0; + JarEntry entry = new JarEntry(path.toString()); + tempJarOutputStream.putNextEntry(entry); + while((bytesRead = fis.read(buffer)) != -1) { + tempJarOutputStream.write(buffer, 0, bytesRead); + } + } + } + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger()); + + // The database type has to be created one line before the thenReturn() to work! + DatabaseType value = DatabaseType.YAML; + when(plugin.getSettings()).thenReturn(pluginSettings); + when(pluginSettings.getDatabaseType()).thenReturn(value); + + // Player + Player p = mock(Player.class); + // Sometimes use Mockito.withSettings().verboseLogging() + when(user.isOp()).thenReturn(false); + uuid = UUID.randomUUID(); + when(user.getUniqueId()).thenReturn(uuid); + when(user.getPlayer()).thenReturn(p); + when(user.getName()).thenReturn("tastybento"); + User.setPlugin(plugin); + + // Island World Manager + IslandWorldManager iwm = mock(IslandWorldManager.class); + when(plugin.getIWM()).thenReturn(iwm); + + // Player has island to begin with + when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); + when(plugin.getIslands()).thenReturn(im); + + // Locales + // Return the reference (USE THIS IN THE FUTURE) + when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); + + // Server + PowerMockito.mockStatic(Bukkit.class); + Server server = mock(Server.class); + when(Bukkit.getServer()).thenReturn(server); + when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger()); + when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class)); + + // Addon + addon = new InvSwitcher(); + File dataFolder = new File("addons/Level"); + addon.setDataFolder(dataFolder); + addon.setFile(jFile); + AddonDescription desc = new AddonDescription.Builder("bentobox", "InvSwitcher", "1.3").description("test").authors("tastybento").build(); + addon.setDescription(desc); + // Addons manager + when(plugin.getAddonsManager()).thenReturn(am); + // One game mode + when(am.getGameModeAddons()).thenReturn(Collections.singletonList(gameMode)); + AddonDescription desc2 = new AddonDescription.Builder("bentobox", "BSkyBlock", "1.3").description("test").authors("tasty").build(); + when(gameMode.getDescription()).thenReturn(desc2); + when(gameMode.getOverWorld()).thenReturn(world); + + // Flags manager + when(plugin.getFlagsManager()).thenReturn(fm); + when(fm.getFlags()).thenReturn(Collections.emptyList()); + + + // Bukkit + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(scheduler); + ItemMeta meta = mock(ItemMeta.class); + ItemFactory itemFactory = mock(ItemFactory.class); + when(itemFactory.getItemMeta(any())).thenReturn(meta); + when(Bukkit.getItemFactory()).thenReturn(itemFactory); + UnsafeValues unsafe = mock(UnsafeValues.class); + when(unsafe.getDataVersion()).thenReturn(777); + when(Bukkit.getUnsafe()).thenReturn(unsafe); + when(Bukkit.getPluginManager()).thenReturn(pim); + + when(Bukkit.getWorld(anyString())).thenReturn(world); + + // placeholders + when(plugin.getPlaceholdersManager()).thenReturn(phm); + + // World + when(world.getName()).thenReturn("bskyblock-world"); + // Island + when(island.getWorld()).thenReturn(world); + when(island.getOwner()).thenReturn(uuid); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + deleteAll(new File("database")); + } + + @AfterClass + public static void cleanUp() throws Exception { + deleteAll(new File("database")); + new File("addon.jar").delete(); + new File("config.yml").delete(); + deleteAll(new File("addons")); + } + + private static void deleteAll(File file) throws IOException { + if (file.exists()) { + Files.walk(file.toPath()) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.InvSwitcher#onEnable()}. + */ + @Test + public void testOnEnable() { + addon.onEnable(); + verify(plugin).logError("[InvSwitcher] This addon is incompatible with YAML database. Please use another type, like JSON."); + assertEquals(State.DISABLED, addon.getState()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.InvSwitcher#onDisable()}. + */ + @Test + public void testOnDisable() { + addon.onDisable(); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.InvSwitcher#onLoad()}. + */ + @Test + public void testOnLoad() { + addon.onLoad(); + verify(plugin).log("No config.yml found. Creating it..."); + File file = new File("config.yml"); + assertTrue(file.exists()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.InvSwitcher#allLoaded()}. + */ + @Test + public void testAllLoaded() { + addon.onLoad(); + addon.getSettings().setWorlds(Set.of("bskyblock-world")); + addon.allLoaded(); + verify(plugin).log("[InvSwitcher] Hooking into the following worlds:"); + verify(plugin, times(3)).log("[InvSwitcher] bskyblock-world"); + verify(am).registerListener(eq(addon), any(PlayerListener.class)); + + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.InvSwitcher#allLoaded()}. + */ + @Test + public void testAllLoadedNoWorlds() { + addon.onLoad(); + addon.getSettings().setWorlds(Collections.emptySet()); + addon.allLoaded(); + verify(plugin).logWarning("[InvSwitcher] Did not hook into any worlds - disabling addon!"); + assertEquals(State.DISABLED, addon.getState()); + verify(plugin, never()).log("Hooking into the following worlds:"); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.InvSwitcher#getStore()}. + */ + @Test + public void testGetStore() { + assertNull(addon.getStore()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.InvSwitcher#getSettings()}. + */ + @Test + public void testGetSettings() { + assertNull(addon.getSettings()); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.InvSwitcher#getWorlds()}. + */ + @Test + public void testGetWorlds() { + assertTrue(addon.getWorlds().isEmpty()); + } + +} From 791694801acd446824c4679953ae806db800a400 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 22:23:46 -0800 Subject: [PATCH 15/39] Clean up test class --- .../invswitcher/InvSwitcherTest.java | 94 +------------------ 1 file changed, 1 insertion(+), 93 deletions(-) diff --git a/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java b/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java index 9c85a27..2b48876 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java @@ -9,7 +9,6 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -25,20 +24,12 @@ import java.util.Collections; import java.util.Comparator; import java.util.Set; -import java.util.UUID; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; import java.util.logging.Logger; import org.bukkit.Bukkit; -import org.bukkit.Server; -import org.bukkit.UnsafeValues; import org.bukkit.World; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemFactory; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.plugin.PluginManager; -import org.bukkit.scheduler.BukkitScheduler; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -46,8 +37,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -59,41 +48,22 @@ import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.Addon.State; import world.bentobox.bentobox.api.addons.AddonDescription; -import world.bentobox.bentobox.api.addons.GameModeAddon; -import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType; -import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.AddonsManager; -import world.bentobox.bentobox.managers.FlagsManager; -import world.bentobox.bentobox.managers.IslandWorldManager; -import world.bentobox.bentobox.managers.IslandsManager; -import world.bentobox.bentobox.managers.PlaceholdersManager; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class, User.class}) +@PrepareForTest({Bukkit.class, BentoBox.class}) public class InvSwitcherTest { private static File jFile; @Mock - private User user; - @Mock - private IslandsManager im; - @Mock - private Island island; - @Mock private BentoBox plugin; @Mock - private FlagsManager fm; - @Mock - private GameModeAddon gameMode; - @Mock private AddonsManager am; - @Mock - private BukkitScheduler scheduler; @Mock private Settings pluginSettings; @@ -103,13 +73,7 @@ public class InvSwitcherTest { @Mock private Logger logger; @Mock - private PlaceholdersManager phm; - @Mock private World world; - private UUID uuid; - - @Mock - private PluginManager pim; @BeforeClass public static void beforeClass() throws IOException { @@ -147,35 +111,6 @@ public void setUp() throws Exception { when(plugin.getSettings()).thenReturn(pluginSettings); when(pluginSettings.getDatabaseType()).thenReturn(value); - // Player - Player p = mock(Player.class); - // Sometimes use Mockito.withSettings().verboseLogging() - when(user.isOp()).thenReturn(false); - uuid = UUID.randomUUID(); - when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); - when(user.getName()).thenReturn("tastybento"); - User.setPlugin(plugin); - - // Island World Manager - IslandWorldManager iwm = mock(IslandWorldManager.class); - when(plugin.getIWM()).thenReturn(iwm); - - // Player has island to begin with - when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); - when(plugin.getIslands()).thenReturn(im); - - // Locales - // Return the reference (USE THIS IN THE FUTURE) - when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); - - // Server - PowerMockito.mockStatic(Bukkit.class); - Server server = mock(Server.class); - when(Bukkit.getServer()).thenReturn(server); - when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger()); - when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class)); - // Addon addon = new InvSwitcher(); File dataFolder = new File("addons/Level"); @@ -185,39 +120,13 @@ public void setUp() throws Exception { addon.setDescription(desc); // Addons manager when(plugin.getAddonsManager()).thenReturn(am); - // One game mode - when(am.getGameModeAddons()).thenReturn(Collections.singletonList(gameMode)); - AddonDescription desc2 = new AddonDescription.Builder("bentobox", "BSkyBlock", "1.3").description("test").authors("tasty").build(); - when(gameMode.getDescription()).thenReturn(desc2); - when(gameMode.getOverWorld()).thenReturn(world); - - // Flags manager - when(plugin.getFlagsManager()).thenReturn(fm); - when(fm.getFlags()).thenReturn(Collections.emptyList()); - // Bukkit PowerMockito.mockStatic(Bukkit.class); - when(Bukkit.getScheduler()).thenReturn(scheduler); - ItemMeta meta = mock(ItemMeta.class); - ItemFactory itemFactory = mock(ItemFactory.class); - when(itemFactory.getItemMeta(any())).thenReturn(meta); - when(Bukkit.getItemFactory()).thenReturn(itemFactory); - UnsafeValues unsafe = mock(UnsafeValues.class); - when(unsafe.getDataVersion()).thenReturn(777); - when(Bukkit.getUnsafe()).thenReturn(unsafe); - when(Bukkit.getPluginManager()).thenReturn(pim); - when(Bukkit.getWorld(anyString())).thenReturn(world); - // placeholders - when(plugin.getPlaceholdersManager()).thenReturn(phm); - // World when(world.getName()).thenReturn("bskyblock-world"); - // Island - when(island.getWorld()).thenReturn(world); - when(island.getOwner()).thenReturn(uuid); } /** @@ -269,7 +178,6 @@ public void testOnDisable() { @Test public void testOnLoad() { addon.onLoad(); - verify(plugin).log("No config.yml found. Creating it..."); File file = new File("config.yml"); assertTrue(file.exists()); } From ae2c1acf73ab0273b197033e6bb3bfd84486fc09 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 22:51:37 -0800 Subject: [PATCH 16/39] Chnaged package name and fixed onDisable() test --- .../com/wasteofplastic/invswitcher/Store.java | 2 +- .../InventoryStorage.java | 2 +- .../invswitcher/InvSwitcherTest.java | 5 ++ .../InventoryStorageTest.java | 72 +++++++++---------- 4 files changed, 43 insertions(+), 38 deletions(-) rename src/main/java/com/wasteofplastic/invswitcher/{dataObjects => dataobjects}/InventoryStorage.java (99%) rename src/test/java/com/wasteofplastic/invswitcher/{dataObjects => dataobjects}/InventoryStorageTest.java (85%) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 8104200..630439d 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -44,7 +44,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import com.wasteofplastic.invswitcher.dataObjects.InventoryStorage; +import com.wasteofplastic.invswitcher.dataobjects.InventoryStorage; import world.bentobox.bentobox.database.Database; import world.bentobox.bentobox.util.Util; diff --git a/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java b/src/main/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorage.java similarity index 99% rename from src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java rename to src/main/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorage.java index 5c30a34..48c2b8e 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java +++ b/src/main/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorage.java @@ -1,4 +1,4 @@ -package com.wasteofplastic.invswitcher.dataObjects; +package com.wasteofplastic.invswitcher.dataobjects; import java.util.ArrayList; import java.util.Collections; diff --git a/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java b/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java index 2b48876..ad2cd1d 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java @@ -169,7 +169,12 @@ public void testOnEnable() { */ @Test public void testOnDisable() { + addon.onLoad(); + addon.getSettings().setWorlds(Set.of("bskyblock-world")); + addon.allLoaded(); addon.onDisable(); + PowerMockito.verifyStatic(Bukkit.class); + Bukkit.getOnlinePlayers(); } /** diff --git a/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java b/src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java similarity index 85% rename from src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java rename to src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java index d8dd5fa..21f5e1c 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorageTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java @@ -1,4 +1,4 @@ -package com.wasteofplastic.invswitcher.dataObjects; +package com.wasteofplastic.invswitcher.dataobjects; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -22,7 +22,7 @@ import org.powermock.modules.junit4.PowerMockRunner; /** - * @author tastybento + * @author tastybentos * */ @RunWith(PowerMockRunner.class) @@ -39,7 +39,7 @@ public void setUp() throws Exception { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getUniqueId()}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getUniqueId()}. */ @Test public void testGetUniqueId() { @@ -47,7 +47,7 @@ public void testGetUniqueId() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setUniqueId(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setUniqueId(java.lang.String)}. */ @Test public void testSetUniqueId() { @@ -57,7 +57,7 @@ public void testSetUniqueId() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getInventory()}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getInventory()}. */ @Test public void testGetInventory() { @@ -65,7 +65,7 @@ public void testGetInventory() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getHealth()}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getHealth()}. */ @Test public void testGetHealth() { @@ -73,7 +73,7 @@ public void testGetHealth() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getFood()}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getFood()}. */ @Test public void testGetFood() { @@ -81,7 +81,7 @@ public void testGetFood() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getExp()}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getExp()}. */ @Test public void testGetExp() { @@ -89,7 +89,7 @@ public void testGetExp() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getLocation()}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getLocation()}. */ @Test public void testGetLocation() { @@ -97,7 +97,7 @@ public void testGetLocation() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setInventory(java.util.Map)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setInventory(java.util.Map)}. */ @Test public void testSetInventoryMapOfStringListOfItemStack() { @@ -108,7 +108,7 @@ public void testSetInventoryMapOfStringListOfItemStack() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setInventory(java.lang.String, java.util.List)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setInventory(java.lang.String, java.util.List)}. */ @Test public void testSetInventoryStringListOfItemStack() { @@ -119,7 +119,7 @@ public void testSetInventoryStringListOfItemStack() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setHealth(java.util.Map)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setHealth(java.util.Map)}. */ @Test public void testSetHealthMapOfStringDouble() { @@ -129,7 +129,7 @@ public void testSetHealthMapOfStringDouble() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setFood(java.util.Map)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setFood(java.util.Map)}. */ @Test public void testSetFoodMapOfStringInteger() { @@ -139,7 +139,7 @@ public void testSetFoodMapOfStringInteger() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setExp(java.util.Map)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setExp(java.util.Map)}. */ @Test public void testSetExpMapOfStringInteger() { @@ -149,7 +149,7 @@ public void testSetExpMapOfStringInteger() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setLocation(java.util.Map)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setLocation(java.util.Map)}. */ @Test public void testSetLocationMapOfStringLocation() { @@ -160,7 +160,7 @@ public void testSetLocationMapOfStringLocation() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setHealth(java.lang.String, double)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setHealth(java.lang.String, double)}. */ @Test public void testSetHealthStringDouble() { @@ -169,7 +169,7 @@ public void testSetHealthStringDouble() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setFood(java.lang.String, int)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setFood(java.lang.String, int)}. */ @Test public void testSetFoodStringInt() { @@ -178,7 +178,7 @@ public void testSetFoodStringInt() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setExp(java.lang.String, int)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setExp(java.lang.String, int)}. */ @Test public void testSetExpStringInt() { @@ -187,7 +187,7 @@ public void testSetExpStringInt() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setLocation(java.lang.String, org.bukkit.Location)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setLocation(java.lang.String, org.bukkit.Location)}. */ @Test public void testSetLocationStringLocation() { @@ -197,7 +197,7 @@ public void testSetLocationStringLocation() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getInventory(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getInventory(java.lang.String)}. */ @Test public void testGetInventoryString() { @@ -205,7 +205,7 @@ public void testGetInventoryString() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#isInventory(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#isInventory(java.lang.String)}. */ @Test public void testIsInventory() { @@ -213,7 +213,7 @@ public void testIsInventory() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setGameMode(java.lang.String, org.bukkit.GameMode)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setGameMode(java.lang.String, org.bukkit.GameMode)}. */ @Test public void testSetGameMode() { @@ -222,7 +222,7 @@ public void testSetGameMode() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getGameMode(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getGameMode(java.lang.String)}. */ @Test public void testGetGameMode() { @@ -232,7 +232,7 @@ public void testGetGameMode() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setAdvancement(java.lang.String, java.lang.String, java.util.List)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setAdvancement(java.lang.String, java.lang.String, java.util.List)}. */ @Test public void testSetAdvancement() { @@ -243,7 +243,7 @@ public void testSetAdvancement() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#clearAdvancement(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#clearAdvancement(java.lang.String)}. */ @Test public void testClearAdvancement() { @@ -256,7 +256,7 @@ public void testClearAdvancement() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getAdvancements(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getAdvancements(java.lang.String)}. */ @Test public void testGetAdvancements() { @@ -264,7 +264,7 @@ public void testGetAdvancements() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getEnderChest(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getEnderChest(java.lang.String)}. */ @Test public void testGetEnderChestString() { @@ -272,7 +272,7 @@ public void testGetEnderChestString() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setEnderChest(java.lang.String, java.util.List)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setEnderChest(java.lang.String, java.util.List)}. */ @Test public void testSetEnderChestStringListOfItemStack() { @@ -283,7 +283,7 @@ public void testSetEnderChestStringListOfItemStack() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getEnderChest()}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getEnderChest()}. */ @Test public void testGetEnderChest() { @@ -291,7 +291,7 @@ public void testGetEnderChest() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setEnderChest(java.util.Map)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setEnderChest(java.util.Map)}. */ @Test public void testSetEnderChestMapOfStringListOfItemStack() { @@ -303,7 +303,7 @@ public void testSetEnderChestMapOfStringListOfItemStack() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#clearStats(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#clearStats(java.lang.String)}. */ @Test public void testClearStats() { @@ -324,7 +324,7 @@ public void testClearStats() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getUntypedStats(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getUntypedStats(java.lang.String)}. */ @Test public void testGetUntypedStats() { @@ -332,7 +332,7 @@ public void testGetUntypedStats() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#setUntypedStats(java.lang.String, java.util.Map)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#setUntypedStats(java.lang.String, java.util.Map)}. */ @Test public void testSetUntypedStats() { @@ -342,7 +342,7 @@ public void testSetUntypedStats() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getBlockStats(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getBlockStats(java.lang.String)}. */ @Test public void testGetBlockStats() { @@ -353,7 +353,7 @@ public void testGetBlockStats() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getItemStats(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getItemStats(java.lang.String)}. */ @Test public void testGetItemStats() { @@ -363,7 +363,7 @@ public void testGetItemStats() { } /** - * Test method for {@link com.wasteofplastic.invswitcher.dataObjects.InventoryStorage#getEntityStats(java.lang.String)}. + * Test method for {@link com.wasteofplastic.invswitcher.dataobjects.InventoryStorage#getEntityStats(java.lang.String)}. */ @Test public void testGetEntityStats() { From 0b4f5fe19bb0182eefe97db371ae681575f35e87 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 22:55:59 -0800 Subject: [PATCH 17/39] Minor refactor to reduce complexity. --- .../com/wasteofplastic/invswitcher/Store.java | 112 +++++++++--------- 1 file changed, 57 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 630439d..9502ae0 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -306,40 +306,42 @@ private void saveStats(InventoryStorage store, Player player, String worldName) */ private void getStats(InventoryStorage store, Player player, String worldName) { // Statistics - Arrays.stream(Statistic.values()).forEach(s -> { - switch(s.getType()) { - case BLOCK: - if (store.getBlockStats(worldName).containsKey(s)) { - for (Entry en : store.getBlockStats(worldName).get(s).entrySet()) { - player.setStatistic(s, en.getKey(), en.getValue()); - } - } - break; - case ITEM: - if (store.getItemStats(worldName).containsKey(s)) { - for (Entry en : store.getItemStats(worldName).get(s).entrySet()) { - player.setStatistic(s, en.getKey(), en.getValue()); - } + Arrays.stream(Statistic.values()).forEach(s -> getStat(s, store, player, worldName)); + + } + + private void getStat(Statistic s, InventoryStorage store, Player player, String worldName) { + switch(s.getType()) { + case BLOCK: + if (store.getBlockStats(worldName).containsKey(s)) { + for (Entry en : store.getBlockStats(worldName).get(s).entrySet()) { + player.setStatistic(s, en.getKey(), en.getValue()); } - break; - case ENTITY: - if (store.getEntityStats(worldName).containsKey(s)) { - for (Entry en : store.getEntityStats(worldName).get(s).entrySet()) { - player.setStatistic(s, en.getKey(), en.getValue()); - } + } + break; + case ITEM: + if (store.getItemStats(worldName).containsKey(s)) { + for (Entry en : store.getItemStats(worldName).get(s).entrySet()) { + player.setStatistic(s, en.getKey(), en.getValue()); } - break; - case UNTYPED: - if (store.getUntypedStats(worldName).containsKey(s)) { - player.setStatistic(s, store.getUntypedStats(worldName).get(s)); + } + break; + case ENTITY: + if (store.getEntityStats(worldName).containsKey(s)) { + for (Entry en : store.getEntityStats(worldName).get(s).entrySet()) { + player.setStatistic(s, en.getKey(), en.getValue()); } - break; - default: - break; - } - }); + break; + case UNTYPED: + if (store.getUntypedStats(worldName).containsKey(s)) { + player.setStatistic(s, store.getUntypedStats(worldName).get(s)); + } + break; + default: + break; + } } @SuppressWarnings("deprecation") @@ -381,38 +383,38 @@ private void clearPlayer(Player player) { { switch (s.getType()) { - case BLOCK: - for (Material m : Material.values()) + case BLOCK: + for (Material m : Material.values()) + { + if (m.isBlock() && !m.isLegacy()) { - if (m.isBlock() && !m.isLegacy()) - { - player.setStatistic(s, m, 0); - } + player.setStatistic(s, m, 0); } - break; - case ITEM: - for (Material m : Material.values()) + } + break; + case ITEM: + for (Material m : Material.values()) + { + if (m.isItem() && !m.isLegacy()) { - if (m.isItem() && !m.isLegacy()) - { - player.setStatistic(s, m, 0); - } + player.setStatistic(s, m, 0); } - break; - case ENTITY: - for (EntityType en : EntityType.values()) + } + break; + case ENTITY: + for (EntityType en : EntityType.values()) + { + if (en.isAlive()) { - if (en.isAlive()) - { - player.setStatistic(s, en, 0); - } + player.setStatistic(s, en, 0); } - break; - case UNTYPED: - player.setStatistic(s, 0); - break; - default: - break; + } + break; + case UNTYPED: + player.setStatistic(s, 0); + break; + default: + break; } }); } From 60937c6d30c7f668082d6414ae4f4f5c5f944b6b Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 23:15:22 -0800 Subject: [PATCH 18/39] Refactored to reduce complexity --- .../com/wasteofplastic/invswitcher/Store.java | 119 ++++++++---------- 1 file changed, 54 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 9502ae0..46769d3 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -24,11 +24,11 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Objects; import java.util.UUID; import java.util.stream.Collectors; @@ -312,39 +312,18 @@ private void getStats(InventoryStorage store, Player player, String worldName) { private void getStat(Statistic s, InventoryStorage store, Player player, String worldName) { switch(s.getType()) { - case BLOCK: - if (store.getBlockStats(worldName).containsKey(s)) { - for (Entry en : store.getBlockStats(worldName).get(s).entrySet()) { - player.setStatistic(s, en.getKey(), en.getValue()); - } - } - break; - case ITEM: - if (store.getItemStats(worldName).containsKey(s)) { - for (Entry en : store.getItemStats(worldName).get(s).entrySet()) { - player.setStatistic(s, en.getKey(), en.getValue()); - } - } - break; - case ENTITY: - if (store.getEntityStats(worldName).containsKey(s)) { - for (Entry en : store.getEntityStats(worldName).get(s).entrySet()) { - player.setStatistic(s, en.getKey(), en.getValue()); - } - } - break; - case UNTYPED: + case BLOCK -> store.getBlockStats(worldName).getOrDefault(s, Collections.emptyMap()).forEach((k,v) -> player.setStatistic(s, k, v)); + case ITEM -> store.getItemStats(worldName).getOrDefault(s, Collections.emptyMap()).forEach((k,v) -> player.setStatistic(s, k, v)); + case ENTITY -> store.getEntityStats(worldName).getOrDefault(s, Collections.emptyMap()).forEach((k,v) -> player.setStatistic(s, k, v)); + case UNTYPED -> { if (store.getUntypedStats(worldName).containsKey(s)) { player.setStatistic(s, store.getUntypedStats(worldName).get(s)); } - break; - default: - break; - + } + default -> {} } } - @SuppressWarnings("deprecation") private void clearPlayer(Player player) { if (this.addon.getSettings().isInventory()) { @@ -361,13 +340,7 @@ private void clearPlayer(Player player) { if (this.addon.getSettings().isAdvancements()) { // Reset advancements - Iterator it = Bukkit.advancementIterator(); - while (it.hasNext()) - { - Advancement a = it.next(); - AdvancementProgress p = player.getAdvancementProgress(a); - p.getAwardedCriteria().forEach(p::revokeCriteria); - } + resetAdv(player); } if (this.addon.getSettings().isEnderChest()) @@ -379,45 +352,61 @@ private void clearPlayer(Player player) { if (this.addon.getSettings().isStatistics()) { // Reset Statistics - Arrays.stream(Statistic.values()).forEach(s -> + resetStats(player); + } + } + + private void resetAdv(Player player) { + Iterator it = Bukkit.advancementIterator(); + while (it.hasNext()) + { + Advancement a = it.next(); + AdvancementProgress p = player.getAdvancementProgress(a); + p.getAwardedCriteria().forEach(p::revokeCriteria); + } + } + + @SuppressWarnings("deprecation") + private void resetStats(Player player) { + Arrays.stream(Statistic.values()).forEach(s -> + { + switch (s.getType()) { - switch (s.getType()) + case BLOCK: + for (Material m : Material.values()) { - case BLOCK: - for (Material m : Material.values()) + if (m.isBlock() && !m.isLegacy()) { - if (m.isBlock() && !m.isLegacy()) - { - player.setStatistic(s, m, 0); - } + player.setStatistic(s, m, 0); } - break; - case ITEM: - for (Material m : Material.values()) + } + break; + case ITEM: + for (Material m : Material.values()) + { + if (m.isItem() && !m.isLegacy()) { - if (m.isItem() && !m.isLegacy()) - { - player.setStatistic(s, m, 0); - } + player.setStatistic(s, m, 0); } - break; - case ENTITY: - for (EntityType en : EntityType.values()) + } + break; + case ENTITY: + for (EntityType en : EntityType.values()) + { + if (en.isAlive()) { - if (en.isAlive()) - { - player.setStatistic(s, en, 0); - } + player.setStatistic(s, en, 0); } - break; - case UNTYPED: - player.setStatistic(s, 0); - break; - default: - break; } - }); - } + break; + case UNTYPED: + player.setStatistic(s, 0); + break; + default: + break; + } + }); + } //new Exp Math from 1.8 From 6114ee22289410c276ece7236c9d0b9bac76c655 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 23:18:51 -0800 Subject: [PATCH 19/39] Remove unneeded default --- src/main/java/com/wasteofplastic/invswitcher/Store.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 46769d3..d78a257 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -320,7 +320,6 @@ private void getStat(Statistic s, InventoryStorage store, Player player, String player.setStatistic(s, store.getUntypedStats(worldName).get(s)); } } - default -> {} } } From 098b9b1fc2a23b9ef5a5b6562019b3b978681024 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 23:27:04 -0800 Subject: [PATCH 20/39] Code clean up from IntelliJ --- .../invswitcher/InvSwitcher.java | 2 +- .../com/wasteofplastic/invswitcher/Store.java | 69 ++++++++++--------- .../invswitcher/InvSwitcherTest.java | 8 +-- .../wasteofplastic/invswitcher/StoreTest.java | 8 +-- .../dataobjects/InventoryStorageTest.java | 7 +- .../listeners/PlayerListenerTest.java | 3 +- 6 files changed, 45 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java b/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java index c1fa469..c5f279a 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java +++ b/src/main/java/com/wasteofplastic/invswitcher/InvSwitcher.java @@ -27,7 +27,7 @@ public class InvSwitcher extends Addon { private Settings settings; - private Config config = new Config<>(this, Settings.class); + private final Config config = new Config<>(this, Settings.class); private Set worlds = new HashSet<>(); diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index d78a257..d5029cb 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -40,6 +40,7 @@ import org.bukkit.advancement.Advancement; import org.bukkit.advancement.AdvancementProgress; import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeInstance; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -125,8 +126,9 @@ private void setHeath(InventoryStorage store, Player player, String overworldNam // Health double health = store.getHealth().getOrDefault(overworldName, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()); - if (health > player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()) { - health = player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); + AttributeInstance attr = player.getAttribute(Attribute.GENERIC_MAX_HEALTH); + if (attr != null && health > attr.getValue()) { + health = attr.getValue(); } if (health < 0D) { health = 0D; @@ -257,42 +259,41 @@ private void saveStats(InventoryStorage store, Player player, String worldName) Arrays.stream(Statistic.values()).forEach(s -> { Map map; Map entMap; - switch(s.getType()) { - case BLOCK: - map = Arrays.stream(Material.values()).filter(Material::isBlock) - .filter(m -> !m.isLegacy()) - .filter(m -> player.getStatistic(s, m) > 0) - .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); - if (!map.isEmpty()) { - store.getBlockStats(worldName).put(s, map); + switch (s.getType()) { + case BLOCK -> { + map = Arrays.stream(Material.values()).filter(Material::isBlock) + .filter(m -> !m.isLegacy()) + .filter(m -> player.getStatistic(s, m) > 0) + .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); + if (!map.isEmpty()) { + store.getBlockStats(worldName).put(s, map); + } } - break; - case ITEM: - map = Arrays.stream(Material.values()).filter(Material::isItem) - .filter(m -> !m.isLegacy()) - .filter(m -> player.getStatistic(s, m) > 0) - .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); - if (!map.isEmpty()) { - store.getItemStats(worldName).put(s, map); + case ITEM -> { + map = Arrays.stream(Material.values()).filter(Material::isItem) + .filter(m -> !m.isLegacy()) + .filter(m -> player.getStatistic(s, m) > 0) + .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); + if (!map.isEmpty()) { + store.getItemStats(worldName).put(s, map); + } } - break; - case ENTITY: - entMap = Arrays.stream(EntityType.values()).filter(EntityType::isAlive) - .filter(m -> player.getStatistic(s, m) > 0) - .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); - if (!entMap.isEmpty()) { - store.getEntityStats(worldName).put(s, entMap); + case ENTITY -> { + entMap = Arrays.stream(EntityType.values()).filter(EntityType::isAlive) + .filter(m -> player.getStatistic(s, m) > 0) + .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); + if (!entMap.isEmpty()) { + store.getEntityStats(worldName).put(s, entMap); + } } - break; - case UNTYPED: - int sc = player.getStatistic(s); - if (sc > 0) { - store.getUntypedStats(worldName).put(s, sc); + case UNTYPED -> { + int sc = player.getStatistic(s); + if (sc > 0) { + store.getUntypedStats(worldName).put(s, sc); + } + } + default -> { } - break; - default: - break; - } }); diff --git a/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java b/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java index ad2cd1d..401bb08 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java @@ -1,6 +1,3 @@ -/** - * - */ package com.wasteofplastic.invswitcher; import static org.junit.Assert.assertEquals; @@ -87,7 +84,7 @@ public static void beforeClass() throws IOException { //Added the new files to the jar. try (FileInputStream fis = new FileInputStream(path.toFile())) { byte[] buffer = new byte[1024]; - int bytesRead = 0; + int bytesRead; JarEntry entry = new JarEntry(path.toString()); tempJarOutputStream.putNextEntry(entry); while((bytesRead = fis.read(buffer)) != -1) { @@ -98,10 +95,9 @@ public static void beforeClass() throws IOException { } /** - * @throws java.lang.Exception */ @Before - public void setUp() throws Exception { + public void setUp() { // Set up plugin Whitebox.setInternalState(BentoBox.class, "instance", plugin); when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger()); diff --git a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java index c8905cd..8dfff41 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java @@ -50,8 +50,6 @@ public class StoreTest { @Mock private World world; - private com.wasteofplastic.invswitcher.Settings sets; - @Before public void setUp() { BentoBox plugin = mock(BentoBox.class); @@ -81,7 +79,7 @@ public void setUp() { World fromWorld = mock(World.class); when(fromWorld.getName()).thenReturn("from_the_end_nether"); - sets = new com.wasteofplastic.invswitcher.Settings(); + com.wasteofplastic.invswitcher.Settings sets = new com.wasteofplastic.invswitcher.Settings(); when(addon.getSettings()).thenReturn(sets); when(addon.getWorlds()).thenReturn(Collections.singleton(world)); @@ -125,8 +123,8 @@ public void testGetInventory() { verify(player).setTotalExperience(0); } - /** - * Test method for {@link Store#storeInventory(Player, World)}. + /* + Test method for {@link Store#storeInventory(Player, World)}. */ /* * TODO: Works in Eclipse, fails in MVN... diff --git a/src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java b/src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java index 21f5e1c..a0a39e8 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java @@ -31,10 +31,9 @@ public class InventoryStorageTest { private InventoryStorage is; /** - * @throws java.lang.Exception */ @Before - public void setUp() throws Exception { + public void setUp() { is = new InventoryStorage(); } @@ -125,7 +124,7 @@ public void testSetInventoryStringListOfItemStack() { public void testSetHealthMapOfStringDouble() { Map map = Map.of("test", 234D); is.setHealth(map); - assertEquals(234D, is.getHealth().get("test").doubleValue(), 0D); + assertEquals(234D, is.getHealth().get("test"), 0D); } /** @@ -165,7 +164,7 @@ public void testSetLocationMapOfStringLocation() { @Test public void testSetHealthStringDouble() { is.setHealth("test", 10D); - assertEquals(10D, is.getHealth().get("test").doubleValue(), 0D); + assertEquals(10D, is.getHealth().get("test"), 0D); } /** diff --git a/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java b/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java index fa8471f..04ff43b 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java @@ -48,10 +48,9 @@ public class PlayerListenerTest { private World notWorld; /** - * @throws java.lang.Exception */ @Before - public void setUp() throws Exception { + public void setUp() { // Util PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); when(Util.sameWorld(world, world)).thenReturn(true); From fdf98e747738901bf47d5c131d84ef66691968d5 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 23:32:21 -0800 Subject: [PATCH 21/39] Remove unused default --- .../com/wasteofplastic/invswitcher/Store.java | 58 +++++++++---------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index d5029cb..6aab494 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -260,41 +260,39 @@ private void saveStats(InventoryStorage store, Player player, String worldName) Map map; Map entMap; switch (s.getType()) { - case BLOCK -> { - map = Arrays.stream(Material.values()).filter(Material::isBlock) - .filter(m -> !m.isLegacy()) - .filter(m -> player.getStatistic(s, m) > 0) - .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); - if (!map.isEmpty()) { - store.getBlockStats(worldName).put(s, map); - } - } - case ITEM -> { - map = Arrays.stream(Material.values()).filter(Material::isItem) - .filter(m -> !m.isLegacy()) - .filter(m -> player.getStatistic(s, m) > 0) - .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); - if (!map.isEmpty()) { - store.getItemStats(worldName).put(s, map); - } + case BLOCK -> { + map = Arrays.stream(Material.values()).filter(Material::isBlock) + .filter(m -> !m.isLegacy()) + .filter(m -> player.getStatistic(s, m) > 0) + .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); + if (!map.isEmpty()) { + store.getBlockStats(worldName).put(s, map); } - case ENTITY -> { - entMap = Arrays.stream(EntityType.values()).filter(EntityType::isAlive) - .filter(m -> player.getStatistic(s, m) > 0) - .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); - if (!entMap.isEmpty()) { - store.getEntityStats(worldName).put(s, entMap); - } + } + case ITEM -> { + map = Arrays.stream(Material.values()).filter(Material::isItem) + .filter(m -> !m.isLegacy()) + .filter(m -> player.getStatistic(s, m) > 0) + .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); + if (!map.isEmpty()) { + store.getItemStats(worldName).put(s, map); } - case UNTYPED -> { - int sc = player.getStatistic(s); - if (sc > 0) { - store.getUntypedStats(worldName).put(s, sc); - } + } + case ENTITY -> { + entMap = Arrays.stream(EntityType.values()).filter(EntityType::isAlive) + .filter(m -> player.getStatistic(s, m) > 0) + .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); + if (!entMap.isEmpty()) { + store.getEntityStats(worldName).put(s, entMap); } - default -> { + } + case UNTYPED -> { + int sc = player.getStatistic(s); + if (sc > 0) { + store.getUntypedStats(worldName).put(s, sc); } } + } }); } From b845ad7007ecbba3de5b81dcbfc7fd31ef58bead Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 23:35:31 -0800 Subject: [PATCH 22/39] Remove nesting. --- .../com/wasteofplastic/invswitcher/Store.java | 59 +++++++++---------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 6aab494..9088db3 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -350,7 +350,8 @@ private void clearPlayer(Player player) { if (this.addon.getSettings().isStatistics()) { // Reset Statistics - resetStats(player); + Arrays.stream(Statistic.values()).forEach(s -> + resetStats(player, s)); } } @@ -365,46 +366,40 @@ private void resetAdv(Player player) { } @SuppressWarnings("deprecation") - private void resetStats(Player player) { - Arrays.stream(Statistic.values()).forEach(s -> + private void resetStats(Player player, Statistic s) { + switch (s.getType()) { - switch (s.getType()) + case BLOCK: + for (Material m : Material.values()) { - case BLOCK: - for (Material m : Material.values()) + if (m.isBlock() && !m.isLegacy()) { - if (m.isBlock() && !m.isLegacy()) - { - player.setStatistic(s, m, 0); - } + player.setStatistic(s, m, 0); } - break; - case ITEM: - for (Material m : Material.values()) + } + break; + case ITEM: + for (Material m : Material.values()) + { + if (m.isItem() && !m.isLegacy()) { - if (m.isItem() && !m.isLegacy()) - { - player.setStatistic(s, m, 0); - } + player.setStatistic(s, m, 0); } - break; - case ENTITY: - for (EntityType en : EntityType.values()) + } + break; + case ENTITY: + for (EntityType en : EntityType.values()) + { + if (en.isAlive()) { - if (en.isAlive()) - { - player.setStatistic(s, en, 0); - } + player.setStatistic(s, en, 0); } - break; - case UNTYPED: - player.setStatistic(s, 0); - break; - default: - break; } - }); - + break; + case UNTYPED: + player.setStatistic(s, 0); + break; + } } //new Exp Math from 1.8 From 8b1f31d41cf31554681d4ea385ee4c8070bf03b0 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 9 Feb 2023 23:42:06 -0800 Subject: [PATCH 23/39] Even less complexity! --- .../com/wasteofplastic/invswitcher/Store.java | 37 +++---------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 9088db3..6066277 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -367,38 +367,11 @@ private void resetAdv(Player player) { @SuppressWarnings("deprecation") private void resetStats(Player player, Statistic s) { - switch (s.getType()) - { - case BLOCK: - for (Material m : Material.values()) - { - if (m.isBlock() && !m.isLegacy()) - { - player.setStatistic(s, m, 0); - } - } - break; - case ITEM: - for (Material m : Material.values()) - { - if (m.isItem() && !m.isLegacy()) - { - player.setStatistic(s, m, 0); - } - } - break; - case ENTITY: - for (EntityType en : EntityType.values()) - { - if (en.isAlive()) - { - player.setStatistic(s, en, 0); - } - } - break; - case UNTYPED: - player.setStatistic(s, 0); - break; + switch (s.getType()) { + case BLOCK -> Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy()).forEach(m -> player.setStatistic(s, m, 0)); + case ITEM -> Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy()).forEach(m -> player.setStatistic(s, m, 0)); + case ENTITY -> Arrays.stream(EntityType.values()).filter(EntityType::isAlive).forEach(en -> player.setStatistic(s, en, 0)); + case UNTYPED -> player.setStatistic(s, 0); } } From 3a90f38279fa734969435bd000d41b19766fe2ea Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 10 Feb 2023 18:47:53 -0800 Subject: [PATCH 24/39] Redid Store test class --- .../com/wasteofplastic/invswitcher/Store.java | 24 ++-- .../wasteofplastic/invswitcher/StoreTest.java | 132 ++++++++++++++++-- 2 files changed, 133 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 6066277..d218f27 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -58,6 +58,11 @@ public class Store { private static final CharSequence THE_END = "_the_end"; private static final CharSequence NETHER = "_nether"; + @SuppressWarnings("deprecation") + private static final List BLOCKS = Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy()).toList(); + @SuppressWarnings("deprecation") + private static final List ITEMS = Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy()).toList(); + private static final List LIVING_ENTITIES = Arrays.stream(EntityType.values()).filter(EntityType::isAlive).toList(); private final Database database; private final Map cache; private final InvSwitcher addon; @@ -252,7 +257,6 @@ public void storeAndSave(Player player, World world) { database.saveObjectAsync(store); } - @SuppressWarnings("deprecation") private void saveStats(InventoryStorage store, Player player, String worldName) { store.clearStats(worldName); // Statistics @@ -261,26 +265,21 @@ private void saveStats(InventoryStorage store, Player player, String worldName) Map entMap; switch (s.getType()) { case BLOCK -> { - map = Arrays.stream(Material.values()).filter(Material::isBlock) - .filter(m -> !m.isLegacy()) - .filter(m -> player.getStatistic(s, m) > 0) + map = BLOCKS.stream().filter(m -> player.getStatistic(s, m) > 0) .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); if (!map.isEmpty()) { store.getBlockStats(worldName).put(s, map); } } case ITEM -> { - map = Arrays.stream(Material.values()).filter(Material::isItem) - .filter(m -> !m.isLegacy()) - .filter(m -> player.getStatistic(s, m) > 0) + map = ITEMS.stream().filter(m -> player.getStatistic(s, m) > 0) .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); if (!map.isEmpty()) { store.getItemStats(worldName).put(s, map); } } case ENTITY -> { - entMap = Arrays.stream(EntityType.values()).filter(EntityType::isAlive) - .filter(m -> player.getStatistic(s, m) > 0) + entMap = LIVING_ENTITIES.stream().filter(m -> player.getStatistic(s, m) > 0) .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); if (!entMap.isEmpty()) { store.getEntityStats(worldName).put(s, entMap); @@ -365,12 +364,11 @@ private void resetAdv(Player player) { } } - @SuppressWarnings("deprecation") private void resetStats(Player player, Statistic s) { switch (s.getType()) { - case BLOCK -> Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy()).forEach(m -> player.setStatistic(s, m, 0)); - case ITEM -> Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy()).forEach(m -> player.setStatistic(s, m, 0)); - case ENTITY -> Arrays.stream(EntityType.values()).filter(EntityType::isAlive).forEach(en -> player.setStatistic(s, en, 0)); + case BLOCK -> BLOCKS.forEach(m -> player.setStatistic(s, m, 0)); + case ITEM -> ITEMS.forEach(m -> player.setStatistic(s, m, 0)); + case ENTITY -> LIVING_ENTITIES.forEach(en -> player.setStatistic(s, en, 0)); case UNTYPED -> player.setStatistic(s, 0); } } diff --git a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java index 8dfff41..777b8d2 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java @@ -1,7 +1,13 @@ package com.wasteofplastic.invswitcher; +import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyFloat; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -12,11 +18,13 @@ import java.util.Collections; import java.util.Comparator; import java.util.UUID; +import java.util.logging.Logger; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.attribute.AttributeInstance; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; @@ -25,6 +33,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -50,8 +59,18 @@ public class StoreTest { @Mock private World world; + private Store s; + private com.wasteofplastic.invswitcher.Settings sets; + + @Mock + private Logger logger; + @Before public void setUp() { + // Bukkit + PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); + + // BentoBox BentoBox plugin = mock(BentoBox.class); Whitebox.setInternalState(BentoBox.class, "instance", plugin); Settings settings = mock(Settings.class); @@ -79,13 +98,20 @@ public void setUp() { World fromWorld = mock(World.class); when(fromWorld.getName()).thenReturn("from_the_end_nether"); - com.wasteofplastic.invswitcher.Settings sets = new com.wasteofplastic.invswitcher.Settings(); + // Settings + sets = new com.wasteofplastic.invswitcher.Settings(); when(addon.getSettings()).thenReturn(sets); when(addon.getWorlds()).thenReturn(Collections.singleton(world)); + // Addon + when(addon.getLogger()).thenReturn(logger); + PowerMockito.mockStatic(Util.class); when(Util.getWorld(world)).thenReturn(world); when(Util.getWorld(fromWorld)).thenReturn(fromWorld); + + // Class under test + s = new Store(addon); } @After @@ -106,8 +132,17 @@ public void clear() throws IOException{ */ @Test public void testStore() { - new Store(addon); - verify(addon).getLogger(); + assertNotNull(s); + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.Store#isWorldStored(org.bukkit.entity.Player, org.bukkit.World)}. + */ + @Test + public void testIsWorldStored() { + assertFalse(s.isWorldStored(player, world)); + s.storeInventory(player, world); + assertTrue(s.isWorldStored(player, world)); } /** @@ -115,7 +150,6 @@ public void testStore() { */ @Test public void testGetInventory() { - Store s = new Store(addon); s.getInventory(player, world); verify(player).setFoodLevel(20); verify(player).setHealth(18); @@ -123,14 +157,92 @@ public void testGetInventory() { verify(player).setTotalExperience(0); } - /* - Test method for {@link Store#storeInventory(Player, World)}. + /** + * Test method for {@link com.wasteofplastic.invswitcher.Store#removeFromCache(org.bukkit.entity.Player)}. */ - /* - * TODO: Works in Eclipse, fails in MVN... @Test - public void testStoreInventoryPlayerWorldLocation() { - new Store(addon).storeInventory(player, world); + public void testRemoveFromCache() { + s.removeFromCache(player); } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.Store#storeInventory(org.bukkit.entity.Player, org.bukkit.World)}. */ + @Test + public void testStoreInventoryNothing() { + // Do not actually save anything + sets.setAdvancements(false); + sets.setEnderChest(false); + sets.setExperience(false); + sets.setFood(false); + sets.setGamemode(false); + sets.setHealth(false); + sets.setInventory(false); + sets.setStatistics(false); + s.storeInventory(player, world); + verify(player, never()).getInventory(); + verify(player, never()).getEnderChest(); + verify(player, never()).getFoodLevel(); + verify(player, never()).getExp(); + verify(player, never()).getLevel(); + verify(player, never()).getHealth(); + verify(player, never()).getGameMode(); + verify(player, never()).getAdvancementProgress(any()); + PowerMockito.verifyStatic(Bukkit.class, never()); + Bukkit.advancementIterator(); + // No Player clearing + verify(player, never()).setExp(anyFloat()); + verify(player, never()).setLevel(anyInt()); + verify(player, never()).setTotalExperience(anyInt()); + verify(player, never()).setStatistic(any(), any(EntityType.class), anyInt()); + verify(player, never()).setStatistic(any(), any(Material.class), anyInt()); + verify(player, never()).setStatistic(any(), anyInt()); + + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.Store#storeInventory(org.bukkit.entity.Player, org.bukkit.World)}. + */ + @Test + public void testStoreInventoryAll() { + // Do not actually save anything + sets.setAdvancements(true); + sets.setEnderChest(true); + sets.setExperience(true); + sets.setFood(true); + sets.setGamemode(true); + sets.setHealth(true); + sets.setInventory(true); + sets.setStatistics(true); + s.storeInventory(player, world); + verify(player, times(2)).getInventory(); + verify(player, times(2)).getEnderChest(); + verify(player).getFoodLevel(); + verify(player).getExp(); + verify(player, times(2)).getLevel(); + verify(player).getHealth(); + verify(player).getGameMode(); + PowerMockito.verifyStatic(Bukkit.class, times(2)); + Bukkit.advancementIterator(); + // Player clearing + verify(player).setExp(0); + verify(player).setLevel(0); + verify(player).setTotalExperience(0); + verify(player, atLeastOnce()).setStatistic(any(), any(EntityType.class), anyInt()); + verify(player, atLeastOnce()).setStatistic(any(), any(Material.class), anyInt()); + verify(player, atLeastOnce()).setStatistic(any(), anyInt()); + + + } + + /** + * Test method for {@link com.wasteofplastic.invswitcher.Store#saveOnlinePlayers()}. + */ + @Test + public void testSaveOnlinePlayers() { + s.saveOnlinePlayers(); + PowerMockito.verifyStatic(Bukkit.class); + Bukkit.getOnlinePlayers(); + } + } From bec429ff87b1e5db98ae328c3736dfd11c19ae78 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 10 Feb 2023 18:52:59 -0800 Subject: [PATCH 25/39] Completed cache test. --- src/test/java/com/wasteofplastic/invswitcher/StoreTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java index 777b8d2..c447828 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java @@ -162,7 +162,9 @@ public void testGetInventory() { */ @Test public void testRemoveFromCache() { + testIsWorldStored(); s.removeFromCache(player); + assertFalse(s.isWorldStored(player, world)); } /** From be91754bcf3cff069ecfae665ed3bdda66828cfa Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 15 Apr 2023 11:32:35 -0700 Subject: [PATCH 26/39] Updated dependencies --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 923c754..887f6cd 100644 --- a/pom.xml +++ b/pom.xml @@ -59,8 +59,8 @@ 2.0.9 - 1.16.5-R0.1-SNAPSHOT - 1.18.0-SNAPSHOT + 1.19.4-R0.1-SNAPSHOT + 1.23.0-SNAPSHOT ${build.version}-SNAPSHOT From 242c719df76909e90451628298c559aaf1dd8e13 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 24 Jun 2023 13:03:34 -0700 Subject: [PATCH 27/39] Update Github Action build script --- .github/workflows/build.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c771fd5..ee925e9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,21 +11,21 @@ jobs: name: Build runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - name: Set up JDK 17 - uses: actions/setup-java@v1 + uses: actions/setup-java@v3 with: java-version: 17 - name: Cache SonarCloud packages - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: ~/.sonar/cache key: ${{ runner.os }}-sonar restore-keys: ${{ runner.os }}-sonar - name: Cache Maven packages - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: ~/.m2 key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} From 2b153f7229101945adb804f6afcff2d0cc5beda3 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 24 Jun 2023 13:55:58 -0700 Subject: [PATCH 28/39] Added distribution required for Github Action --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ee925e9..825b18d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,6 +17,7 @@ jobs: - name: Set up JDK 17 uses: actions/setup-java@v3 with: + distribution: 'adopt' java-version: 17 - name: Cache SonarCloud packages uses: actions/cache@v3 From 8649f28ab2dfe5769ef1d19d120c661dd96977f6 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 10 Jul 2023 21:23:10 -0700 Subject: [PATCH 29/39] Update Jacoco --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 887f6cd..0ad7cd1 100644 --- a/pom.xml +++ b/pom.xml @@ -283,7 +283,7 @@ org.jacoco jacoco-maven-plugin - 0.8.7 + 0.8.11 true From ee5bf51fdf93a95e2287f51a35b0068ddd203b8b Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 10 Jul 2023 21:40:56 -0700 Subject: [PATCH 30/39] Update pom.xml --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 0ad7cd1..7f8bfa3 100644 --- a/pom.xml +++ b/pom.xml @@ -283,7 +283,7 @@ org.jacoco jacoco-maven-plugin - 0.8.11 + 0.8.10 true @@ -314,4 +314,4 @@ - \ No newline at end of file + From 1f3fe2391f034a6c50e5eda7b1e560ecac46b37e Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 12 Nov 2023 12:36:52 -0800 Subject: [PATCH 31/39] Update to BentoBox 2.0.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7f8bfa3..132d073 100644 --- a/pom.xml +++ b/pom.xml @@ -60,7 +60,7 @@ 2.0.9 1.19.4-R0.1-SNAPSHOT - 1.23.0-SNAPSHOT + 2.0.0-SNAPSHOT ${build.version}-SNAPSHOT From 590fad3e0da3c8314defef49f1f9455a5990a3ab Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 6 Dec 2023 14:03:43 -0800 Subject: [PATCH 32/39] Update to 1.20.3 Also fixes test issue with Material being too big. --- pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 132d073..912a519 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ 2.0.9 - 1.19.4-R0.1-SNAPSHOT + 1.20.3-R0.1-SNAPSHOT 2.0.0-SNAPSHOT ${build.version}-SNAPSHOT @@ -290,6 +290,7 @@ **/*Names* + org/bukkit/Material* From ac2369dc37d4e9492716cb037cdf7936e71f4bf0 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 2 Jul 2024 15:49:19 -0700 Subject: [PATCH 33/39] Version 1.13.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 912a519..fc4c2ca 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ -LOCAL - 1.12.0 + 1.13.0 BentoBoxWorld_addon-invSwitcher bentobox-world From 915348c723ed3054e0d603fde7c57bb44151f177 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 2 Jul 2024 15:49:38 -0700 Subject: [PATCH 34/39] Remove constants that were causing lag at start. --- .../com/wasteofplastic/invswitcher/Store.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index d218f27..6131108 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -58,11 +58,6 @@ public class Store { private static final CharSequence THE_END = "_the_end"; private static final CharSequence NETHER = "_nether"; - @SuppressWarnings("deprecation") - private static final List BLOCKS = Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy()).toList(); - @SuppressWarnings("deprecation") - private static final List ITEMS = Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy()).toList(); - private static final List LIVING_ENTITIES = Arrays.stream(EntityType.values()).filter(EntityType::isAlive).toList(); private final Database database; private final Map cache; private final InvSwitcher addon; @@ -257,6 +252,7 @@ public void storeAndSave(Player player, World world) { database.saveObjectAsync(store); } + @SuppressWarnings("deprecation") private void saveStats(InventoryStorage store, Player player, String worldName) { store.clearStats(worldName); // Statistics @@ -265,21 +261,24 @@ private void saveStats(InventoryStorage store, Player player, String worldName) Map entMap; switch (s.getType()) { case BLOCK -> { - map = BLOCKS.stream().filter(m -> player.getStatistic(s, m) > 0) + map = Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy()) + .filter(m -> player.getStatistic(s, m) > 0) .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); if (!map.isEmpty()) { store.getBlockStats(worldName).put(s, map); } } case ITEM -> { - map = ITEMS.stream().filter(m -> player.getStatistic(s, m) > 0) + map = Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy()) + .filter(m -> player.getStatistic(s, m) > 0) .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); if (!map.isEmpty()) { store.getItemStats(worldName).put(s, map); } } case ENTITY -> { - entMap = LIVING_ENTITIES.stream().filter(m -> player.getStatistic(s, m) > 0) + entMap = Arrays.stream(EntityType.values()).filter(EntityType::isAlive) + .filter(m -> player.getStatistic(s, m) > 0) .collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v))); if (!entMap.isEmpty()) { store.getEntityStats(worldName).put(s, entMap); @@ -364,11 +363,15 @@ private void resetAdv(Player player) { } } + @SuppressWarnings("deprecation") private void resetStats(Player player, Statistic s) { switch (s.getType()) { - case BLOCK -> BLOCKS.forEach(m -> player.setStatistic(s, m, 0)); - case ITEM -> ITEMS.forEach(m -> player.setStatistic(s, m, 0)); - case ENTITY -> LIVING_ENTITIES.forEach(en -> player.setStatistic(s, en, 0)); + case BLOCK -> Arrays.stream(Material.values()).filter(Material::isBlock).filter(m -> !m.isLegacy()) + .forEach(m -> player.setStatistic(s, m, 0)); + case ITEM -> Arrays.stream(Material.values()).filter(Material::isItem).filter(m -> !m.isLegacy()) + .forEach(m -> player.setStatistic(s, m, 0)); + case ENTITY -> + Arrays.stream(EntityType.values()).filter(EntityType::isAlive).forEach(en -> player.setStatistic(s, en, 0)); case UNTYPED -> player.setStatistic(s, 0); } } From 582102bfac3db32826b2b8a2e9602aad63318d07 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 2 Jul 2024 15:54:12 -0700 Subject: [PATCH 35/39] Updated some POM items Note that there is an issue with Spigot 1.20.6 or later where ItemType cannot be mocked for some reason. --- pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index fc4c2ca..8c8df79 100644 --- a/pom.xml +++ b/pom.xml @@ -55,12 +55,12 @@ UTF-8 UTF-8 - 17 + 21 2.0.9 - 1.20.3-R0.1-SNAPSHOT - 2.0.0-SNAPSHOT + 1.20.4-R0.1-SNAPSHOT + 2.4.1-SNAPSHOT ${build.version}-SNAPSHOT From 3c9813877d267ce4cfe0819770ec0cb296627070 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 5 Jul 2024 15:57:20 -0700 Subject: [PATCH 36/39] Replace PowerMockito with Mockito 5 (#28) --- pom.xml | 22 ++--- .../invswitcher/InvSwitcherTest.java | 63 +++++++++----- .../wasteofplastic/invswitcher/StoreTest.java | 87 ++++++++++++------- .../dataobjects/InventoryStorageTest.java | 5 +- .../listeners/PlayerListenerTest.java | 26 +++--- 5 files changed, 127 insertions(+), 76 deletions(-) diff --git a/pom.xml b/pom.xml index 8c8df79..fe049d9 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ UTF-8 21 - 2.0.9 + 5.12.0 1.20.4-R0.1-SNAPSHOT 2.4.1-SNAPSHOT @@ -142,19 +142,19 @@ org.mockito mockito-core - 3.11.2 + ${mockito.version} test - org.powermock - powermock-module-junit4 - ${powermock.version} + org.mockito + mockito-inline + 5.0.0 test - org.powermock - powermock-api-mockito2 - ${powermock.version} + junit + junit + 4.13.2 test @@ -202,7 +202,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.7.0 + 3.13.0 ${java.version} @@ -210,7 +210,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M5 + 3.3.0 ${argLine} @@ -283,7 +283,7 @@ org.jacoco jacoco-maven-plugin - 0.8.10 + 0.8.12 true diff --git a/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java b/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java index 401bb08..646ade7 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/InvSwitcherTest.java @@ -6,6 +6,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -15,6 +16,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -34,10 +36,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; import com.wasteofplastic.invswitcher.listeners.PlayerListener; @@ -52,8 +53,7 @@ * @author tastybento * */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class}) +@RunWith(MockitoJUnitRunner.class) public class InvSwitcherTest { private static File jFile; @@ -95,11 +95,20 @@ public static void beforeClass() throws IOException { } /** + * @throws SecurityException + * @throws NoSuchFieldException + * @throws IllegalAccessException + * @throws IllegalArgumentException */ @Before - public void setUp() { + public void setUp() + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { // Set up plugin - Whitebox.setInternalState(BentoBox.class, "instance", plugin); + // Use reflection to set the private static field "instance" in BentoBox + Field instanceField = BentoBox.class.getDeclaredField("instance"); + + instanceField.setAccessible(true); + instanceField.set(null, plugin); when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger()); // The database type has to be created one line before the thenReturn() to work! @@ -117,10 +126,6 @@ public void setUp() { // Addons manager when(plugin.getAddonsManager()).thenReturn(am); - // Bukkit - PowerMockito.mockStatic(Bukkit.class); - when(Bukkit.getWorld(anyString())).thenReturn(world); - // World when(world.getName()).thenReturn("bskyblock-world"); } @@ -165,12 +170,17 @@ public void testOnEnable() { */ @Test public void testOnDisable() { - addon.onLoad(); - addon.getSettings().setWorlds(Set.of("bskyblock-world")); - addon.allLoaded(); - addon.onDisable(); - PowerMockito.verifyStatic(Bukkit.class); - Bukkit.getOnlinePlayers(); + // Mock the static method + try (MockedStatic mockedBukkit = mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS)) { + when(Bukkit.getWorld(anyString())).thenReturn(world); + // Run code to test + addon.onLoad(); + addon.getSettings().setWorlds(Set.of("bskyblock-world")); + addon.allLoaded(); + addon.onDisable(); + // Verify that the static method was never called + mockedBukkit.verify(() -> Bukkit.getOnlinePlayers()); + } } /** @@ -188,12 +198,17 @@ public void testOnLoad() { */ @Test public void testAllLoaded() { - addon.onLoad(); - addon.getSettings().setWorlds(Set.of("bskyblock-world")); - addon.allLoaded(); - verify(plugin).log("[InvSwitcher] Hooking into the following worlds:"); - verify(plugin, times(3)).log("[InvSwitcher] bskyblock-world"); - verify(am).registerListener(eq(addon), any(PlayerListener.class)); + // Mock the static method + try (MockedStatic mockedBukkit = mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS)) { + when(Bukkit.getWorld(anyString())).thenReturn(world); + // Run code to test + addon.onLoad(); + addon.getSettings().setWorlds(Set.of("bskyblock-world")); + addon.allLoaded(); + verify(plugin).log("[InvSwitcher] Hooking into the following worlds:"); + verify(plugin, times(3)).log("[InvSwitcher] bskyblock-world"); + verify(am).registerListener(eq(addon), any(PlayerListener.class)); + } } diff --git a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java index c447828..3c396cb 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java @@ -1,11 +1,14 @@ package com.wasteofplastic.invswitcher; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyFloat; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -13,9 +16,9 @@ import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Collections; import java.util.Comparator; import java.util.UUID; import java.util.logging.Logger; @@ -23,6 +26,7 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.World.Environment; import org.bukkit.attribute.AttributeInstance; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -33,11 +37,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; +import org.mockito.junit.MockitoJUnitRunner; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; @@ -48,8 +50,7 @@ * @author tastybento * */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, Util.class}) +@RunWith(MockitoJUnitRunner.class) public class StoreTest { @Mock @@ -58,24 +59,29 @@ public class StoreTest { private Player player; @Mock private World world; + @Mock + private Settings settings; private Store s; + private com.wasteofplastic.invswitcher.Settings sets; @Mock private Logger logger; @Before - public void setUp() { - // Bukkit - PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); + public void setUp() + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { // BentoBox BentoBox plugin = mock(BentoBox.class); - Whitebox.setInternalState(BentoBox.class, "instance", plugin); - Settings settings = mock(Settings.class); + // Use reflection to set the private static field "instance" in BentoBox + Field instanceField = BentoBox.class.getDeclaredField("instance"); + + instanceField.setAccessible(true); + instanceField.set(null, plugin); + when(plugin.getSettings()).thenReturn(settings); - when(settings.getDatabaseType()).thenReturn(DatabaseType.YAML); // Player mock UUID uuid = UUID.randomUUID(); @@ -93,22 +99,25 @@ public void setUp() { // World mock when(world.getName()).thenReturn("world_the_end_nether"); + when(world.getEnvironment()).thenReturn(Environment.NORMAL); // World 2 World fromWorld = mock(World.class); - when(fromWorld.getName()).thenReturn("from_the_end_nether"); // Settings sets = new com.wasteofplastic.invswitcher.Settings(); when(addon.getSettings()).thenReturn(sets); - when(addon.getWorlds()).thenReturn(Collections.singleton(world)); // Addon when(addon.getLogger()).thenReturn(logger); - PowerMockito.mockStatic(Util.class); - when(Util.getWorld(world)).thenReturn(world); - when(Util.getWorld(fromWorld)).thenReturn(fromWorld); + //PowerMockito.mockStatic(Util.class); + try (MockedStatic utilities = Mockito.mockStatic(Util.class)) { + utilities.when(() -> Util.getWorld(world)).thenReturn(world); + utilities.when(() -> Util.getWorld(fromWorld)).thenReturn(fromWorld); + } + DatabaseType mockDbt = mock(DatabaseType.class); + when(settings.getDatabaseType()).thenReturn(mockDbt); // Class under test s = new Store(addon); @@ -141,7 +150,11 @@ public void testStore() { @Test public void testIsWorldStored() { assertFalse(s.isWorldStored(player, world)); - s.storeInventory(player, world); + // Mock the static method + try (MockedStatic mockedBukkit = mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS)) { + // Run the code under test + s.storeInventory(player, world); + } assertTrue(s.isWorldStored(player, world)); } @@ -181,7 +194,14 @@ public void testStoreInventoryNothing() { sets.setHealth(false); sets.setInventory(false); sets.setStatistics(false); - s.storeInventory(player, world); + // Mock the static method + try (MockedStatic mockedBukkit = mockStatic(Bukkit.class)) { + // Run the code under test + s.storeInventory(player, world); + + // Verify that the static method was never called + mockedBukkit.verify(() -> Bukkit.advancementIterator(), never()); + } verify(player, never()).getInventory(); verify(player, never()).getEnderChest(); verify(player, never()).getFoodLevel(); @@ -190,8 +210,7 @@ public void testStoreInventoryNothing() { verify(player, never()).getHealth(); verify(player, never()).getGameMode(); verify(player, never()).getAdvancementProgress(any()); - PowerMockito.verifyStatic(Bukkit.class, never()); - Bukkit.advancementIterator(); + // No Player clearing verify(player, never()).setExp(anyFloat()); verify(player, never()).setLevel(anyInt()); @@ -216,7 +235,14 @@ public void testStoreInventoryAll() { sets.setHealth(true); sets.setInventory(true); sets.setStatistics(true); - s.storeInventory(player, world); + // Mock the static method + try (MockedStatic mockedBukkit = mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS)) { + // Run the code under test + s.storeInventory(player, world); + + // Verify that the static method was called + mockedBukkit.verify(() -> Bukkit.advancementIterator(), times(2)); + } verify(player, times(2)).getInventory(); verify(player, times(2)).getEnderChest(); verify(player).getFoodLevel(); @@ -224,8 +250,6 @@ public void testStoreInventoryAll() { verify(player, times(2)).getLevel(); verify(player).getHealth(); verify(player).getGameMode(); - PowerMockito.verifyStatic(Bukkit.class, times(2)); - Bukkit.advancementIterator(); // Player clearing verify(player).setExp(0); verify(player).setLevel(0); @@ -242,9 +266,14 @@ public void testStoreInventoryAll() { */ @Test public void testSaveOnlinePlayers() { - s.saveOnlinePlayers(); - PowerMockito.verifyStatic(Bukkit.class); - Bukkit.getOnlinePlayers(); + // Mock the static method + try (MockedStatic mockedBukkit = mockStatic(Bukkit.class)) { + // Run the code under test + s.saveOnlinePlayers(); + + // Verify that the static method was called + mockedBukkit.verify(() -> Bukkit.getOnlinePlayers()); + } } } diff --git a/src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java b/src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java index a0a39e8..fff1f4a 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/dataobjects/InventoryStorageTest.java @@ -19,15 +19,16 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; /** * @author tastybentos * */ -@RunWith(PowerMockRunner.class) +@RunWith(MockitoJUnitRunner.class) public class InventoryStorageTest { + private InventoryStorage is; /** diff --git a/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java b/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java index 04ff43b..901a30f 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/listeners/PlayerListenerTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertNotNull; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -17,10 +18,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.junit.MockitoJUnitRunner; import com.wasteofplastic.invswitcher.InvSwitcher; import com.wasteofplastic.invswitcher.Store; @@ -31,8 +31,7 @@ * @author tastybento * */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(Util.class) +@RunWith(MockitoJUnitRunner.class) public class PlayerListenerTest { @Mock @@ -52,8 +51,11 @@ public class PlayerListenerTest { @Before public void setUp() { // Util - PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); - when(Util.sameWorld(world, world)).thenReturn(true); + // Mock the static method + try (MockedStatic mockedBukkit = mockStatic(Util.class, Mockito.RETURNS_MOCKS)) { + when(Util.sameWorld(any(), any())).thenReturn(true); + } + when(world.getName()).thenReturn("world"); // Player when(player.getWorld()).thenReturn(world); // Addon @@ -87,9 +89,13 @@ public void testOnWorldEnterSameWorld() { @Test public void testOnWorldEnterDifferentWorld() { PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(player, notWorld); - pl.onWorldEnter(event); - verify(store).storeInventory(any(), any()); - verify(store).getInventory(any(), any()); + // Mock the static method + try (MockedStatic mockedBukkit = mockStatic(Util.class, Mockito.RETURNS_MOCKS)) { + when(Util.sameWorld(world, world)).thenReturn(true); + pl.onWorldEnter(event); + verify(store).storeInventory(any(), any()); + verify(store).getInventory(any(), any()); + } } /** From 813142b4ff9e4fafc7453d03942048012c48f0d2 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 5 Jul 2024 15:58:47 -0700 Subject: [PATCH 37/39] Update GitHub Build script to Java 21 --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 825b18d..0bc21e0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,11 +14,11 @@ jobs: - uses: actions/checkout@v3 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - - name: Set up JDK 17 + - name: Set up JDK 21 uses: actions/setup-java@v3 with: distribution: 'adopt' - java-version: 17 + java-version: 21 - name: Cache SonarCloud packages uses: actions/cache@v3 with: From f627a5a8cc8056fe175d85e36d5a3769b65a02b9 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 5 Jul 2024 16:11:50 -0700 Subject: [PATCH 38/39] Try different build script for GitHub Action --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0bc21e0..956c975 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,7 +17,7 @@ jobs: - name: Set up JDK 21 uses: actions/setup-java@v3 with: - distribution: 'adopt' + distribution: 'temurin' java-version: 21 - name: Cache SonarCloud packages uses: actions/cache@v3 @@ -35,4 +35,5 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + MAVEN_OPTS: "-XX:+EnableDynamicAgentLoading" run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \ No newline at end of file From 21018867b6d1c2170b31a16dbb46105c7faf47c1 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 5 Jul 2024 16:18:43 -0700 Subject: [PATCH 39/39] Use the silent runner --- src/test/java/com/wasteofplastic/invswitcher/StoreTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java index 3c396cb..24b87f7 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java @@ -50,7 +50,7 @@ * @author tastybento * */ -@RunWith(MockitoJUnitRunner.class) +@RunWith(MockitoJUnitRunner.Silent.class) public class StoreTest { @Mock