From 94fa3f0a1dd265855ba68b770572f098162136af Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 9 Dec 2017 08:55:11 +1300 Subject: [PATCH] Allow disguises to use quoted strings to allow spaces in strings --- .../disguise/DisguiseConfig.java | 2 +- .../disguise/commands/DisguiseCommand.java | 2 +- .../commands/DisguiseEntityCommand.java | 2 +- .../commands/DisguisePlayerCommand.java | 2 +- .../commands/DisguiseRadiusCommand.java | 2 +- .../disguise/utilities/DisguiseParser.java | 76 +++++++++---------- 6 files changed, 41 insertions(+), 45 deletions(-) diff --git a/src/me/libraryaddict/disguise/DisguiseConfig.java b/src/me/libraryaddict/disguise/DisguiseConfig.java index a20b859c..e5f8e612 100644 --- a/src/me/libraryaddict/disguise/DisguiseConfig.java +++ b/src/me/libraryaddict/disguise/DisguiseConfig.java @@ -233,7 +233,7 @@ public class DisguiseConfig { try { Disguise disguise = DisguiseParser - .parseDisguise(Bukkit.getConsoleSender(), "disguise", toParse.split(" "), + .parseDisguise(Bukkit.getConsoleSender(), "disguise", DisguiseParser.split(toParse), DisguiseParser.getPermissions(Bukkit.getConsoleSender(), "disguise")); customDisguises.put(key, disguise); diff --git a/src/me/libraryaddict/disguise/commands/DisguiseCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseCommand.java index 2d9de915..b5c9f115 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseCommand.java @@ -45,7 +45,7 @@ public class DisguiseCommand extends DisguiseBaseCommand implements TabCompleter Disguise disguise; try { - disguise = DisguiseParser.parseDisguise(sender, getPermNode(), args, getPermissions(sender)); + disguise = DisguiseParser.parseDisguise(sender, getPermNode(), DisguiseParser.split(StringUtils.join(args)), getPermissions(sender)); } catch (DisguiseParseException ex) { if (ex.getMessage() != null) { diff --git a/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java index b06e0e6c..e42ce9c9 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java @@ -46,7 +46,7 @@ public class DisguiseEntityCommand extends DisguiseBaseCommand implements TabCom Disguise disguise; try { - disguise = DisguiseParser.parseDisguise(sender, getPermNode(), args, getPermissions(sender)); + disguise = DisguiseParser.parseDisguise(sender, getPermNode(), DisguiseParser.split(StringUtils.join(args)), getPermissions(sender)); } catch (DisguiseParseException ex) { if (ex.getMessage() != null) { diff --git a/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java b/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java index f05448ba..4c52ad98 100644 --- a/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java @@ -72,7 +72,7 @@ public class DisguisePlayerCommand extends DisguiseBaseCommand implements TabCom Disguise disguise; try { - disguise = DisguiseParser.parseDisguise(sender, getPermNode(), newArgs, map); + disguise = DisguiseParser.parseDisguise(sender, getPermNode(), DisguiseParser.split(StringUtils.join(newArgs)), map); } catch (DisguiseParseException ex) { if (ex.getMessage() != null) { diff --git a/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java index 44410ebd..1790d336 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java @@ -130,7 +130,7 @@ public class DisguiseRadiusCommand extends DisguiseBaseCommand implements TabCom } try { - disguise = DisguiseParser.parseDisguise(sender, getPermNode(), newArgs, map); + disguise = DisguiseParser.parseDisguise(sender, getPermNode(), DisguiseParser.split(StringUtils.join(newArgs)), map); } catch (DisguiseParseException ex) { if (ex.getMessage() != null) { diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseParser.java b/src/me/libraryaddict/disguise/utilities/DisguiseParser.java index 0e781887..83421334 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseParser.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseParser.java @@ -1,15 +1,9 @@ package me.libraryaddict.disguise.utilities; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map.Entry; - -import com.google.gson.GsonBuilder; -import com.mojang.authlib.GameProfile; -import me.libraryaddict.disguise.utilities.json.SerializerGameProfile; -import org.apache.commons.lang3.StringUtils; +import com.comphenix.protocol.wrappers.BlockPosition; +import com.comphenix.protocol.wrappers.WrappedGameProfile; +import me.libraryaddict.disguise.DisguiseConfig; +import me.libraryaddict.disguise.disguisetypes.*; import org.bukkit.Art; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -20,19 +14,11 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.potion.PotionEffectType; -import com.comphenix.protocol.wrappers.BlockPosition; -import com.comphenix.protocol.wrappers.WrappedGameProfile; - -import me.libraryaddict.disguise.DisguiseConfig; -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 sun.reflect.Reflection; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map.Entry; public class DisguiseParser { public static class DisguiseParseException extends Exception { @@ -201,8 +187,8 @@ public class DisguiseParser { } public static DisguisePerm[] getDisguisePerms() { - DisguisePerm[] perms = new DisguisePerm[DisguiseType.values().length + DisguiseConfig.getCustomDisguises() - .size()]; + DisguisePerm[] perms = new DisguisePerm[DisguiseType.values().length + + DisguiseConfig.getCustomDisguises().size()]; int i = 0; for (DisguiseType disguiseType : DisguiseType.values()) { @@ -449,13 +435,22 @@ public class DisguiseParser { } } + /** + * Splits a string while respecting quotes + */ + public static String[] split(String string) { + return string.split(" (?=\")|(?<=\")\\s"); + } + /** * 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 + * commandsender is purely used for checking permissions. Would defeat the purpose otherwise. To reach this + * point, the * disguise has been feed a proper disguisetype. */ public static Disguise parseDisguise(CommandSender sender, String permNode, String[] args, - HashMap, Boolean>> permissionMap) throws DisguiseParseException, IllegalAccessException, InvocationTargetException { + HashMap, Boolean>> permissionMap) throws DisguiseParseException, + IllegalAccessException, InvocationTargetException { if (sender instanceof Player) { DisguiseUtilities.setCommandsUsed(); } @@ -525,8 +520,8 @@ public class DisguiseParser { // He needs to give the player name throw new DisguiseParseException(LibsMsg.PARSE_SUPPLY_PLAYER); } else { - if (!disguiseOptions.isEmpty() && (!disguiseOptions - .containsKey(args[1].toLowerCase()) || !disguiseOptions.get(args[1].toLowerCase()))) { + if (!disguiseOptions.isEmpty() && (!disguiseOptions.containsKey(args[1].toLowerCase()) || + !disguiseOptions.get(args[1].toLowerCase()))) { throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_NAME); } @@ -540,8 +535,8 @@ public class DisguiseParser { boolean adult = true; if (args.length > 1) { - if (args[1].equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS.get("baby")) || args[1] - .equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS.get("adult"))) { + if (args[1].equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS.get("baby")) || + args[1].equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS.get("adult"))) { usedOptions.add("setbaby"); doCheck(sender, optionPermissions, usedOptions); adult = args[1].equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS.get("adult")); @@ -571,8 +566,8 @@ public class DisguiseParser { if (isInteger(args[1])) { miscId = Integer.parseInt(args[1]); } else { - if (disguisePerm.getType() == DisguiseType.FALLING_BLOCK || disguisePerm - .getType() == DisguiseType.DROPPED_ITEM) { + if (disguisePerm.getType() == DisguiseType.FALLING_BLOCK || + disguisePerm.getType() == DisguiseType.DROPPED_ITEM) { for (Material mat : Material.values()) { if (mat.name().replace("_", "").equalsIgnoreCase(args[1].replace("_", ""))) { miscId = mat.getId(); @@ -606,8 +601,8 @@ public class DisguiseParser { toSkip++; } if (secondArg != null) { - if (disguisePerm.getType() != DisguiseType.FALLING_BLOCK && disguisePerm - .getType() != DisguiseType.DROPPED_ITEM) { + if (disguisePerm.getType() != DisguiseType.FALLING_BLOCK && + disguisePerm.getType() != DisguiseType.DROPPED_ITEM) { throw new DisguiseParseException(LibsMsg.PARSE_USE_SECOND_NUM, DisguiseType.FALLING_BLOCK.toReadable(), DisguiseType.DROPPED_ITEM.toReadable()); @@ -667,7 +662,8 @@ public class DisguiseParser { public static void callMethods(CommandSender sender, Disguise disguise, HashMap, Boolean> optionPermissions, ArrayList usedOptions, - String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, DisguiseParseException { + String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, + DisguiseParseException { Method[] methods = ReflectionFlagWatchers.getDisguiseWatcherMethods(disguise.getWatcher().getClass()); for (int i = 0; i < args.length; i += 2) { @@ -977,13 +973,13 @@ public class DisguiseParser { boolean myPerms = true; for (String option : usedOptions) { - if (!sender.getName().equals("CONSOLE") && option.equalsIgnoreCase("setInvisible") && DisguiseConfig - .isDisabledInvisibility()) { + if (!sender.getName().equals("CONSOLE") && option.equalsIgnoreCase("setInvisible") && + DisguiseConfig.isDisabledInvisibility()) { myPerms = false; } - if (!(theirPermissions.get(list) && list.contains("*")) && (list.contains(option) != theirPermissions - .get(list))) { + if (!(theirPermissions.get(list) && list.contains("*")) && + (list.contains(option) != theirPermissions.get(list))) { myPerms = false; break; }