diff --git a/src/main/java/world/bentobox/bentobox/util/Util.java b/src/main/java/world/bentobox/bentobox/util/Util.java index fb61465ff..82eda60b5 100644 --- a/src/main/java/world/bentobox/bentobox/util/Util.java +++ b/src/main/java/world/bentobox/bentobox/util/Util.java @@ -18,6 +18,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Chunk; import org.bukkit.Location; +import org.bukkit.Server; import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.attribute.Attribute; @@ -688,5 +689,22 @@ public class Util { throw new IllegalStateException("Class " + clazz.getName() + " does not implement NMSAbstraction"); } } - + + /** + * Broadcast a localized message to all players with the permission {@link Server#BROADCAST_CHANNEL_USERS} + * + * @param localeKey locale key for the message to broadcast + * @param variables any variables for the message + * @return number of message recipients + */ + public static int broadcast(String localeKey, String... variables) { + int count = 0; + for (Player p : Bukkit.getOnlinePlayers()) { + if (p.hasPermission(Server.BROADCAST_CHANNEL_USERS)) { + User.getInstance(p).sendMessage(localeKey, variables); + count++; + } + } + return count; + } } diff --git a/src/test/java/world/bentobox/bentobox/util/UtilTest.java b/src/test/java/world/bentobox/bentobox/util/UtilTest.java index d166a376f..639a58e08 100644 --- a/src/test/java/world/bentobox/bentobox/util/UtilTest.java +++ b/src/test/java/world/bentobox/bentobox/util/UtilTest.java @@ -42,8 +42,11 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.LocalesManager; +import world.bentobox.bentobox.managers.PlaceholdersManager; /** * @author tastybento @@ -96,6 +99,7 @@ public class UtilTest { when(Bukkit.getConsoleSender()).thenReturn(sender); // Bukkit - online players + User.setPlugin(plugin); Map online = new HashMap<>(); Set onlinePlayers = new HashSet<>(); @@ -104,12 +108,24 @@ public class UtilTest { UUID uuid = UUID.randomUUID(); when(p1.getUniqueId()).thenReturn(uuid); when(p1.getName()).thenReturn(name); + when(p1.hasPermission(anyString())).thenReturn(true); online.put(uuid, name); onlinePlayers.add(p1); + // Add to User cache + User.getInstance(p1); } when(Bukkit.getOnlinePlayers()).then((Answer>) invocation -> onlinePlayers); when(user.isPlayer()).thenReturn(true); + // Locales & Placeholders + LocalesManager lm = mock(LocalesManager.class); + when(lm.get(any(), any())).thenAnswer((Answer) invocation -> invocation.getArgument(1, String.class)); + PlaceholdersManager phm = mock(PlaceholdersManager.class); + when(plugin.getPlaceholdersManager()).thenReturn(phm); + when(phm.replacePlaceholders(any(), any())).thenAnswer((Answer) invocation -> invocation.getArgument(1, String.class)); + + when(plugin.getLocalesManager()).thenReturn(lm); + } @After @@ -442,4 +458,24 @@ public class UtilTest { Bukkit.dispatchCommand(sender, "replace tastybento"); verify(plugin).logError("Could not execute test command as console: replace tastybento"); } + + /** + * Test for {@link Util#broadcast(String, String...)} + */ + @Test + public void testBroadcastStringStringNoPlayers() { + when(Bukkit.getOnlinePlayers()).thenReturn(Collections.emptySet()); + int result = Util.broadcast("test.key", TextVariables.DESCRIPTION, "hello"); + assertEquals(0, result); + } + + /** + * Test for {@link Util#broadcast(String, String...)} + */ + @Test + public void testBroadcastStringStringHasPerm() { + int result = Util.broadcast("test.key", TextVariables.DESCRIPTION, "hello"); + assertEquals(11, result); + + } }