Adding support for both display names and sender names in the chat API.

This commit is contained in:
Glitchfinder 2013-02-01 18:59:22 -05:00
parent b633f36131
commit 60033d47ec
11 changed files with 100 additions and 93 deletions

View File

@ -8,6 +8,21 @@ import com.gmail.nossr50.util.ChatManager;
public final class ChatAPI { public final class ChatAPI {
private ChatAPI() {} private ChatAPI() {}
/**
* Send a message to all members of a party
* </br>
* This function is designed for API usage.
*
* @param plugin The plugin sending the message
* @param sender The name of the sender
* @param displayName The display name of the sender
* @param party The name of the party to send to
* @param message The message to send
*/
public static void sendPartyChat(Plugin plugin, String sender, String displayName, String party, String message) {
ChatManager.handlePartyChat(plugin, PartyManager.getParty(party), sender, displayName, message);
}
/** /**
* Send a message to all members of a party * Send a message to all members of a party
* </br> * </br>
@ -19,7 +34,7 @@ public final class ChatAPI {
* @param message The message to send * @param message The message to send
*/ */
public static void sendPartyChat(Plugin plugin, String sender, String party, String message) { public static void sendPartyChat(Plugin plugin, String sender, String party, String message) {
ChatManager.handlePartyChat(plugin, PartyManager.getParty(party), sender, message); ChatManager.handlePartyChat(plugin, PartyManager.getParty(party), sender, sender, message);
} }
/** /**
@ -35,7 +50,21 @@ public final class ChatAPI {
*/ */
@Deprecated @Deprecated
public static void sendPartyChat(String sender, String party, String message) { public static void sendPartyChat(String sender, String party, String message) {
sendPartyChat(null, party, sender, message); sendPartyChat(null, party, sender, sender, message);
}
/**
* Send a message to administrators
* </br>
* This function is designed for API usage.
*
* @param plugin The plugin sending the message
* @param sender The name of the sender
* @param displayName The display name of the sender
* @param message The message to send
*/
public static void sendAdminChat(Plugin plugin, String sender, String displayName, String message) {
ChatManager.handleAdminChat(plugin, sender, displayName, message);
} }
/** /**
@ -48,7 +77,7 @@ public final class ChatAPI {
* @param message The message to send * @param message The message to send
*/ */
public static void sendAdminChat(Plugin plugin, String sender, String message) { public static void sendAdminChat(Plugin plugin, String sender, String message) {
ChatManager.handleAdminChat(plugin, sender, message); ChatManager.handleAdminChat(plugin, sender, sender, message);
} }
/** /**
@ -63,6 +92,6 @@ public final class ChatAPI {
*/ */
@Deprecated @Deprecated
public static void sendAdminChat(String sender, String message) { public static void sendAdminChat(String sender, String message) {
sendAdminChat(null, sender, message); sendAdminChat(null, sender, sender, message);
} }
} }

View File

@ -34,6 +34,8 @@ public class Config extends ConfigLoader {
public int getSaveInterval() { return config.getInt("General.Save_Interval", 10); } public int getSaveInterval() { return config.getInt("General.Save_Interval", 10); }
public boolean getStatsTrackingEnabled() { return config.getBoolean("General.Stats_Tracking", true); } public boolean getStatsTrackingEnabled() { return config.getBoolean("General.Stats_Tracking", true); }
public boolean getEventCallbackEnabled() { return config.getBoolean("General.Event_Callback", true); } public boolean getEventCallbackEnabled() { return config.getBoolean("General.Event_Callback", true); }
public boolean getPartyDisplayNames() { return config.getBoolean("Commands.p.Use_Display_Names", true); }
public boolean getAdminDisplayNames() { return config.getBoolean("Commands.a.Use_Display_Names", true); }
/* Database Purging */ /* Database Purging */
public int getPurgeInterval() { return config.getInt("Database_Purging.Purge_Interval", -1); } public int getPurgeInterval() { return config.getInt("Database_Purging.Purge_Interval", -1); }

View File

@ -6,7 +6,7 @@ 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) { public McMMOAdminChatEvent(Plugin plugin, String sender, String displayName, String message) {
super(plugin, sender, message); super(plugin, sender, displayName, message);
} }
} }

