Minor code cleanup, add item placeholders to disguises

This commit is contained in:
libraryaddict 2020-01-06 09:28:30 +13:00
parent 54bd979506
commit 0ed798e8e7
No known key found for this signature in database
GPG Key ID: 052E4FBCD257AEA4
9 changed files with 97 additions and 37 deletions

View File

@ -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());

View File

@ -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);

View File

@ -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) {

View File

@ -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);

View File

@ -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;
}

View File

@ -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]);

View File

@ -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)) {

View File

@ -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"),

View File

@ -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 <Item>,<Item>,<Item>,<Item> 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%"'
#