From 0ed798e8e70867d5ea88dfd56437db88760ed17d Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Mon, 6 Jan 2020 09:28:30 +1300 Subject: [PATCH] Minor code cleanup, add item placeholders to disguises --- .../disguise/DisguiseListener.java | 4 +- .../commands/DisguiseModifyCommand.java | 4 +- .../commands/DisguiseModifyPlayerCommand.java | 7 +- .../commands/DisguiseModifyRadiusCommand.java | 3 +- .../disguise/disguisetypes/LibsEquipment.java | 13 +++- .../utilities/parser/DisguiseParser.java | 76 +++++++++++++------ .../parser/params/ParamInfoManager.java | 14 ++++ .../utilities/translations/LibsMsg.java | 2 +- src/main/resources/disguises.yml | 11 +++ 9 files changed, 97 insertions(+), 37 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseListener.java b/src/main/java/me/libraryaddict/disguise/DisguiseListener.java index 2ea31766..6f4dc0c1 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseListener.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseListener.java @@ -526,9 +526,7 @@ public class DisguiseListener implements Listener { return; } - options = DisguiseParser - .parsePlaceholders(options, p.getName(), DisguiseParser.getSkin(p), DisguiseParser.getName(entity), - DisguiseParser.getSkin(entity)); + options = DisguiseParser.parsePlaceholders(options, p, entity); DisguisePermissions perms = DisguiseParser.getPermissions(p, "disguiseentitymodify"); DisguisePerm disguisePerm = new DisguisePerm(disguise.getType()); diff --git a/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyCommand.java b/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyCommand.java index c7193d6e..1200e8ab 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyCommand.java +++ b/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyCommand.java @@ -55,9 +55,7 @@ public class DisguiseModifyCommand extends DisguiseBaseCommand implements TabCom String[] options = DisguiseUtilities.split(StringUtils.join(args, " ")); - options = DisguiseParser - .parsePlaceholders(options, sender.getName(), DisguiseParser.getSkin(sender), sender.getName(), - DisguiseParser.getSkin(sender)); + options = DisguiseParser.parsePlaceholders(options, sender, sender); try { DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(), options); diff --git a/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyPlayerCommand.java b/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyPlayerCommand.java index 9c512392..fbbec796 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyPlayerCommand.java +++ b/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyPlayerCommand.java @@ -84,13 +84,10 @@ public class DisguiseModifyPlayerCommand extends DisguiseBaseCommand implements String[] options = DisguiseUtilities.split(StringUtils.join(newArgs, " ")); - options = DisguiseParser - .parsePlaceholders(options, sender.getName(), DisguiseParser.getSkin(sender), DisguiseParser.getName(entityTarget), - DisguiseParser.getSkin(entityTarget)); + options = DisguiseParser.parsePlaceholders(options, sender, entityTarget); try { - DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(), - options); + DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(), options); } catch (DisguiseParseException ex) { if (ex.getMessage() != null) { diff --git a/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyRadiusCommand.java b/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyRadiusCommand.java index 17365acf..6e07d6a4 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyRadiusCommand.java +++ b/src/main/java/me/libraryaddict/disguise/commands/DisguiseModifyRadiusCommand.java @@ -151,8 +151,7 @@ public class DisguiseModifyRadiusCommand extends DisguiseBaseCommand implements } String[] tempArgs = Arrays.copyOf(disguiseArgs, disguiseArgs.length); - tempArgs = DisguiseParser.parsePlaceholders(tempArgs, sender.getName(), DisguiseParser.getSkin(sender), - DisguiseParser.getName(entity), DisguiseParser.getSkin(entity)); + tempArgs = DisguiseParser.parsePlaceholders(tempArgs, sender, entity); try { DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(), tempArgs); diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/LibsEquipment.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/LibsEquipment.java index 6986f62d..0b0e1f1c 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/LibsEquipment.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/LibsEquipment.java @@ -1,4 +1,5 @@ -package me.libraryaddict.disguise.disguisetypes; // Its here so I can make use of flagWatcher.sendItemStack() which is protected +package me.libraryaddict.disguise.disguisetypes; // Its here so I can make use of flagWatcher.sendItemStack() which +// is protected import org.bukkit.entity.Entity; import org.bukkit.inventory.EntityEquipment; @@ -15,6 +16,16 @@ public class LibsEquipment implements EntityEquipment { this.flagWatcher = flagWatcher; } + public void setEquipment(EntityEquipment equipment) { + if (equipment == null) { + return; + } + + setArmorContents(equipment.getArmorContents()); + setItemInMainHand(equipment.getItemInMainHand()); + setItemInOffHand(equipment.getItemInOffHand()); + } + protected void setFlagWatcher(FlagWatcher flagWatcher) { this.flagWatcher = flagWatcher; } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java index 797ba933..bb47926b 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java @@ -15,7 +15,9 @@ import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.ItemStack; import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.potion.PotionEffectType; @@ -347,7 +349,7 @@ public class DisguiseParser { return !disguiseOptions.containsValue(true); } - public static String getName(Entity entity) { + public static String getName(CommandSender entity) { if (entity == null) { return "??"; } @@ -356,14 +358,16 @@ public class DisguiseParser { return entity.getName(); } - if (entity.getCustomName() != null && entity.getCustomName().length() > 0) { - return entity.getCustomName(); + if (entity instanceof Entity) { + if (((Entity) entity).getCustomName() != null && ((Entity) entity).getCustomName().length() > 0) { + return ((Entity) entity).getCustomName(); + } } return entity.getName(); } - public static String getSkin(CommandSender entity) { + private static String getSkin(CommandSender entity) { if (entity == null) { return "??"; } @@ -372,7 +376,6 @@ public class DisguiseParser { WrappedGameProfile gameProfile = ReflectionManager.getGameProfile((Player) entity); if (gameProfile != null) { - return DisguiseUtilities.getGson().toJson(gameProfile); } } @@ -380,27 +383,40 @@ public class DisguiseParser { return "{}"; } + 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; + } + public static String[] parsePlaceholders(String[] args, String userName, String userSkin, String targetName, - String targetSkin) { + String targetSkin, EntityEquipment equip, EntityEquipment targetEquip) { for (int i = 0; i < args.length; i++) { String arg = args[i]; - if (arg.contains("%user-name%")) { - arg = arg.replace("%user-name%", userName); - } + 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()); - if (arg.contains("%user-skin%")) { - arg = arg.replace("%user-skin%", userSkin); - } - - if (arg.contains("%target-name%")) { - arg = arg.replace("%target-name%", targetName); - } - - if (arg.contains("%target-skin%")) { - arg = arg.replace("%target-skin%", targetSkin); - } + 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()); args[i] = arg; } @@ -408,6 +424,22 @@ public class DisguiseParser { return args; } + private static String replace(String string, String value, Object toReplace) { + if (!string.contains(value)) { + return string; + } + + String oValue; + + if (toReplace != null) { + oValue = ParamInfoManager.toString(toReplace); + } else { + oValue = "null"; + } + + return string.replace(value, oValue); + } + public static long parseStringToTime(String string) throws DisguiseParseException { string = string.toLowerCase(); @@ -466,7 +498,7 @@ public class DisguiseParser { String skin = "{\"id\":\"a149f81bf7844f8987c554afdd4db533\",\"name\":\"libraryaddict\"," + "\"properties\":[]}"; // Fill in fake data - args = parsePlaceholders(args, "libraryaddict", skin, "libraryaddict", skin); + args = parsePlaceholders(args, "libraryaddict", skin, "libraryaddict", skin, null, null); // Parse disguise return parseDisguise(sender, null, permNode, args, permissions); @@ -544,7 +576,7 @@ public class DisguiseParser { args = DisguiseUtilities.split(customDisguise.getValue()); } - args = parsePlaceholders(args, sender.getName(), getSkin(sender), getName(target), getSkin(target)); + args = parsePlaceholders(args, sender, target); if (disguisePerm == null) { throw new DisguiseParseException(LibsMsg.PARSE_DISG_NO_EXIST, args[0]); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoManager.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoManager.java index e4dd4f0a..518e2bae 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoManager.java @@ -23,6 +23,20 @@ public class ParamInfoManager { return paramList; } + public static String toString(Object object) { + if (object == null) { + return "null"; + } + + ParamInfo info = getParamInfo(object.getClass()); + + if (info == null) { + throw new IllegalArgumentException(object.getClass() + " is not handled by ParamInfo!"); + } + + return info.toString(object); + } + public static ParamInfo getParamInfo(Class c) { for (ParamInfo info : getParamInfos()) { if (!info.isParam(c)) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java index e6a88d01..7e7577dd 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java @@ -160,7 +160,7 @@ public enum LibsMsg { ChatColor.RED + "Error! You do not have permission to use the parameter %s on the %s disguise!"), PARSE_NO_PERM_REF(ChatColor.RED + "You do not have permission to use disguise references!"), PARSE_NO_REF(ChatColor.RED + "Cannot find a disguise under the reference %s"), - PARSE_OPTION_NA(ChatColor.RED + "Cannot find the option %s"), + PARSE_OPTION_NA(ChatColor.RED + "Cannot find the option '%s'"), PARSE_SUPPLY_PLAYER(ChatColor.RED + "Error! You need to give a player name!"), PARSE_TOO_MANY_ARGS(ChatColor.RED + "Error! %s doesn't know what to do with %s!"), PARSE_INVALID_TIME(ChatColor.RED + "Error! %s is not a valid time! Use s,m,h,d or secs,mins,hours,days"), diff --git a/src/main/resources/disguises.yml b/src/main/resources/disguises.yml index 58e1dc01..254dc32a 100644 --- a/src/main/resources/disguises.yml +++ b/src/main/resources/disguises.yml @@ -17,6 +17,17 @@ # %target-skin% - If target is a player, replaces %target-skin% with their skin for use with player disguises # If target is not a player, will silently fail +# %held-item% - The currently held item in the main item slot +# %offhand-item% - The offhand item +# %armor% - The armor in ,,, format +# %helmet% %chestplate% %leggings% %boots% - Obvious. +# These are best used in armor slots, or in settings that accept items. Can also be used alongside /copydisguise +# to get the string format of an item. By /disguise zombie setiteminmainhand %held-item% - Then /copydisguise. +# But the plugin will attempt to parse to the "simplest" format. So best used with an item that has more custom data +# than just the amount. + +# These can be used again for the 'target' by prepending 'target-' to the above. So %target-armor% %target-held-item% + # The below disguise would give a disguised sheep the nametag; Me: libraryaddict, Them: Sheep # Example: 'cow setCustomName "Me: %user-name%, Them: %target-name%"' #