View File

@ -9,11 +9,13 @@ public class McMMOChatEvent extends Event implements Cancellable {
private boolean cancelled; private boolean cancelled;
private Plugin plugin; private Plugin plugin;
private String sender; private String sender;
private String displayName;
private String message; private String message;
protected McMMOChatEvent(Plugin plugin, String sender, String message) { protected McMMOChatEvent(Plugin plugin, String sender, String displayName, String message) {
this.plugin = plugin; this.plugin = plugin;
this.sender = sender; this.sender = sender;
this.displayName = displayName;
this.message = message; this.message = message;
} }
@ -31,6 +33,13 @@ public class McMMOChatEvent extends Event implements Cancellable {
return sender; return sender;
} }
/**
* @return String display name of the player who sent the chat, or "Console"
*/
public String getDisplayName() {
return displayName;
}
/** /**
* @return String message that will be sent * @return String message that will be sent
*/ */
@ -38,6 +47,13 @@ public class McMMOChatEvent extends Event implements Cancellable {
return message; return message;
} }
/**
* @param displayName String display name of the player who sent the chat
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
/** /**
* @param message String message to be sent in chat * @param message String message to be sent in chat
*/ */

View File

@ -8,8 +8,8 @@ import org.bukkit.plugin.Plugin;
public class McMMOPartyChatEvent extends McMMOChatEvent { public class McMMOPartyChatEvent extends McMMOChatEvent {
private String party; private String party;
public McMMOPartyChatEvent(Plugin plugin, String sender, String party, String message) { public McMMOPartyChatEvent(Plugin plugin, String sender, String displayName, String party, String message) {
super(plugin, sender, message); super(plugin, sender, displayName, message);
this.party = party; this.party = party;
} }

View File

@ -339,11 +339,11 @@ public class PlayerListener implements Listener {
return; return;
} }
ChatManager.handlePartyChat(plugin, party, player.getDisplayName(), event.getMessage()); ChatManager.handlePartyChat(plugin, party, player.getName(), player.getDisplayName(), event.getMessage());
event.setCancelled(true); event.setCancelled(true);
} }
else if (profile.getAdminChatMode()) { else if (profile.getAdminChatMode()) {
ChatManager.handleAdminChat(plugin, player.getDisplayName(), event.getMessage()); ChatManager.handleAdminChat(plugin, player.getName(), player.getDisplayName(), event.getMessage());
event.setCancelled(true); event.setCancelled(true);
} }
} }

View File

@ -8,8 +8,8 @@ import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.commands.CommandHelper; import com.gmail.nossr50.commands.CommandHelper;
import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.events.chat.McMMOAdminChatEvent;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.ChatManager;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.Users; import com.gmail.nossr50.util.Users;
@ -60,43 +60,11 @@ public class ACommand implements CommandExecutor {
if (sender instanceof Player) { if (sender instanceof Player) {
Player player = (Player) sender; Player player = (Player) sender;
ChatManager.handleAdminChat(mcMMO.p, player.getName(), player.getDisplayName(), message);
McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(mcMMO.p, player.getName(), message);
mcMMO.p.getServer().getPluginManager().callEvent(chatEvent);
if (chatEvent.isCancelled()) {
return true;
}
message = chatEvent.getMessage();
String prefix = LocaleLoader.getString("Commands.AdminChat.Prefix", new Object[] {player.getName()} );
mcMMO.p.getLogger().info("[A]<" + player.getName() + "> " + message);
for (Player otherPlayer : mcMMO.p.getServer().getOnlinePlayers()) {
if (Permissions.adminChat(otherPlayer) || otherPlayer.isOp()) {
otherPlayer.sendMessage(prefix + message);
}
}
} }
else { else {
McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(mcMMO.p, "Console", message); String ssender = LocaleLoader.getString("Commands.Chat.Console");
mcMMO.p.getServer().getPluginManager().callEvent(chatEvent); ChatManager.handleAdminChat(mcMMO.p, ssender, ssender, message);
if (chatEvent.isCancelled()) {
return true;
}
message = chatEvent.getMessage();
String prefix = LocaleLoader.getString("Commands.AdminChat.Prefix", new Object[] {LocaleLoader.getString("Commands.Chat.Console")} );
mcMMO.p.getLogger().info("[A]<*Console*> " + message);
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
if (Permissions.adminChat(player) || player.isOp()) {
player.sendMessage(prefix + message);
}
}
} }
return true; return true;

