Add Util method to enable multi-lingual broadcasts.

This commit is contained in:
tastybento 2021-03-21 09:04:30 -07:00
parent 1bc58facee
commit 9e0fdbd210
2 changed files with 55 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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<UUID, String> online = new HashMap<>();
Set<Player> 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<Set<Player>>) invocation -> onlinePlayers);
when(user.isPlayer()).thenReturn(true);
// Locales & Placeholders
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
PlaceholdersManager phm = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(phm);
when(phm.replacePlaceholders(any(), any())).thenAnswer((Answer<String>) 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);
}
}