Minor code cleanup, add item placeholders to disguises
This commit is contained in:
parent
54bd979506
commit
0ed798e8e7
@ -526,9 +526,7 @@ public class DisguiseListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
options = DisguiseParser
|
options = DisguiseParser.parsePlaceholders(options, p, entity);
|
||||||
.parsePlaceholders(options, p.getName(), DisguiseParser.getSkin(p), DisguiseParser.getName(entity),
|
|
||||||
DisguiseParser.getSkin(entity));
|
|
||||||
|
|
||||||
DisguisePermissions perms = DisguiseParser.getPermissions(p, "disguiseentitymodify");
|
DisguisePermissions perms = DisguiseParser.getPermissions(p, "disguiseentitymodify");
|
||||||
DisguisePerm disguisePerm = new DisguisePerm(disguise.getType());
|
DisguisePerm disguisePerm = new DisguisePerm(disguise.getType());
|
||||||
|
@ -55,9 +55,7 @@ public class DisguiseModifyCommand extends DisguiseBaseCommand implements TabCom
|
|||||||
|
|
||||||
String[] options = DisguiseUtilities.split(StringUtils.join(args, " "));
|
String[] options = DisguiseUtilities.split(StringUtils.join(args, " "));
|
||||||
|
|
||||||
options = DisguiseParser
|
options = DisguiseParser.parsePlaceholders(options, sender, sender);
|
||||||
.parsePlaceholders(options, sender.getName(), DisguiseParser.getSkin(sender), sender.getName(),
|
|
||||||
DisguiseParser.getSkin(sender));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(), options);
|
DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(), options);
|
||||||
|
@ -84,13 +84,10 @@ public class DisguiseModifyPlayerCommand extends DisguiseBaseCommand implements
|
|||||||
|
|
||||||
String[] options = DisguiseUtilities.split(StringUtils.join(newArgs, " "));
|
String[] options = DisguiseUtilities.split(StringUtils.join(newArgs, " "));
|
||||||
|
|
||||||
options = DisguiseParser
|
options = DisguiseParser.parsePlaceholders(options, sender, entityTarget);
|
||||||
.parsePlaceholders(options, sender.getName(), DisguiseParser.getSkin(sender), DisguiseParser.getName(entityTarget),
|
|
||||||
DisguiseParser.getSkin(entityTarget));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(),
|
DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(), options);
|
||||||
options);
|
|
||||||
}
|
}
|
||||||
catch (DisguiseParseException ex) {
|
catch (DisguiseParseException ex) {
|
||||||
if (ex.getMessage() != null) {
|
if (ex.getMessage() != null) {
|
||||||
|
@ -151,8 +151,7 @@ public class DisguiseModifyRadiusCommand extends DisguiseBaseCommand implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
String[] tempArgs = Arrays.copyOf(disguiseArgs, disguiseArgs.length);
|
String[] tempArgs = Arrays.copyOf(disguiseArgs, disguiseArgs.length);
|
||||||
tempArgs = DisguiseParser.parsePlaceholders(tempArgs, sender.getName(), DisguiseParser.getSkin(sender),
|
tempArgs = DisguiseParser.parsePlaceholders(tempArgs, sender, entity);
|
||||||
DisguiseParser.getName(entity), DisguiseParser.getSkin(entity));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(), tempArgs);
|
DisguiseParser.callMethods(sender, disguise, permissions, disguisePerm, new ArrayList<>(), tempArgs);
|
||||||
|
@ -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.entity.Entity;
|
||||||
import org.bukkit.inventory.EntityEquipment;
|
import org.bukkit.inventory.EntityEquipment;
|
||||||
@ -15,6 +16,16 @@ public class LibsEquipment implements EntityEquipment {
|
|||||||
this.flagWatcher = flagWatcher;
|
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) {
|
protected void setFlagWatcher(FlagWatcher flagWatcher) {
|
||||||
this.flagWatcher = flagWatcher;
|
this.flagWatcher = flagWatcher;
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,9 @@ import org.bukkit.ChatColor;
|
|||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
|
import org.bukkit.entity.LivingEntity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.EntityEquipment;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||||
import org.bukkit.potion.PotionEffectType;
|
import org.bukkit.potion.PotionEffectType;
|
||||||
@ -347,7 +349,7 @@ public class DisguiseParser {
|
|||||||
return !disguiseOptions.containsValue(true);
|
return !disguiseOptions.containsValue(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getName(Entity entity) {
|
public static String getName(CommandSender entity) {
|
||||||
if (entity == null) {
|
if (entity == null) {
|
||||||
return "??";
|
return "??";
|
||||||
}
|
}
|
||||||
@ -356,14 +358,16 @@ public class DisguiseParser {
|
|||||||
return entity.getName();
|
return entity.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entity.getCustomName() != null && entity.getCustomName().length() > 0) {
|
if (entity instanceof Entity) {
|
||||||
return entity.getCustomName();
|
if (((Entity) entity).getCustomName() != null && ((Entity) entity).getCustomName().length() > 0) {
|
||||||
|
return ((Entity) entity).getCustomName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return entity.getName();
|
return entity.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getSkin(CommandSender entity) {
|
private static String getSkin(CommandSender entity) {
|
||||||
if (entity == null) {
|
if (entity == null) {
|
||||||
return "??";
|
return "??";
|
||||||
}
|
}
|
||||||
@ -372,7 +376,6 @@ public class DisguiseParser {
|
|||||||
WrappedGameProfile gameProfile = ReflectionManager.getGameProfile((Player) entity);
|
WrappedGameProfile gameProfile = ReflectionManager.getGameProfile((Player) entity);
|
||||||
|
|
||||||
if (gameProfile != null) {
|
if (gameProfile != null) {
|
||||||
|
|
||||||
return DisguiseUtilities.getGson().toJson(gameProfile);
|
return DisguiseUtilities.getGson().toJson(gameProfile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -380,27 +383,40 @@ public class DisguiseParser {
|
|||||||
return "{}";
|
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,
|
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++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
String arg = args[i];
|
String arg = args[i];
|
||||||
|
|
||||||
if (arg.contains("%user-name%")) {
|
arg = replace(arg, "%user-name%", userName);
|
||||||
arg = arg.replace("%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 = replace(arg, "%target-held-item%", targetEquip == null ? null : targetEquip.getItemInMainHand());
|
||||||
arg = arg.replace("%user-skin%", userSkin);
|
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());
|
||||||
if (arg.contains("%target-name%")) {
|
arg = replace(arg, "%target-chestplate%", targetEquip == null ? null : targetEquip.getChestplate());
|
||||||
arg = arg.replace("%target-name%", targetName);
|
arg = replace(arg, "%target-leggings%%", targetEquip == null ? null : targetEquip.getLeggings());
|
||||||
}
|
arg = replace(arg, "%target-boots%", targetEquip == null ? null : targetEquip.getBoots());
|
||||||
|
|
||||||
if (arg.contains("%target-skin%")) {
|
|
||||||
arg = arg.replace("%target-skin%", targetSkin);
|
|
||||||
}
|
|
||||||
|
|
||||||
args[i] = arg;
|
args[i] = arg;
|
||||||
}
|
}
|
||||||
@ -408,6 +424,22 @@ public class DisguiseParser {
|
|||||||
return args;
|
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 {
|
public static long parseStringToTime(String string) throws DisguiseParseException {
|
||||||
string = string.toLowerCase();
|
string = string.toLowerCase();
|
||||||
|
|
||||||
@ -466,7 +498,7 @@ public class DisguiseParser {
|
|||||||
|
|
||||||
String skin = "{\"id\":\"a149f81bf7844f8987c554afdd4db533\",\"name\":\"libraryaddict\"," + "\"properties\":[]}";
|
String skin = "{\"id\":\"a149f81bf7844f8987c554afdd4db533\",\"name\":\"libraryaddict\"," + "\"properties\":[]}";
|
||||||
// Fill in fake data
|
// Fill in fake data
|
||||||
args = parsePlaceholders(args, "libraryaddict", skin, "libraryaddict", skin);
|
args = parsePlaceholders(args, "libraryaddict", skin, "libraryaddict", skin, null, null);
|
||||||
|
|
||||||
// Parse disguise
|
// Parse disguise
|
||||||
return parseDisguise(sender, null, permNode, args, permissions);
|
return parseDisguise(sender, null, permNode, args, permissions);
|
||||||
@ -544,7 +576,7 @@ public class DisguiseParser {
|
|||||||
args = DisguiseUtilities.split(customDisguise.getValue());
|
args = DisguiseUtilities.split(customDisguise.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
args = parsePlaceholders(args, sender.getName(), getSkin(sender), getName(target), getSkin(target));
|
args = parsePlaceholders(args, sender, target);
|
||||||
|
|
||||||
if (disguisePerm == null) {
|
if (disguisePerm == null) {
|
||||||
throw new DisguiseParseException(LibsMsg.PARSE_DISG_NO_EXIST, args[0]);
|
throw new DisguiseParseException(LibsMsg.PARSE_DISG_NO_EXIST, args[0]);
|
||||||
|
@ -23,6 +23,20 @@ public class ParamInfoManager {
|
|||||||
return paramList;
|
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) {
|
public static ParamInfo getParamInfo(Class c) {
|
||||||
for (ParamInfo info : getParamInfos()) {
|
for (ParamInfo info : getParamInfos()) {
|
||||||
if (!info.isParam(c)) {
|
if (!info.isParam(c)) {
|
||||||
|
@ -160,7 +160,7 @@ public enum LibsMsg {
|
|||||||
ChatColor.RED + "Error! You do not have permission to use the parameter %s on the %s disguise!"),
|
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_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_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_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_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"),
|
PARSE_INVALID_TIME(ChatColor.RED + "Error! %s is not a valid time! Use s,m,h,d or secs,mins,hours,days"),
|
||||||
|
@ -17,6 +17,17 @@
|
|||||||
# %target-skin% - If target is a player, replaces %target-skin% with their skin for use with player disguises
|
# %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
|
# 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
|
# The below disguise would give a disguised sheep the nametag; Me: libraryaddict, Them: Sheep
|
||||||
# Example: 'cow setCustomName "Me: %user-name%, Them: %target-name%"'
|
# Example: 'cow setCustomName "Me: %user-name%, Them: %target-name%"'
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user