2016-05-18 01:27:04 +02:00
|
|
|
package me.libraryaddict.disguise.commands;
|
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
2016-11-28 15:01:06 +01:00
|
|
|
import java.util.Iterator;
|
2016-06-14 20:40:45 +02:00
|
|
|
import java.util.Map;
|
2016-05-18 01:27:04 +02:00
|
|
|
import java.util.Map.Entry;
|
2016-06-14 20:40:45 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.regex.Pattern;
|
2016-05-18 01:27:04 +02:00
|
|
|
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.BlockFace;
|
|
|
|
import org.bukkit.command.CommandExecutor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Ageable;
|
|
|
|
import org.bukkit.entity.Animals;
|
2016-11-25 13:07:02 +01:00
|
|
|
import org.bukkit.entity.Llama;
|
2016-05-18 01:27:04 +02:00
|
|
|
import org.bukkit.entity.Monster;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.permissions.PermissionAttachmentInfo;
|
|
|
|
import org.bukkit.potion.PotionEffectType;
|
|
|
|
|
2016-07-08 07:59:30 +02:00
|
|
|
import com.comphenix.protocol.wrappers.BlockPosition;
|
2016-06-14 20:40:45 +02:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
|
|
|
import com.comphenix.protocol.wrappers.WrappedSignedProperty;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.AnimalColor;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.MiscDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.RabbitType;
|
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
2016-11-28 19:30:25 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.ReflectionFlagWatchers;
|
2016-05-18 01:27:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author libraryaddict
|
|
|
|
*/
|
2016-11-28 22:47:48 +01:00
|
|
|
public abstract class DisguiseBaseCommand implements CommandExecutor {
|
2016-11-25 13:07:02 +01:00
|
|
|
public class DisguiseParseException extends Exception {
|
2016-05-18 01:27:04 +02:00
|
|
|
private static final long serialVersionUID = 1276971370793124510L;
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
public DisguiseParseException() {
|
2016-05-18 01:27:04 +02:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
public DisguiseParseException(String string) {
|
2016-05-18 01:27:04 +02:00
|
|
|
super(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:01:06 +01:00
|
|
|
private Object callValueOf(Class<?> param, String valueString, String methodName, String description)
|
|
|
|
throws DisguiseParseException {
|
|
|
|
Object value;
|
|
|
|
try {
|
|
|
|
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
throw parseToException(description, valueString, methodName);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doCheck(HashMap<ArrayList<String>, Boolean> optionPermissions, ArrayList<String> usedOptions)
|
|
|
|
throws DisguiseParseException {
|
|
|
|
|
|
|
|
if (!passesCheck(optionPermissions, usedOptions)) {
|
|
|
|
throw new DisguiseParseException(ChatColor.RED + "You do not have the permission to use the option "
|
|
|
|
+ usedOptions.get(usedOptions.size() - 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ArrayList<String> filterTabs(ArrayList<String> list, String[] origArgs) {
|
|
|
|
if (origArgs.length == 0)
|
|
|
|
return list;
|
|
|
|
|
|
|
|
Iterator<String> itel = list.iterator();
|
|
|
|
String label = origArgs[origArgs.length - 1].toLowerCase();
|
|
|
|
|
|
|
|
while (itel.hasNext()) {
|
|
|
|
String name = itel.next();
|
|
|
|
|
|
|
|
if (name.toLowerCase().startsWith(label))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
itel.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
protected ArrayList<String> getAllowedDisguises(HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> hashMap) {
|
2016-05-18 01:27:04 +02:00
|
|
|
ArrayList<String> allowedDisguises = new ArrayList<>();
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (DisguiseType type : hashMap.keySet()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
allowedDisguises.add(type.toReadable().replace(" ", "_"));
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
Collections.sort(allowedDisguises, String.CASE_INSENSITIVE_ORDER);
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
return allowedDisguises;
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:01:06 +01:00
|
|
|
protected String[] getArgs(String[] args) {
|
|
|
|
ArrayList<String> newArgs = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (int i = 0; i < args.length - 1; i++) {
|
|
|
|
String s = args[i];
|
|
|
|
|
|
|
|
if (s.trim().isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
newArgs.add(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newArgs.toArray(new String[0]);
|
2016-05-18 01:27:04 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 22:47:48 +01:00
|
|
|
public String getPermNode() {
|
|
|
|
if (this instanceof DisguiseCommand) {
|
|
|
|
return "disguise";
|
|
|
|
}
|
|
|
|
else if (this instanceof DisguiseEntityCommand) {
|
|
|
|
return "disguiseentity";
|
|
|
|
}
|
|
|
|
else if (this instanceof DisguisePlayerCommand) {
|
|
|
|
return "disguiseplayer";
|
|
|
|
}
|
|
|
|
else if (this instanceof DisguiseRadiusCommand) {
|
|
|
|
return "disguiseradius";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new UnsupportedOperationException("Unknown disguise command, perm node not found");
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
protected HashMap<String, Boolean> getDisguiseOptions(CommandSender sender, DisguiseType type) {
|
|
|
|
switch (type) {
|
2016-05-18 01:27:04 +02:00
|
|
|
case PLAYER:
|
|
|
|
case FALLING_BLOCK:
|
|
|
|
case PAINTING:
|
|
|
|
case SPLASH_POTION:
|
|
|
|
case FISHING_HOOK:
|
|
|
|
case DROPPED_ITEM:
|
|
|
|
HashMap<String, Boolean> returns = new HashMap<>();
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-28 22:47:48 +01:00
|
|
|
String beginning = "libsdisguises.options." + getPermNode() + ".";
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (PermissionAttachmentInfo permission : sender.getEffectivePermissions()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
String lowerPerm = permission.getPermission().toLowerCase();
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (lowerPerm.startsWith(beginning)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
String[] split = lowerPerm.substring(beginning.length()).split("\\.");
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (split.length > 1) {
|
|
|
|
if (split[0].replace("_", "").equals(type.name().toLowerCase().replace("_", ""))) {
|
|
|
|
for (int i = 1; i < split.length; i++) {
|
2016-05-18 01:27:04 +02:00
|
|
|
returns.put(split[i], permission.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
return returns;
|
|
|
|
default:
|
|
|
|
return new HashMap<>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:01:06 +01:00
|
|
|
private Entry<Method, Integer> getMethod(Method[] methods, String methodName, int toStart) {
|
|
|
|
for (int i = toStart; i < methods.length; i++) {
|
|
|
|
Method method = methods[i];
|
|
|
|
if (!method.getName().startsWith("get") && method.getName().equalsIgnoreCase(methodName)
|
|
|
|
&& method.getAnnotation(Deprecated.class) == null && method.getParameterTypes().length == 1) {
|
|
|
|
return new HashMap.SimpleEntry(method, ++i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private HashMap<ArrayList<String>, Boolean> getOptions(String perm) {
|
|
|
|
ArrayList<String> list = new ArrayList<>();
|
|
|
|
boolean isRemove = true;
|
|
|
|
String[] split = perm.split("\\.");
|
|
|
|
for (int i = 1; i < split.length; i++) {
|
|
|
|
String option = split[i];
|
|
|
|
boolean value = option.startsWith("-");
|
|
|
|
if (value) {
|
|
|
|
option = option.substring(1);
|
|
|
|
isRemove = false;
|
|
|
|
}
|
|
|
|
if (option.equals("baby")) {
|
|
|
|
option = "setbaby";
|
|
|
|
}
|
|
|
|
list.add(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMap<ArrayList<String>, Boolean> options = new HashMap<>();
|
|
|
|
options.put(list, isRemove);
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> getPermissions(CommandSender sender) {
|
2016-11-28 22:47:48 +01:00
|
|
|
return getPermissions(sender, "libsdisguises." + getPermNode() + ".");
|
2016-11-28 15:01:06 +01:00
|
|
|
}
|
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
/**
|
|
|
|
* Get perms for the node. Returns a hashmap of allowed disguisetypes and their options
|
|
|
|
*
|
|
|
|
* @param sender
|
|
|
|
* @param permissionNode
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
protected HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> getPermissions(CommandSender sender,
|
2016-11-25 13:07:02 +01:00
|
|
|
String permissionNode) {
|
2016-05-18 01:27:04 +02:00
|
|
|
HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> singleDisguises = new HashMap<>();
|
|
|
|
HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> rangeDisguises = new HashMap<>();
|
|
|
|
HashMap<String, Boolean> perms = new HashMap<>();
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (PermissionAttachmentInfo permission : sender.getEffectivePermissions()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
String perm = permission.getPermission().toLowerCase();
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (perm.startsWith(permissionNode) && (!perms.containsKey(perm) || !permission.getValue())) {
|
2016-05-18 01:27:04 +02:00
|
|
|
perms.put(perm, permission.getValue());
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (!perms.containsKey(permissionNode + "*") && sender.hasPermission(permissionNode + "*")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
perms.put(permissionNode + "*", true);
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (!perms.containsKey(permissionNode + "*.*") && sender.hasPermission(permissionNode + "*.*")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
perms.put(permissionNode + "*.*", true);
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (String perm : perms.keySet()) {
|
|
|
|
if (perms.get(perm)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
perm = perm.substring(permissionNode.length());
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
String disguiseType = perm.split("\\.")[0];
|
|
|
|
DisguiseType dType = null;
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (DisguiseType t : DisguiseType.values()) {
|
|
|
|
if (t.name().replace("_", "").equalsIgnoreCase(disguiseType.replace("_", ""))) {
|
2016-05-18 01:27:04 +02:00
|
|
|
dType = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (dType != null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
HashMap<ArrayList<String>, Boolean> list;
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (singleDisguises.containsKey(dType)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
list = singleDisguises.get(dType);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
list = new HashMap<>();
|
|
|
|
singleDisguises.put(dType, list);
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
HashMap<ArrayList<String>, Boolean> map1 = getOptions(perm);
|
|
|
|
list.put(map1.keySet().iterator().next(), map1.values().iterator().next());
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
|
|
|
for (DisguiseType type : DisguiseType.values()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
HashMap<ArrayList<String>, Boolean> options = null;
|
|
|
|
Class entityClass = type.getEntityClass();
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (disguiseType.equals("mob")) {
|
|
|
|
if (type.isMob()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
options = getOptions(perm);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (disguiseType.equals("animal") || disguiseType.equals("animals")) {
|
|
|
|
if (Animals.class.isAssignableFrom(entityClass)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
options = getOptions(perm);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (disguiseType.equals("monster") || disguiseType.equals("monsters")) {
|
|
|
|
if (Monster.class.isAssignableFrom(entityClass)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
options = getOptions(perm);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (disguiseType.equals("misc")) {
|
|
|
|
if (type.isMisc()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
options = getOptions(perm);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (disguiseType.equals("ageable")) {
|
|
|
|
if (Ageable.class.isAssignableFrom(entityClass)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
options = getOptions(perm);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (disguiseType.equals("*")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
options = getOptions(perm);
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (options != null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
HashMap<ArrayList<String>, Boolean> list;
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (rangeDisguises.containsKey(type)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
list = rangeDisguises.get(type);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
list = new HashMap<>();
|
|
|
|
rangeDisguises.put(type, list);
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
HashMap<ArrayList<String>, Boolean> map1 = getOptions(perm);
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
list.put(map1.keySet().iterator().next(), map1.values().iterator().next());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (String perm : perms.keySet()) {
|
|
|
|
if (!perms.get(perm)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
perm = perm.substring(permissionNode.length());
|
2016-11-28 22:47:48 +01:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
String disguiseType = perm.split("\\.")[0];
|
|
|
|
DisguiseType dType = null;
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (DisguiseType t : DisguiseType.values()) {
|
|
|
|
if (t.name().replace("_", "").equalsIgnoreCase(disguiseType.replace("_", ""))) {
|
2016-05-18 01:27:04 +02:00
|
|
|
dType = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (dType != null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
singleDisguises.remove(dType);
|
|
|
|
rangeDisguises.remove(dType);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
|
|
|
for (DisguiseType type : DisguiseType.values()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
boolean foundHim = false;
|
|
|
|
Class entityClass = type.getEntityClass();
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
switch (disguiseType) {
|
2016-05-18 01:27:04 +02:00
|
|
|
case "mob":
|
2016-11-25 13:07:02 +01:00
|
|
|
if (type.isMob()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
foundHim = true;
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
break;
|
|
|
|
case "animal":
|
|
|
|
case "animals":
|
2016-11-25 13:07:02 +01:00
|
|
|
if (Animals.class.isAssignableFrom(entityClass)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
foundHim = true;
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
break;
|
|
|
|
case "monster":
|
|
|
|
case "monsters":
|
2016-11-25 13:07:02 +01:00
|
|
|
if (Monster.class.isAssignableFrom(entityClass)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
foundHim = true;
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
break;
|
|
|
|
case "misc":
|
2016-11-25 13:07:02 +01:00
|
|
|
if (type.isMisc()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
foundHim = true;
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
break;
|
|
|
|
case "ageable":
|
2016-11-25 13:07:02 +01:00
|
|
|
if (Ageable.class.isAssignableFrom(entityClass)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
foundHim = true;
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
break;
|
|
|
|
case "*":
|
|
|
|
foundHim = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (foundHim) {
|
2016-05-18 01:27:04 +02:00
|
|
|
rangeDisguises.remove(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map = new HashMap<>();
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (DisguiseType type : DisguiseType.values()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
HashMap<ArrayList<String>, Boolean> temp = new HashMap<>();
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (singleDisguises.containsKey(type)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
temp.putAll(singleDisguises.get(type));
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (rangeDisguises.containsKey(type)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
temp.putAll(rangeDisguises.get(type));
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (!temp.isEmpty()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
map.put(type, temp);
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
protected boolean isDouble(String string) {
|
|
|
|
try {
|
2016-05-18 01:27:04 +02:00
|
|
|
Float.parseFloat(string);
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-05-18 01:27:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
protected boolean isNumeric(String string) {
|
|
|
|
try {
|
2016-05-18 01:27:04 +02:00
|
|
|
Integer.parseInt(string);
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-05-18 01:27:04 +02:00
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param sender
|
|
|
|
* @param args
|
|
|
|
* @param map
|
|
|
|
* @return
|
2016-11-28 22:47:48 +01:00
|
|
|
* @throws DisguiseBaseCommand.DisguiseParseException
|
2016-05-18 01:27:04 +02:00
|
|
|
* @throws java.lang.IllegalAccessException
|
|
|
|
* @throws java.lang.reflect.InvocationTargetException
|
|
|
|
*/
|
|
|
|
protected Disguise parseDisguise(CommandSender sender, String[] args,
|
|
|
|
HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map)
|
2016-11-25 13:07:02 +01:00
|
|
|
throws DisguiseParseException, IllegalAccessException, InvocationTargetException {
|
|
|
|
if (map.isEmpty()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(ChatColor.RED + "You are forbidden to use this command.");
|
|
|
|
}
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (args.length == 0) {
|
2016-05-18 01:27:04 +02:00
|
|
|
sendCommandUsage(sender, map);
|
|
|
|
throw new DisguiseParseException();
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
// How many args to skip due to the disugise being constructed
|
|
|
|
// Time to start constructing the disguise.
|
|
|
|
// We will need to check between all 3 kinds of disguises
|
|
|
|
int toSkip = 1;
|
|
|
|
ArrayList<String> usedOptions = new ArrayList<>();
|
|
|
|
Disguise disguise = null;
|
|
|
|
HashMap<ArrayList<String>, Boolean> optionPermissions;
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (args[0].startsWith("@")) {
|
|
|
|
if (sender.hasPermission("libsdisguises.disguise.disguiseclone")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
disguise = DisguiseUtilities.getClonedDisguise(args[0].toLowerCase());
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (disguise == null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(ChatColor.RED + "Cannot find a disguise under the reference " + args[0]);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(ChatColor.RED + "You do not have perimssion to use disguise references!");
|
|
|
|
}
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
optionPermissions = (map.containsKey(disguise.getType()) ? map.get(disguise.getType())
|
|
|
|
: new HashMap<ArrayList<String>, Boolean>());
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
DisguiseType disguiseType = null;
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (args[0].equalsIgnoreCase("p")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
disguiseType = DisguiseType.PLAYER;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
|
|
|
for (DisguiseType type : DisguiseType.values()) {
|
|
|
|
if (args[0].equalsIgnoreCase(type.name()) || args[0].equalsIgnoreCase(type.name().replace("_", ""))) {
|
2016-05-18 01:27:04 +02:00
|
|
|
disguiseType = type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (disguiseType == null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(
|
|
|
|
ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0] + ChatColor.RED + " doesn't exist!");
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (disguiseType.isUnknown()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(ChatColor.RED + "Error! You cannot disguise as " + ChatColor.GREEN + "Unknown!");
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (disguiseType.getEntityType() == null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(ChatColor.RED + "Error! This version of minecraft does not have that disguise!");
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (!map.containsKey(disguiseType)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(ChatColor.RED + "You are forbidden to use this disguise.");
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
optionPermissions = map.get(disguiseType);
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-06-18 17:16:32 +02:00
|
|
|
HashMap<String, Boolean> disguiseOptions = this.getDisguiseOptions(sender, disguiseType);
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (disguiseType.isPlayer()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// If he is doing a player disguise
|
2016-11-25 13:07:02 +01:00
|
|
|
if (args.length == 1) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// He needs to give the player name
|
|
|
|
throw new DisguiseParseException(ChatColor.RED + "Error! You need to give a player name!");
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
if (!disguiseOptions.isEmpty() && (!disguiseOptions.containsKey(args[1].toLowerCase())
|
2016-11-25 13:07:02 +01:00
|
|
|
|| !disguiseOptions.get(args[1].toLowerCase()))) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(ChatColor.RED + "Error! You don't have permission to use that name!");
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
args[1] = args[1].replace("\\_", " ");
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
// Construct the player disguise
|
|
|
|
disguise = new PlayerDisguise(ChatColor.translateAlternateColorCodes('&', args[1]));
|
|
|
|
toSkip++;
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
|
|
|
if (disguiseType.isMob()) { // Its a mob, use the mob constructor
|
2016-05-18 01:27:04 +02:00
|
|
|
boolean adult = true;
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (args.length > 1) {
|
|
|
|
if (args[1].equalsIgnoreCase("baby") || args[1].equalsIgnoreCase("adult")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
usedOptions.add("setbaby");
|
|
|
|
doCheck(optionPermissions, usedOptions);
|
|
|
|
adult = args[1].equalsIgnoreCase("adult");
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
toSkip++;
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
disguise = new MobDisguise(disguiseType, adult);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (disguiseType.isMisc()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Its a misc, we are going to use the MiscDisguise constructor.
|
|
|
|
int miscId = -1;
|
|
|
|
int miscData = -1;
|
|
|
|
String secondArg = null;
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (args.length > 1) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// They have defined more arguments!
|
|
|
|
// If the first arg is a number
|
2016-11-25 13:07:02 +01:00
|
|
|
if (args[1].contains(":")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
String[] split = args[1].split(":");
|
2016-11-25 13:07:02 +01:00
|
|
|
if (isNumeric(split[1])) {
|
2016-05-18 01:27:04 +02:00
|
|
|
secondArg = split[1];
|
|
|
|
}
|
|
|
|
args[1] = split[0];
|
|
|
|
}
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (isNumeric(args[1])) {
|
2016-05-18 01:27:04 +02:00
|
|
|
miscId = Integer.parseInt(args[1]);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
|
|
|
if (disguiseType == DisguiseType.FALLING_BLOCK || disguiseType == DisguiseType.DROPPED_ITEM) {
|
|
|
|
for (Material mat : Material.values()) {
|
|
|
|
if (mat.name().replace("_", "").equalsIgnoreCase(args[1].replace("_", ""))) {
|
2016-05-18 01:27:04 +02:00
|
|
|
miscId = mat.getId();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
if (miscId != -1) {
|
|
|
|
switch (disguiseType) {
|
2016-05-18 01:27:04 +02:00
|
|
|
case PAINTING:
|
|
|
|
case FALLING_BLOCK:
|
|
|
|
case SPLASH_POTION:
|
|
|
|
case DROPPED_ITEM:
|
|
|
|
case FISHING_HOOK:
|
|
|
|
case ARROW:
|
|
|
|
case TIPPED_ARROW:
|
|
|
|
case SPECTRAL_ARROW:
|
|
|
|
case SMALL_FIREBALL:
|
|
|
|
case FIREBALL:
|
|
|
|
case WITHER_SKULL:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DisguiseParseException(ChatColor.RED + "Error! " + disguiseType.toReadable()
|
|
|
|
+ " doesn't know what to do with " + args[1] + "!");
|
|
|
|
}
|
|
|
|
toSkip++;
|
|
|
|
// If they also defined a data value
|
2016-11-25 13:07:02 +01:00
|
|
|
if (args.length > 2 && secondArg == null && isNumeric(args[2])) {
|
2016-05-18 01:27:04 +02:00
|
|
|
secondArg = args[2];
|
|
|
|
toSkip++;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
if (secondArg != null) {
|
|
|
|
if (disguiseType != DisguiseType.FALLING_BLOCK && disguiseType != DisguiseType.DROPPED_ITEM) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(ChatColor.RED + "Error! Only the disguises "
|
|
|
|
+ DisguiseType.FALLING_BLOCK.toReadable() + " and "
|
|
|
|
+ DisguiseType.DROPPED_ITEM.toReadable() + " uses a second number!");
|
|
|
|
}
|
|
|
|
miscData = Integer.parseInt(secondArg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (!disguiseOptions.isEmpty() && miscId != -1) {
|
2016-05-18 01:27:04 +02:00
|
|
|
String toCheck = "" + miscId;
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (miscData == 0 || miscData == -1) {
|
|
|
|
if (!disguiseOptions.containsKey(toCheck) || !disguiseOptions.get(toCheck)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
toCheck += ":0";
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
toCheck += ":" + miscData;
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (!disguiseOptions.containsKey(toCheck) || !disguiseOptions.get(toCheck)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(
|
|
|
|
ChatColor.RED + "Error! You do not have permission to use the parameter " + toCheck
|
|
|
|
+ " on the " + disguiseType.toReadable() + " disguise!");
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (miscId != -1) {
|
|
|
|
if (disguiseType == DisguiseType.FALLING_BLOCK) {
|
2016-05-18 01:27:04 +02:00
|
|
|
usedOptions.add("setblock");
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
doCheck(optionPermissions, usedOptions);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (disguiseType == DisguiseType.PAINTING) {
|
2016-05-18 01:27:04 +02:00
|
|
|
usedOptions.add("setpainting");
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
doCheck(optionPermissions, usedOptions);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (disguiseType == DisguiseType.SPLASH_POTION) {
|
2016-05-18 01:27:04 +02:00
|
|
|
usedOptions.add("setpotionid");
|
2016-11-28 15:01:06 +01:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
doCheck(optionPermissions, usedOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Construct the disguise
|
|
|
|
disguise = new MiscDisguise(disguiseType, miscId, miscData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Copy strings to their new range
|
|
|
|
String[] newArgs = new String[args.length - toSkip];
|
|
|
|
System.arraycopy(args, toSkip, newArgs, 0, args.length - toSkip);
|
|
|
|
args = newArgs;
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-28 19:30:25 +01:00
|
|
|
Method[] methods = ReflectionFlagWatchers.getDisguiseWatcherMethods(disguise.getWatcher().getClass());
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (int i = 0; i < args.length; i += 2) {
|
2016-05-18 01:27:04 +02:00
|
|
|
String methodName = args[i];
|
|
|
|
String valueString = (args.length - 1 == i ? null : args[i + 1]);
|
|
|
|
Method methodToUse = null;
|
|
|
|
Object value = null;
|
|
|
|
DisguiseParseException storedEx = null;
|
|
|
|
int c = 0;
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
while (c < methods.length) {
|
|
|
|
try {
|
2016-05-18 01:27:04 +02:00
|
|
|
Entry<Method, Integer> entry = getMethod(methods, methodName, c);
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (entry == null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
methodToUse = entry.getKey();
|
|
|
|
c = entry.getValue();
|
|
|
|
methodName = methodToUse.getName();
|
|
|
|
Class<?>[] types = methodToUse.getParameterTypes();
|
|
|
|
Class param = types[0];
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (valueString != null) {
|
|
|
|
if (int.class == param) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to integer
|
2016-11-25 13:07:02 +01:00
|
|
|
if (isNumeric(valueString)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
value = Integer.parseInt(valueString);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("number", valueString, methodName);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (WrappedGameProfile.class == param && valueString.length() > 20) {
|
|
|
|
try {
|
2016-06-14 20:40:45 +02:00
|
|
|
Map<String, Object> response = new Gson().fromJson(valueString, Map.class);
|
|
|
|
|
|
|
|
String id = (String) response.get("id");
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (!id.contains("-")) {
|
2016-06-14 20:40:45 +02:00
|
|
|
id = Pattern
|
|
|
|
.compile(
|
|
|
|
"([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)")
|
|
|
|
.matcher(id).replaceFirst("$1-$2-$3-$4-$5");
|
|
|
|
}
|
|
|
|
|
|
|
|
WrappedGameProfile gameProfile = new WrappedGameProfile(UUID.fromString(id),
|
|
|
|
(String) response.get("name"));
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (response.containsKey("properties")) {
|
2016-06-14 20:40:45 +02:00
|
|
|
ArrayList<Map<String, String>> properties = (ArrayList) response.get("properties");
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (Map<String, String> s : properties) {
|
2016-06-14 20:40:45 +02:00
|
|
|
String gName = null;
|
|
|
|
String gValue = null;
|
|
|
|
String gSigned = null;
|
|
|
|
|
|
|
|
if (s.containsKey("name"))
|
|
|
|
gName = s.get("name");
|
|
|
|
|
|
|
|
if (s.containsKey("value"))
|
|
|
|
gValue = s.get("value");
|
|
|
|
|
|
|
|
if (s.containsKey("signature"))
|
|
|
|
gSigned = s.get("signature");
|
|
|
|
|
|
|
|
gameProfile.getProperties().put(gName, new WrappedSignedProperty(gName, gValue, gSigned));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value = gameProfile;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-14 20:40:45 +02:00
|
|
|
throw parseToException("gameprofile", valueString, methodName);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (float.class == param || double.class == param) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to number
|
2016-11-25 13:07:02 +01:00
|
|
|
if (isDouble(valueString)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
float obj = Float.parseFloat(valueString);
|
2016-11-25 13:07:02 +01:00
|
|
|
if (param == float.class) {
|
2016-05-18 01:27:04 +02:00
|
|
|
value = obj;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param == double.class) {
|
2016-05-18 01:27:04 +02:00
|
|
|
value = (double) obj;
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("number.0", valueString, methodName);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param == String.class) {
|
2016-06-14 20:40:45 +02:00
|
|
|
if (methodName.equalsIgnoreCase("setskin") && valueString.length() > 20)
|
|
|
|
continue;
|
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to string
|
|
|
|
value = ChatColor.translateAlternateColorCodes('&', valueString);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param == AnimalColor.class) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to animal color
|
2016-11-25 13:07:02 +01:00
|
|
|
try {
|
2016-05-18 01:27:04 +02:00
|
|
|
value = AnimalColor.valueOf(valueString.toUpperCase());
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("animal color", valueString, methodName);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param == Llama.Color.class) {
|
|
|
|
try {
|
|
|
|
value = Llama.Color.valueOf(valueString.toUpperCase());
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
throw parseToException("llama color", valueString, methodName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (param == ItemStack.class) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to itemstack
|
2016-11-25 13:07:02 +01:00
|
|
|
try {
|
2016-05-18 01:27:04 +02:00
|
|
|
value = parseToItemstack(valueString);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(String.format(ex.getMessage(), methodName));
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param == ItemStack[].class) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to itemstack array
|
|
|
|
ItemStack[] items = new ItemStack[4];
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
String[] split = valueString.split(",");
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (split.length == 4) {
|
|
|
|
for (int a = 0; a < 4; a++) {
|
|
|
|
try {
|
2016-05-18 01:27:04 +02:00
|
|
|
items[a] = parseToItemstack(split[a]);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
|
|
|
|
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
|
|
|
|
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
value = items;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param.getSimpleName().equals("Color")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to horse color
|
|
|
|
value = callValueOf(param, valueString, methodName, "a horse color");
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param.getSimpleName().equals("Style")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to horse style
|
|
|
|
value = callValueOf(param, valueString, methodName, "a horse style");
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param.getSimpleName().equals("Profession")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to villager profession
|
|
|
|
value = callValueOf(param, valueString, methodName, "a villager profession");
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param.getSimpleName().equals("Art")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to art type
|
|
|
|
value = callValueOf(param, valueString, methodName, "a painting art");
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param.getSimpleName().equals("Type")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to ocelot type
|
|
|
|
value = callValueOf(param, valueString, methodName, "a ocelot type");
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param == PotionEffectType.class) {
|
2016-05-18 01:27:04 +02:00
|
|
|
// Parse to potion effect
|
2016-11-25 13:07:02 +01:00
|
|
|
try {
|
2016-05-18 01:27:04 +02:00
|
|
|
PotionEffectType potionType = PotionEffectType.getByName(valueString.toUpperCase());
|
2016-11-25 13:07:02 +01:00
|
|
|
if (potionType == null && isNumeric(valueString)) {
|
2016-05-18 01:27:04 +02:00
|
|
|
potionType = PotionEffectType.getById(Integer.parseInt(valueString));
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
if (potionType == null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException();
|
|
|
|
}
|
|
|
|
value = potionType;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("a potioneffect type", valueString, methodName);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param == int[].class) {
|
2016-05-18 01:27:04 +02:00
|
|
|
String[] split = valueString.split(",");
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
int[] values = new int[split.length];
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
for (int b = 0; b < values.length; b++) {
|
|
|
|
try {
|
2016-05-18 01:27:04 +02:00
|
|
|
values[b] = Integer.parseInt(split[b]);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (NumberFormatException ex) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("Number,Number,Number...", valueString, methodName);
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
value = values;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param == BlockFace.class) {
|
|
|
|
try {
|
2016-05-18 01:27:04 +02:00
|
|
|
BlockFace face = BlockFace.valueOf(valueString.toUpperCase());
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (face.ordinal() > 5) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException();
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
value = face;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-07-08 07:59:30 +02:00
|
|
|
throw parseToException("a direction (north, east, south, west, up, down)", valueString,
|
|
|
|
methodName);
|
2016-05-18 01:27:04 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param == RabbitType.class) {
|
|
|
|
try {
|
|
|
|
for (RabbitType type : RabbitType.values()) {
|
2016-05-18 01:27:04 +02:00
|
|
|
if (type.name().replace("_", "")
|
2016-11-25 13:07:02 +01:00
|
|
|
.equalsIgnoreCase(valueString.replace("_", "").replace(" ", ""))) {
|
2016-05-18 01:27:04 +02:00
|
|
|
value = type;
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
if (value == null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("rabbit type (white, brown, patches...)", valueString, methodName);
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (param == BlockPosition.class) {
|
|
|
|
try {
|
2016-07-08 07:59:30 +02:00
|
|
|
String[] split = valueString.split(",");
|
|
|
|
|
|
|
|
assert split.length == 3;
|
|
|
|
|
|
|
|
value = new BlockPosition(Integer.parseInt(split[0]), Integer.parseInt(split[1]),
|
|
|
|
Integer.parseInt(split[2]));
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
|
|
|
throw parseToException("three numbers Number,Number,Number", valueString, methodName);
|
2016-07-08 07:59:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-18 01:27:04 +02:00
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (value == null && boolean.class == param) {
|
|
|
|
if (valueString == null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
value = true;
|
|
|
|
i--;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (valueString.equalsIgnoreCase("true")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
value = true;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else if (valueString.equalsIgnoreCase("false")) {
|
2016-05-18 01:27:04 +02:00
|
|
|
value = false;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
|
|
|
if (getMethod(methods, valueString, 0) == null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("true/false", valueString, methodName);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
value = true;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 20:40:45 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (value != null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (DisguiseParseException ex) {
|
2016-05-18 01:27:04 +02:00
|
|
|
storedEx = ex;
|
|
|
|
methodToUse = null;
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-18 01:27:04 +02:00
|
|
|
methodToUse = null;
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
if (methodToUse == null) {
|
|
|
|
if (storedEx != null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw storedEx;
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(ChatColor.RED + "Cannot find the option " + methodName);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
if (value == null) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw new DisguiseParseException(ChatColor.RED + "No value was given for the option " + methodName);
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (!usedOptions.contains(methodName.toLowerCase())) {
|
2016-05-18 01:27:04 +02:00
|
|
|
usedOptions.add(methodName.toLowerCase());
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
doCheck(optionPermissions, usedOptions);
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (FlagWatcher.class.isAssignableFrom(methodToUse.getDeclaringClass())) {
|
2016-05-18 01:27:04 +02:00
|
|
|
methodToUse.invoke(disguise.getWatcher(), value);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
methodToUse.invoke(disguise, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Alright. We've constructed our disguise.
|
|
|
|
return disguise;
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
private DisguiseParseException parseToException(String expectedValue, String receivedInstead, String methodName) {
|
2016-05-18 01:27:04 +02:00
|
|
|
return new DisguiseParseException(
|
|
|
|
ChatColor.RED + "Expected " + ChatColor.GREEN + expectedValue + ChatColor.RED + ", received " + ChatColor.GREEN
|
|
|
|
+ receivedInstead + ChatColor.RED + " instead for " + ChatColor.GREEN + methodName);
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
private ItemStack parseToItemstack(String string) throws Exception {
|
2016-05-18 01:27:04 +02:00
|
|
|
String[] split = string.split(":", -1);
|
2016-11-28 19:30:25 +01:00
|
|
|
|
|
|
|
int itemId = -1;
|
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (isNumeric(split[0])) {
|
2016-11-28 19:30:25 +01:00
|
|
|
itemId = Integer.parseInt(split[0]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
itemId = Material.valueOf(split[0].toUpperCase()).getId();
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemId != -1) {
|
2016-05-18 01:27:04 +02:00
|
|
|
short itemDura = 0;
|
2016-11-28 19:30:25 +01:00
|
|
|
|
2016-11-25 13:07:02 +01:00
|
|
|
if (split.length > 1) {
|
|
|
|
if (isNumeric(split[1])) {
|
2016-05-18 01:27:04 +02:00
|
|
|
itemDura = Short.parseShort(split[1]);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("item ID:Durability combo", string, "%s");
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 19:30:25 +01:00
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
return new ItemStack(itemId, 1, itemDura);
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
|
|
|
if (split.length == 1) {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("item ID", string, "%s");
|
|
|
|
}
|
2016-11-25 13:07:02 +01:00
|
|
|
else {
|
2016-05-18 01:27:04 +02:00
|
|
|
throw parseToException("item ID:Durability combo", string, "%s");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:01:06 +01:00
|
|
|
public boolean passesCheck(HashMap<ArrayList<String>, Boolean> theirPermissions, ArrayList<String> usedOptions) {
|
|
|
|
boolean hasPermission = false;
|
|
|
|
|
|
|
|
for (ArrayList<String> list : theirPermissions.keySet()) {
|
|
|
|
boolean myPerms = true;
|
|
|
|
|
|
|
|
for (String option : usedOptions) {
|
|
|
|
if (!(theirPermissions.get(list) && list.contains("*"))
|
|
|
|
&& (list.contains(option) != theirPermissions.get(list))) {
|
|
|
|
myPerms = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (myPerms) {
|
|
|
|
hasPermission = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasPermission;
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:27:04 +02:00
|
|
|
protected abstract void sendCommandUsage(CommandSender sender,
|
|
|
|
HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map);
|
|
|
|
}
|