diff --git a/src/main/java/com/gmail/nossr50/api/ChatAPI.java b/src/main/java/com/gmail/nossr50/api/ChatAPI.java index 413e5d0ff..95a605382 100644 --- a/src/main/java/com/gmail/nossr50/api/ChatAPI.java +++ b/src/main/java/com/gmail/nossr50/api/ChatAPI.java @@ -4,6 +4,9 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import com.gmail.nossr50.chat.ChatManager; +import com.gmail.nossr50.chat.ChatManagerFactory; +import com.gmail.nossr50.chat.ChatMode; +import com.gmail.nossr50.chat.PartyChatManager; import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.util.player.UserManager; @@ -22,7 +25,7 @@ public final class ChatAPI { * @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); + getPartyChatManager(plugin, party).handleChat(sender, displayName, message); } /** @@ -36,7 +39,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, sender, message); + getPartyChatManager(plugin, party).handleChat(sender, message); } /** @@ -50,7 +53,7 @@ public final class ChatAPI { * @param message The message to send */ public static void sendAdminChat(Plugin plugin, String sender, String displayName, String message) { - ChatManager.handleAdminChat(plugin, sender, displayName, message); + ChatManagerFactory.getChatManager(plugin, ChatMode.ADMIN).handleChat(sender, displayName, message); } /** @@ -63,7 +66,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, sender, message); + ChatManagerFactory.getChatManager(plugin, ChatMode.ADMIN).handleChat(sender, message); } /** @@ -73,7 +76,7 @@ public final class ChatAPI { * @return true if the player is using party chat, false otherwise */ public static boolean isUsingPartyChat(Player player) { - return isUsingPartyChat(player.getName()); + return UserManager.getPlayer(player).getPartyChatMode(); } /** @@ -93,7 +96,7 @@ public final class ChatAPI { * @return true if the player is using admin chat, false otherwise */ public static boolean isUsingAdminChat(Player player) { - return isUsingAdminChat(player.getName()); + return UserManager.getPlayer(player).getAdminChatMode(); } /** @@ -112,7 +115,7 @@ public final class ChatAPI { * @param player The player to toggle party chat on. */ public static void togglePartyChat(Player player) { - togglePartyChat(player.getName()); + UserManager.getPlayer(player).togglePartyChat(); } /** @@ -121,7 +124,7 @@ public final class ChatAPI { * @param playerName The name of the player to toggle party chat on. */ public static void togglePartyChat(String playerName) { - UserManager.getPlayer(playerName).setPartyChat(!isUsingPartyChat(playerName)); + UserManager.getPlayer(playerName).togglePartyChat(); } /** @@ -130,7 +133,7 @@ public final class ChatAPI { * @param player The player to toggle admin chat on. */ public static void toggleAdminChat(Player player) { - toggleAdminChat(player.getName()); + UserManager.getPlayer(player).toggleAdminChat(); } /** @@ -139,6 +142,13 @@ public final class ChatAPI { * @param playerName The name of the player to toggle party chat on. */ public static void toggleAdminChat(String playerName) { - UserManager.getPlayer(playerName).setAdminChat(!isUsingAdminChat(playerName)); + UserManager.getPlayer(playerName).toggleAdminChat(); + } + + private static ChatManager getPartyChatManager(Plugin plugin, String party) { + ChatManager chatManager = ChatManagerFactory.getChatManager(plugin, ChatMode.PARTY); + ((PartyChatManager) chatManager).setParty(PartyManager.getParty(party)); + + return chatManager; } } diff --git a/src/main/java/com/gmail/nossr50/chat/AdminChatManager.java b/src/main/java/com/gmail/nossr50/chat/AdminChatManager.java new file mode 100644 index 000000000..70da46c3c --- /dev/null +++ b/src/main/java/com/gmail/nossr50/chat/AdminChatManager.java @@ -0,0 +1,22 @@ +package com.gmail.nossr50.chat; + +import org.bukkit.plugin.Plugin; + +import com.gmail.nossr50.config.Config; +import com.gmail.nossr50.events.chat.McMMOAdminChatEvent; + +public class AdminChatManager extends ChatManager { + protected AdminChatManager(Plugin plugin) { + super(plugin, Config.getInstance().getAdminDisplayNames(), "Commands.AdminChat.Prefix"); + } + + @Override + public void handleChat(String senderName, String displayName, String message, boolean isAsync) { + handleChat(new McMMOAdminChatEvent(plugin, senderName, displayName, message, isAsync)); + } + + @Override + protected void sendMessage() { + plugin.getServer().broadcast(message, "mcmmo.chat.adminchat"); + } +} diff --git a/src/main/java/com/gmail/nossr50/chat/ChatManager.java b/src/main/java/com/gmail/nossr50/chat/ChatManager.java index bd44d0384..ca290ff62 100644 --- a/src/main/java/com/gmail/nossr50/chat/ChatManager.java +++ b/src/main/java/com/gmail/nossr50/chat/ChatManager.java @@ -1,78 +1,51 @@ package com.gmail.nossr50.chat; -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.datatypes.party.Party; -import com.gmail.nossr50.events.chat.McMMOAdminChatEvent; -import com.gmail.nossr50.events.chat.McMMOPartyChatEvent; +import com.gmail.nossr50.events.chat.McMMOChatEvent; import com.gmail.nossr50.locale.LocaleLoader; -public final class ChatManager { - public ChatManager() {} +public abstract class ChatManager { + protected Plugin plugin; + protected boolean useDisplayNames; + protected String chatPrefix; - public static void handleAdminChat(Plugin plugin, String playerName, String displayName, String message, boolean isAsync) { - McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(plugin, playerName, displayName, message, isAsync); - mcMMO.p.getServer().getPluginManager().callEvent(chatEvent); + protected String displayName; + protected String message; - if (chatEvent.isCancelled()) { + protected ChatManager(Plugin plugin, boolean useDisplayNames, String chatPrefix) { + this.plugin = plugin; + this.useDisplayNames = useDisplayNames; + this.chatPrefix = chatPrefix; + } + + protected void handleChat(McMMOChatEvent event) { + plugin.getServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { return; } - if (Config.getInstance().getAdminDisplayNames()) { - displayName = chatEvent.getDisplayName(); - } - else { - displayName = chatEvent.getSender(); - } + displayName = useDisplayNames ? event.getDisplayName() : event.getSender(); + message = LocaleLoader.getString(chatPrefix, displayName) + event.getMessage(); - String adminMessage = chatEvent.getMessage(); - - mcMMO.p.getServer().broadcast(LocaleLoader.getString("Commands.AdminChat.Prefix", displayName) + adminMessage, "mcmmo.chat.adminchat"); + sendMessage(); } - public static void handleAdminChat(Plugin plugin, String senderName, String message) { - handleAdminChat(plugin, senderName, senderName, message); + public void handleChat(String senderName, String message) { + handleChat(senderName, senderName, message, false); } - public static void handleAdminChat(Plugin plugin, String playerName, String displayName, String message) { - handleAdminChat(plugin, playerName, displayName, message, false); + public void handleChat(Player player, String message, boolean isAsync) { + handleChat(player.getName(), player.getDisplayName(), message, isAsync); } - public static void handlePartyChat(Plugin plugin, Party party, String playerName, String displayName, String message, boolean isAsync) { - String partyName = party.getName(); - - McMMOPartyChatEvent chatEvent = new McMMOPartyChatEvent(plugin, playerName, displayName, partyName, message, isAsync); - 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(); - - for (Player member : party.getOnlineMembers()) { - member.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Prefix", displayName) + partyMessage); - } - - mcMMO.p.getLogger().info("[P](" + partyName + ")" + "<" + ChatColor.stripColor(displayName) + "> " + partyMessage); + public void handleChat(String senderName, String displayName, String message) { + handleChat(senderName, displayName, message, false); } - public static void handlePartyChat(Plugin plugin, Party party, String senderName, String message) { - handlePartyChat(plugin, party, senderName, senderName, message); - } + public abstract void handleChat(String senderName, String displayName, String message, boolean isAsync); - public static void handlePartyChat(Plugin plugin, Party party, String playerName, String displayName, String message) { - handlePartyChat(plugin, party, playerName, displayName, message, false); - } + protected abstract void sendMessage(); } diff --git a/src/main/java/com/gmail/nossr50/chat/ChatManagerFactory.java b/src/main/java/com/gmail/nossr50/chat/ChatManagerFactory.java new file mode 100644 index 000000000..7bdbac9b4 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/chat/ChatManagerFactory.java @@ -0,0 +1,29 @@ +package com.gmail.nossr50.chat; + +import java.util.HashMap; + +import org.bukkit.plugin.Plugin; + +public class ChatManagerFactory { + private static final HashMap adminChatManagers = new HashMap(); + private static final HashMap partyChatManagers = new HashMap(); + + public static ChatManager getChatManager(Plugin plugin, ChatMode mode) { + switch (mode) { + case ADMIN: + if (!adminChatManagers.containsKey(plugin)) { + adminChatManagers.put(plugin, new AdminChatManager(plugin)); + } + + return adminChatManagers.get(plugin); + case PARTY: + if (!partyChatManagers.containsKey(plugin)) { + partyChatManagers.put(plugin, new PartyChatManager(plugin)); + } + + return partyChatManagers.get(plugin); + default: + return null; + } + } +} diff --git a/src/main/java/com/gmail/nossr50/chat/PartyChatManager.java b/src/main/java/com/gmail/nossr50/chat/PartyChatManager.java new file mode 100644 index 000000000..57a6366ac --- /dev/null +++ b/src/main/java/com/gmail/nossr50/chat/PartyChatManager.java @@ -0,0 +1,36 @@ +package com.gmail.nossr50.chat; + +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import com.gmail.nossr50.config.Config; +import com.gmail.nossr50.datatypes.party.Party; +import com.gmail.nossr50.events.chat.McMMOPartyChatEvent; +import com.gmail.nossr50.locale.LocaleLoader; + +public class PartyChatManager extends ChatManager { + private Party party; + + protected PartyChatManager(Plugin plugin) { + super(plugin, Config.getInstance().getPartyDisplayNames(), "Commands.Party.Chat.Prefix"); + } + + public void setParty(Party party) { + this.party = party; + } + + @Override + public void handleChat(String senderName, String displayName, String message, boolean isAsync) { + handleChat(new McMMOPartyChatEvent(plugin, senderName, displayName, party.getName(), message, isAsync)); + } + + @Override + protected void sendMessage() { + for (Player member : party.getOnlineMembers()) { + member.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Prefix", displayName) + message); + } + + plugin.getLogger().info("[P](" + party.getName() + ")" + "<" + ChatColor.stripColor(displayName) + "> " + message); + } +} diff --git a/src/main/java/com/gmail/nossr50/commands/chat/AdminChatCommand.java b/src/main/java/com/gmail/nossr50/commands/chat/AdminChatCommand.java index 9842c2cb2..1ff8d7628 100644 --- a/src/main/java/com/gmail/nossr50/commands/chat/AdminChatCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/chat/AdminChatCommand.java @@ -1,12 +1,8 @@ package com.gmail.nossr50.commands.chat; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import com.gmail.nossr50.mcMMO; -import com.gmail.nossr50.chat.ChatManager; import com.gmail.nossr50.chat.ChatMode; -import com.gmail.nossr50.locale.LocaleLoader; public class AdminChatCommand extends ChatCommand { public AdminChatCommand() { @@ -15,12 +11,6 @@ public class AdminChatCommand extends ChatCommand { @Override protected void handleChatSending(CommandSender sender, String[] args) { - if (sender instanceof Player) { - Player player = (Player) sender; - ChatManager.handleAdminChat(mcMMO.p, player.getName(), player.getDisplayName(), buildChatMessage(args, 0)); - } - else { - ChatManager.handleAdminChat(mcMMO.p, LocaleLoader.getString("Commands.Chat.Console"), buildChatMessage(args, 0)); - } + chatManager.handleChat(sender.getName(), getDisplayName(sender), buildChatMessage(args, 0)); } } diff --git a/src/main/java/com/gmail/nossr50/commands/chat/ChatCommand.java b/src/main/java/com/gmail/nossr50/commands/chat/ChatCommand.java index 64e9bec9a..f9e616075 100644 --- a/src/main/java/com/gmail/nossr50/commands/chat/ChatCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/chat/ChatCommand.java @@ -9,6 +9,9 @@ import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import org.bukkit.util.StringUtil; +import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.chat.ChatManager; +import com.gmail.nossr50.chat.ChatManagerFactory; import com.gmail.nossr50.chat.ChatMode; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.locale.LocaleLoader; @@ -19,10 +22,12 @@ import com.google.common.collect.ImmutableList; public abstract class ChatCommand implements TabExecutor { protected ChatMode chatMode; + protected ChatManager chatManager; private McMMOPlayer mcMMOPlayer; public ChatCommand(ChatMode chatMode) { this.chatMode = chatMode; + this.chatManager = ChatManagerFactory.getChatManager(mcMMO.p, chatMode); } @Override @@ -97,6 +102,10 @@ public abstract class ChatCommand implements TabExecutor { return builder.toString(); } + protected String getDisplayName(CommandSender sender) { + return (sender instanceof Player) ? ((Player) sender).getDisplayName() : LocaleLoader.getString("Commands.Chat.Console"); + } + protected abstract void handleChatSending(CommandSender sender, String[] args); private void enableChatMode(CommandSender sender) { diff --git a/src/main/java/com/gmail/nossr50/commands/chat/PartyChatCommand.java b/src/main/java/com/gmail/nossr50/commands/chat/PartyChatCommand.java index 6f198c338..8fc50adf9 100644 --- a/src/main/java/com/gmail/nossr50/commands/chat/PartyChatCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/chat/PartyChatCommand.java @@ -3,9 +3,8 @@ package com.gmail.nossr50.commands.chat; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import com.gmail.nossr50.mcMMO; -import com.gmail.nossr50.chat.ChatManager; import com.gmail.nossr50.chat.ChatMode; +import com.gmail.nossr50.chat.PartyChatManager; import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.PartyManager; @@ -18,16 +17,18 @@ public class PartyChatCommand extends ChatCommand { @Override protected void handleChatSending(CommandSender sender, String[] args) { + Party party; + String message; + if (sender instanceof Player) { - Player player = (Player) sender; - Party party = UserManager.getPlayer(player).getParty(); + party = UserManager.getPlayer((Player) sender).getParty(); if (party == null) { sender.sendMessage(LocaleLoader.getString("Commands.Party.None")); return; } - ChatManager.handlePartyChat(mcMMO.p, party, player.getName(), player.getDisplayName(), buildChatMessage(args, 0)); + message = buildChatMessage(args, 0); } else { if (args.length < 2) { @@ -35,14 +36,17 @@ public class PartyChatCommand extends ChatCommand { return; } - Party party = PartyManager.getParty(args[0]); + party = PartyManager.getParty(args[0]); if (party == null) { sender.sendMessage(LocaleLoader.getString("Party.InvalidName")); return; } - ChatManager.handlePartyChat(mcMMO.p, party, LocaleLoader.getString("Commands.Chat.Console"), buildChatMessage(args, 1)); + message = buildChatMessage(args, 1); } + + ((PartyChatManager) chatManager).setParty(party); + chatManager.handleChat(sender.getName(), getDisplayName(sender), message); } } diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index d987a2300..fe8cacd9f 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -28,6 +28,9 @@ import org.bukkit.inventory.ItemStack; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.chat.ChatManager; +import com.gmail.nossr50.chat.ChatManagerFactory; +import com.gmail.nossr50.chat.ChatMode; +import com.gmail.nossr50.chat.PartyChatManager; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.datatypes.player.McMMOPlayer; @@ -528,6 +531,8 @@ public class PlayerListener implements Listener { return; } + ChatManager chatManager = null; + if (mcMMOPlayer.getPartyChatMode()) { Party party = mcMMOPlayer.getParty(); @@ -537,11 +542,15 @@ public class PlayerListener implements Listener { return; } - ChatManager.handlePartyChat(plugin, party, player.getName(), player.getDisplayName(), event.getMessage(), event.isAsynchronous()); - event.setCancelled(true); + chatManager = ChatManagerFactory.getChatManager(plugin, ChatMode.PARTY); + ((PartyChatManager) chatManager).setParty(party); } else if (mcMMOPlayer.getAdminChatMode()) { - ChatManager.handleAdminChat(plugin, player.getName(), player.getDisplayName(), event.getMessage(), event.isAsynchronous()); + chatManager = ChatManagerFactory.getChatManager(plugin, ChatMode.ADMIN); + } + + if (chatManager != null) { + chatManager.handleChat(player, event.getMessage(), event.isAsynchronous()); event.setCancelled(true); } }