2018-09-07 04:35:38 +02:00
|
|
|
package me.libraryaddict.disguise.utilities.parser;
|
|
|
|
|
2019-03-05 05:46:47 +01:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
2019-12-31 07:35:21 +01:00
|
|
|
import com.google.gson.Gson;
|
2018-09-07 04:35:38 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseConfig;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.*;
|
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
2019-01-12 04:56:17 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
|
2019-05-18 08:54:51 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.parser.params.ParamInfoManager;
|
2019-03-05 05:46:47 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
|
2019-01-03 03:13:03 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.translations.LibsMsg;
|
|
|
|
import me.libraryaddict.disguise.utilities.translations.TranslateType;
|
2019-04-16 22:10:15 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2018-09-07 04:35:38 +02:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.command.CommandSender;
|
2019-03-05 05:46:47 +01:00
|
|
|
import org.bukkit.entity.Entity;
|
2020-01-05 21:28:30 +01:00
|
|
|
import org.bukkit.entity.LivingEntity;
|
2018-10-05 01:04:30 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2020-01-05 21:28:30 +01:00
|
|
|
import org.bukkit.inventory.EntityEquipment;
|
2018-09-07 04:35:38 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.permissions.PermissionAttachmentInfo;
|
2019-12-31 07:35:21 +01:00
|
|
|
import org.bukkit.potion.PotionEffectType;
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
2018-10-23 23:13:13 +02:00
|
|
|
import java.util.*;
|
2018-09-07 04:35:38 +02:00
|
|
|
import java.util.Map.Entry;
|
2019-03-06 03:16:14 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
public class DisguiseParser {
|
2020-01-19 03:48:26 +01:00
|
|
|
/**
|
|
|
|
* <Setter, <Getter, DefaultValue>>
|
|
|
|
*/
|
2019-12-31 07:35:21 +01:00
|
|
|
private static HashMap<Method, Map.Entry<Method, Object>> defaultWatcherValues = new HashMap<>();
|
|
|
|
|
|
|
|
public static void createDefaultMethods() {
|
|
|
|
try {
|
|
|
|
for (DisguiseType type : DisguiseType.values()) {
|
|
|
|
Disguise disguise;
|
|
|
|
|
|
|
|
if (type.isMisc()) {
|
|
|
|
disguise = new MiscDisguise(type);
|
|
|
|
} else if (type.isMob()) {
|
|
|
|
disguise = new MobDisguise(type);
|
|
|
|
} else {
|
|
|
|
disguise = new PlayerDisguise("Foobar");
|
|
|
|
}
|
|
|
|
|
|
|
|
FlagWatcher watcher = type.getWatcherClass().getConstructor(Disguise.class).newInstance(disguise);
|
|
|
|
|
|
|
|
Method[] methods = ParamInfoManager.getDisguiseWatcherMethods(watcher.getClass());
|
|
|
|
|
|
|
|
for (Method setMethod : methods) {
|
|
|
|
// Invalidate methods that can't be handled normally
|
|
|
|
if (setMethod.getName().equals("addPotionEffect") || (setMethod.getName().equals("setSkin") &&
|
|
|
|
setMethod.getParameterTypes()[0] == String.class) ||
|
|
|
|
(setMethod.getName().equals("setTarget") &&
|
|
|
|
setMethod.getParameterTypes()[0] != int.class) ||
|
|
|
|
(setMethod.getName().equals("setItemInMainHand") &&
|
|
|
|
setMethod.getParameterTypes()[0] == Material.class)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName = setMethod.getName().substring(3); // Remove 'set'
|
|
|
|
|
|
|
|
if (getName.equals("HasNectar")) {
|
|
|
|
getName = "hasNectar";
|
|
|
|
} else if (getName.equals("HasStung")) {
|
|
|
|
getName = "hasStung";
|
|
|
|
} else if (setMethod.getParameterTypes()[0].isAssignableFrom(boolean.class)) {
|
|
|
|
getName = "is" + getName;
|
|
|
|
} else {
|
|
|
|
getName = "get" + getName;
|
|
|
|
}
|
|
|
|
|
|
|
|
Method getMethod = null;
|
|
|
|
|
|
|
|
for (Method m : setMethod.getDeclaringClass().getDeclaredMethods()) {
|
|
|
|
if (!m.getName().equals(getName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.getParameterTypes().length > 0 || m.getReturnType() != setMethod.getParameterTypes()[0]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMethod = m;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getMethod == null) {
|
|
|
|
DisguiseUtilities.getLogger().severe(String
|
|
|
|
.format("No such method '%s' when looking for the companion of '%s' in '%s'", getName,
|
|
|
|
setMethod.getName(), setMethod.getDeclaringClass().getSimpleName()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object defaultValue = null;
|
|
|
|
|
|
|
|
// Value is randomish so shouldn't be checked, should always specify value when setting
|
|
|
|
if (!setMethod.isAnnotationPresent(RandomDefaultValue.class)) {
|
|
|
|
Object invokeWith = watcher;
|
|
|
|
|
|
|
|
if (!FlagWatcher.class.isAssignableFrom(getMethod.getDeclaringClass())) {
|
|
|
|
invokeWith = disguise;
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultValue = getMethod.invoke(invokeWith);
|
|
|
|
}
|
|
|
|
|
|
|
|
addWatcherDefault(setMethod, getMethod, defaultValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 03:48:26 +01:00
|
|
|
public static HashMap<Method, Entry<Method, Object>> getMethodDefaults() {
|
|
|
|
return defaultWatcherValues;
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:35:21 +01:00
|
|
|
public static String parseToString(Disguise disguise) {
|
2020-01-02 05:10:36 +01:00
|
|
|
return parseToString(disguise, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not outputting skin information is not garanteed to display the correct player name
|
|
|
|
*/
|
|
|
|
public static String parseToString(Disguise disguise, boolean outputSkinData) {
|
2019-12-31 07:35:21 +01:00
|
|
|
try {
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
stringBuilder.append(disguise.getType().name());
|
|
|
|
|
|
|
|
if (disguise.isPlayerDisguise()) {
|
2020-01-20 01:35:55 +01:00
|
|
|
stringBuilder.append(" ").append(DisguiseUtilities.quote(((PlayerDisguise) disguise).getName()));
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Method m : ParamInfoManager.getDisguiseWatcherMethods(disguise.getType().getWatcherClass())) {
|
|
|
|
// Special handling for this method
|
|
|
|
if (m.getName().equals("addPotionEffect")) {
|
|
|
|
PotionEffectType[] types = (PotionEffectType[]) m.getDeclaringClass().getMethod("getPotionEffects")
|
|
|
|
.invoke(disguise.getWatcher());
|
|
|
|
|
|
|
|
for (PotionEffectType type : types) {
|
|
|
|
if (type == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
stringBuilder.append(" ").append(m.getName()).append(" ").append(type.getName());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Entry<Method, Object> entry = defaultWatcherValues.get(m);
|
|
|
|
|
|
|
|
if (entry == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object invokeWith = m.getDeclaringClass().isInstance(disguise) ? disguise : disguise.getWatcher();
|
|
|
|
|
|
|
|
Object ourValue = entry.getKey().invoke(invokeWith);
|
|
|
|
|
|
|
|
// Escape a hacky fix for custom names, disguised players with custom names don't want to show it
|
|
|
|
// so it was set to an empty string.
|
|
|
|
if ("".equals(ourValue) && m.getName().equals("setCustomName")) {
|
|
|
|
ourValue = null;
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:10:36 +01:00
|
|
|
if (m.getName().equals("setSkin") && !outputSkinData) {
|
|
|
|
PlayerDisguise pDisg = (PlayerDisguise) disguise;
|
|
|
|
ourValue = pDisg.getName();
|
|
|
|
|
|
|
|
if (pDisg.getSkin() != null) {
|
|
|
|
ourValue = pDisg.getSkin();
|
|
|
|
} else if (pDisg.getGameProfile() != null && pDisg.getGameProfile().getName() != null) {
|
|
|
|
ourValue = pDisg.getGameProfile().getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ourValue.equals(pDisg.getName())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If its the same as default, continue
|
|
|
|
if (!m.isAnnotationPresent(RandomDefaultValue.class) &&
|
|
|
|
Objects.deepEquals(entry.getValue(), ourValue)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stringBuilder.append(" ").append(m.getName());
|
|
|
|
|
|
|
|
if (ourValue instanceof Boolean && (Boolean) ourValue) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String valueString;
|
|
|
|
|
|
|
|
if (ourValue != null) {
|
|
|
|
valueString = ParamInfoManager.getParamInfo(ourValue.getClass()).toString(ourValue);
|
|
|
|
|
2020-01-02 05:10:36 +01:00
|
|
|
valueString = DisguiseUtilities.quote(valueString);
|
2019-12-31 07:35:21 +01:00
|
|
|
} else {
|
|
|
|
valueString = "null";
|
|
|
|
}
|
|
|
|
|
|
|
|
stringBuilder.append(" ").append(valueString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringBuilder.toString();
|
|
|
|
}
|
|
|
|
catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void addWatcherDefault(Method setMethod, Method getMethod, Object object) {
|
|
|
|
Map.Entry<Method, Object> entry = new HashMap.SimpleEntry<>(getMethod, object);
|
|
|
|
|
|
|
|
if (defaultWatcherValues.containsKey(setMethod)) {
|
|
|
|
Object dObj = defaultWatcherValues.get(setMethod);
|
|
|
|
|
|
|
|
if (!Objects.deepEquals(defaultWatcherValues.get(setMethod).getValue(), object)) {
|
|
|
|
throw new IllegalStateException(String.format("%s has conflicting values!", setMethod.getName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultWatcherValues.put(setMethod, entry);
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
private static void doCheck(CommandSender sender, DisguisePermissions permissions, DisguisePerm disguisePerm,
|
|
|
|
Collection<String> usedOptions) throws DisguiseParseException {
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
if (!permissions.isAllowedDisguise(disguisePerm, usedOptions)) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.D_PARSE_NOPERM,
|
|
|
|
usedOptions.stream().reduce((first, second) -> second).orElse(null));
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static HashMap<String, Boolean> getDisguiseOptions(CommandSender sender, String permNode,
|
|
|
|
DisguisePerm type) {
|
|
|
|
switch (type.getType()) {
|
|
|
|
case PLAYER:
|
|
|
|
case FALLING_BLOCK:
|
|
|
|
case PAINTING:
|
|
|
|
case SPLASH_POTION:
|
|
|
|
case FISHING_HOOK:
|
|
|
|
case DROPPED_ITEM:
|
|
|
|
HashMap<String, Boolean> returns = new HashMap<>();
|
|
|
|
|
2019-11-08 23:07:54 +01:00
|
|
|
String beginning = "libsdisguises.options." + permNode.toLowerCase() + ".";
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
for (PermissionAttachmentInfo permission : sender.getEffectivePermissions()) {
|
|
|
|
String lowerPerm = permission.getPermission().toLowerCase();
|
|
|
|
|
|
|
|
if (lowerPerm.startsWith(beginning)) {
|
|
|
|
String[] split = lowerPerm.substring(beginning.length()).split("\\.");
|
|
|
|
|
|
|
|
if (split.length > 1) {
|
|
|
|
if (split[0].replace("_", "").equals(type.toReadable().toLowerCase().replace(" ", ""))) {
|
|
|
|
for (int i = 1; i < split.length; i++) {
|
|
|
|
returns.put(split[i], permission.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returns;
|
|
|
|
default:
|
|
|
|
return new HashMap<>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DisguisePerm getDisguisePerm(String name) {
|
|
|
|
for (DisguisePerm perm : getDisguisePerms()) {
|
|
|
|
if (!perm.toReadable().replaceAll("[ |_]", "").equalsIgnoreCase(name.replaceAll("[ |_]", "")))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return perm;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name.equalsIgnoreCase("p"))
|
|
|
|
return getDisguisePerm(DisguiseType.PLAYER.toReadable());
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DisguisePerm[] getDisguisePerms() {
|
|
|
|
DisguisePerm[] perms = new DisguisePerm[DisguiseType.values().length +
|
|
|
|
DisguiseConfig.getCustomDisguises().size()];
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (DisguiseType disguiseType : DisguiseType.values()) {
|
|
|
|
perms[i++] = new DisguisePerm(disguiseType);
|
|
|
|
}
|
|
|
|
|
2019-03-05 05:46:47 +01:00
|
|
|
for (Entry<DisguisePerm, String> entry : DisguiseConfig.getCustomDisguises().entrySet()) {
|
|
|
|
perms[i++] = entry.getKey();
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return perms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get perms for the node. Returns a hashmap of allowed disguisetypes and their options
|
|
|
|
*/
|
2018-10-23 23:13:13 +02:00
|
|
|
public static DisguisePermissions getPermissions(CommandSender sender, String commandName) {
|
|
|
|
return new DisguisePermissions(sender, commandName);
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isDouble(String string) {
|
|
|
|
try {
|
|
|
|
Float.parseFloat(string);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isInteger(String string) {
|
|
|
|
try {
|
|
|
|
Integer.parseInt(string);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:56:17 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the string is found in the map, or it's not a whitelisted setup
|
2019-03-05 05:46:47 +01:00
|
|
|
* <p>
|
2019-01-12 04:56:17 +01:00
|
|
|
* Returns if command user can access the disguise creation permission type
|
|
|
|
*/
|
|
|
|
private static boolean hasPermissionOption(HashMap<String, Boolean> disguiseOptions, String string) {
|
2020-01-12 05:26:44 +01:00
|
|
|
string = string.toLowerCase();
|
2019-01-12 04:56:17 +01:00
|
|
|
// If no permissions were defined, return true
|
|
|
|
if (disguiseOptions.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If they were explictly defined, can just return the value
|
|
|
|
if (disguiseOptions.containsKey(string)) {
|
|
|
|
return disguiseOptions.get(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is at least one whitelisted value, then they needed the whitelist to use it
|
2019-11-08 23:07:54 +01:00
|
|
|
return !disguiseOptions.containsValue(true);
|
2019-01-12 04:56:17 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
public static String getName(CommandSender entity) {
|
2019-03-05 05:46:47 +01:00
|
|
|
if (entity == null) {
|
|
|
|
return "??";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity instanceof Player) {
|
|
|
|
return entity.getName();
|
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
if (entity instanceof Entity) {
|
|
|
|
if (((Entity) entity).getCustomName() != null && ((Entity) entity).getCustomName().length() > 0) {
|
|
|
|
return ((Entity) entity).getCustomName();
|
|
|
|
}
|
2019-03-05 05:46:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return entity.getName();
|
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
private static String getSkin(CommandSender entity) {
|
2019-03-05 05:46:47 +01:00
|
|
|
if (entity == null) {
|
|
|
|
return "??";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity instanceof Player) {
|
|
|
|
WrappedGameProfile gameProfile = ReflectionManager.getGameProfile((Player) entity);
|
|
|
|
|
|
|
|
if (gameProfile != null) {
|
|
|
|
return DisguiseUtilities.getGson().toJson(gameProfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "{}";
|
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
public static String[] parsePlaceholders(String[] args, CommandSender user, CommandSender target) {
|
|
|
|
return parsePlaceholders(args, getName(user), getSkin(user), getName(target), DisguiseParser.getSkin(target),
|
|
|
|
getEntityEquipment(user), getEntityEquipment(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static EntityEquipment getEntityEquipment(CommandSender entity) {
|
|
|
|
return entity instanceof LivingEntity ? ((LivingEntity) entity).getEquipment() : null;
|
|
|
|
}
|
|
|
|
|
2019-03-05 05:46:47 +01:00
|
|
|
public static String[] parsePlaceholders(String[] args, String userName, String userSkin, String targetName,
|
2020-01-05 21:28:30 +01:00
|
|
|
String targetSkin, EntityEquipment equip, EntityEquipment targetEquip) {
|
2019-03-05 05:46:47 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < args.length; i++) {
|
|
|
|
String arg = args[i];
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
arg = replace(arg, "%user-name%", userName);
|
|
|
|
arg = replace(arg, "%user-skin%", userSkin);
|
|
|
|
arg = replace(arg, "%target-name%", targetName);
|
|
|
|
arg = replace(arg, "%target-skin%", targetSkin);
|
|
|
|
arg = replace(arg, "%held-item%", equip == null ? null : equip.getItemInMainHand());
|
|
|
|
arg = replace(arg, "%offhand-item%", equip == null ? null : equip.getItemInOffHand());
|
|
|
|
arg = replace(arg, "%armor%", equip == null ? null : equip.getArmorContents());
|
|
|
|
arg = replace(arg, "%helmet%", equip == null ? null : equip.getHelmet());
|
|
|
|
arg = replace(arg, "%chestplate%", equip == null ? null : equip.getChestplate());
|
|
|
|
arg = replace(arg, "%leggings%%", equip == null ? null : equip.getLeggings());
|
|
|
|
arg = replace(arg, "%boots%", equip == null ? null : equip.getBoots());
|
|
|
|
|
|
|
|
arg = replace(arg, "%target-held-item%", targetEquip == null ? null : targetEquip.getItemInMainHand());
|
|
|
|
arg = replace(arg, "%target-offhand-item%", targetEquip == null ? null : targetEquip.getItemInOffHand());
|
|
|
|
arg = replace(arg, "%target-armor%", targetEquip == null ? null : targetEquip.getArmorContents());
|
|
|
|
arg = replace(arg, "%target-helmet%", targetEquip == null ? null : targetEquip.getHelmet());
|
|
|
|
arg = replace(arg, "%target-chestplate%", targetEquip == null ? null : targetEquip.getChestplate());
|
|
|
|
arg = replace(arg, "%target-leggings%%", targetEquip == null ? null : targetEquip.getLeggings());
|
|
|
|
arg = replace(arg, "%target-boots%", targetEquip == null ? null : targetEquip.getBoots());
|
2019-03-05 05:46:47 +01:00
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
args[i] = arg;
|
|
|
|
}
|
2019-03-05 05:46:47 +01:00
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
return args;
|
|
|
|
}
|
2019-03-05 05:46:47 +01:00
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
private static String replace(String string, String value, Object toReplace) {
|
|
|
|
if (!string.contains(value)) {
|
|
|
|
return string;
|
|
|
|
}
|
2019-03-05 05:46:47 +01:00
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
String oValue;
|
|
|
|
|
|
|
|
if (toReplace != null) {
|
|
|
|
oValue = ParamInfoManager.toString(toReplace);
|
|
|
|
} else {
|
|
|
|
oValue = "null";
|
2019-03-05 05:46:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
return string.replace(value, oValue);
|
2019-03-05 05:46:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-06 03:16:14 +01:00
|
|
|
public static long parseStringToTime(String string) throws DisguiseParseException {
|
|
|
|
string = string.toLowerCase();
|
|
|
|
|
|
|
|
if (!string.matches("([0-9]+[a-z]+)+")) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_INVALID_TIME_SEQUENCE, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
String[] split = string.split("((?<=[a-zA-Z])(?=[0-9]))|((?<=[0-9])(?=[a-zA-Z]))");
|
|
|
|
|
|
|
|
long time = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < split.length; i += 2) {
|
|
|
|
String t = split[i + 1];
|
|
|
|
long v = Long.parseLong(split[i]);
|
|
|
|
|
|
|
|
if (t.equals("s") || t.equals("sec") || t.equals("secs") || t.equals("seconds")) {
|
|
|
|
time += v;
|
|
|
|
} else if (t.equals("m") || t.equals("min") || t.equals("minute") || t.equals("minutes")) {
|
|
|
|
time += TimeUnit.MINUTES.toSeconds(v);
|
|
|
|
} else if (t.equals("h") || t.equals("hour") || t.equals("hours")) {
|
|
|
|
time += TimeUnit.HOURS.toSeconds(v);
|
|
|
|
} else if (t.equals("d") || t.equals("day") || t.equals("days")) {
|
|
|
|
time += TimeUnit.DAYS.toSeconds(v);
|
|
|
|
} else if (t.equals("w") || t.equals("week") || t.equals("weeks")) {
|
|
|
|
time += TimeUnit.DAYS.toSeconds(v) * 7;
|
|
|
|
} else if (t.equals("mon") || t.equals("month") || t.equals("months")) {
|
|
|
|
time += TimeUnit.DAYS.toSeconds(v) * 31;
|
|
|
|
} else if (t.equals("y") || t.equals("year") || t.equals("years")) {
|
|
|
|
time += TimeUnit.DAYS.toSeconds(v) * 365;
|
|
|
|
} else {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_INVALID_TIME, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2019-03-05 05:46:47 +01:00
|
|
|
/**
|
|
|
|
* Experimentally parses the arguments to test if this is a valid disguise
|
|
|
|
*
|
|
|
|
* @param sender
|
|
|
|
* @param permNode
|
|
|
|
* @param args
|
|
|
|
* @param permissions
|
|
|
|
* @return
|
|
|
|
* @throws DisguiseParseException
|
|
|
|
* @throws IllegalAccessException
|
|
|
|
* @throws InvocationTargetException
|
|
|
|
*/
|
|
|
|
public static Disguise parseTestDisguise(CommandSender sender, String permNode, String[] args,
|
|
|
|
DisguisePermissions permissions) throws DisguiseParseException, IllegalAccessException,
|
|
|
|
InvocationTargetException {
|
|
|
|
|
|
|
|
// Clone array so original array isn't modified
|
|
|
|
args = Arrays.copyOf(args, args.length);
|
|
|
|
|
|
|
|
String skin = "{\"id\":\"a149f81bf7844f8987c554afdd4db533\",\"name\":\"libraryaddict\"," + "\"properties\":[]}";
|
|
|
|
// Fill in fake data
|
2020-01-05 21:28:30 +01:00
|
|
|
args = parsePlaceholders(args, "libraryaddict", skin, "libraryaddict", skin, null, null);
|
2019-03-05 05:46:47 +01:00
|
|
|
|
|
|
|
// Parse disguise
|
|
|
|
return parseDisguise(sender, null, permNode, args, permissions);
|
|
|
|
}
|
|
|
|
|
2019-04-16 22:10:15 +02:00
|
|
|
public static Disguise parseDisguise(
|
|
|
|
String disguise) throws IllegalAccessException, InvocationTargetException, DisguiseParseException {
|
|
|
|
return parseDisguise(Bukkit.getConsoleSender(), null, disguise);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Disguise parseDisguise(CommandSender sender, Entity target,
|
|
|
|
String disguise) throws IllegalAccessException, InvocationTargetException, DisguiseParseException {
|
|
|
|
return parseDisguise(sender, target, "disguise", DisguiseUtilities.split(disguise),
|
|
|
|
new DisguisePermissions(Bukkit.getConsoleSender(), "disguise"));
|
|
|
|
}
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-03-05 05:46:47 +01:00
|
|
|
public static Disguise parseDisguise(CommandSender sender, Entity target, String permNode, String[] args,
|
2018-10-23 23:13:13 +02:00
|
|
|
DisguisePermissions permissions) throws DisguiseParseException, IllegalAccessException,
|
|
|
|
InvocationTargetException {
|
2018-09-07 04:35:38 +02:00
|
|
|
if (sender instanceof Player) {
|
|
|
|
DisguiseUtilities.setCommandsUsed();
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
if (!permissions.hasPermissions()) {
|
2018-09-07 04:35:38 +02:00
|
|
|
throw new DisguiseParseException(LibsMsg.NO_PERM);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.length == 0) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_ARGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2018-10-23 23:13:13 +02:00
|
|
|
DisguisePerm disguisePerm;
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
if (args[0].startsWith("@")) {
|
|
|
|
if (sender.hasPermission("libsdisguises.disguise.disguiseclone")) {
|
|
|
|
disguise = DisguiseUtilities.getClonedDisguise(args[0].toLowerCase());
|
|
|
|
|
|
|
|
if (disguise == null) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_REF, args[0]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_REF);
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
disguisePerm = new DisguisePerm(disguise.getType());
|
|
|
|
|
|
|
|
if (disguisePerm.isUnknown()) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_CANT_DISG_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disguisePerm.getEntityType() == null) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_CANT_LOAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!permissions.isAllowedDisguise(disguisePerm)) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.NO_PERM_DISGUISE);
|
|
|
|
}
|
2018-09-07 04:35:38 +02:00
|
|
|
} else {
|
2018-10-23 23:13:13 +02:00
|
|
|
disguisePerm = getDisguisePerm(args[0]);
|
2019-05-16 08:07:53 +02:00
|
|
|
Entry<DisguisePerm, String> customDisguise = DisguiseConfig.getRawCustomDisguise(args[0]);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
if (customDisguise != null) {
|
2019-03-05 05:46:47 +01:00
|
|
|
args = DisguiseUtilities.split(customDisguise.getValue());
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
args = parsePlaceholders(args, sender, target);
|
2019-03-05 05:46:47 +01:00
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
if (disguisePerm == null) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_DISG_NO_EXIST, args[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disguisePerm.isUnknown()) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_CANT_DISG_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disguisePerm.getEntityType() == null) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_CANT_LOAD);
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
if (!permissions.isAllowedDisguise(disguisePerm)) {
|
2018-09-07 04:35:38 +02:00
|
|
|
throw new DisguiseParseException(LibsMsg.NO_PERM_DISGUISE);
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMap<String, Boolean> disguiseOptions = getDisguiseOptions(sender, permNode, disguisePerm);
|
|
|
|
|
|
|
|
if (disguise == null) {
|
|
|
|
if (disguisePerm.isPlayer()) {
|
|
|
|
// If he is doing a player disguise
|
|
|
|
if (args.length == 1) {
|
|
|
|
// He needs to give the player name
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_SUPPLY_PLAYER);
|
|
|
|
} else {
|
2019-01-12 04:56:17 +01:00
|
|
|
// If they can't use this name, throw error
|
|
|
|
if (!hasPermissionOption(disguiseOptions, args[1].toLowerCase())) {
|
2018-09-07 04:35:38 +02:00
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
args[1] = args[1].replace("\\_", " ");
|
|
|
|
|
|
|
|
// Construct the player disguise
|
|
|
|
disguise = new PlayerDisguise(ChatColor.translateAlternateColorCodes('&', args[1]));
|
|
|
|
toSkip++;
|
|
|
|
}
|
|
|
|
} else if (disguisePerm.isMob()) { // Its a mob, use the mob constructor
|
|
|
|
boolean adult = true;
|
|
|
|
|
|
|
|
if (args.length > 1) {
|
|
|
|
if (args[1].equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS.get("baby")) ||
|
|
|
|
args[1].equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS.get("adult"))) {
|
|
|
|
usedOptions.add("setbaby");
|
2018-10-23 23:13:13 +02:00
|
|
|
doCheck(sender, permissions, disguisePerm, usedOptions);
|
2018-09-07 04:35:38 +02:00
|
|
|
adult = args[1].equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS.get("adult"));
|
|
|
|
|
|
|
|
toSkip++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disguise = new MobDisguise(disguisePerm.getType(), adult);
|
|
|
|
} else if (disguisePerm.isMisc()) {
|
|
|
|
// Its a misc, we are going to use the MiscDisguise constructor.
|
|
|
|
ItemStack itemStack = new ItemStack(Material.STONE);
|
|
|
|
int miscId = -1;
|
|
|
|
|
|
|
|
if (args.length > 1) {
|
2018-12-12 23:57:54 +01:00
|
|
|
switch (disguisePerm.getType()) {
|
|
|
|
case FALLING_BLOCK:
|
|
|
|
case DROPPED_ITEM:
|
|
|
|
Material material = null;
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
for (Material mat : Material.values()) {
|
2018-12-12 23:57:54 +01:00
|
|
|
if (!mat.name().replace("_", "").equalsIgnoreCase(args[1].replace("_", ""))) {
|
|
|
|
continue;
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
2018-12-12 23:57:54 +01:00
|
|
|
|
|
|
|
material = mat;
|
|
|
|
break;
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
2018-12-12 23:57:54 +01:00
|
|
|
|
|
|
|
if (material == null) {
|
2018-09-07 04:35:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-12 23:57:54 +01:00
|
|
|
itemStack = new ItemStack(material);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2019-01-12 04:56:17 +01:00
|
|
|
if (!hasPermissionOption(disguiseOptions, itemStack.getType().name().toLowerCase())) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_PARAM,
|
|
|
|
itemStack.getType().name(), disguisePerm.toReadable());
|
2018-12-12 23:57:54 +01:00
|
|
|
}
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2018-12-12 23:57:54 +01:00
|
|
|
toSkip++;
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2018-12-12 23:57:54 +01:00
|
|
|
if (disguisePerm.getType() == DisguiseType.FALLING_BLOCK) {
|
|
|
|
usedOptions.add("setblock");
|
|
|
|
} else {
|
|
|
|
usedOptions.add("setitemstack");
|
|
|
|
}
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2018-12-12 23:57:54 +01:00
|
|
|
doCheck(sender, permissions, disguisePerm, usedOptions);
|
|
|
|
break;
|
|
|
|
case PAINTING:
|
|
|
|
case SPLASH_POTION:
|
|
|
|
if (!isInteger(args[1])) {
|
|
|
|
break;
|
|
|
|
}
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2018-12-12 23:57:54 +01:00
|
|
|
miscId = Integer.parseInt(args[1]);
|
|
|
|
toSkip++;
|
|
|
|
|
2019-01-12 04:56:17 +01:00
|
|
|
if (!hasPermissionOption(disguiseOptions, miscId + "")) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_PARAM, miscId + "",
|
|
|
|
disguisePerm.toReadable());
|
2018-12-12 23:57:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (disguisePerm.getType() == DisguiseType.PAINTING) {
|
|
|
|
usedOptions.add("setpainting");
|
|
|
|
} else {
|
|
|
|
usedOptions.add("setpotionid");
|
|
|
|
}
|
|
|
|
|
|
|
|
doCheck(sender, permissions, disguisePerm, usedOptions);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the disguise
|
2018-09-23 01:02:53 +02:00
|
|
|
if (disguisePerm.getType() == DisguiseType.DROPPED_ITEM ||
|
|
|
|
disguisePerm.getType() == DisguiseType.FALLING_BLOCK) {
|
|
|
|
disguise = new MiscDisguise(disguisePerm.getType(), itemStack);
|
2018-09-07 04:35:38 +02:00
|
|
|
} else {
|
2018-12-12 23:57:54 +01:00
|
|
|
disguise = new MiscDisguise(disguisePerm.getType(), miscId);
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy strings to their new range
|
|
|
|
String[] newArgs = new String[args.length - toSkip];
|
|
|
|
System.arraycopy(args, toSkip, newArgs, 0, args.length - toSkip);
|
|
|
|
|
2020-01-12 05:26:44 +01:00
|
|
|
callMethods(sender, disguise, permissions, disguisePerm, usedOptions, newArgs, permNode);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
// Alright. We've constructed our disguise.
|
|
|
|
return disguise;
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
public static void callMethods(CommandSender sender, Disguise disguise, DisguisePermissions disguisePermission,
|
2020-01-12 05:26:44 +01:00
|
|
|
DisguisePerm disguisePerm, Collection<String> usedOptions, String[] args,
|
|
|
|
String permNode) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
|
2018-09-07 04:35:38 +02:00
|
|
|
DisguiseParseException {
|
|
|
|
Method[] methods = ParamInfoManager.getDisguiseWatcherMethods(disguise.getWatcher().getClass());
|
|
|
|
List<String> list = new ArrayList<>(Arrays.asList(args));
|
2020-01-12 05:26:44 +01:00
|
|
|
HashMap<String, Boolean> disguiseOptions = null;
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
for (int argIndex = 0; argIndex < args.length; argIndex++) {
|
|
|
|
// This is the method name they provided
|
|
|
|
String methodNameProvided = list.remove(0);
|
|
|
|
// Translate the name they provided, to a name we recognize
|
|
|
|
String methodNameJava = TranslateType.DISGUISE_OPTIONS.reverseGet(methodNameProvided);
|
|
|
|
// The method we'll use
|
|
|
|
Method methodToUse = null;
|
|
|
|
Object valueToSet = null;
|
|
|
|
DisguiseParseException parseException = null;
|
|
|
|
|
|
|
|
for (Method method : methods) {
|
|
|
|
if (!method.getName().equalsIgnoreCase(methodNameJava)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Class paramType = method.getParameterTypes()[0];
|
|
|
|
|
|
|
|
ParamInfo paramInfo = ParamInfoManager.getParamInfo(paramType);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Store how many args there were before calling the param
|
|
|
|
int argCount = list.size();
|
|
|
|
|
|
|
|
if (argCount < paramInfo.getMinArguments()) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_OPTION_VALUE,
|
|
|
|
TranslateType.DISGUISE_OPTIONS.reverseGet(method.getName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
valueToSet = paramInfo.fromString(list);
|
|
|
|
|
|
|
|
if (valueToSet == null && !paramInfo.canReturnNull()) {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip ahead as many args as were consumed on successful parse
|
|
|
|
argIndex += argCount - list.size();
|
|
|
|
|
|
|
|
methodToUse = method;
|
|
|
|
// We've found a method which will accept a valid value, break
|
|
|
|
break;
|
|
|
|
}
|
2018-09-23 01:02:53 +02:00
|
|
|
catch (DisguiseParseException ex) {
|
|
|
|
parseException = ex;
|
|
|
|
}
|
2018-09-07 04:35:38 +02:00
|
|
|
catch (Exception ignored) {
|
|
|
|
parseException = new DisguiseParseException(LibsMsg.PARSE_EXPECTED_RECEIVED,
|
|
|
|
paramInfo.getDescriptiveName(), list.isEmpty() ? null : list.get(0),
|
|
|
|
TranslateType.DISGUISE_OPTIONS.reverseGet(method.getName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (methodToUse == null) {
|
|
|
|
if (parseException != null) {
|
|
|
|
throw parseException;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_OPTION_NA, methodNameProvided);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!usedOptions.contains(methodToUse.getName().toLowerCase())) {
|
|
|
|
usedOptions.add(methodToUse.getName().toLowerCase());
|
|
|
|
}
|
|
|
|
|
2020-01-12 05:26:44 +01:00
|
|
|
if (methodToUse.getName().equalsIgnoreCase("setpainting") ||
|
|
|
|
methodToUse.getName().equalsIgnoreCase("setpotionid") ||
|
|
|
|
methodToUse.getName().equalsIgnoreCase("setitemstack") ||
|
|
|
|
methodToUse.getName().equalsIgnoreCase("setblock")) {
|
|
|
|
if (disguiseOptions == null) {
|
|
|
|
disguiseOptions = getDisguiseOptions(sender, permNode, disguisePerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
String stringValue = ParamInfoManager.toString(valueToSet);
|
|
|
|
|
2020-01-14 07:41:40 +01:00
|
|
|
if (!hasPermissionOption(disguiseOptions, stringValue)) {
|
2020-01-12 05:26:44 +01:00
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_PARAM, stringValue,
|
|
|
|
disguisePerm.toReadable());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
doCheck(sender, disguisePermission, disguisePerm, usedOptions);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
if (FlagWatcher.class.isAssignableFrom(methodToUse.getDeclaringClass())) {
|
|
|
|
methodToUse.invoke(disguise.getWatcher(), valueToSet);
|
|
|
|
} else {
|
|
|
|
methodToUse.invoke(disguise, valueToSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|