2013-11-05 16:06:15 +01:00
|
|
|
package me.libraryaddict.disguise;
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
2013-11-06 07:01:31 +01:00
|
|
|
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.AnimalColor;
|
2013-11-05 16:06:15 +01:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.MiscDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.command.CommandExecutor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
2013-11-06 09:28:35 +01:00
|
|
|
import org.bukkit.entity.Horse.Color;
|
|
|
|
import org.bukkit.entity.Horse.Style;
|
|
|
|
import org.bukkit.entity.Ocelot.Type;
|
|
|
|
import org.bukkit.entity.Villager.Profession;
|
2013-11-06 07:01:31 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2013-11-06 09:28:35 +01:00
|
|
|
import org.bukkit.potion.PotionEffect;
|
|
|
|
import org.bukkit.potion.PotionEffectType;
|
2013-11-05 16:06:15 +01:00
|
|
|
|
|
|
|
public abstract class BaseDisguiseCommand implements CommandExecutor {
|
|
|
|
protected ArrayList<String> getAllowedDisguises(CommandSender sender, String permissionNode) {
|
|
|
|
ArrayList<String> names = new ArrayList<String>();
|
|
|
|
for (DisguiseType type : DisguiseType.values()) {
|
|
|
|
String name = type.name().toLowerCase();
|
|
|
|
if (sender.hasPermission("libsdisguises." + permissionNode + ".*")
|
|
|
|
|| sender.hasPermission("libsdisguises." + permissionNode + "." + name))
|
|
|
|
names.add(name);
|
|
|
|
}
|
|
|
|
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2013-11-05 17:53:33 +01:00
|
|
|
protected boolean isDouble(String string) {
|
2013-11-05 16:06:15 +01:00
|
|
|
try {
|
2013-11-05 17:53:33 +01:00
|
|
|
Float.parseFloat(string);
|
2013-11-05 16:06:15 +01:00
|
|
|
return true;
|
|
|
|
} catch (Exception ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 17:53:33 +01:00
|
|
|
protected boolean isNumeric(String string) {
|
2013-11-05 16:06:15 +01:00
|
|
|
try {
|
2013-11-05 17:53:33 +01:00
|
|
|
Integer.parseInt(string);
|
2013-11-05 16:06:15 +01:00
|
|
|
return true;
|
|
|
|
} catch (Exception ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the disguise if it all parsed correctly. Returns a exception with a complete message if it didn't. The
|
|
|
|
* commandsender is purely used for checking permissions. Would defeat the purpose otherwise. To reach this point, the
|
|
|
|
* disguise has been feed a proper disguisetype.
|
|
|
|
*/
|
|
|
|
protected Disguise parseDisguise(CommandSender sender, String[] args) throws Exception {
|
|
|
|
String permissionNode = getClass().getSimpleName().replace("Command", "").toLowerCase();
|
|
|
|
ArrayList<String> allowedDisguises = getAllowedDisguises(sender, permissionNode);
|
|
|
|
if (allowedDisguises.isEmpty()) {
|
|
|
|
throw new Exception(ChatColor.RED + "You are forbidden to use this command.");
|
|
|
|
}
|
|
|
|
if (args.length == 0) {
|
|
|
|
sendCommandUsage(sender);
|
|
|
|
throw new Exception();
|
|
|
|
}
|
2013-11-05 17:41:34 +01:00
|
|
|
DisguiseType disguiseType = null;
|
|
|
|
for (DisguiseType type : DisguiseType.values()) {
|
|
|
|
if (args[0].equalsIgnoreCase(type.name()) || type.name().replace("_", "").equalsIgnoreCase(args[0])) {
|
|
|
|
disguiseType = type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (disguiseType == null) {
|
2013-11-05 16:06:15 +01:00
|
|
|
throw new Exception(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0] + ChatColor.RED
|
|
|
|
+ " doesn't exist!");
|
|
|
|
}
|
2013-11-05 17:44:54 +01:00
|
|
|
if (!allowedDisguises.contains(disguiseType.name().toLowerCase())) {
|
2013-11-05 16:06:15 +01:00
|
|
|
throw new Exception(ChatColor.RED + "You are forbidden to use this disguise!");
|
|
|
|
}
|
|
|
|
Disguise disguise = null;
|
|
|
|
// How many args to skip due to the disugise being constructed
|
|
|
|
int toSkip = 1;
|
|
|
|
// Time to start constructing the disguise.
|
|
|
|
// We will need to check between all 3 kinds of disguises
|
|
|
|
if (disguiseType.isPlayer()) {// If he is doing a player disguise
|
|
|
|
if (args.length == 1) {
|
|
|
|
// He needs to give the player name
|
|
|
|
throw new Exception(ChatColor.RED + "Error! You need to give a player name!");
|
|
|
|
} else {
|
|
|
|
// Construct the player disguise
|
|
|
|
disguise = new PlayerDisguise(ChatColor.translateAlternateColorCodes('&', args[1]));
|
2013-11-05 16:51:01 +01:00
|
|
|
toSkip++;
|
2013-11-05 16:06:15 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (disguiseType.isMob()) { // Its a mob, use the mob constructor
|
|
|
|
boolean adult = true;
|
|
|
|
if (args.length > 1) {
|
2013-11-05 16:51:01 +01:00
|
|
|
if (args[1].equalsIgnoreCase("true") || args[1].equalsIgnoreCase("false")) {
|
|
|
|
adult = "false".equalsIgnoreCase(args[1]);
|
2013-11-05 16:06:15 +01:00
|
|
|
toSkip++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disguise = new MobDisguise(disguiseType, adult);
|
|
|
|
} else if (disguiseType.isMisc()) {
|
|
|
|
// Its a misc, we are going to use the MiscDisguise constructor.
|
|
|
|
int miscId = -1;
|
|
|
|
int miscData = -1;
|
|
|
|
if (args.length > 1) {
|
|
|
|
// They have defined more arguements!
|
|
|
|
// If the first arg is a number
|
|
|
|
if (isNumeric(args[1])) {
|
|
|
|
miscId = Integer.parseInt(args[1]);
|
|
|
|
toSkip++;
|
|
|
|
// If they also defined a data value
|
|
|
|
if (args.length > 2) {
|
|
|
|
if (isNumeric(args[2])) {
|
|
|
|
miscData = Integer.parseInt(args[2]);
|
|
|
|
toSkip++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Construct the disguise
|
|
|
|
disguise = new MiscDisguise(disguiseType, true, miscId, miscData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Copy strings to their new range
|
|
|
|
String[] newArgs = new String[args.length - toSkip];
|
|
|
|
for (int i = toSkip; i < args.length; i++) {
|
|
|
|
newArgs[i - toSkip] = args[i];
|
|
|
|
}
|
|
|
|
args = newArgs;
|
|
|
|
// Don't throw a error about uneven methods names and values so we can throw the error about what is unknown later.
|
|
|
|
for (int i = 0; i < args.length; i += 2) {
|
|
|
|
String methodName = args[i];
|
|
|
|
if (i + 1 >= args.length) {
|
2013-11-05 17:53:33 +01:00
|
|
|
throw new Exception(ChatColor.RED + "No value was given for the option " + methodName);
|
2013-11-05 16:06:15 +01:00
|
|
|
}
|
|
|
|
String valueString = args[i + 1];
|
|
|
|
Method methodToUse = null;
|
|
|
|
Object value = null;
|
|
|
|
for (Method method : disguise.getWatcher().getClass().getMethods()) {
|
2013-11-05 16:34:38 +01:00
|
|
|
if (!method.getName().startsWith("get") && method.getName().equalsIgnoreCase(methodName)) {
|
2013-11-05 16:06:15 +01:00
|
|
|
methodToUse = method;
|
|
|
|
methodName = method.getName();
|
|
|
|
Class<?>[] types = method.getParameterTypes();
|
|
|
|
if (types.length == 1) {
|
|
|
|
Class param = types[0];
|
2013-11-06 09:28:35 +01:00
|
|
|
// Parse to number
|
2013-11-05 16:34:38 +01:00
|
|
|
if (float.class == param || double.class == param || int.class == param) {
|
2013-11-05 16:06:15 +01:00
|
|
|
if (isDouble(valueString)) {
|
2013-11-05 16:56:23 +01:00
|
|
|
float obj = Float.parseFloat(valueString);
|
|
|
|
if (param == float.class) {
|
|
|
|
value = (float) obj;
|
|
|
|
} else if (param == int.class) {
|
|
|
|
value = (int) obj;
|
|
|
|
} else if (param == double.class) {
|
|
|
|
value = (double) obj;
|
|
|
|
}
|
2013-11-05 16:06:15 +01:00
|
|
|
} else {
|
2013-11-06 07:02:31 +01:00
|
|
|
throw parseToException("number", valueString, methodName);
|
2013-11-05 16:06:15 +01:00
|
|
|
}
|
2013-11-06 09:28:35 +01:00
|
|
|
// Parse to boolean
|
2013-11-05 16:34:38 +01:00
|
|
|
} else if (boolean.class == param) {
|
2013-11-05 16:56:23 +01:00
|
|
|
if (!("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString)))
|
2013-11-06 07:02:31 +01:00
|
|
|
throw parseToException("true/false", valueString, methodName);
|
2013-11-05 16:56:23 +01:00
|
|
|
value = (boolean) "true".equalsIgnoreCase(valueString);
|
2013-11-06 09:28:35 +01:00
|
|
|
// Parse to string
|
2013-11-05 16:06:15 +01:00
|
|
|
} else if (param == String.class) {
|
2013-11-05 16:57:58 +01:00
|
|
|
value = ChatColor.translateAlternateColorCodes('&', valueString);
|
2013-11-06 09:28:35 +01:00
|
|
|
// Parse to animal color
|
2013-11-06 07:01:31 +01:00
|
|
|
} else if (param == AnimalColor.class) {
|
|
|
|
try {
|
|
|
|
value = AnimalColor.valueOf(valueString.toUpperCase());
|
|
|
|
} catch (Exception ex) {
|
2013-11-06 07:02:31 +01:00
|
|
|
throw parseToException("animal color", valueString, methodName);
|
2013-11-06 07:01:31 +01:00
|
|
|
}
|
2013-11-06 09:28:35 +01:00
|
|
|
// Parse to itemstack
|
2013-11-06 07:01:31 +01:00
|
|
|
} else if (param == ItemStack.class) {
|
|
|
|
try {
|
2013-11-06 07:02:31 +01:00
|
|
|
value = parseToItemstack(valueString);
|
2013-11-06 07:01:31 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
throw new Exception(String.format(ex.getMessage(), methodName));
|
|
|
|
}
|
2013-11-06 09:28:35 +01:00
|
|
|
// Parse to itemstack array
|
2013-11-06 07:01:31 +01:00
|
|
|
} 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 {
|
2013-11-06 07:02:31 +01:00
|
|
|
ItemStack item = parseToItemstack(split[a]);
|
2013-11-06 07:01:31 +01:00
|
|
|
items[a] = item;
|
|
|
|
} catch (Exception ex) {
|
2013-11-06 07:02:31 +01:00
|
|
|
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
|
2013-11-06 07:01:31 +01:00
|
|
|
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-06 07:02:31 +01:00
|
|
|
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
|
2013-11-06 07:01:31 +01:00
|
|
|
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
|
|
|
}
|
|
|
|
value = items;
|
2013-11-06 09:28:35 +01:00
|
|
|
// Parse to horse color
|
|
|
|
} else if (param == Color.class) {
|
|
|
|
try {
|
|
|
|
value = Color.valueOf(valueString.toUpperCase());
|
|
|
|
} catch (Exception ex) {
|
|
|
|
throw parseToException("horse color", valueString, methodName);
|
|
|
|
}
|
|
|
|
// Parse to horse style
|
|
|
|
} else if (param == Style.class) {
|
|
|
|
try {
|
|
|
|
value = Style.valueOf(valueString.toUpperCase());
|
|
|
|
} catch (Exception ex) {
|
|
|
|
throw parseToException("horse style", valueString, methodName);
|
|
|
|
}
|
|
|
|
// Parse to villager profession
|
|
|
|
} else if (param == Profession.class) {
|
|
|
|
try {
|
|
|
|
value = Profession.valueOf(valueString.toUpperCase());
|
|
|
|
} catch (Exception ex) {
|
|
|
|
throw parseToException("villager profession", valueString, methodName);
|
|
|
|
}
|
|
|
|
// Parse to ocelot type
|
|
|
|
} else if (param == Type.class) {
|
|
|
|
try {
|
|
|
|
value = Type.valueOf(valueString.toUpperCase());
|
|
|
|
} catch (Exception ex) {
|
|
|
|
throw parseToException("ocelot type", valueString, methodName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse to potion effect
|
|
|
|
} else if (param == PotionEffect.class) {
|
|
|
|
try {
|
|
|
|
PotionEffectType potionType = PotionEffectType.getByName(valueString.toUpperCase());
|
|
|
|
if (potionType == null && isNumeric(valueString)) {
|
|
|
|
potionType = PotionEffectType.getById(Integer.parseInt(valueString));
|
|
|
|
}
|
|
|
|
value = new PotionEffect(potionType, 0, 0);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
throw parseToException("potioneffect type", valueString, methodName);
|
|
|
|
}
|
2013-11-05 16:06:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (methodToUse == null) {
|
2013-11-06 07:01:31 +01:00
|
|
|
throw new Exception(ChatColor.RED + "Cannot find the option " + methodName);
|
2013-11-05 16:06:15 +01:00
|
|
|
}
|
|
|
|
methodToUse.invoke(disguise.getWatcher(), value);
|
|
|
|
}
|
|
|
|
// Alright. We've constructed our disguise.
|
|
|
|
return disguise;
|
|
|
|
}
|
2013-11-05 17:53:33 +01:00
|
|
|
|
2013-11-06 07:02:31 +01:00
|
|
|
private Exception parseToException(String expectedValue, String receivedInstead, String methodName) {
|
2013-11-06 07:01:31 +01:00
|
|
|
return new Exception(ChatColor.RED + "Expected " + ChatColor.GREEN + expectedValue + ChatColor.RED + ", received "
|
|
|
|
+ ChatColor.GREEN + receivedInstead + ChatColor.RED + " instead for " + ChatColor.GREEN + methodName);
|
|
|
|
}
|
|
|
|
|
2013-11-06 07:02:31 +01:00
|
|
|
private ItemStack parseToItemstack(String string) throws Exception {
|
2013-11-06 07:01:31 +01:00
|
|
|
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 {
|
2013-11-06 07:02:31 +01:00
|
|
|
throw parseToException("item ID:Durability combo", string, "%s");
|
2013-11-06 07:01:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return new ItemStack(itemId, 1, itemDura);
|
|
|
|
} else {
|
|
|
|
if (split.length == 1) {
|
2013-11-06 07:02:31 +01:00
|
|
|
throw parseToException("item ID", string, "%s");
|
2013-11-06 07:01:31 +01:00
|
|
|
} else {
|
2013-11-06 07:02:31 +01:00
|
|
|
throw parseToException("item ID:Durability combo", string, "%s");
|
2013-11-06 07:01:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 17:53:33 +01:00
|
|
|
protected abstract void sendCommandUsage(CommandSender sender);
|
2013-11-05 16:06:15 +01:00
|
|
|
}
|