From 60033d47ecf5b8e79f2c33309f26189e3eedf202 Mon Sep 17 00:00:00 2001 From: Glitchfinder Date: Fri, 1 Feb 2013 18:59:22 -0500 Subject: [PATCH] Adding support for both display names and sender names in the chat API. --- .../java/com/gmail/nossr50/api/ChatAPI.java | 37 +++++++++++++++-- .../nossr50/commands/admin/McgodCommand.java | 2 +- .../java/com/gmail/nossr50/config/Config.java | 2 + .../events/chat/McMMOAdminChatEvent.java | 4 +- .../nossr50/events/chat/McMMOChatEvent.java | 18 ++++++++- .../events/chat/McMMOPartyChatEvent.java | 4 +- .../nossr50/listeners/PlayerListener.java | 4 +- .../nossr50/party/commands/ACommand.java | 40 ++----------------- .../nossr50/party/commands/PCommand.java | 40 ++++--------------- .../com/gmail/nossr50/util/ChatManager.java | 36 ++++++++++++----- src/main/resources/config.yml | 6 ++- 11 files changed, 100 insertions(+), 93 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/api/ChatAPI.java b/src/main/java/com/gmail/nossr50/api/ChatAPI.java index 357b285df..66b7dad95 100644 --- a/src/main/java/com/gmail/nossr50/api/ChatAPI.java +++ b/src/main/java/com/gmail/nossr50/api/ChatAPI.java @@ -8,6 +8,21 @@ import com.gmail.nossr50.util.ChatManager; public final class ChatAPI { private ChatAPI() {} + /** + * Send a message to all members of a party + *
+ * 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 *
@@ -19,7 +34,7 @@ public final class ChatAPI { * @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); + ChatManager.handlePartyChat(plugin, PartyManager.getParty(party), sender, sender, message); } /** @@ -35,7 +50,21 @@ public final class ChatAPI { */ @Deprecated 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 + *
+ * 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 */ 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 public static void sendAdminChat(String sender, String message) { - sendAdminChat(null, sender, message); + sendAdminChat(null, sender, sender, message); } } diff --git a/src/main/java/com/gmail/nossr50/commands/admin/McgodCommand.java b/src/main/java/com/gmail/nossr50/commands/admin/McgodCommand.java index 6446d3cd1..2a1cf4ae7 100644 --- a/src/main/java/com/gmail/nossr50/commands/admin/McgodCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/admin/McgodCommand.java @@ -18,7 +18,7 @@ public class McgodCommand implements CommandExecutor { PlayerProfile profile; switch (args.length) { - case 0: + case 0: if (CommandHelper.noConsoleUsage(sender)) { return true; } diff --git a/src/main/java/com/gmail/nossr50/config/Config.java b/src/main/java/com/gmail/nossr50/config/Config.java index 6484fd5d4..a9e8a2f2e 100644 --- a/src/main/java/com/gmail/nossr50/config/Config.java +++ b/src/main/java/com/gmail/nossr50/config/Config.java @@ -34,6 +34,8 @@ public class Config extends ConfigLoader { public int getSaveInterval() { return config.getInt("General.Save_Interval", 10); } public boolean getStatsTrackingEnabled() { return config.getBoolean("General.Stats_Tracking", 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 */ public int getPurgeInterval() { return config.getInt("Database_Purging.Purge_Interval", -1); } diff --git a/src/main/java/com/gmail/nossr50/events/chat/McMMOAdminChatEvent.java b/src/main/java/com/gmail/nossr50/events/chat/McMMOAdminChatEvent.java index 0f45bd33d..2d33c4d1b 100644 --- a/src/main/java/com/gmail/nossr50/events/chat/McMMOAdminChatEvent.java +++ b/src/main/java/com/gmail/nossr50/events/chat/McMMOAdminChatEvent.java @@ -6,7 +6,7 @@ import org.bukkit.plugin.Plugin; * Called when a chat is sent to the admin chat channel */ public class McMMOAdminChatEvent extends McMMOChatEvent { - public McMMOAdminChatEvent(Plugin plugin, String sender, String message) { - super(plugin, sender, message); + public McMMOAdminChatEvent(Plugin plugin, String sender, String displayName, String message) { + super(plugin, sender, displayName, message); } } diff --git a/src/main/java/com/gmail/nossr50/events/chat/McMMOChatEvent.java b/src/main/java/com/gmail/nossr50/events/chat/McMMOChatEvent.java index 7143ca8ef..e7d8b3dcc 100644 --- a/src/main/java/com/gmail/nossr50/events/chat/McMMOChatEvent.java +++ b/src/main/java/com/gmail/nossr50/events/chat/McMMOChatEvent.java @@ -9,11 +9,13 @@ public class McMMOChatEvent extends Event implements Cancellable { private boolean cancelled; private Plugin plugin; private String sender; + private String displayName; 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.sender = sender; + this.displayName = displayName; this.message = message; } @@ -31,6 +33,13 @@ public class McMMOChatEvent extends Event implements Cancellable { 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 */ @@ -38,6 +47,13 @@ public class McMMOChatEvent extends Event implements Cancellable { 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 */ diff --git a/src/main/java/com/gmail/nossr50/events/chat/McMMOPartyChatEvent.java b/src/main/java/com/gmail/nossr50/events/chat/McMMOPartyChatEvent.java index 3ee44641c..7e03dc644 100644 --- a/src/main/java/com/gmail/nossr50/events/chat/McMMOPartyChatEvent.java +++ b/src/main/java/com/gmail/nossr50/events/chat/McMMOPartyChatEvent.java @@ -8,8 +8,8 @@ import org.bukkit.plugin.Plugin; public class McMMOPartyChatEvent extends McMMOChatEvent { private String party; - public McMMOPartyChatEvent(Plugin plugin, String sender, String party, String message) { - super(plugin, sender, message); + public McMMOPartyChatEvent(Plugin plugin, String sender, String displayName, String party, String message) { + super(plugin, sender, displayName, message); this.party = party; } diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index b49452e53..df102208e 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -339,11 +339,11 @@ public class PlayerListener implements Listener { return; } - ChatManager.handlePartyChat(plugin, party, player.getDisplayName(), event.getMessage()); + ChatManager.handlePartyChat(plugin, party, player.getName(), player.getDisplayName(), event.getMessage()); event.setCancelled(true); } else if (profile.getAdminChatMode()) { - ChatManager.handleAdminChat(plugin, player.getDisplayName(), event.getMessage()); + ChatManager.handleAdminChat(plugin, player.getName(), player.getDisplayName(), event.getMessage()); event.setCancelled(true); } } diff --git a/src/main/java/com/gmail/nossr50/party/commands/ACommand.java b/src/main/java/com/gmail/nossr50/party/commands/ACommand.java index 5a191479b..28aeeaae6 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/ACommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/ACommand.java @@ -8,8 +8,8 @@ import org.bukkit.entity.Player; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.commands.CommandHelper; import com.gmail.nossr50.datatypes.PlayerProfile; -import com.gmail.nossr50.events.chat.McMMOAdminChatEvent; import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.util.ChatManager; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Users; @@ -60,43 +60,11 @@ public class ACommand implements CommandExecutor { if (sender instanceof Player) { Player player = (Player) sender; - - 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); - } - } + ChatManager.handleAdminChat(mcMMO.p, player.getName(), player.getDisplayName(), message); } else { - McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(mcMMO.p, "Console", message); - mcMMO.p.getServer().getPluginManager().callEvent(chatEvent); - - 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); - } - } + String ssender = LocaleLoader.getString("Commands.Chat.Console"); + ChatManager.handleAdminChat(mcMMO.p, ssender, ssender, message); } return true; diff --git a/src/main/java/com/gmail/nossr50/party/commands/PCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PCommand.java index e5612b949..845966c22 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PCommand.java @@ -8,10 +8,10 @@ import org.bukkit.entity.Player; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.commands.CommandHelper; import com.gmail.nossr50.datatypes.PlayerProfile; -import com.gmail.nossr50.events.chat.McMMOPartyChatEvent; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.Party; import com.gmail.nossr50.party.PartyManager; +import com.gmail.nossr50.util.ChatManager; import com.gmail.nossr50.util.Users; public class PCommand implements CommandExecutor { @@ -73,22 +73,7 @@ public class PCommand implements CommandExecutor { } String message = builder.toString(); - - 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); - } + ChatManager.handlePartyChat(plugin, party, player.getName(), player.getDisplayName(), message); } else { if (args.length < 2) { @@ -96,7 +81,9 @@ public class PCommand implements CommandExecutor { return true; } - if (!PartyManager.isParty(args[0])) { + Party party = PartyManager.getParty(args[0]); + + if (party == null) { sender.sendMessage(LocaleLoader.getString("Party.InvalidName")); return true; } @@ -109,23 +96,10 @@ public class PCommand implements CommandExecutor { builder.append(args[i]); } + String ssender = LocaleLoader.getString("Commands.Chat.Console"); String message = builder.toString(); - McMMOPartyChatEvent chatEvent = new McMMOPartyChatEvent(plugin, "Console", args[0], 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); - } + ChatManager.handlePartyChat(plugin, party, ssender, ssender, message); } return true; diff --git a/src/main/java/com/gmail/nossr50/util/ChatManager.java b/src/main/java/com/gmail/nossr50/util/ChatManager.java index 9245f4d4b..6a9f41313 100644 --- a/src/main/java/com/gmail/nossr50/util/ChatManager.java +++ b/src/main/java/com/gmail/nossr50/util/ChatManager.java @@ -1,9 +1,11 @@ package com.gmail.nossr50.util; +import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.config.Config; import com.gmail.nossr50.events.chat.McMMOAdminChatEvent; import com.gmail.nossr50.events.chat.McMMOPartyChatEvent; import com.gmail.nossr50.locale.LocaleLoader; @@ -12,41 +14,53 @@ import com.gmail.nossr50.party.Party; public final class ChatManager { public ChatManager () {} - public static void handleAdminChat(Plugin plugin, String playerName, String message) { - McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(plugin, playerName, message); + public static void handleAdminChat(Plugin plugin, String playerName, String displayName, String message) { + McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(plugin, playerName, displayName, message); mcMMO.p.getServer().getPluginManager().callEvent(chatEvent); if (chatEvent.isCancelled()) { return; } + if(Config.getInstance().getAdminDisplayNames()) + displayName = chatEvent.getDisplayName(); + else + displayName = chatEvent.getSender(); + String adminMessage = chatEvent.getMessage(); - mcMMO.p.getLogger().info("[A]<" + playerName + "> " + adminMessage); - for (Player otherPlayer : mcMMO.p.getServer().getOnlinePlayers()) { 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); } } + + displayName = ChatColor.stripColor(displayName); + mcMMO.p.getLogger().info("[A]<" + displayName + "> " + adminMessage); } - public static void handlePartyChat(Plugin plugin, Party party, String playerName, String message) { + public static void handlePartyChat(Plugin plugin, Party party, String playerName, String displayName, String message) { 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); if (chatEvent.isCancelled()) { return; } + if(Config.getInstance().getPartyDisplayNames()) + displayName = chatEvent.getDisplayName(); + else + displayName = chatEvent.getSender(); + String partyMessage = chatEvent.getMessage(); - mcMMO.p.getLogger().info("[P](" + partyName + ")" + "<" + playerName + "> " + partyMessage); - 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); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7021d7b7b..b330941de 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -369,5 +369,9 @@ Commands: Request_Timeout: 300 p: Enabled: true + #Allow mcMMO to use player display names in chat instead of their usernames + Use_Display_Names: true a: - Enabled: true \ No newline at end of file + Enabled: true + #Allow mcMMO to use player display names in chat instead of their usernames + Use_Display_Names: true \ No newline at end of file