Minor change

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

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