- * Assumes 0 patch version. - * - * @param minor Min Minor Version - * @return Meets the version requested - */ - public static boolean isVersion(int minor) { - return PaperLib.isVersion(minor); - } - - /** - * Detects if the current MC version is at least the following version. - * @param minor Min Minor Version - * @param patch Min Patch Version - * @return Meets the version requested - */ - public static boolean isVersion(int minor, int patch) { - return PaperLib.isVersion(minor, patch); - } - - /** - * Gets the current Minecraft Minor version. IE: 1.13.1 returns 13 - * @return The Minor Version - */ - public static int getMinecraftVersion() { - return PaperLib.getMinecraftVersion(); - } - - /** - * Gets the current Minecraft Patch version. IE: 1.13.1 returns 1 - * @return The Patch Version - */ - public static int getMinecraftPatchVersion() { - return PaperLib.getMinecraftPatchVersion(); + return world.isChunkGenerated(x, z); } /** @@ -562,42 +540,26 @@ public class Util { return !(!isRequiredSnapshot && isVersionSnapshot); } - /** - * Check if the server has access to the Spigot API - * @return True for Spigot and Paper environments - */ - public static boolean isSpigot() { - return PaperLib.isSpigot(); - } - /** * Check if the server has access to the Paper API * @return True for Paper environments */ public static boolean isPaper() { - return !isJUnitTest() && PaperLib.isPaper(); - } - - /** - * I don't like doing this, but otherwise we need to set a flag in every test - */ - private static boolean isJUnitTest() { - StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); - for (StackTraceElement element : stackTrace) { - if (element.getClassName().startsWith("org.junit.")) { - return true; - } + try { + Class.forName("com.destroystokyo.paper.PaperConfig"); + return true; // Paper-specific class exists + } catch (ClassNotFoundException e) { + return false; // Not a Paper server } - return false; } - /** * This method translates color codes in given string and strips whitespace after them. * This code parses both: hex and old color codes. * @param textToColor Text which color codes must be parsed. * @return String text with parsed colors and stripped whitespaces after them. */ + @SuppressWarnings("deprecation") @NonNull public static String translateColorCodes(@NonNull String textToColor) { // Use matcher to find hex patterns in given text. @@ -760,8 +722,14 @@ public class Util { * @param player - player */ public static void resetHealth(Player player) { - double maxHealth = player.getAttribute(Attribute.MAX_HEALTH).getBaseValue(); - player.setHealth(maxHealth); + try { + // Paper + double maxHealth = player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getBaseValue(); + player.setHealth(maxHealth); + } catch (Exception e) { + // Spigot + player.setHealth(20D); + } } /** @@ -862,6 +830,7 @@ public class Util { * @param input Input that need to be sanitized. * @return A sanitized input without illegal characters in names. */ + @SuppressWarnings("deprecation") public static String sanitizeInput(String input) { return ChatColor.stripColor( diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java index a65cbfecb..93536c386 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java @@ -72,6 +72,7 @@ public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest { private IslandTeamInviteCommand itl; private UUID notUUID; + @SuppressWarnings("deprecation") @Before public void setUp() throws Exception { super.setUp(); @@ -168,7 +169,7 @@ public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest { when(itemFactory.getItemMeta(any())).thenReturn(bannerMeta); when(Bukkit.getItemFactory()).thenReturn(itemFactory); Inventory inventory = mock(Inventory.class); - when(Bukkit.createInventory(eq(null), anyInt(), any())).thenReturn(inventory); + when(Bukkit.createInventory(eq(null), anyInt(), anyString())).thenReturn(inventory); // Command under test itl = new IslandTeamInviteCommand(ic); diff --git a/src/test/java/world/bentobox/bentobox/database/json/adapters/ItemStackTypeAdapterTest.java b/src/test/java/world/bentobox/bentobox/database/json/adapters/ItemStackTypeAdapterTest.java index abe0e4c78..7ad33bf8c 100644 --- a/src/test/java/world/bentobox/bentobox/database/json/adapters/ItemStackTypeAdapterTest.java +++ b/src/test/java/world/bentobox/bentobox/database/json/adapters/ItemStackTypeAdapterTest.java @@ -3,6 +3,7 @@ package world.bentobox.bentobox.database.json.adapters; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -94,7 +95,7 @@ public class ItemStackTypeAdapterTest { // Mock up the deserialization PowerMockito.mockStatic(ItemStack.class); - when(ItemStack.deserialize(any())).thenReturn(new ItemStack(Material.STONE, 4)); + when(ItemStack.deserialize(anyMap())).thenReturn(new ItemStack(Material.STONE, 4)); } @After diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreakBlocksListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreakBlocksListenerTest.java index 6a14ef448..c85479552 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreakBlocksListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BreakBlocksListenerTest.java @@ -338,7 +338,7 @@ public class BreakBlocksListenerTest extends AbstractCommonSetup { when(island.isAllowed(any(), any())).thenReturn(false); Vehicle vehicle = mock(Vehicle.class); when(vehicle.getLocation()).thenReturn(location); - when(vehicle.getType()).thenReturn(EntityType.OAK_BOAT); + when(vehicle.getType()).thenReturn(EntityType.BOAT); VehicleDamageEvent e = new VehicleDamageEvent(vehicle, mockPlayer, 10); bbl.onVehicleDamageEvent(e); assertTrue(e.isCancelled()); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java index fc7181d3f..a4a1139b3 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java @@ -181,7 +181,7 @@ public class InvincibleVisitorsListenerTest { when(top.getSize()).thenReturn(9); when(panel.getInventory()).thenReturn(top); - when(Bukkit.createInventory(any(), anyInt(), any())).thenReturn(top); + when(Bukkit.createInventory(any(), anyInt(), anyString())).thenReturn(top); } @After diff --git a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java index c64cbf844..74f6cc514 100644 --- a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java @@ -74,9 +74,6 @@ import com.github.puregero.multilib.MultiLib; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; -import io.papermc.lib.PaperLib; -import io.papermc.lib.environments.CraftBukkitEnvironment; -import io.papermc.lib.environments.Environment; import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; @@ -148,8 +145,6 @@ public class IslandsManagerTest extends AbstractCommonSetup { private Material sign; private Material wallSign; - private Environment env; - // Class under test IslandsManager im; @@ -170,7 +165,7 @@ public class IslandsManagerTest extends AbstractCommonSetup { } @Override - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "deprecation" }) @Before public void setUp() throws Exception { super.setUp(); @@ -350,10 +345,6 @@ public class IslandsManagerTest extends AbstractCommonSetup { sign = Material.BIRCH_SIGN; wallSign = Material.ACACIA_WALL_SIGN; - // PaperLib - env = new CraftBukkitEnvironment(); - PaperLib.setCustomEnvironment(env); - // Util strip spaces when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod(); @@ -423,7 +414,6 @@ public class IslandsManagerTest extends AbstractCommonSetup { assertFalse(im.isSafeLocation(location)); } - @SuppressWarnings("deprecation") @Test @Ignore("Material#isSolid() cannot be tested") public void testCheckIfSafeTrapdoor() { diff --git a/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java index d550ca413..1716a5394 100644 --- a/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java @@ -181,7 +181,7 @@ public class PlayersManagerTest { when(p.getUniqueId()).thenReturn(uuid); AttributeInstance at = mock(AttributeInstance.class); when(at.getValue()).thenReturn(20D); - when(p.getAttribute(Attribute.MAX_HEALTH)).thenReturn(at); + when(p.getAttribute(Attribute.GENERIC_MAX_HEALTH)).thenReturn(at); when(p.getName()).thenReturn("tastybento"); User.getInstance(p); diff --git a/src/test/java/world/bentobox/bentobox/mocks/ServerMocks.java b/src/test/java/world/bentobox/bentobox/mocks/ServerMocks.java index 8fd039318..282caceba 100644 --- a/src/test/java/world/bentobox/bentobox/mocks/ServerMocks.java +++ b/src/test/java/world/bentobox/bentobox/mocks/ServerMocks.java @@ -1,5 +1,6 @@ package world.bentobox.bentobox.mocks; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.notNull; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; @@ -21,10 +22,26 @@ import org.bukkit.Server; import org.bukkit.Tag; import org.bukkit.UnsafeValues; import org.eclipse.jdt.annotation.NonNull; +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 io.papermc.paper.ServerBuildInfo; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(ServerBuildInfo.class) public final class ServerMocks { + @Mock + private static ServerBuildInfo sbi; public static @NonNull Server newServer() { + PowerMockito.mockStatic(ServerBuildInfo.class, Mockito.RETURNS_MOCKS); + when(sbi.asString(any())).thenReturn("Mock server version"); + when(ServerBuildInfo.buildInfo()).thenReturn(sbi); + Server mock = mock(Server.class); Logger noOp = mock(Logger.class); @@ -66,7 +83,7 @@ public final class ServerMocks { doReturn(key).when(keyed).getKey(); return keyed; }); - }).when(registry).get(notNull()); + }).when(registry).get((NamespacedKey) notNull()); return registry; })).when(mock).getRegistry(notNull()); diff --git a/src/test/java/world/bentobox/bentobox/panels/BlueprintManagementPanelTest.java b/src/test/java/world/bentobox/bentobox/panels/BlueprintManagementPanelTest.java index aa0b06016..f0101694b 100644 --- a/src/test/java/world/bentobox/bentobox/panels/BlueprintManagementPanelTest.java +++ b/src/test/java/world/bentobox/bentobox/panels/BlueprintManagementPanelTest.java @@ -80,7 +80,7 @@ public class BlueprintManagementPanelTest { ItemFactory itemFac = mock(ItemFactory.class); when(Bukkit.getItemFactory()).thenReturn(itemFac); // Panel inventory - when(Bukkit.createInventory(any(), Mockito.anyInt(), any())).thenReturn(inv); + when(Bukkit.createInventory(any(), Mockito.anyInt(), anyString())).thenReturn(inv); // Player Player player = mock(Player.class); diff --git a/src/test/java/world/bentobox/bentobox/panels/customizable/IslandCreationPanelTest.java b/src/test/java/world/bentobox/bentobox/panels/customizable/IslandCreationPanelTest.java index 0f8bb9433..c94653483 100644 --- a/src/test/java/world/bentobox/bentobox/panels/customizable/IslandCreationPanelTest.java +++ b/src/test/java/world/bentobox/bentobox/panels/customizable/IslandCreationPanelTest.java @@ -37,6 +37,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import io.papermc.paper.ServerBuildInfo; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.GameModeAddon; @@ -48,13 +49,14 @@ import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.mocks.ServerMocks; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({ Bukkit.class, BentoBox.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, ServerBuildInfo.class }) public class IslandCreationPanelTest { @Mock @@ -89,6 +91,8 @@ public class IslandCreationPanelTest { */ @Before public void setUp() throws Exception { + ServerMocks.newServer(); + PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); // Set up plugin @@ -160,7 +164,8 @@ public class IslandCreationPanelTest { when(plugin.getIWM()).thenReturn(iwm); // Panel inventory - when(Bukkit.createInventory(any(), Mockito.anyInt(), any())).thenReturn(inv); + + when(Bukkit.createInventory(any(), Mockito.anyInt(), anyString())).thenReturn(inv); // Item Factory (needed for ItemStack) ItemFactory itemF = mock(ItemFactory.class); @@ -202,6 +207,7 @@ public class IslandCreationPanelTest { public void tearDown() { User.clearUsers(); Mockito.framework().clearInlineMocks(); + ServerMocks.unsetBukkitServer(); } /** diff --git a/src/test/java/world/bentobox/bentobox/panels/customizable/LanguagePanelTest.java b/src/test/java/world/bentobox/bentobox/panels/customizable/LanguagePanelTest.java index 2a2ad5526..7b8639bda 100644 --- a/src/test/java/world/bentobox/bentobox/panels/customizable/LanguagePanelTest.java +++ b/src/test/java/world/bentobox/bentobox/panels/customizable/LanguagePanelTest.java @@ -131,7 +131,7 @@ public class LanguagePanelTest { // Panel PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); - when(Bukkit.createInventory(any(), Mockito.anyInt(), any())).thenReturn(inv); + when(Bukkit.createInventory(any(), Mockito.anyInt(), anyString())).thenReturn(inv); // Item Factory (needed for ItemStack) ItemFactory itemF = mock(ItemFactory.class);