Skip to content

Commit

Permalink
Merge pull request #142 from BentoBoxWorld/MC_1.21.3
Browse files Browse the repository at this point in the history
Ship to MC 1.21.3
  • Loading branch information
tastybento authored Nov 26, 2024
2 parents 2617020 + aa66e1f commit d2f32d3
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 21 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@
<!-- Non-minecraft related dependencies -->
<powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.19.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.0.0-SNAPSHOT</bentobox.version>
<spigot.version>1.21.3-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>3.0.0-SNAPSHOT</bentobox.version>
<level.version>2.7.0-SNAPSHOT</level.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>1.15.1</build.version>
<build.version>1.16.0</build.version>
<!-- Sonar Cloud -->
<sonar.projectKey>BentoBoxWorld_Warps</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/addon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
48 changes: 45 additions & 3 deletions src/test/java/world/bentobox/warps/WarpSignsManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<TextComponent> 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<TextComponent> 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
Expand All @@ -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);

Expand Down Expand Up @@ -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");
}

/**
Expand All @@ -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");
}

/**
Expand Down Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,19 +34,22 @@
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;
import org.powermock.api.mockito.PowerMockito;
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;
Expand All @@ -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
Expand Down Expand Up @@ -86,6 +96,8 @@ public class WarpSignsListenerTest {
private IslandWorldManager iwm;
@Mock
private Island island;
@Mock
private Spigot spigot;

@Before
public void setUp() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<TextComponent> 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<TextComponent> 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));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));
}

Expand All @@ -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));
}

Expand All @@ -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]");
}
Expand Down

0 comments on commit d2f32d3

Please sign in to comment.