mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-02 08:39:49 +01:00
McMMOChatEvent: added a field to hold the dispatching plugin
Currently, there is no sane way to detect which plugin dispatched an McMMOChatEvent. This is problematic for plugins using the ChatAPI while also listening for McMMOChatEvents as it leads to a message loop being created. A "simple" solution is to store a reference to the dispatching plugin in the McMMOChatEvent. This allows for a plugin to determine if it is an event dispatched by itself, or some other plugin. In addition, this can allow for better control within third party plugins to determine where McMMOChatEvents are dispatched from. This patch is backwards with existing interfaces.
This commit is contained in:
parent
a8fa625b5c
commit
251c152efa
@ -1,5 +1,7 @@
|
|||||||
package com.gmail.nossr50.api;
|
package com.gmail.nossr50.api;
|
||||||
|
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import com.gmail.nossr50.party.PartyManager;
|
import com.gmail.nossr50.party.PartyManager;
|
||||||
import com.gmail.nossr50.util.ChatManager;
|
import com.gmail.nossr50.util.ChatManager;
|
||||||
|
|
||||||
@ -11,10 +13,27 @@ public final class ChatAPI {
|
|||||||
* </br>
|
* </br>
|
||||||
* This function is designed for API usage.
|
* This function is designed for API usage.
|
||||||
*
|
*
|
||||||
|
* @param plugin The plugin sending the message
|
||||||
* @param sender The name of the sender to display in the chat
|
* @param sender The name of the sender to display in the chat
|
||||||
* @param party The name of the party to send to
|
* @param party The name of the party to send to
|
||||||
* @param message The message to send
|
* @param message The message to send
|
||||||
*/
|
*/
|
||||||
|
public static void sendPartyChat(Plugin plugin, String sender, String party, String message) {
|
||||||
|
ChatManager.handlePartyChat(plugin, PartyManager.getParty(party), sender, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message to all members of a party
|
||||||
|
* </br>
|
||||||
|
* This function is designed for API usage.
|
||||||
|
*
|
||||||
|
* @deprecated Replaced by sendPartyChat(Plugin, String, String, String)
|
||||||
|
*
|
||||||
|
* @param sender The name of the sender to display in the chat
|
||||||
|
* @param party The name of the party to send to
|
||||||
|
* @param message The message to send
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public static void sendPartyChat(String sender, String party, String message) {
|
public static void sendPartyChat(String sender, String party, String message) {
|
||||||
ChatManager.handlePartyChat(PartyManager.getParty(party), sender, message);
|
ChatManager.handlePartyChat(PartyManager.getParty(party), sender, message);
|
||||||
}
|
}
|
||||||
@ -24,9 +43,25 @@ public final class ChatAPI {
|
|||||||
* </br>
|
* </br>
|
||||||
* This function is designed for API usage.
|
* This function is designed for API usage.
|
||||||
*
|
*
|
||||||
|
* @param plugin The plugin sending the message
|
||||||
* @param sender The name of the sender to display in the chat
|
* @param sender The name of the sender to display in the chat
|
||||||
* @param message The message to send
|
* @param message The message to send
|
||||||
*/
|
*/
|
||||||
|
public static void sendAdminChat(Plugin plugin, String sender, String message) {
|
||||||
|
ChatManager.handleAdminChat(plugin, sender, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message to administrators
|
||||||
|
* </br>
|
||||||
|
* This function is designed for API usage.
|
||||||
|
*
|
||||||
|
* @deprecated Replaced by sendAdminChat(Plugin, String, String)
|
||||||
|
*
|
||||||
|
* @param sender The name of the sender to display in the chat
|
||||||
|
* @param message The message to send
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public static void sendAdminChat(String sender, String message) {
|
public static void sendAdminChat(String sender, String message) {
|
||||||
ChatManager.handleAdminChat(sender, message);
|
ChatManager.handleAdminChat(sender, message);
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
package com.gmail.nossr50.events.chat;
|
package com.gmail.nossr50.events.chat;
|
||||||
|
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when a chat is sent to the admin chat channel
|
* Called when a chat is sent to the admin chat channel
|
||||||
*/
|
*/
|
||||||
public class McMMOAdminChatEvent extends McMMOChatEvent {
|
public class McMMOAdminChatEvent extends McMMOChatEvent {
|
||||||
|
public McMMOAdminChatEvent(Plugin plugin, String sender, String message) {
|
||||||
|
super(plugin, sender, message);
|
||||||
|
}
|
||||||
|
|
||||||
public McMMOAdminChatEvent(String sender, String message) {
|
public McMMOAdminChatEvent(String sender, String message) {
|
||||||
super(sender, message);
|
this(null, sender, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,17 +3,33 @@ package com.gmail.nossr50.events.chat;
|
|||||||
import org.bukkit.event.Cancellable;
|
import org.bukkit.event.Cancellable;
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import com.gmail.nossr50.mcMMO;
|
||||||
|
|
||||||
public class McMMOChatEvent extends Event implements Cancellable {
|
public class McMMOChatEvent extends Event implements Cancellable {
|
||||||
private boolean cancelled;
|
private boolean cancelled;
|
||||||
|
private Plugin plugin;
|
||||||
private String sender;
|
private String sender;
|
||||||
private String message;
|
private String message;
|
||||||
|
|
||||||
protected McMMOChatEvent(String sender, String message) {
|
protected McMMOChatEvent(Plugin plugin, String sender, String message) {
|
||||||
|
if (plugin == null) {
|
||||||
|
plugin = mcMMO.p;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.plugin = plugin;
|
||||||
this.sender = sender;
|
this.sender = sender;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The plugin responsible for this event
|
||||||
|
*/
|
||||||
|
public Plugin getPlugin() {
|
||||||
|
return plugin;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return String name of the player who sent the chat, or "Console"
|
* @return String name of the player who sent the chat, or "Console"
|
||||||
*/
|
*/
|
||||||
|
@ -1,16 +1,22 @@
|
|||||||
package com.gmail.nossr50.events.chat;
|
package com.gmail.nossr50.events.chat;
|
||||||
|
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when a chat is sent to a party channel
|
* Called when a chat is sent to a party channel
|
||||||
*/
|
*/
|
||||||
public class McMMOPartyChatEvent extends McMMOChatEvent {
|
public class McMMOPartyChatEvent extends McMMOChatEvent {
|
||||||
private String party;
|
private String party;
|
||||||
|
|
||||||
public McMMOPartyChatEvent(String sender, String party, String message) {
|
public McMMOPartyChatEvent(Plugin plugin, String sender, String party, String message) {
|
||||||
super(sender, message);
|
super(plugin, sender, message);
|
||||||
this.party = party;
|
this.party = party;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public McMMOPartyChatEvent(String sender, String party, String message) {
|
||||||
|
this(null, sender, party, message);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return String name of the party the message will be sent to
|
* @return String name of the party the message will be sent to
|
||||||
*/
|
*/
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.gmail.nossr50.util;
|
package com.gmail.nossr50.util;
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import com.gmail.nossr50.mcMMO;
|
import com.gmail.nossr50.mcMMO;
|
||||||
import com.gmail.nossr50.events.chat.McMMOAdminChatEvent;
|
import com.gmail.nossr50.events.chat.McMMOAdminChatEvent;
|
||||||
@ -11,8 +12,8 @@ import com.gmail.nossr50.party.Party;
|
|||||||
public final class ChatManager {
|
public final class ChatManager {
|
||||||
public ChatManager () {}
|
public ChatManager () {}
|
||||||
|
|
||||||
public static void handleAdminChat(String playerName, String message) {
|
public static void handleAdminChat(Plugin plugin, String playerName, String message) {
|
||||||
McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(playerName, message);
|
McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(plugin, playerName, message);
|
||||||
mcMMO.p.getServer().getPluginManager().callEvent(chatEvent);
|
mcMMO.p.getServer().getPluginManager().callEvent(chatEvent);
|
||||||
|
|
||||||
if (chatEvent.isCancelled()) {
|
if (chatEvent.isCancelled()) {
|
||||||
@ -30,10 +31,14 @@ public final class ChatManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void handlePartyChat(Party party, String playerName, String message) {
|
public static void handleAdminChat(String playerName, String message) {
|
||||||
String partyName = party.getName();
|
handleAdminChat(null, playerName, message);
|
||||||
|
}
|
||||||
|
|
||||||
McMMOPartyChatEvent chatEvent = new McMMOPartyChatEvent(playerName, partyName, message);
|
public static void handlePartyChat(Plugin plugin, Party party, String playerName, String message) {
|
||||||
|
String partyName = party.getName();
|
||||||
|
|
||||||
|
McMMOPartyChatEvent chatEvent = new McMMOPartyChatEvent(plugin, playerName, partyName, message);
|
||||||
mcMMO.p.getServer().getPluginManager().callEvent(chatEvent);
|
mcMMO.p.getServer().getPluginManager().callEvent(chatEvent);
|
||||||
|
|
||||||
if (chatEvent.isCancelled()) {
|
if (chatEvent.isCancelled()) {
|
||||||
@ -48,4 +53,8 @@ public final class ChatManager {
|
|||||||
member.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Prefix", new Object[] {playerName}) + partyMessage);
|
member.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Prefix", new Object[] {playerName}) + partyMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void handlePartyChat(Party party, String playerName, String message) {
|
||||||
|
handlePartyChat(null, party, playerName, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user