Abstract the Chat commands.

This commit is contained in:
GJ 2013-02-07 15:59:18 -05:00
parent 6c266a8926
commit 4f45431dd3
5 changed files with 201 additions and 166 deletions

View File

@ -40,6 +40,10 @@ public final class ChatManager {
mcMMO.p.getLogger().info("[A]<" + displayName + "> " + adminMessage); 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) { public static void handlePartyChat(Plugin plugin, Party party, String playerName, String displayName, String message) {
String partyName = party.getName(); String partyName = party.getName();
@ -64,4 +68,8 @@ public final class ChatManager {
displayName = ChatColor.stripColor(displayName); displayName = ChatColor.stripColor(displayName);
mcMMO.p.getLogger().info("[P](" + partyName + ")" + "<" + displayName + "> " + partyMessage); 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);
}
} }

View File

@ -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;
}
}

View File

@ -1,87 +1,28 @@
package com.gmail.nossr50.chat.commands; package com.gmail.nossr50.chat.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.chat.ChatManager; 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.locale.LocaleLoader;
import com.gmail.nossr50.util.Users;
public class AdminChatCommand implements CommandExecutor { public class AdminChatCommand extends ChatCommand {
public AdminChatCommand() {
super(ChatMode.ADMIN);
}
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { protected void handleChatSending(CommandSender sender, String[] args) {
McMMOPlayer mcMMOPlayer; String message = buildChatMessage(args, 0);
switch (args.length) { if (sender instanceof Player) {
case 0: Player player = (Player) sender;
if (!(sender instanceof Player)) { ChatManager.handleAdminChat(mcMMO.p, player.getName(), player.getDisplayName(), message);
return false; }
} else {
ChatManager.handleAdminChat(mcMMO.p, LocaleLoader.getString("Commands.Chat.Console"), message);
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;
} }
} }
} }

View File

@ -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);
}

View File

@ -1,117 +1,48 @@
package com.gmail.nossr50.chat.commands; package com.gmail.nossr50.chat.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.chat.ChatManager; 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.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.Users; import com.gmail.nossr50.util.Users;
public class PartyChatCommand implements CommandExecutor { public class PartyChatCommand extends ChatCommand {
public PartyChatCommand() {
super(ChatMode.PARTY);
}
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { protected void handleChatSending(CommandSender sender, String[] args) {
McMMOPlayer mcMMOPlayer; if (sender instanceof Player) {
Player player = (Player) sender;
Party party = Users.getPlayer(player).getParty();
switch (args.length) { if (party == null) {
case 0: sender.sendMessage(LocaleLoader.getString("Commands.Party.None"));
if (!(sender instanceof Player)) { return;
return false;
} }
mcMMOPlayer = Users.getPlayer((Player) sender); ChatManager.handlePartyChat(mcMMO.p, party, player.getName(), player.getDisplayName(), buildChatMessage(args, 0));
}
// Can't have both party & admin chat at the same time. else {
if (mcMMOPlayer.getAdminChatMode()) { if (args.length < 2) {
mcMMOPlayer.toggleAdminChat(); sender.sendMessage(LocaleLoader.getString("Party.Specify"));
return;
} }
mcMMOPlayer.togglePartyChat(); Party party = PartyManager.getParty(args[0]);
if (mcMMOPlayer.getPartyChatMode()) { if (party == null) {
sender.sendMessage(LocaleLoader.getString("Commands.Party.Chat.On")); sender.sendMessage(LocaleLoader.getString("Party.InvalidName"));
} return;
else {
sender.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Off"));
} }
return true; ChatManager.handlePartyChat(mcMMO.p, party, LocaleLoader.getString("Commands.Chat.Console"), buildChatMessage(args, 1));
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;
} }
} }
} }