From aa66e1f5502b79a22278d8c46cd34a81f909e0dd Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 25 Nov 2024 20:15:25 -0800 Subject: [PATCH] Ship to MC 1.21.3 --- pom.xml | 6 +- src/main/resources/addon.yml | 2 +- src/main/resources/plugin.yml | 2 +- .../bentobox/warps/WarpSignsManagerTest.java | 48 +++++++++++- .../listeners/WarpSignsListenerTest.java | 76 +++++++++++++++---- 5 files changed, 113 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index b879d01..3dd98dd 100644 --- a/pom.xml +++ b/pom.xml @@ -58,15 +58,15 @@ 2.0.9 - 1.19.4-R0.1-SNAPSHOT - 2.0.0-SNAPSHOT + 1.21.3-R0.1-SNAPSHOT + 3.0.0-SNAPSHOT 2.7.0-SNAPSHOT ${build.version}-SNAPSHOT -LOCAL - 1.15.1 + 1.16.0 BentoBoxWorld_Warps bentobox-world diff --git a/src/main/resources/addon.yml b/src/main/resources/addon.yml index b8de846..6be901f 100755 --- a/src/main/resources/addon.yml +++ b/src/main/resources/addon.yml @@ -2,7 +2,7 @@ name: Warps main: world.bentobox.warps.Warp version: ${version}${build.number} icon: OAK_SIGN -api-version: 1.17 +api-version: 3.0.0 authors: tastybento diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index bbd3194..3fa488c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: BentoBox-Warps main: world.bentobox.warps.WarpsPladdon version: ${project.version}${build.number} -api-version: "1.17" +api-version: "1.21" authors: [tastybento] contributors: ["The BentoBoxWorld Community"] diff --git a/src/test/java/world/bentobox/warps/WarpSignsManagerTest.java b/src/test/java/world/bentobox/warps/WarpSignsManagerTest.java index a6e3eb2..b55fe35 100644 --- a/src/test/java/world/bentobox/warps/WarpSignsManagerTest.java +++ b/src/test/java/world/bentobox/warps/WarpSignsManagerTest.java @@ -4,10 +4,12 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -34,6 +36,7 @@ import org.bukkit.block.BlockFace; import org.bukkit.block.Sign; import org.bukkit.entity.Player; +import org.bukkit.entity.Player.Spigot; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.plugin.PluginManager; import org.eclipse.jdt.annotation.Nullable; @@ -51,6 +54,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import net.md_5.bungee.api.chat.TextComponent; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.AbstractDatabaseHandler; @@ -119,7 +123,45 @@ public class WarpSignsManagerTest { private IslandsManager im; @Mock private Island island; + @Mock + private Spigot spigot; + + /** + * Check that spigot sent the message + * @param message - message to check + */ + public void checkSpigotMessage(String expectedMessage) { + checkSpigotMessage(expectedMessage, 1); + } + + public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) { + // Capture the argument passed to spigot().sendMessage(...) if messages are sent + ArgumentCaptor captor = ArgumentCaptor.forClass(TextComponent.class); + // Verify that sendMessage() was called at least 0 times (capture any sent messages) + verify(spigot, atLeast(0)).sendMessage(captor.capture()); + + // Get all captured TextComponents + List capturedMessages = captor.getAllValues(); + + // Count the number of occurrences of the expectedMessage in the captured messages + long actualOccurrences = capturedMessages.stream().map(component -> component.toLegacyText()) // Convert each TextComponent to plain text + .filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text + .count(); // Count how many times the expected message appears + + // Assert that the number of occurrences matches the expectedOccurrences + assertEquals("Expected message occurrence mismatch: " + expectedMessage, expectedOccurrences, + actualOccurrences); + } + + public void checkNoSpigotMessages() { + try { + // Verify that sendMessage was never called + verify(spigot, never()).sendMessage(any(TextComponent.class)); + } catch (AssertionError e) { + fail("Expected no messages to be sent, but some messages were sent."); + } + } @SuppressWarnings("unchecked") @BeforeClass @@ -146,6 +188,7 @@ public void setUp() throws Exception { when(player.getUniqueId()).thenReturn(uuid); when(player.isOnline()).thenReturn(true); when(player.canSee(any(Player.class))).thenReturn(true); + when(player.spigot()).thenReturn(spigot); User.setPlugin(plugin); User.getInstance(player); @@ -336,7 +379,7 @@ public void testAddWarpNullLocation() { @Test public void testAddWarpReplaceOldSign() { assertTrue(wsm.addWarp(uuid, location)); - verify(player).sendMessage("warps.sign-removed"); + this.checkSpigotMessage("warps.sign-removed"); } /** @@ -345,7 +388,7 @@ public void testAddWarpReplaceOldSign() { @Test public void testAddWarpReplaceOldSignDifferentPlayer() { assertTrue(wsm.addWarp(UUID.randomUUID(), location)); - verify(player).sendMessage("warps.sign-removed"); + this.checkSpigotMessage("warps.sign-removed"); } /** @@ -446,7 +489,6 @@ public void testWarpPlayer() { wsm.warpPlayer(world, u, uuid); PowerMockito.verifyStatic(Util.class); Util.teleportAsync(eq(p), any(), eq(TeleportCause.COMMAND)); - verify(player).sendMessage(anyString()); verify(pim).callEvent(any(WarpInitiateEvent.class)); } diff --git a/src/test/java/world/bentobox/warps/listeners/WarpSignsListenerTest.java b/src/test/java/world/bentobox/warps/listeners/WarpSignsListenerTest.java index 66c15b2..2840671 100644 --- a/src/test/java/world/bentobox/warps/listeners/WarpSignsListenerTest.java +++ b/src/test/java/world/bentobox/warps/listeners/WarpSignsListenerTest.java @@ -5,12 +5,19 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.UUID; @@ -27,12 +34,14 @@ import org.bukkit.block.Sign; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; +import org.bukkit.entity.Player.Spigot; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.plugin.PluginManager; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.stubbing.Answer; @@ -40,6 +49,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import net.md_5.bungee.api.chat.TextComponent; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent; import world.bentobox.bentobox.api.flags.Flag; @@ -50,10 +60,10 @@ import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.util.Util; -import world.bentobox.warps.objects.PlayerWarp; import world.bentobox.warps.Warp; -import world.bentobox.warps.managers.WarpSignsManager; import world.bentobox.warps.config.Settings; +import world.bentobox.warps.managers.WarpSignsManager; +import world.bentobox.warps.objects.PlayerWarp; /** * @author tastybento @@ -86,6 +96,8 @@ public class WarpSignsListenerTest { private IslandWorldManager iwm; @Mock private Island island; + @Mock + private Spigot spigot; @Before public void setUp() { @@ -119,6 +131,7 @@ public void setUp() { when(player.hasPermission(anyString())).thenReturn(false); UUID uuid = UUID.randomUUID(); when(player.getUniqueId()).thenReturn(uuid); + when(player.spigot()).thenReturn(spigot); s = mock(Sign.class); when(s.getLine(anyInt())).thenReturn(ChatColor.GREEN + "[WELCOME]"); when(block.getState()).thenReturn(s); @@ -185,6 +198,43 @@ public void setUp() { } + /** + * Check that spigot sent the message + * @param message - message to check + */ + public void checkSpigotMessage(String expectedMessage) { + checkSpigotMessage(expectedMessage, 1); + } + + public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) { + // Capture the argument passed to spigot().sendMessage(...) if messages are sent + ArgumentCaptor captor = ArgumentCaptor.forClass(TextComponent.class); + + // Verify that sendMessage() was called at least 0 times (capture any sent messages) + verify(spigot, atLeast(0)).sendMessage(captor.capture()); + + // Get all captured TextComponents + List capturedMessages = captor.getAllValues(); + + // Count the number of occurrences of the expectedMessage in the captured messages + long actualOccurrences = capturedMessages.stream().map(component -> component.toLegacyText()) // Convert each TextComponent to plain text + .filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text + .count(); // Count how many times the expected message appears + + // Assert that the number of occurrences matches the expectedOccurrences + assertEquals("Expected message occurrence mismatch: " + expectedMessage, expectedOccurrences, + actualOccurrences); + } + + public void checkNoSpigotMessages() { + try { + // Verify that sendMessage was never called + verify(spigot, never()).sendMessage(any(TextComponent.class)); + } catch (AssertionError e) { + fail("Expected no messages to be sent, but some messages were sent."); + } + } + @Test public void testWarpSignsListener() { assertNotNull(new WarpSignsListener(addon)); @@ -240,7 +290,7 @@ public void testOnSignRemovePlayerSignWrongPlayer() { BlockBreakEvent e = new BlockBreakEvent(block, player); wsl.onSignBreak(e); assertTrue(e.isCancelled()); - verify(player).sendMessage("warps.error.no-remove"); + checkSpigotMessage("warps.error.no-remove"); } @Test @@ -301,7 +351,7 @@ public void testOnCreateNotGameWorldAllowed() { SignChangeEvent e = new SignChangeEvent(block, player, lines); when(addon.inRegisteredWorld(any())).thenReturn(false); wsl.onSignWarpCreate(e); - verify(player).sendMessage("warps.success"); + checkSpigotMessage("warps.success"); assertEquals(ChatColor.GREEN + "[WELCOME]", e.getLine(0)); } @@ -314,7 +364,7 @@ public void testOnCreateWithoutCorrectRankNotAllowed() { when(island.getRank(player.getUniqueId())).thenReturn(0); when(island.getFlag(any())).thenReturn(1000); wsl.onSignWarpCreate(e); - verify(player).sendMessage("warps.error.not-correct-rank"); + checkSpigotMessage("warps.error.not-correct-rank"); } @Test @@ -374,7 +424,7 @@ public void testOnCreateNotGameWorldNoPerm() { SignChangeEvent e = new SignChangeEvent(block, player, lines); when(addon.inRegisteredWorld(any())).thenReturn(false); wsl.onSignWarpCreate(e); - verify(player).sendMessage("warps.error.no-permission"); + checkSpigotMessage("warps.error.no-permission"); } @Test @@ -386,7 +436,7 @@ public void testOnCreateWrongText() { SignChangeEvent e = new SignChangeEvent(block, player, lines); wsl.onSignWarpCreate(e); verify(settings).getWelcomeLine(); - verify(player, Mockito.never()).sendMessage(anyString()); + checkNoSpigotMessages(); } @Test @@ -395,7 +445,7 @@ public void testOnCreateNoPerm() { WarpSignsListener wsl = new WarpSignsListener(addon); SignChangeEvent e = new SignChangeEvent(block, player, lines); wsl.onSignWarpCreate(e); - verify(player).sendMessage("warps.error.no-permission"); + this.checkSpigotMessage("warps.error.no-permission"); } @Test @@ -405,7 +455,7 @@ public void testOnLevelPresentNotHighEnough() { WarpSignsListener wsl = new WarpSignsListener(addon); SignChangeEvent e = new SignChangeEvent(block, player, lines); wsl.onSignWarpCreate(e); - verify(player).sendMessage("warps.error.not-enough-level"); + this.checkSpigotMessage("warps.error.not-enough-level"); } @Test @@ -415,7 +465,7 @@ public void testOnNoIsland() { WarpSignsListener wsl = new WarpSignsListener(addon); SignChangeEvent e = new SignChangeEvent(block, player, lines); wsl.onSignWarpCreate(e); - verify(player).sendMessage("warps.error.not-on-island"); + this.checkSpigotMessage("warps.error.not-on-island"); assertEquals(ChatColor.RED + "[WELCOME]", e.getLine(0)); } @@ -426,7 +476,7 @@ public void testCreateNoSignAlreadyUniqueSpot() { WarpSignsListener wsl = new WarpSignsListener(addon); SignChangeEvent e = new SignChangeEvent(block, player, lines); wsl.onSignWarpCreate(e); - verify(player).sendMessage("warps.success"); + this.checkSpigotMessage("warps.success"); assertEquals(ChatColor.GREEN + "[WELCOME]", e.getLine(0)); } @@ -436,7 +486,7 @@ public void testCreateNoSignDeactivateOldSign() { WarpSignsListener wsl = new WarpSignsListener(addon); SignChangeEvent e = new SignChangeEvent(block, player, lines); wsl.onSignWarpCreate(e); - verify(player).sendMessage("warps.success"); + this.checkSpigotMessage("warps.success"); assertEquals(ChatColor.GREEN + "[WELCOME]", e.getLine(0)); verify(s).setLine(0, ChatColor.RED + "[WELCOME]"); }