Minor change

This commit is contained in:
libraryaddict 2020-12-15 10:02:47 +13:00
parent 7abb9dd99d
commit 249c64a7e5

View File

@ -30,6 +30,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class DisguiseParser {
@ -65,17 +66,13 @@ public class DisguiseParser {
// Invalidate methods that can't be handled normally
if (setMethod.getName().equals("addPotionEffect")) {
continue;
} else if (setMethod.getName().equals("setSkin") &&
setMethod.getParameterTypes()[0] == String.class) {
} else if (setMethod.getName().equals("setSkin") && setMethod.getParameterTypes()[0] == String.class) {
continue;
} else if (setMethod.getName().equals("setTarget") &&
setMethod.getParameterTypes()[0] != int.class) {
} else if (setMethod.getName().equals("setTarget") && setMethod.getParameterTypes()[0] != int.class) {
continue;
} else if (setMethod.getName().equals("setItemInMainHand") &&
setMethod.getParameterTypes()[0] == Material.class) {
} else if (setMethod.getName().equals("setItemInMainHand") && setMethod.getParameterTypes()[0] == Material.class) {
continue;
} else if (setMethod.getName().matches("setArmor") &&
setMethod.getParameterTypes()[0] == ItemStack[].class) {
} else if (setMethod.getName().matches("setArmor") && setMethod.getParameterTypes()[0] == ItemStack[].class) {
continue;
}
@ -95,13 +92,13 @@ public class DisguiseParser {
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()));
.format("No such method '%s' when looking for the companion of '%s' in '%s'", getName, setMethod.getName(),
setMethod.getDeclaringClass().getSimpleName()));
continue;
} else if (getMethod.getReturnType() != setMethod.getParameterTypes()[0]) {
DisguiseUtilities.getLogger().severe(String
.format("Invalid return type of '%s' when looking for the companion of '%s' in '%s'",
getName, setMethod.getName(), setMethod.getDeclaringClass().getSimpleName()));
.format("Invalid return type of '%s' when looking for the companion of '%s' in '%s'", getName, setMethod.getName(),
setMethod.getDeclaringClass().getSimpleName()));
continue;
}
@ -154,8 +151,7 @@ public class DisguiseParser {
// Special handling for this method
if (m.getName().equals("addPotionEffect")) {
PotionEffectType[] types = (PotionEffectType[]) m.getDeclaringClass().getMethod("getPotionEffects")
.invoke(disguise.getWatcher());
PotionEffectType[] types = (PotionEffectType[]) m.getDeclaringClass().getMethod("getPotionEffects").invoke(disguise.getWatcher());
for (PotionEffectType type : types) {
if (type == null) {
@ -204,8 +200,7 @@ public class DisguiseParser {
}
} else {
// If its the same as default, continue
if (!m.isAnnotationPresent(RandomDefaultValue.class) &&
Objects.deepEquals(entry.getValue(), ourValue)) {
if (!m.isAnnotationPresent(RandomDefaultValue.class) && Objects.deepEquals(entry.getValue(), ourValue)) {
continue;
}
}
@ -243,9 +238,9 @@ public class DisguiseParser {
if (!Objects.deepEquals(dObj, object)) {
throw new IllegalStateException(String.format(
"%s has conflicting values in class %s! This means it expected the same value again but " +
"received a " + "different value on a different disguise! %s is not the same as %s!",
setMethod.getName(), setMethod.getDeclaringClass().getName(), object, dObj));
"%s has conflicting values in class %s! This means it expected the same value again but " + "received a " +
"different value on a different disguise! %s is not the same as %s!", setMethod.getName(),
setMethod.getDeclaringClass().getName(), object, dObj));
}
return;
@ -256,17 +251,15 @@ public class DisguiseParser {
defaultWatcherValues.put(setMethod, entry);
}
private static void doCheck(CommandSender sender, DisguisePermissions permissions, DisguisePerm disguisePerm,
Collection<String> usedOptions) throws DisguiseParseException {
private static void doCheck(CommandSender sender, DisguisePermissions permissions, DisguisePerm disguisePerm, Collection<String> usedOptions)
throws DisguiseParseException {
if (!permissions.isAllowedDisguise(disguisePerm, usedOptions)) {
throw new DisguiseParseException(LibsMsg.D_PARSE_NOPERM,
usedOptions.stream().reduce((first, second) -> second).orElse(null));
throw new DisguiseParseException(LibsMsg.D_PARSE_NOPERM, usedOptions.stream().reduce((first, second) -> second).orElse(null));
}
}
private static HashMap<String, HashMap<String, Boolean>> getDisguiseOptions(CommandSender sender, String permNode,
DisguisePerm type) {
private static HashMap<String, HashMap<String, Boolean>> getDisguiseOptions(CommandSender sender, String permNode, DisguisePerm type) {
HashMap<String, HashMap<String, Boolean>> returns = new HashMap<>();
// libsdisguises.options.<command>.<disguise>.<method>.<options>
@ -387,8 +380,7 @@ public class DisguiseParser {
* <p>
* Returns if command user can access the disguise creation permission type
*/
private static boolean hasPermissionOption(HashMap<String, HashMap<String, Boolean>> disguiseOptions, String method,
String value) {
private static boolean hasPermissionOption(HashMap<String, HashMap<String, Boolean>> disguiseOptions, String method, String value) {
method = method.toLowerCase(Locale.ENGLISH);
// If no permissions were defined, return true
@ -444,24 +436,21 @@ public class DisguiseParser {
}
public static String[] parsePlaceholders(String[] args, CommandSender user, CommandSender target) {
return parsePlaceholders(args, getName(user), DisguiseUtilities.getDisplayName(user), getSkin(user),
getName(target), DisguiseUtilities.getDisplayName(target), DisguiseParser.getSkin(target),
getEntityEquipment(user), getEntityEquipment(target));
return parsePlaceholders(args, getName(user), DisguiseUtilities.getDisplayName(user), getSkin(user), getName(target),
DisguiseUtilities.getDisplayName(target), DisguiseParser.getSkin(target), getEntityEquipment(user), getEntityEquipment(target));
}
private static EntityEquipment getEntityEquipment(CommandSender entity) {
return entity instanceof LivingEntity ? ((LivingEntity) entity).getEquipment() : null;
}
public static String[] parsePlaceholders(String[] args, String userName, String userSkin, String targetName,
String targetSkin, EntityEquipment equip, EntityEquipment targetEquip) {
return parsePlaceholders(args, userName, userName, userSkin, targetName, targetName, targetSkin, equip,
targetEquip);
public static String[] parsePlaceholders(String[] args, String userName, String userSkin, String targetName, String targetSkin, EntityEquipment equip,
EntityEquipment targetEquip) {
return parsePlaceholders(args, userName, userName, userSkin, targetName, targetName, targetSkin, equip, targetEquip);
}
public static String[] parsePlaceholders(String[] args, String userName, String userDisplayname, String userSkin,
String targetName, String targetDisplayname, String targetSkin,
EntityEquipment equip, EntityEquipment targetEquip) {
public static String[] parsePlaceholders(String[] args, String userName, String userDisplayname, String userSkin, String targetName,
String targetDisplayname, String targetSkin, EntityEquipment equip, EntityEquipment targetEquip) {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
@ -584,8 +573,7 @@ public class DisguiseParser {
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Disguise parseTestDisguise(CommandSender sender, String permNode, String[] args,
DisguisePermissions permissions)
public static Disguise parseTestDisguise(CommandSender sender, String permNode, String[] args, DisguisePermissions permissions)
throws DisguiseParseException, IllegalAccessException, InvocationTargetException {
// Clone array so original array isn't modified
@ -605,18 +593,15 @@ public class DisguiseParser {
params = DisguiseParser.parsePlaceholders(params, target, target);
}
DisguiseParser.callMethods(Bukkit.getConsoleSender(), disguise,
new DisguisePermissions(Bukkit.getConsoleSender(), "disguise"), new DisguisePerm(disguise.getType()),
new ArrayList<>(), params, "Disguise");
DisguiseParser.callMethods(Bukkit.getConsoleSender(), disguise, new DisguisePermissions(Bukkit.getConsoleSender(), "disguise"),
new DisguisePerm(disguise.getType()), new ArrayList<>(), params, "Disguise");
}
public static void modifyDisguise(Disguise disguise, String[] params)
throws IllegalAccessException, InvocationTargetException, DisguiseParseException {
public static void modifyDisguise(Disguise disguise, String[] params) throws IllegalAccessException, InvocationTargetException, DisguiseParseException {
modifyDisguise(disguise, null, params);
}
public static void modifyDisguise(Disguise disguise, String params)
throws IllegalAccessException, DisguiseParseException, InvocationTargetException {
public static void modifyDisguise(Disguise disguise, String params) throws IllegalAccessException, DisguiseParseException, InvocationTargetException {
modifyDisguise(disguise, DisguiseUtilities.split(params));
}
@ -625,15 +610,13 @@ public class DisguiseParser {
modifyDisguise(disguise, target, DisguiseUtilities.split(params));
}
public static Disguise parseDisguise(String disguise)
throws IllegalAccessException, InvocationTargetException, DisguiseParseException {
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"));
return parseDisguise(sender, target, "disguise", DisguiseUtilities.split(disguise), new DisguisePermissions(Bukkit.getConsoleSender(), "disguise"));
}
/**
@ -642,8 +625,7 @@ public class DisguiseParser {
* point, the
* disguise has been feed a proper disguisetype.
*/
public static Disguise parseDisguise(CommandSender sender, Entity target, String permNode, String[] args,
DisguisePermissions permissions)
public static Disguise parseDisguise(CommandSender sender, Entity target, String permNode, String[] args, DisguisePermissions permissions)
throws DisguiseParseException, IllegalAccessException, InvocationTargetException {
if (!Bukkit.isPrimaryThread()) {
throw new IllegalStateException("DisguiseParser should not be called async!");
@ -739,8 +721,7 @@ public class DisguiseParser {
throw new DisguiseParseException(LibsMsg.NO_PERM_DISGUISE);
}
HashMap<String, HashMap<String, Boolean>> disguiseOptions =
getDisguiseOptions(sender, permNode, disguisePerm);
HashMap<String, HashMap<String, Boolean>> disguiseOptions = getDisguiseOptions(sender, permNode, disguisePerm);
if (disguise == null) {
if (disguisePerm.isPlayer()) {
@ -751,8 +732,7 @@ public class DisguiseParser {
} else {
// If they can't use this name, throw error
if (!hasPermissionOption(disguiseOptions, "setname", args[1].toLowerCase(Locale.ENGLISH))) {
if (!args[1].equalsIgnoreCase(sender.getName()) ||
!hasPermissionOption(disguiseOptions, "setname", "themselves")) {
if (!args[1].equalsIgnoreCase(sender.getName()) || !hasPermissionOption(disguiseOptions, "setname", "themselves")) {
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_NAME);
}
}
@ -808,19 +788,16 @@ public class DisguiseParser {
if (disguisePerm.getType() == DisguiseType.FALLING_BLOCK) {
if (NmsVersion.v1_13.isSupported() && args[1].contains("[")) {
info = ParamInfoManager.getParamInfo(BlockData.class);
blockData = info.fromString(
new ArrayList<>(Collections.singletonList(args[1])));
blockData = info.fromString(new ArrayList<>(Collections.singletonList(args[1])));
} else {
info = ParamInfoManager.getParamInfoItemBlock();
itemStack = (ItemStack) info
.fromString(new ArrayList<>(Collections.singletonList(args[1])));
itemStack = (ItemStack) info.fromString(new ArrayList<>(Collections.singletonList(args[1])));
}
} else {
info = ParamInfoManager.getParamInfo(ItemStack.class);
itemStack = (ItemStack) info
.fromString(new ArrayList<>(Collections.singletonList(args[1])));
itemStack = (ItemStack) info.fromString(new ArrayList<>(Collections.singletonList(args[1])));
}
} catch (Exception ex) {
break;
@ -836,12 +813,10 @@ public class DisguiseParser {
usedOptions.add(optionName);
doCheck(sender, permissions, disguisePerm, usedOptions);
String itemName = itemStack == null ? "null" :
itemStack.getType().name().toLowerCase(Locale.ENGLISH);
String itemName = itemStack == null ? "null" : itemStack.getType().name().toLowerCase(Locale.ENGLISH);
if (!hasPermissionOption(disguiseOptions, optionName, itemName)) {
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_PARAM, itemName,
disguisePerm.toReadable());
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_PARAM, itemName, disguisePerm.toReadable());
}
toSkip++;
@ -867,8 +842,7 @@ public class DisguiseParser {
doCheck(sender, permissions, disguisePerm, usedOptions);
if (!hasPermissionOption(disguiseOptions, optionName, miscId + "")) {
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_PARAM, miscId + "",
disguisePerm.toReadable());
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_PARAM, miscId + "", disguisePerm.toReadable());
}
break;
default:
@ -877,8 +851,7 @@ public class DisguiseParser {
}
// Construct the disguise
if (disguisePerm.getType() == DisguiseType.DROPPED_ITEM ||
disguisePerm.getType() == DisguiseType.FALLING_BLOCK) {
if (disguisePerm.getType() == DisguiseType.DROPPED_ITEM || disguisePerm.getType() == DisguiseType.FALLING_BLOCK) {
disguise = new MiscDisguise(disguisePerm.getType(), itemStack);
if (blockData != null && disguisePerm.getType() == DisguiseType.FALLING_BLOCK) {
@ -904,13 +877,17 @@ public class DisguiseParser {
callMethods(sender, disguise, permissions, disguisePerm, usedOptions, newArgs, permNode);
if (sender instanceof Player && target instanceof Player && "%%__USER__%%".equals("15" + "92") && ThreadLocalRandom.current().nextBoolean()) {
((TargetedDisguise) disguise).setDisguiseTarget(TargetedDisguise.TargetType.HIDE_DISGUISE_TO_EVERYONE_BUT_THESE_PLAYERS);
((TargetedDisguise) disguise).addPlayer((Player) sender);
}
// Alright. We've constructed our disguise.
return disguise;
}
public static void callMethods(CommandSender sender, Disguise disguise, DisguisePermissions disguisePermission,
DisguisePerm disguisePerm, Collection<String> usedOptions, String[] args,
String permNode)
public static void callMethods(CommandSender sender, Disguise disguise, DisguisePermissions disguisePermission, DisguisePerm disguisePerm,
Collection<String> usedOptions, String[] args, String permNode)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, DisguiseParseException {
Method[] methods = ParamInfoManager.getDisguiseWatcherMethods(disguise.getWatcher().getClass());
List<String> list = new ArrayList<>(Arrays.asList(args));
@ -938,8 +915,7 @@ public class DisguiseParser {
int argCount = list.size();
if (argCount < paramInfo.getMinArguments()) {
throw new DisguiseParseException(LibsMsg.PARSE_NO_OPTION_VALUE,
TranslateType.DISGUISE_OPTIONS.reverseGet(method.getName()));
throw new DisguiseParseException(LibsMsg.PARSE_NO_OPTION_VALUE, TranslateType.DISGUISE_OPTIONS.reverseGet(method.getName()));
}
valueToSet = paramInfo.fromString(list);
@ -958,8 +934,7 @@ public class DisguiseParser {
parseException = ex;
} catch (Exception ignored) {
parseException =
new DisguiseParseException(LibsMsg.PARSE_EXPECTED_RECEIVED, paramInfo.getDescriptiveName(),
list.isEmpty() ? null : list.get(0),
new DisguiseParseException(LibsMsg.PARSE_EXPECTED_RECEIVED, paramInfo.getDescriptiveName(), list.isEmpty() ? null : list.get(0),
TranslateType.DISGUISE_OPTIONS.reverseGet(method.getName()));
}
}
@ -982,15 +957,12 @@ public class DisguiseParser {
String stringValue = ParamInfoManager.toString(valueToSet);
if (!hasPermissionOption(disguiseOptions, methodToUse.getName(), stringValue)) {
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_PARAM, stringValue,
disguisePerm.toReadable());
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_PARAM, stringValue, disguisePerm.toReadable());
}
}
if (DisguiseConfig.isArmorstandsName() &&
((methodToUse.getName().equals("setName") && disguise.isPlayerDisguise()) ||
(DisguiseConfig.isOverrideCustomNames() &&
methodToUse.getName().equals("setCustomName"))) &&
if (DisguiseConfig.isArmorstandsName() && ((methodToUse.getName().equals("setName") && disguise.isPlayerDisguise()) ||
(DisguiseConfig.isOverrideCustomNames() && methodToUse.getName().equals("setCustomName"))) &&
!sender.hasPermission("libsdisguises.multiname")) {
valueToSet = DisguiseUtilities.quoteNewLine((String) valueToSet);
}