|
35 | 35 | import org.bukkit.command.CommandSender; |
36 | 36 | import org.bukkit.command.SimpleCommandMap; |
37 | 37 | import org.bukkit.entity.Player; |
| 38 | +import org.bukkit.event.Event; |
38 | 39 | import org.bukkit.event.inventory.InventoryType; |
39 | | -import org.bukkit.event.server.BroadcastMessageEvent; |
40 | 40 | import org.bukkit.inventory.InventoryHolder; |
41 | 41 | import org.bukkit.inventory.Recipe; |
42 | 42 |
|
@@ -254,19 +254,27 @@ public void broadcastMessage(String message, Set<MCCommandSender> recipients) { |
254 | 254 | */ |
255 | 255 | private int bukkitBroadcastMessage(String message, Set<CommandSender> recipients) { |
256 | 256 |
|
257 | | - // Fire a BroadcastMessageEvent for this broadcast. |
258 | | - BroadcastMessageEvent broadcastMessageEvent = new BroadcastMessageEvent(message, recipients); |
259 | | - this.s.getPluginManager().callEvent(broadcastMessageEvent); |
| 257 | + try { |
| 258 | + // Fire a BroadcastMessageEvent for this broadcast. |
| 259 | + // We have to use reflection to prevent the entire plugin from failing to load if not on MC 1.12+ |
| 260 | + Class broadcastMessageClass = Class.forName("org.bukkit.event.server.BroadcastMessageEvent"); |
| 261 | + Event broadcastMessageEvent = (Event) ReflectionUtils.newInstance(broadcastMessageClass, |
| 262 | + new Class[]{String.class, Set.class}, |
| 263 | + new Object[]{message, recipients}); |
| 264 | + this.s.getPluginManager().callEvent(broadcastMessageEvent); |
| 265 | + |
| 266 | + // Return if the event was cancelled. |
| 267 | + if((Boolean) ReflectionUtils.invokeMethod(broadcastMessageEvent, "isCancelled")) { |
| 268 | + return 0; |
| 269 | + } |
260 | 270 |
|
261 | | - // Return if the event was cancelled. |
262 | | - if(broadcastMessageEvent.isCancelled()) { |
263 | | - return 0; |
| 271 | + // Get the possibly modified message and recipients. |
| 272 | + message = (String) ReflectionUtils.invokeMethod(broadcastMessageEvent, "getMessage"); |
| 273 | + recipients = (Set<CommandSender>) ReflectionUtils.invokeMethod(broadcastMessageEvent, "getRecipients"); // This returns the same reference, but breaks less likely. |
| 274 | + } catch (ClassNotFoundException ex) { |
| 275 | + // probably prior to 1.12 |
264 | 276 | } |
265 | 277 |
|
266 | | - // Get the possibly modified message and recipients. |
267 | | - message = broadcastMessageEvent.getMessage(); |
268 | | - recipients = broadcastMessageEvent.getRecipients(); // This returns the same reference, but breaks less likely. |
269 | | - |
270 | 278 | // Perform the actual broadcast to all remaining recipients. |
271 | 279 | for(CommandSender recipient : recipients) { |
272 | 280 | recipient.sendMessage(message); |
|
0 commit comments