diff --git a/src/main/java/com/gmail/nossr50/chat/ChatManager.java b/src/main/java/com/gmail/nossr50/chat/ChatManager.java index e84946dc3..75a965b3c 100644 --- a/src/main/java/com/gmail/nossr50/chat/ChatManager.java +++ b/src/main/java/com/gmail/nossr50/chat/ChatManager.java @@ -40,6 +40,10 @@ public final class ChatManager { mcMMO.p.getLogger().info("[A]<" + displayName + "> " + adminMessage); } + public static void handleAdminChat(Plugin plugin, String senderName, String message) { + handleAdminChat(plugin, senderName, senderName, message); + } + public static void handlePartyChat(Plugin plugin, Party party, String playerName, String displayName, String message) { String partyName = party.getName(); @@ -64,4 +68,8 @@ public final class ChatManager { displayName = ChatColor.stripColor(displayName); mcMMO.p.getLogger().info("[P](" + partyName + ")" + "<" + displayName + "> " + partyMessage); } + + public static void handlePartyChat(Plugin plugin, Party party, String senderName, String message) { + handlePartyChat(plugin, party, senderName, senderName, message); + } } diff --git a/src/main/java/com/gmail/nossr50/chat/ChatMode.java b/src/main/java/com/gmail/nossr50/chat/ChatMode.java new file mode 100644 index 000000000..5cf18a10b --- /dev/null +++ b/src/main/java/com/gmail/nossr50/chat/ChatMode.java @@ -0,0 +1,70 @@ +package com.gmail.nossr50.chat; + +import com.gmail.nossr50.datatypes.McMMOPlayer; +import com.gmail.nossr50.locale.LocaleLoader; + +public enum ChatMode { + ADMIN(LocaleLoader.getString("Commands.AdminChat.On"), LocaleLoader.getString("Commands.AdminChat.Off")), + PARTY(LocaleLoader.getString("Commands.Party.Chat.On"), LocaleLoader.getString("Commands.Party.Chat.Off")); + + private String enabledMessage; + private String disabledMessage; + + private ChatMode (String enabledMessage, String disabledMessage) { + this.enabledMessage = enabledMessage; + this.disabledMessage = disabledMessage; + } + + public boolean isEnabled(McMMOPlayer mcMMOPlayer) { + switch (this) { + case ADMIN: + return mcMMOPlayer.getAdminChatMode(); + + case PARTY: + return mcMMOPlayer.getPartyChatMode(); + + default: + return false; + } + } + + public void disable(McMMOPlayer mcMMOPlayer) { + switch (this) { + case ADMIN: + mcMMOPlayer.setAdminChat(false); + return; + + case PARTY: + mcMMOPlayer.setPartyChat(false); + return; + + default: + return; + } + } + + public void enable(McMMOPlayer mcMMOPlayer) { + switch (this) { + case ADMIN: + mcMMOPlayer.setAdminChat(true); + mcMMOPlayer.setPartyChat(false); + return; + + case PARTY: + mcMMOPlayer.setPartyChat(true); + mcMMOPlayer.setAdminChat(false); + return; + + default: + return; + } + } + + public String getEnabledMessage() { + return enabledMessage; + } + + public String getDisabledMessage() { + return disabledMessage; + } +} diff --git a/src/main/java/com/gmail/nossr50/chat/commands/AdminChatCommand.java b/src/main/java/com/gmail/nossr50/chat/commands/AdminChatCommand.java index 2d059bc6d..9b4a2025d 100644 --- a/src/main/java/com/gmail/nossr50/chat/commands/AdminChatCommand.java +++ b/src/main/java/com/gmail/nossr50/chat/commands/AdminChatCommand.java @@ -1,87 +1,28 @@ package com.gmail.nossr50.chat.commands; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; 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.datatypes.McMMOPlayer; +import com.gmail.nossr50.chat.ChatMode; import com.gmail.nossr50.locale.LocaleLoader; -import com.gmail.nossr50.util.Users; -public class AdminChatCommand implements CommandExecutor { +public class AdminChatCommand extends ChatCommand { + public AdminChatCommand() { + super(ChatMode.ADMIN); + } + @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - McMMOPlayer mcMMOPlayer; + protected void handleChatSending(CommandSender sender, String[] args) { + String message = buildChatMessage(args, 0); - switch (args.length) { - case 0: - if (!(sender instanceof Player)) { - return false; - } - - mcMMOPlayer = Users.getPlayer((Player) sender); - - // Can't have both party & admin chat at the same time. - if (mcMMOPlayer.getPartyChatMode()) { - mcMMOPlayer.togglePartyChat(); - } - - mcMMOPlayer.toggleAdminChat(); - - if (mcMMOPlayer.getAdminChatMode()) { - sender.sendMessage(LocaleLoader.getString("Commands.AdminChat.On")); - } - else { - sender.sendMessage(LocaleLoader.getString("Commands.AdminChat.Off")); - } - - return true; - - default: - if (args.length == 1) { - if (!(sender instanceof Player)) { - return false; - } - - mcMMOPlayer = Users.getPlayer((Player) sender); - - if (args[0].equalsIgnoreCase("on")) { - mcMMOPlayer.setPartyChat(false); - mcMMOPlayer.setAdminChat(true); - sender.sendMessage(LocaleLoader.getString("Commands.AdminChat.On")); - return true; - } - - if (args[0].equalsIgnoreCase("off")) { - mcMMOPlayer.setAdminChat(false); - sender.sendMessage(LocaleLoader.getString("Commands.AdminChat.Off")); - return true; - } - } - - StringBuilder builder = new StringBuilder(); - builder.append(args[0]); - - for (int i = 1; i < args.length; i++) { - builder.append(" "); - builder.append(args[i]); - } - - String message = builder.toString(); - - if (sender instanceof Player) { - Player player = (Player) sender; - ChatManager.handleAdminChat(mcMMO.p, player.getName(), player.getDisplayName(), message); - } - else { - String ssender = LocaleLoader.getString("Commands.Chat.Console"); - ChatManager.handleAdminChat(mcMMO.p, ssender, ssender, message); - } - - return true; + if (sender instanceof Player) { + Player player = (Player) sender; + ChatManager.handleAdminChat(mcMMO.p, player.getName(), player.getDisplayName(), message); + } + else { + ChatManager.handleAdminChat(mcMMO.p, LocaleLoader.getString("Commands.Chat.Console"), message); } } } diff --git a/src/main/java/com/gmail/nossr50/chat/commands/ChatCommand.java b/src/main/java/com/gmail/nossr50/chat/commands/ChatCommand.java new file mode 100644 index 000000000..7a06ca25f --- /dev/null +++ b/src/main/java/com/gmail/nossr50/chat/commands/ChatCommand.java @@ -0,0 +1,85 @@ +package com.gmail.nossr50.chat.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.gmail.nossr50.chat.ChatMode; +import com.gmail.nossr50.datatypes.McMMOPlayer; +import com.gmail.nossr50.util.Users; + +public abstract class ChatCommand implements CommandExecutor { + protected McMMOPlayer mcMMOPlayer; + protected ChatMode chatMode; + + public ChatCommand (ChatMode chatMode) { + this.chatMode = chatMode; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + switch (args.length) { + case 0: + if (!(sender instanceof Player)) { + return false; + } + + mcMMOPlayer = Users.getPlayer((Player) sender); + + if (chatMode.isEnabled(mcMMOPlayer)) { + disableChatMode(sender); + return true; + } + + enableChatMode(sender); + return true; + + default: + if (args.length == 1) { + if (!(sender instanceof Player)) { + return false; + } + + mcMMOPlayer = Users.getPlayer((Player) sender); + + if (args[0].equalsIgnoreCase("on")) { + enableChatMode(sender); + return true; + } + + if (args[0].equalsIgnoreCase("off")) { + disableChatMode(sender); + return true; + } + } + + handleChatSending(sender, args); + return true; + } + } + + private void enableChatMode(CommandSender sender) { + chatMode.enable(mcMMOPlayer); + sender.sendMessage(chatMode.getEnabledMessage()); + } + + private void disableChatMode(CommandSender sender) { + chatMode.disable(mcMMOPlayer); + sender.sendMessage(chatMode.getDisabledMessage()); + } + + protected String buildChatMessage(String[] args, int index) { + StringBuilder builder = new StringBuilder(); + builder.append(args[index]); + + for (int i = index + 1; i < args.length; i++) { + builder.append(" "); + builder.append(args[i]); + } + + return builder.toString(); + } + + protected abstract void handleChatSending(CommandSender sender, String[] args); +} diff --git a/src/main/java/com/gmail/nossr50/chat/commands/PartyChatCommand.java b/src/main/java/com/gmail/nossr50/chat/commands/PartyChatCommand.java index c96e095c1..daafd9113 100644 --- a/src/main/java/com/gmail/nossr50/chat/commands/PartyChatCommand.java +++ b/src/main/java/com/gmail/nossr50/chat/commands/PartyChatCommand.java @@ -1,117 +1,48 @@ package com.gmail.nossr50.chat.commands; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; 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.datatypes.McMMOPlayer; +import com.gmail.nossr50.chat.ChatMode; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.Party; import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.util.Users; -public class PartyChatCommand implements CommandExecutor { +public class PartyChatCommand extends ChatCommand { + public PartyChatCommand() { + super(ChatMode.PARTY); + } + @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - McMMOPlayer mcMMOPlayer; + protected void handleChatSending(CommandSender sender, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + Party party = Users.getPlayer(player).getParty(); - switch (args.length) { - case 0: - if (!(sender instanceof Player)) { - return false; + if (party == null) { + sender.sendMessage(LocaleLoader.getString("Commands.Party.None")); + return; } - mcMMOPlayer = Users.getPlayer((Player) sender); - - // Can't have both party & admin chat at the same time. - if (mcMMOPlayer.getAdminChatMode()) { - mcMMOPlayer.toggleAdminChat(); + ChatManager.handlePartyChat(mcMMO.p, party, player.getName(), player.getDisplayName(), buildChatMessage(args, 0)); + } + else { + if (args.length < 2) { + sender.sendMessage(LocaleLoader.getString("Party.Specify")); + return; } - mcMMOPlayer.togglePartyChat(); + Party party = PartyManager.getParty(args[0]); - if (mcMMOPlayer.getPartyChatMode()) { - sender.sendMessage(LocaleLoader.getString("Commands.Party.Chat.On")); - } - else { - sender.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Off")); + if (party == null) { + sender.sendMessage(LocaleLoader.getString("Party.InvalidName")); + return; } - return true; - - default: - if (args.length == 1) { - if (!(sender instanceof Player)) { - return false; - } - - mcMMOPlayer = Users.getPlayer((Player) sender); - - if (args[0].equalsIgnoreCase("on")) { - mcMMOPlayer.setAdminChat(false); - mcMMOPlayer.setPartyChat(true); - sender.sendMessage(LocaleLoader.getString("Commands.Party.Chat.On")); - return true; - } - - if (args[0].equalsIgnoreCase("off")) { - mcMMOPlayer.setPartyChat(false); - sender.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Off")); - return true; - } - } - - if (sender instanceof Player) { - Player player = (Player) sender; - Party party = Users.getPlayer(player).getParty(); - - if (party == null) { - sender.sendMessage(LocaleLoader.getString("Commands.Party.None")); - return true; - } - - StringBuilder builder = new StringBuilder(); - builder.append(args[0]); - - for (int i = 1; i < args.length; i++) { - builder.append(" "); - builder.append(args[i]); - } - - String message = builder.toString(); - ChatManager.handlePartyChat(mcMMO.p, party, player.getName(), player.getDisplayName(), message); - } - else { - if (args.length < 2) { - sender.sendMessage(LocaleLoader.getString("Party.Specify")); - return true; - } - - Party party = PartyManager.getParty(args[0]); - - if (party == null) { - sender.sendMessage(LocaleLoader.getString("Party.InvalidName")); - return true; - } - - StringBuilder builder = new StringBuilder(); - builder.append(args[1]); - - for (int i = 2; i < args.length; i++) { - builder.append(" "); - builder.append(args[i]); - } - - String consoleSender = LocaleLoader.getString("Commands.Chat.Console"); - String message = builder.toString(); - - ChatManager.handlePartyChat(mcMMO.p, party, consoleSender, consoleSender, message); - } - - return true; + ChatManager.handlePartyChat(mcMMO.p, party, LocaleLoader.getString("Commands.Chat.Console"), buildChatMessage(args, 1)); } } }