View File

@ -8,10 +8,10 @@ import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.commands.CommandHelper; import com.gmail.nossr50.commands.CommandHelper;
import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.events.chat.McMMOPartyChatEvent;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.Party; import com.gmail.nossr50.party.Party;
import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.party.PartyManager;
import com.gmail.nossr50.util.ChatManager;
import com.gmail.nossr50.util.Users; import com.gmail.nossr50.util.Users;
public class PCommand implements CommandExecutor { public class PCommand implements CommandExecutor {
@ -73,22 +73,7 @@ public class PCommand implements CommandExecutor {
} }
String message = builder.toString(); String message = builder.toString();
ChatManager.handlePartyChat(plugin, party, player.getName(), player.getDisplayName(), message);
McMMOPartyChatEvent chatEvent = new McMMOPartyChatEvent(plugin, player.getName(), party.getName(), message);
plugin.getServer().getPluginManager().callEvent(chatEvent);
if (chatEvent.isCancelled()) {
return true;
}
message = chatEvent.getMessage();
String prefix = LocaleLoader.getString("Commands.Party.Chat.Prefix", new Object[] {player.getName()} );
plugin.getLogger().info("[P](" + party.getName() + ")" + "<" + player.getName() + "> " + message);
for (Player member : party.getOnlineMembers()) {
member.sendMessage(prefix + message);
}
} }
else { else {
if (args.length < 2) { if (args.length < 2) {
@ -96,7 +81,9 @@ public class PCommand implements CommandExecutor {
return true; return true;
} }
if (!PartyManager.isParty(args[0])) { Party party = PartyManager.getParty(args[0]);
if (party == null) {
sender.sendMessage(LocaleLoader.getString("Party.InvalidName")); sender.sendMessage(LocaleLoader.getString("Party.InvalidName"));
return true; return true;
} }
@ -109,23 +96,10 @@ public class PCommand implements CommandExecutor {
builder.append(args[i]); builder.append(args[i]);
} }
String ssender = LocaleLoader.getString("Commands.Chat.Console");
String message = builder.toString(); String message = builder.toString();
McMMOPartyChatEvent chatEvent = new McMMOPartyChatEvent(plugin, "Console", args[0], message); ChatManager.handlePartyChat(plugin, party, ssender, ssender, message);
plugin.getServer().getPluginManager().callEvent(chatEvent);
if (chatEvent.isCancelled()) {
return true;
}
message = chatEvent.getMessage();
String prefix = LocaleLoader.getString("Commands.Party.Chat.Prefix", new Object[] {LocaleLoader.getString("Commands.Chat.Console")} );
plugin.getLogger().info("[P](" + args[0] + ")" + "<*Console*> " + message);
for (Player member : PartyManager.getOnlineMembers(args[0])) {
member.sendMessage(prefix + message);
}
} }
return true; return true;

View File

