From 03dcdb9c6154e62e983d046b22a0408e37e298b4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 6 Nov 2013 19:01:31 +1300 Subject: [PATCH] Read desc Added animal colors to the list of settable options. Added itemstack and itemstack[] to the list of settable options Added /disguisehelp colors to get the list of usable colors Colored in the errors when constructing a disguise options --- .../disguise/BaseDisguiseCommand.java | 68 +++++++++++++++++-- .../commands/DisguiseHelpCommand.java | 23 ++++++- .../disguise/disguisetypes/AnimalColor.java | 4 +- 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/src/me/libraryaddict/disguise/BaseDisguiseCommand.java b/src/me/libraryaddict/disguise/BaseDisguiseCommand.java index 828f5c4e..e117de12 100644 --- a/src/me/libraryaddict/disguise/BaseDisguiseCommand.java +++ b/src/me/libraryaddict/disguise/BaseDisguiseCommand.java @@ -3,6 +3,8 @@ package me.libraryaddict.disguise; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; + +import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.MiscDisguise; @@ -11,6 +13,7 @@ import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; import org.bukkit.ChatColor; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.inventory.ItemStack; public abstract class BaseDisguiseCommand implements CommandExecutor { protected ArrayList getAllowedDisguises(CommandSender sender, String permissionNode) { @@ -152,23 +155,51 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { value = (double) obj; } } else { - throw new Exception(ChatColor.RED + "Expected a number, received " + valueString - + " instead for " + methodName); + throw parseString("number", valueString, methodName); } } else if (boolean.class == param) { if (!("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString))) - throw new Exception(ChatColor.RED + "Expected true/false, received " + valueString - + " instead for " + methodName); + throw parseString("true/false", valueString, methodName); value = (boolean) "true".equalsIgnoreCase(valueString); } else if (param == String.class) { value = ChatColor.translateAlternateColorCodes('&', valueString); + } else if (param == AnimalColor.class) { + try { + value = AnimalColor.valueOf(valueString.toUpperCase()); + } catch (Exception ex) { + throw parseString("animal color", valueString, methodName); + } + } else if (param == ItemStack.class) { + try { + value = parseString(valueString); + } catch (Exception ex) { + throw new Exception(String.format(ex.getMessage(), methodName)); + } + } else if (param == ItemStack[].class) { + ItemStack[] items = new ItemStack[4]; + String[] split = valueString.split(","); + if (split.length == 4) { + for (int a = 0; a < 4; a++) { + try { + ItemStack item = parseString(split[a]); + items[a] = item; + } catch (Exception ex) { + throw parseString("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN + + "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName); + } + } + } else { + throw parseString("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN + + "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName); + } + value = items; } } break; } } if (methodToUse == null) { - throw new Exception(ChatColor.RED + "Cannot find option " + methodName); + throw new Exception(ChatColor.RED + "Cannot find the option " + methodName); } methodToUse.invoke(disguise.getWatcher(), value); } @@ -176,5 +207,32 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { return disguise; } + private Exception parseString(String expectedValue, String receivedInstead, String methodName) { + return new Exception(ChatColor.RED + "Expected " + ChatColor.GREEN + expectedValue + ChatColor.RED + ", received " + + ChatColor.GREEN + receivedInstead + ChatColor.RED + " instead for " + ChatColor.GREEN + methodName); + } + + private ItemStack parseString(String string) throws Exception { + String[] split = string.split(":", -1); + if (isNumeric(split[0])) { + int itemId = Integer.parseInt(split[0]); + short itemDura = 0; + if (split.length > 1) { + if (isNumeric(split[1])) { + itemDura = Short.parseShort(split[1]); + } else { + throw parseString("item ID:Durability combo", string, "%s"); + } + } + return new ItemStack(itemId, 1, itemDura); + } else { + if (split.length == 1) { + throw parseString("item ID", string, "%s"); + } else { + throw parseString("item ID:Durability combo", string, "%s"); + } + } + } + protected abstract void sendCommandUsage(CommandSender sender); } diff --git a/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java index dac4b6d1..f3c2544b 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java @@ -5,11 +5,13 @@ import java.util.ArrayList; import java.util.Collections; import me.libraryaddict.disguise.BaseDisguiseCommand; +import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.DisguiseType; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.inventory.ItemStack; public class DisguiseHelpCommand extends BaseDisguiseCommand { @@ -19,10 +21,19 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand { ArrayList allowedDisguises = getAllowedDisguises(sender, node); if (!allowedDisguises.isEmpty()) { if (args.length == 0) { - sender.sendMessage(ChatColor.RED + "/disguisehelp "); return true; // sender.sendMessage(ChatColor.RED + "/disguisehelp