Skip to content

Commit 436a1fb

Browse files
committed
Make broadcast() trigger events consistently + Extend broadcast() docs
- Make broadcast(message, players) fire an event through its Bukkit implementation so that it matches the broadcast(message) and broadcast(message, permission) behaviour. - Extend broadcast() documentation to include all behaviour.
1 parent 401d4c2 commit 436a1fb

File tree

3 files changed

+71
-23
lines changed

3 files changed

+71
-23
lines changed

src/main/java/com/laytonsmith/abstraction/MCServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public interface MCServer extends AbstractionObject {
3333

3434
void broadcastMessage(String message, String permission);
3535

36+
void broadcastMessage(String message, Set<MCCommandSender> recipients);
37+
3638
MCConsoleCommandSender getConsole();
3739

3840
MCItemFactory getItemFactory();

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCServer.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.bukkit.command.SimpleCommandMap;
3939
import org.bukkit.entity.Player;
4040
import org.bukkit.event.inventory.InventoryType;
41+
import org.bukkit.event.server.BroadcastMessageEvent;
4142
import org.bukkit.inventory.InventoryHolder;
4243
import org.bukkit.inventory.Recipe;
4344

@@ -214,6 +215,35 @@ public void broadcastMessage(String message, String permission) {
214215
s.broadcast(message, permission);
215216
}
216217

218+
@Override
219+
public void broadcastMessage(String message, Set<MCCommandSender> recipients) {
220+
221+
// Convert MCCommandsSender recipients to CommandSender recipients.
222+
Set<CommandSender> bukkitRecipients = new HashSet<>();
223+
if(recipients != null) {
224+
for(MCCommandSender mcSender : recipients) {
225+
bukkitRecipients.add((CommandSender) mcSender.getHandle());
226+
}
227+
}
228+
229+
// Fire a BroadcastMessageEvent for this broadcast.
230+
BroadcastMessageEvent broadcastMessageEvent = new BroadcastMessageEvent(message, bukkitRecipients);
231+
this.s.getPluginManager().callEvent(broadcastMessageEvent);
232+
233+
// Return if the event was cancelled.
234+
if(broadcastMessageEvent.isCancelled()) {
235+
return;
236+
}
237+
238+
// Get the possibly modified message.
239+
message = broadcastMessageEvent.getMessage();
240+
241+
// Perform the actual broadcast to all remaining recipients.
242+
for(CommandSender recipient : broadcastMessageEvent.getRecipients()) {
243+
recipient.sendMessage(message);
244+
}
245+
}
246+
217247
@Override
218248
public MCConsoleCommandSender getConsole() {
219249
return new BukkitMCConsoleCommandSender(s.getConsoleSender());
@@ -490,8 +520,8 @@ public List<MCRecipe> getRecipesFor(MCItemStack result) {
490520
@Override
491521
public List<MCRecipe> allRecipes() {
492522
List<MCRecipe> ret = new ArrayList<>();
493-
for(Iterator recipes = s.recipeIterator(); recipes.hasNext();) {
494-
Recipe recipe = (Recipe) recipes.next();
523+
for(Iterator<Recipe> recipes = s.recipeIterator(); recipes.hasNext();) {
524+
Recipe recipe = recipes.next();
495525
ret.add(BukkitConvertor.BukkitGetRecipe(recipe));
496526
}
497527
return ret;

src/main/java/com/laytonsmith/core/functions/Echoes.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
3131
import java.util.Collections;
3232
import java.util.EnumSet;
33+
import java.util.HashSet;
3334
import java.util.Map;
3435
import java.util.Set;
3536
import java.util.TreeMap;
@@ -625,7 +626,10 @@ public Integer[] numArgs() {
625626
public String docs() {
626627
return "void {message, [permission] | message, [players]} Broadcasts a message to all or some players."
627628
+ " If permission is given, only players with that permission will see the broadcast."
628-
+ " If an array is given, only players in the list will see the broadcast.";
629+
+ " If an array is given, only online players in the list will see the broadcast."
630+
+ " Offline players in the list will be ignored."
631+
+ " If permission/players is null, all players will see the broadcast."
632+
+ " Throws a CREFormatException when the given players array is associative.";
629633
}
630634

631635
@Override
@@ -646,29 +650,41 @@ public CHVersion since() {
646650
@Override
647651
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
648652
final MCServer server = Static.getServer();
649-
String permission = null;
650-
if(args.length == 2) {
651-
if(args[1] instanceof CArray) {
652-
CArray array = (CArray) args[1];
653-
if(!array.isAssociative()) {
654-
for(Construct p : array.asList()) {
655-
try {
656-
Static.GetPlayer(p, t).sendMessage(args[0].val());
657-
} catch (CREPlayerOfflineException cre) {
658-
// ignore offline players
659-
}
660-
}
661-
return CVoid.VOID;
653+
654+
// Handle "broadcast(message, [null])".
655+
if(args.length == 1 || args[1].nval() == null) { // args.length can only be 1 or 2 due to the numArgs().
656+
server.broadcastMessage(args[0].val());
657+
return CVoid.VOID;
658+
}
659+
660+
// Handle "broadcast(message, playerArray)".
661+
if(args[1] instanceof CArray) {
662+
663+
// Get the CArray and validate that it is non-associative.
664+
CArray array = (CArray) args[1];
665+
if(array.isAssociative()) {
666+
throw new CREFormatException(
667+
"Expected a non-associative array or permission as the second parameter.", t);
668+
}
669+
670+
// Get the player recipients from the array.
671+
Set<MCCommandSender> recipients = new HashSet<>();
672+
for(Construct p : array.asList()) {
673+
try {
674+
recipients.add(Static.GetPlayer(p, t));
675+
} catch (CREPlayerOfflineException cre) {
676+
// Ignore offline players.
662677
}
663-
throw new CREFormatException("Expected a normal array or permission as the second parameter.", t);
664678
}
665-
permission = args[1].nval();
666-
}
667-
if(permission == null) {
668-
server.broadcastMessage(args[0].val());
669-
} else {
670-
server.broadcastMessage(args[0].val(), permission);
679+
680+
// Perform the broadcast and return cvoid.
681+
server.broadcastMessage(args[0].val(), recipients);
682+
return CVoid.VOID;
671683
}
684+
685+
// Handle "broadcast(message, permission)".
686+
String permission = args[1].nval();
687+
server.broadcastMessage(args[0].val(), permission);
672688
return CVoid.VOID;
673689
}
674690

0 commit comments

Comments
 (0)