@ -1,9 +1,11 @@
package com.gmail.nossr50.util; package com.gmail.nossr50.util;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.events.chat.McMMOAdminChatEvent; import com.gmail.nossr50.events.chat.McMMOAdminChatEvent;
import com.gmail.nossr50.events.chat.McMMOPartyChatEvent; import com.gmail.nossr50.events.chat.McMMOPartyChatEvent;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
@ -12,41 +14,53 @@ import com.gmail.nossr50.party.Party;
public final class ChatManager { public final class ChatManager {
public ChatManager () {} public ChatManager () {}
public static void handleAdminChat(Plugin plugin, String playerName, String message) { public static void handleAdminChat(Plugin plugin, String playerName, String displayName, String message) {
McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(plugin, playerName, message); McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(plugin, playerName, displayName, message);
mcMMO.p.getServer().getPluginManager().callEvent(chatEvent); mcMMO.p.getServer().getPluginManager().callEvent(chatEvent);
if (chatEvent.isCancelled()) { if (chatEvent.isCancelled()) {
return; return;
} }
if(Config.getInstance().getAdminDisplayNames())
displayName = chatEvent.getDisplayName();
else
displayName = chatEvent.getSender();
String adminMessage = chatEvent.getMessage(); String adminMessage = chatEvent.getMessage();
mcMMO.p.getLogger().info("[A]<" + playerName + "> " + adminMessage);
for (Player otherPlayer : mcMMO.p.getServer().getOnlinePlayers()) { for (Player otherPlayer : mcMMO.p.getServer().getOnlinePlayers()) {
if (Permissions.adminChat(otherPlayer) || otherPlayer.isOp()) { if (Permissions.adminChat(otherPlayer) || otherPlayer.isOp()) {
otherPlayer.sendMessage(LocaleLoader.getString("Commands.AdminChat.Prefix", new Object[] {playerName}) + adminMessage); otherPlayer.sendMessage(LocaleLoader.getString("Commands.AdminChat.Prefix", new Object[] {displayName}) + adminMessage);
}
} }
} }
public static void handlePartyChat(Plugin plugin, Party party, String playerName, String message) { displayName = ChatColor.stripColor(displayName);
mcMMO.p.getLogger().info("[A]<" + displayName + "> " + adminMessage);
}
public static void handlePartyChat(Plugin plugin, Party party, String playerName, String displayName, String message) {
String partyName = party.getName(); String partyName = party.getName();
McMMOPartyChatEvent chatEvent = new McMMOPartyChatEvent(plugin, playerName, partyName, message); McMMOPartyChatEvent chatEvent = new McMMOPartyChatEvent(plugin, playerName, displayName, partyName, message);
mcMMO.p.getServer().getPluginManager().callEvent(chatEvent); mcMMO.p.getServer().getPluginManager().callEvent(chatEvent);
if (chatEvent.isCancelled()) { if (chatEvent.isCancelled()) {
return; return;
} }
if(Config.getInstance().getPartyDisplayNames())
displayName = chatEvent.getDisplayName();
else
displayName = chatEvent.getSender();
String partyMessage = chatEvent.getMessage(); String partyMessage = chatEvent.getMessage();
mcMMO.p.getLogger().info("[P](" + partyName + ")" + "<" + playerName + "> " + partyMessage);
for (Player member : party.getOnlineMembers()) { for (Player member : party.getOnlineMembers()) {
member.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Prefix", new Object[] {playerName}) + partyMessage); member.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Prefix", new Object[] {displayName}) + partyMessage);
} }
displayName = ChatColor.stripColor(displayName);
mcMMO.p.getLogger().info("[P](" + partyName + ")" + "<" + displayName + "> " + partyMessage);
} }
} }

View File

@ -369,5 +369,9 @@ Commands:
Request_Timeout: 300 Request_Timeout: 300
p: p:
Enabled: true Enabled: true
#Allow mcMMO to use player display names in chat instead of their usernames
Use_Display_Names: true
a: a:
Enabled: true Enabled: true
#Allow mcMMO to use player display names in chat instead of their usernames
Use_Display_Names: true