Skip to content

Commit

Permalink
Fix CH loading on server versions prior to MC 1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoKnight committed Jul 22, 2018
1 parent 8a87e0c commit 2e28bfa
Showing 1 changed file with 19 additions and 11 deletions.
Expand Up @@ -35,8 +35,8 @@
import org.bukkit.command.CommandSender;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.server.BroadcastMessageEvent;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.Recipe;

Expand Down Expand Up @@ -254,19 +254,27 @@ public void broadcastMessage(String message, Set<MCCommandSender> recipients) {
*/
private int bukkitBroadcastMessage(String message, Set<CommandSender> recipients) {

// Fire a BroadcastMessageEvent for this broadcast.
BroadcastMessageEvent broadcastMessageEvent = new BroadcastMessageEvent(message, recipients);
this.s.getPluginManager().callEvent(broadcastMessageEvent);
try {
// Fire a BroadcastMessageEvent for this broadcast.
// We have to use reflection to prevent the entire plugin from failing to load if not on MC 1.12+
Class broadcastMessageClass = Class.forName("org.bukkit.event.server.BroadcastMessageEvent");
Event broadcastMessageEvent = (Event) ReflectionUtils.newInstance(broadcastMessageClass,
new Class[]{String.class, Set.class},
new Object[]{message, recipients});
this.s.getPluginManager().callEvent(broadcastMessageEvent);

// Return if the event was cancelled.
if((Boolean) ReflectionUtils.invokeMethod(broadcastMessageEvent, "isCancelled")) {
return 0;
}

// Return if the event was cancelled.
if(broadcastMessageEvent.isCancelled()) {
return 0;
// Get the possibly modified message and recipients.
message = (String) ReflectionUtils.invokeMethod(broadcastMessageEvent, "getMessage");
recipients = (Set<CommandSender>) ReflectionUtils.invokeMethod(broadcastMessageEvent, "getRecipients"); // This returns the same reference, but breaks less likely.
} catch (ClassNotFoundException ex) {
// probably prior to 1.12
}

// Get the possibly modified message and recipients.
message = broadcastMessageEvent.getMessage();
recipients = broadcastMessageEvent.getRecipients(); // This returns the same reference, but breaks less likely.

// Perform the actual broadcast to all remaining recipients.
for(CommandSender recipient : recipients) {
recipient.sendMessage(message);
Expand Down

0 comments on commit 2e28bfa

Please sign in to comment.