2018-09-07 04:35:38 +02:00
|
|
|
package me.libraryaddict.disguise.utilities.parser;
|
|
|
|
|
2019-03-05 05:46:47 +01:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
2018-09-07 04:35:38 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseConfig;
|
2022-02-02 08:10:00 +01:00
|
|
|
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.ModdedDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
|
2018-09-07 04:35:38 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
2020-04-18 22:19:03 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.modded.ModdedEntity;
|
2020-04-15 14:55:35 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.modded.ModdedManager;
|
2020-02-19 00:57:39 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.params.ParamInfo;
|
|
|
|
import me.libraryaddict.disguise.utilities.params.ParamInfoManager;
|
2023-04-23 14:02:46 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.ArtPaintingDisguiseParam;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.BlockDisplayDisguiseParam;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.ExtraDisguiseParam;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.FallingBlockDisguiseParamNew;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.FallingBlockDisguiseParamOld;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.IntegerPaintingDisguiseParam;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.ItemDisguiseParam;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.ItemFrameDisguiseParam;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.PlayerDisguiseParam;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.SplashPotionDisguiseParam;
|
|
|
|
import me.libraryaddict.disguise.utilities.parser.constructors.TextDisplayParam;
|
2019-03-05 05:46:47 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
|
2019-01-03 03:13:03 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.translations.LibsMsg;
|
|
|
|
import me.libraryaddict.disguise.utilities.translations.TranslateType;
|
2021-05-21 16:42:56 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.watchers.DisguiseMethods;
|
2019-04-16 22:10:15 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2018-09-07 04:35:38 +02:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.command.CommandSender;
|
2019-03-05 05:46:47 +01:00
|
|
|
import org.bukkit.entity.Entity;
|
2020-01-05 21:28:30 +01:00
|
|
|
import org.bukkit.entity.LivingEntity;
|
2018-10-05 01:04:30 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2020-01-05 21:28:30 +01:00
|
|
|
import org.bukkit.inventory.EntityEquipment;
|
2018-09-07 04:35:38 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2019-12-31 07:35:21 +01:00
|
|
|
import org.bukkit.potion.PotionEffectType;
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2022-02-02 08:16:24 +01:00
|
|
|
import java.lang.invoke.MethodHandle;
|
|
|
|
import java.lang.invoke.MethodHandles;
|
|
|
|
import java.lang.invoke.MethodType;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
public class DisguiseParser {
|
2020-01-19 03:48:26 +01:00
|
|
|
/**
|
|
|
|
* <Setter, <Getter, DefaultValue>>
|
|
|
|
*/
|
2023-03-31 05:24:17 +02:00
|
|
|
private static final HashMap<WatcherMethod, Map.Entry<WatcherMethod, Object>> defaultWatcherValues = new HashMap<>();
|
2023-04-23 14:02:46 +02:00
|
|
|
private static final List<ExtraDisguiseParam> extraDisguiseParams = new ArrayList<>();
|
2019-12-31 07:35:21 +01:00
|
|
|
|
|
|
|
public static void createDefaultMethods() {
|
|
|
|
try {
|
2021-05-20 20:23:01 +02:00
|
|
|
ArrayList<WatcherMethod> allMethods = ParamInfoManager.getDisguiseMethods().getMethods();
|
|
|
|
|
2019-12-31 07:35:21 +01:00
|
|
|
for (DisguiseType type : DisguiseType.values()) {
|
2020-02-06 00:15:20 +01:00
|
|
|
if (type.getEntityType() == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:35:21 +01:00
|
|
|
Disguise disguise;
|
|
|
|
|
|
|
|
if (type.isMisc()) {
|
|
|
|
disguise = new MiscDisguise(type);
|
|
|
|
} else if (type.isMob()) {
|
|
|
|
disguise = new MobDisguise(type);
|
2020-05-09 11:57:07 +02:00
|
|
|
} else if (type.isPlayer()) {
|
2019-12-31 07:35:21 +01:00
|
|
|
disguise = new PlayerDisguise("Foobar");
|
2020-05-09 11:57:07 +02:00
|
|
|
} else {
|
|
|
|
continue;
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FlagWatcher watcher = type.getWatcherClass().getConstructor(Disguise.class).newInstance(disguise);
|
|
|
|
|
2021-09-10 16:33:01 +02:00
|
|
|
WatcherMethod[] methods = ParamInfoManager.getDisguiseWatcherMethods(watcher.getClass(), true);
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
for (WatcherMethod setMethod : methods) {
|
2019-12-31 07:35:21 +01:00
|
|
|
// Invalidate methods that can't be handled normally
|
2020-10-08 23:09:37 +02:00
|
|
|
if (setMethod.getName().equals("addPotionEffect")) {
|
|
|
|
continue;
|
2021-05-20 20:23:01 +02:00
|
|
|
} else if (setMethod.getName().equals("setSkin") && setMethod.getParam() == String.class) {
|
2020-10-08 23:09:37 +02:00
|
|
|
continue;
|
2021-05-20 20:23:01 +02:00
|
|
|
} else if (setMethod.getName().equals("setTarget") && setMethod.getParam() != int.class) {
|
2020-10-08 23:09:37 +02:00
|
|
|
continue;
|
2022-02-17 12:29:21 +01:00
|
|
|
} else if ((setMethod.getName().equals("setCustomName") || setMethod.getName().equals("setCustomNameVisible")) &&
|
|
|
|
disguise.isPlayerDisguise()) {
|
|
|
|
// Player Disguise overrides the behavior of custom name, so we definitely don't want it judged on a global scale
|
|
|
|
continue;
|
2021-05-20 20:23:01 +02:00
|
|
|
} else if (setMethod.getName().equals("setItemInMainHand") && setMethod.getParam() == Material.class) {
|
2020-10-08 23:09:37 +02:00
|
|
|
continue;
|
2021-05-20 20:23:01 +02:00
|
|
|
} else if (setMethod.getName().matches("setArmor") && setMethod.getParam() == ItemStack[].class) {
|
2019-12-31 07:35:21 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName = setMethod.getName().substring(3); // Remove 'set'
|
|
|
|
|
|
|
|
if (getName.equals("HasNectar")) {
|
|
|
|
getName = "hasNectar";
|
|
|
|
} else if (getName.equals("HasStung")) {
|
|
|
|
getName = "hasStung";
|
2022-06-14 14:29:54 +02:00
|
|
|
} else if (getName.matches("^Has((Left)|(Right))Horn$")) {
|
|
|
|
getName = "has" + getName.substring(3);
|
2021-05-20 20:23:01 +02:00
|
|
|
} else if (setMethod.getParam().isAssignableFrom(boolean.class)) {
|
2019-12-31 07:35:21 +01:00
|
|
|
getName = "is" + getName;
|
|
|
|
} else {
|
|
|
|
getName = "get" + getName;
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
WatcherMethod getMethod = null;
|
|
|
|
|
|
|
|
for (WatcherMethod m : allMethods) {
|
|
|
|
if (m.getWatcherClass() != setMethod.getWatcherClass() || m.getParam() != null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m.getName().equals(getName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMethod = m;
|
|
|
|
break;
|
|
|
|
}
|
2019-12-31 07:35:21 +01:00
|
|
|
|
|
|
|
if (getMethod == null) {
|
2022-01-17 04:43:03 +01:00
|
|
|
DisguiseUtilities.getLogger().severe(
|
|
|
|
String.format("No such method '%s' when looking for the companion of '%s' in '%s'", getName, setMethod.getName(),
|
|
|
|
setMethod.getWatcherClass().getSimpleName()));
|
2020-02-13 07:47:02 +01:00
|
|
|
continue;
|
2021-05-20 20:23:01 +02:00
|
|
|
} else if (getMethod.getReturnType() != setMethod.getParam()) {
|
2022-01-17 04:43:03 +01:00
|
|
|
DisguiseUtilities.getLogger().severe(
|
|
|
|
String.format("Invalid return type of '%s' when looking for the companion of '%s' in '%s'", getName, setMethod.getName(),
|
|
|
|
setMethod.getWatcherClass().getSimpleName()));
|
2019-12-31 07:35:21 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object defaultValue = null;
|
|
|
|
|
|
|
|
// Value is randomish so shouldn't be checked, should always specify value when setting
|
2021-05-20 20:23:01 +02:00
|
|
|
if (!setMethod.isRandomDefault()) {
|
2019-12-31 07:35:21 +01:00
|
|
|
Object invokeWith = watcher;
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
if (!FlagWatcher.class.isAssignableFrom(getMethod.getWatcherClass())) {
|
2019-12-31 07:35:21 +01:00
|
|
|
invokeWith = disguise;
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
defaultValue = getMethod.getMethod().bindTo(invokeWith).invoke();
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
addWatcherDefault(setMethod, getMethod, defaultValue);
|
|
|
|
}
|
|
|
|
}
|
2023-04-23 14:02:46 +02:00
|
|
|
} catch (Throwable e) {
|
2019-12-31 07:35:21 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2023-04-23 14:02:46 +02:00
|
|
|
|
|
|
|
extraDisguiseParams.add(new ArtPaintingDisguiseParam());
|
|
|
|
extraDisguiseParams.add(new BlockDisplayDisguiseParam());
|
|
|
|
extraDisguiseParams.add(new FallingBlockDisguiseParamNew());
|
|
|
|
extraDisguiseParams.add(new FallingBlockDisguiseParamOld());
|
|
|
|
extraDisguiseParams.add(new IntegerPaintingDisguiseParam());
|
|
|
|
extraDisguiseParams.add(new ItemDisguiseParam());
|
|
|
|
extraDisguiseParams.add(new ItemFrameDisguiseParam());
|
|
|
|
extraDisguiseParams.add(new PlayerDisguiseParam());
|
|
|
|
extraDisguiseParams.add(new SplashPotionDisguiseParam());
|
|
|
|
extraDisguiseParams.add(new TextDisplayParam());
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
public static HashMap<WatcherMethod, Entry<WatcherMethod, Object>> getMethodDefaults() {
|
2020-01-19 03:48:26 +01:00
|
|
|
return defaultWatcherValues;
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:35:21 +01:00
|
|
|
public static String parseToString(Disguise disguise) {
|
2020-01-02 05:10:36 +01:00
|
|
|
return parseToString(disguise, true);
|
|
|
|
}
|
|
|
|
|
2021-05-21 16:42:56 +02:00
|
|
|
public static String parseToString(Disguise disguise, boolean outputSkinData) {
|
|
|
|
return parseToString(disguise, outputSkinData, false);
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:10:36 +01:00
|
|
|
/**
|
|
|
|
* Not outputting skin information is not garanteed to display the correct player name
|
|
|
|
*/
|
2021-05-21 16:42:56 +02:00
|
|
|
public static String parseToString(Disguise disguise, boolean outputSkinData, boolean includeCustomData) {
|
2019-12-31 07:35:21 +01:00
|
|
|
try {
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
2023-05-27 14:38:10 +02:00
|
|
|
stringBuilder.append(TranslateType.DISGUISES.reverseGet(disguise.getType().toReadable().replace(" ", "_")));
|
2019-12-31 07:35:21 +01:00
|
|
|
|
|
|
|
if (disguise.isPlayerDisguise()) {
|
2020-01-20 01:35:55 +01:00
|
|
|
stringBuilder.append(" ").append(DisguiseUtilities.quote(((PlayerDisguise) disguise).getName()));
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
WatcherMethod[] methods = ParamInfoManager.getDisguiseWatcherMethods(disguise.getType().getWatcherClass());
|
2020-06-13 07:27:39 +02:00
|
|
|
|
|
|
|
for (int i = methods.length - 1; i >= 0; i--) {
|
2021-05-20 20:23:01 +02:00
|
|
|
WatcherMethod m = methods[i];
|
2020-06-13 07:27:39 +02:00
|
|
|
|
2019-12-31 07:35:21 +01:00
|
|
|
// Special handling for this method
|
|
|
|
if (m.getName().equals("addPotionEffect")) {
|
2021-05-20 20:23:01 +02:00
|
|
|
MethodHandle getPotion =
|
2022-01-17 04:43:03 +01:00
|
|
|
MethodHandles.publicLookup().bind(disguise.getWatcher(), "getPotionEffects", MethodType.methodType(PotionEffectType[].class));
|
2021-05-20 20:23:01 +02:00
|
|
|
PotionEffectType[] types = (PotionEffectType[]) getPotion.invoke();
|
2019-12-31 07:35:21 +01:00
|
|
|
|
|
|
|
for (PotionEffectType type : types) {
|
|
|
|
if (type == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-27 14:38:10 +02:00
|
|
|
stringBuilder.append(" ").append(TranslateType.DISGUISE_OPTIONS.reverseGet(m.getName())).append(" ")
|
|
|
|
.append(TranslateType.DISGUISE_OPTIONS_PARAMETERS.reverseGet(type.getName()));
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 12:19:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2020-05-17 12:19:50 +02:00
|
|
|
// Also for this method. You can't override it, so why output it
|
|
|
|
if (m.getName().equals("setNoGravity")) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
Entry<WatcherMethod, Object> entry = defaultWatcherValues.get(m);
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2020-05-17 12:19:50 +02:00
|
|
|
if (entry == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
Object invokeWith = m.getWatcherClass().isInstance(disguise) ? disguise : disguise.getWatcher();
|
2020-01-02 05:10:36 +01:00
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
Object ourValue = entry.getKey().getMethod().bindTo(invokeWith).invoke();
|
2020-01-02 05:10:36 +01:00
|
|
|
|
2020-05-17 12:19:50 +02:00
|
|
|
// Escape a hacky fix for custom names, disguised players with custom names don't want to show it
|
|
|
|
// so it was set to an empty string.
|
|
|
|
if ("".equals(ourValue) && m.getName().equals("setCustomName")) {
|
|
|
|
ourValue = null;
|
|
|
|
}
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2020-05-17 12:19:50 +02:00
|
|
|
if (m.getName().equals("setSkin") && !outputSkinData) {
|
|
|
|
PlayerDisguise pDisg = (PlayerDisguise) disguise;
|
|
|
|
ourValue = pDisg.getName();
|
|
|
|
|
|
|
|
if (pDisg.getSkin() != null) {
|
|
|
|
ourValue = pDisg.getSkin();
|
|
|
|
} else if (pDisg.getGameProfile() != null && pDisg.getGameProfile().getName() != null) {
|
|
|
|
ourValue = pDisg.getGameProfile().getName();
|
|
|
|
}
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2020-05-17 12:19:50 +02:00
|
|
|
if (ourValue.equals(pDisg.getName())) {
|
2019-12-31 07:35:21 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-17 12:19:50 +02:00
|
|
|
} else {
|
|
|
|
// If its the same as default, continue
|
2021-05-20 20:23:01 +02:00
|
|
|
if (!m.isRandomDefault() && Objects.deepEquals(entry.getValue(), ourValue)) {
|
2020-05-17 12:19:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2023-05-27 14:38:10 +02:00
|
|
|
stringBuilder.append(" ").append(TranslateType.DISGUISE_OPTIONS.reverseGet(m.getName()));
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2020-05-17 12:19:50 +02:00
|
|
|
if (ourValue instanceof Boolean && (Boolean) ourValue) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2020-05-17 12:19:50 +02:00
|
|
|
String valueString;
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2020-05-17 12:19:50 +02:00
|
|
|
if (ourValue != null) {
|
|
|
|
valueString = ParamInfoManager.getParamInfo(ourValue.getClass()).toString(ourValue);
|
|
|
|
|
2022-09-22 07:00:36 +02:00
|
|
|
if (ourValue instanceof String) {
|
2023-05-27 14:38:10 +02:00
|
|
|
valueString = TranslateType.DISGUISE_OPTIONS_PARAMETERS.reverseGet(valueString);
|
2022-09-22 07:00:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 12:19:50 +02:00
|
|
|
valueString = DisguiseUtilities.quote(valueString);
|
|
|
|
} else {
|
2023-05-27 14:38:10 +02:00
|
|
|
valueString = TranslateType.DISGUISE_OPTIONS_PARAMETERS.reverseGet("null");
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
2020-05-17 12:19:50 +02:00
|
|
|
|
|
|
|
stringBuilder.append(" ").append(valueString);
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 16:42:56 +02:00
|
|
|
if (includeCustomData) {
|
2021-06-03 07:31:16 +02:00
|
|
|
HashMap<String, Object> meta = disguise.getCustomData();
|
2021-05-21 16:42:56 +02:00
|
|
|
LinkedHashMap<String, String> serializedMeta = new LinkedHashMap<>();
|
|
|
|
|
|
|
|
for (Entry<String, Object> entry : meta.entrySet()) {
|
|
|
|
Object val = entry.getValue();
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (val == null) {
|
|
|
|
serializedMeta.put(entry.getKey(), "null");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String serialized = DisguiseUtilities.getGson().toJson(val);
|
|
|
|
|
|
|
|
serializedMeta.put(entry.getKey(), val.getClass().getName() + ":" + serialized);
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
DisguiseUtilities.getLogger()
|
2022-01-17 04:43:03 +01:00
|
|
|
.warning("Unable to properly serialize the metadata on a disguise, the metadata was saved under name '" + entry.getKey() + "'");
|
2021-05-21 16:42:56 +02:00
|
|
|
|
|
|
|
if (!(throwable instanceof StackOverflowError)) {
|
|
|
|
throwable.printStackTrace();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!serializedMeta.isEmpty()) {
|
|
|
|
String serialized = DisguiseUtilities.getGson().toJson(serializedMeta);
|
|
|
|
|
|
|
|
stringBuilder.append(" ").append("setCustomData").append(" ").append(DisguiseUtilities.quote(serialized));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:35:21 +01:00
|
|
|
return stringBuilder.toString();
|
2021-05-20 20:23:01 +02:00
|
|
|
} catch (Throwable e) {
|
2019-12-31 07:35:21 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
private static void addWatcherDefault(WatcherMethod setMethod, WatcherMethod getMethod, Object object) {
|
2019-12-31 07:35:21 +01:00
|
|
|
if (defaultWatcherValues.containsKey(setMethod)) {
|
2020-05-08 07:26:07 +02:00
|
|
|
Object dObj = defaultWatcherValues.get(setMethod).getValue();
|
2019-12-31 07:35:21 +01:00
|
|
|
|
2020-05-08 07:26:07 +02:00
|
|
|
if (!Objects.deepEquals(dObj, object)) {
|
|
|
|
throw new IllegalStateException(String.format(
|
2022-01-17 04:43:03 +01:00
|
|
|
"%s has conflicting values in class %s! This means it expected the same value again but " + "received a " +
|
2023-03-31 05:24:17 +02:00
|
|
|
"different value on a different disguise! %s is not the same as %s!", setMethod.toString(), setMethod, object, dObj));
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
Map.Entry<WatcherMethod, Object> entry = new HashMap.SimpleEntry<>(getMethod, object);
|
2020-05-08 07:26:07 +02:00
|
|
|
|
2019-12-31 07:35:21 +01:00
|
|
|
defaultWatcherValues.put(setMethod, entry);
|
|
|
|
}
|
|
|
|
|
2020-12-14 22:02:47 +01:00
|
|
|
private static void doCheck(CommandSender sender, DisguisePermissions permissions, DisguisePerm disguisePerm, Collection<String> usedOptions)
|
2022-01-17 04:43:03 +01:00
|
|
|
throws DisguiseParseException {
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
if (!permissions.isAllowedDisguise(disguisePerm, usedOptions)) {
|
2020-12-14 22:02:47 +01:00
|
|
|
throw new DisguiseParseException(LibsMsg.D_PARSE_NOPERM, usedOptions.stream().reduce((first, second) -> second).orElse(null));
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DisguisePerm getDisguisePerm(String name) {
|
2023-11-01 03:33:22 +01:00
|
|
|
name = name.replaceAll("[ |_]", "").toLowerCase();
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
for (DisguisePerm perm : getDisguisePerms()) {
|
2023-11-01 03:33:22 +01:00
|
|
|
if (!perm.getRegexedName().equals(name)) {
|
2018-09-07 04:35:38 +02:00
|
|
|
continue;
|
2020-08-23 10:11:59 +02:00
|
|
|
}
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
return perm;
|
|
|
|
}
|
|
|
|
|
2023-11-01 03:33:22 +01:00
|
|
|
if (name.equals("p")) {
|
2018-09-07 04:35:38 +02:00
|
|
|
return getDisguisePerm(DisguiseType.PLAYER.toReadable());
|
2020-08-23 10:11:59 +02:00
|
|
|
}
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DisguisePerm[] getDisguisePerms() {
|
2020-02-06 00:15:20 +01:00
|
|
|
ArrayList<DisguisePerm> perms = new ArrayList<>();
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
for (DisguiseType disguiseType : DisguiseType.values()) {
|
2020-04-15 14:55:35 +02:00
|
|
|
if (disguiseType.getEntityType() == null || disguiseType.isCustom()) {
|
2020-02-06 00:15:20 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
perms.add(new DisguisePerm(disguiseType));
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 05:46:47 +01:00
|
|
|
for (Entry<DisguisePerm, String> entry : DisguiseConfig.getCustomDisguises().entrySet()) {
|
2020-02-06 00:15:20 +01:00
|
|
|
perms.add(entry.getKey());
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:55:35 +02:00
|
|
|
perms.addAll(ModdedManager.getDisguiseTypes());
|
|
|
|
|
2020-02-06 00:15:20 +01:00
|
|
|
return perms.toArray(new DisguisePerm[0]);
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get perms for the node. Returns a hashmap of allowed disguisetypes and their options
|
|
|
|
*/
|
2018-10-23 23:13:13 +02:00
|
|
|
public static DisguisePermissions getPermissions(CommandSender sender, String commandName) {
|
2023-11-12 01:06:53 +01:00
|
|
|
return DisguisePermissions.getPermissions(sender, commandName);
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isInteger(String string) {
|
|
|
|
try {
|
|
|
|
Integer.parseInt(string);
|
|
|
|
return true;
|
2020-08-23 10:11:59 +02:00
|
|
|
} catch (Exception ex) {
|
2018-09-07 04:35:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
public static String getName(CommandSender entity) {
|
2019-03-05 05:46:47 +01:00
|
|
|
if (entity == null) {
|
|
|
|
return "??";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity instanceof Player) {
|
|
|
|
return entity.getName();
|
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
if (entity instanceof Entity) {
|
|
|
|
if (((Entity) entity).getCustomName() != null && ((Entity) entity).getCustomName().length() > 0) {
|
|
|
|
return ((Entity) entity).getCustomName();
|
|
|
|
}
|
2019-03-05 05:46:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return entity.getName();
|
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
private static String getSkin(CommandSender entity) {
|
2019-03-05 05:46:47 +01:00
|
|
|
if (entity == null) {
|
|
|
|
return "??";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity instanceof Player) {
|
|
|
|
WrappedGameProfile gameProfile = ReflectionManager.getGameProfile((Player) entity);
|
|
|
|
|
|
|
|
if (gameProfile != null) {
|
|
|
|
return DisguiseUtilities.getGson().toJson(gameProfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "{}";
|
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
public static String[] parsePlaceholders(String[] args, CommandSender user, CommandSender target) {
|
2020-12-14 22:02:47 +01:00
|
|
|
return parsePlaceholders(args, getName(user), DisguiseUtilities.getDisplayName(user), getSkin(user), getName(target),
|
2022-01-17 04:43:03 +01:00
|
|
|
DisguiseUtilities.getDisplayName(target), DisguiseParser.getSkin(target), getEntityEquipment(user), getEntityEquipment(target));
|
2020-01-05 21:28:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static EntityEquipment getEntityEquipment(CommandSender entity) {
|
|
|
|
return entity instanceof LivingEntity ? ((LivingEntity) entity).getEquipment() : null;
|
|
|
|
}
|
|
|
|
|
2020-12-14 22:02:47 +01:00
|
|
|
public static String[] parsePlaceholders(String[] args, String userName, String userSkin, String targetName, String targetSkin, EntityEquipment equip,
|
|
|
|
EntityEquipment targetEquip) {
|
|
|
|
return parsePlaceholders(args, userName, userName, userSkin, targetName, targetName, targetSkin, equip, targetEquip);
|
2020-08-10 22:28:43 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 22:02:47 +01:00
|
|
|
public static String[] parsePlaceholders(String[] args, String userName, String userDisplayname, String userSkin, String targetName,
|
|
|
|
String targetDisplayname, String targetSkin, EntityEquipment equip, EntityEquipment targetEquip) {
|
2019-03-05 05:46:47 +01:00
|
|
|
for (int i = 0; i < args.length; i++) {
|
|
|
|
String arg = args[i];
|
|
|
|
|
2020-08-10 22:39:11 +02:00
|
|
|
arg = replace(arg, "%name%", userName);
|
|
|
|
arg = replace(arg, "%displayname%", userDisplayname);
|
|
|
|
arg = replace(arg, "%skin%", userSkin);
|
2020-01-05 21:28:30 +01:00
|
|
|
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());
|
2020-08-25 20:48:57 +02:00
|
|
|
arg = replace(arg, "%leggings%", equip == null ? null : equip.getLeggings());
|
2020-01-05 21:28:30 +01:00
|
|
|
arg = replace(arg, "%boots%", equip == null ? null : equip.getBoots());
|
|
|
|
|
2020-08-10 22:39:11 +02:00
|
|
|
for (Player p : Bukkit.getOnlinePlayers()) {
|
|
|
|
if (!arg.contains("%" + p.getName() + "-")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String name = p.getName();
|
|
|
|
|
2020-08-10 22:45:14 +02:00
|
|
|
arg = replace(arg, "%" + name + "-name%", name);
|
|
|
|
arg = replace(arg, "%" + name + "-displayname%", DisguiseUtilities.getDisplayName(p));
|
|
|
|
arg = replace(arg, "%" + name + "-skin%", getSkin(p));
|
2020-08-10 22:39:11 +02:00
|
|
|
|
|
|
|
EntityEquipment pEquip = p.getEquipment();
|
|
|
|
|
|
|
|
arg = replace(arg, "%" + name + "-held-item%", pEquip == null ? null : pEquip.getItemInMainHand());
|
|
|
|
arg = replace(arg, "%" + name + "-offhand-item%", pEquip == null ? null : pEquip.getItemInOffHand());
|
|
|
|
arg = replace(arg, "%" + name + "-armor%", pEquip == null ? null : pEquip.getArmorContents());
|
|
|
|
arg = replace(arg, "%" + name + "-helmet%", pEquip == null ? null : pEquip.getHelmet());
|
|
|
|
arg = replace(arg, "%" + name + "-chestplate%", pEquip == null ? null : pEquip.getChestplate());
|
2020-08-25 20:48:57 +02:00
|
|
|
arg = replace(arg, "%" + name + "-leggings%", pEquip == null ? null : pEquip.getLeggings());
|
2020-08-10 22:39:11 +02:00
|
|
|
arg = replace(arg, "%" + name + "-boots%", pEquip == null ? null : pEquip.getBoots());
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = replace(arg, "%target-name%", targetName);
|
|
|
|
arg = replace(arg, "%target-displayname%", targetDisplayname);
|
|
|
|
arg = replace(arg, "%target-skin%", targetSkin);
|
2020-01-05 21:28:30 +01:00
|
|
|
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());
|
2020-08-25 20:48:57 +02:00
|
|
|
arg = replace(arg, "%target-leggings%", targetEquip == null ? null : targetEquip.getLeggings());
|
2020-01-05 21:28:30 +01:00
|
|
|
arg = replace(arg, "%target-boots%", targetEquip == null ? null : targetEquip.getBoots());
|
2019-03-05 05:46:47 +01:00
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
args[i] = arg;
|
|
|
|
}
|
2019-03-05 05:46:47 +01:00
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
return args;
|
|
|
|
}
|
2019-03-05 05:46:47 +01:00
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
private static String replace(String string, String value, Object toReplace) {
|
|
|
|
if (!string.contains(value)) {
|
|
|
|
return string;
|
|
|
|
}
|
2019-03-05 05:46:47 +01:00
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
String oValue;
|
|
|
|
|
|
|
|
if (toReplace != null) {
|
|
|
|
oValue = ParamInfoManager.toString(toReplace);
|
|
|
|
} else {
|
|
|
|
oValue = "null";
|
2019-03-05 05:46:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 21:28:30 +01:00
|
|
|
return string.replace(value, oValue);
|
2019-03-05 05:46:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-06 03:16:14 +01:00
|
|
|
public static long parseStringToTime(String string) throws DisguiseParseException {
|
2020-09-14 05:01:05 +02:00
|
|
|
string = string.toLowerCase(Locale.ENGLISH);
|
2019-03-06 03:16:14 +01:00
|
|
|
|
|
|
|
if (!string.matches("([0-9]+[a-z]+)+")) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_INVALID_TIME_SEQUENCE, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
String[] split = string.split("((?<=[a-zA-Z])(?=[0-9]))|((?<=[0-9])(?=[a-zA-Z]))");
|
|
|
|
|
|
|
|
long time = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < split.length; i += 2) {
|
|
|
|
String t = split[i + 1];
|
|
|
|
long v = Long.parseLong(split[i]);
|
|
|
|
|
|
|
|
if (t.equals("s") || t.equals("sec") || t.equals("secs") || t.equals("seconds")) {
|
|
|
|
time += v;
|
|
|
|
} else if (t.equals("m") || t.equals("min") || t.equals("minute") || t.equals("minutes")) {
|
|
|
|
time += TimeUnit.MINUTES.toSeconds(v);
|
|
|
|
} else if (t.equals("h") || t.equals("hour") || t.equals("hours")) {
|
|
|
|
time += TimeUnit.HOURS.toSeconds(v);
|
|
|
|
} else if (t.equals("d") || t.equals("day") || t.equals("days")) {
|
|
|
|
time += TimeUnit.DAYS.toSeconds(v);
|
|
|
|
} else if (t.equals("w") || t.equals("week") || t.equals("weeks")) {
|
|
|
|
time += TimeUnit.DAYS.toSeconds(v) * 7;
|
|
|
|
} else if (t.equals("mon") || t.equals("month") || t.equals("months")) {
|
|
|
|
time += TimeUnit.DAYS.toSeconds(v) * 31;
|
|
|
|
} else if (t.equals("y") || t.equals("year") || t.equals("years")) {
|
|
|
|
time += TimeUnit.DAYS.toSeconds(v) * 365;
|
|
|
|
} else {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_INVALID_TIME, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2019-03-05 05:46:47 +01:00
|
|
|
/**
|
|
|
|
* Experimentally parses the arguments to test if this is a valid disguise
|
|
|
|
*
|
|
|
|
* @param sender
|
|
|
|
* @param permNode
|
|
|
|
* @param args
|
|
|
|
* @param permissions
|
|
|
|
* @return
|
|
|
|
* @throws DisguiseParseException
|
|
|
|
* @throws IllegalAccessException
|
|
|
|
* @throws InvocationTargetException
|
|
|
|
*/
|
2021-05-20 20:23:01 +02:00
|
|
|
public static Disguise parseTestDisguise(CommandSender sender, String permNode, String[] args, DisguisePermissions permissions) throws Throwable {
|
2019-03-05 05:46:47 +01:00
|
|
|
|
|
|
|
// Clone array so original array isn't modified
|
|
|
|
args = Arrays.copyOf(args, args.length);
|
|
|
|
|
|
|
|
String skin = "{\"id\":\"a149f81bf7844f8987c554afdd4db533\",\"name\":\"libraryaddict\"," + "\"properties\":[]}";
|
|
|
|
// Fill in fake data
|
2020-01-05 21:28:30 +01:00
|
|
|
args = parsePlaceholders(args, "libraryaddict", skin, "libraryaddict", skin, null, null);
|
2019-03-05 05:46:47 +01:00
|
|
|
|
|
|
|
// Parse disguise
|
|
|
|
return parseDisguise(sender, null, permNode, args, permissions);
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
public static void modifyDisguise(Disguise disguise, Entity target, String[] params) throws Throwable {
|
2020-02-25 03:20:44 +01:00
|
|
|
if (target != null) {
|
|
|
|
params = DisguiseParser.parsePlaceholders(params, target, target);
|
|
|
|
}
|
|
|
|
|
2023-11-15 01:10:24 +01:00
|
|
|
DisguiseParser.callMethods(Bukkit.getConsoleSender(), disguise, DisguisePermissions.getPermissions(Bukkit.getConsoleSender(), "disguise"),
|
2022-01-17 04:43:03 +01:00
|
|
|
new DisguisePerm(disguise.getType()), new ArrayList<>(), params, "Disguise");
|
2020-02-25 03:20:44 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
public static void modifyDisguise(Disguise disguise, String[] params) throws Throwable {
|
2020-02-25 03:20:44 +01:00
|
|
|
modifyDisguise(disguise, null, params);
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
public static void modifyDisguise(Disguise disguise, String params) throws Throwable {
|
2020-02-25 03:20:44 +01:00
|
|
|
modifyDisguise(disguise, DisguiseUtilities.split(params));
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
public static void modifyDisguise(Disguise disguise, Entity target, String params) throws Throwable {
|
2020-02-25 03:20:44 +01:00
|
|
|
modifyDisguise(disguise, target, DisguiseUtilities.split(params));
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
public static Disguise parseDisguise(String disguise) throws Throwable {
|
2019-04-16 22:10:15 +02:00
|
|
|
return parseDisguise(Bukkit.getConsoleSender(), null, disguise);
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
public static Disguise parseDisguise(CommandSender sender, Entity target, String disguise) throws Throwable {
|
2023-11-12 01:06:53 +01:00
|
|
|
return parseDisguise(sender, target, "disguise", DisguiseUtilities.split(disguise),
|
|
|
|
DisguisePermissions.getPermissions(Bukkit.getConsoleSender(), "disguise"));
|
2019-04-16 22:10:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* disguise has been feed a proper disguisetype.
|
|
|
|
*/
|
2020-12-14 22:02:47 +01:00
|
|
|
public static Disguise parseDisguise(CommandSender sender, Entity target, String permNode, String[] args, DisguisePermissions permissions)
|
2022-01-17 04:43:03 +01:00
|
|
|
throws Throwable {
|
2020-03-25 03:52:23 +01:00
|
|
|
if (!Bukkit.isPrimaryThread()) {
|
2020-08-23 10:11:59 +02:00
|
|
|
throw new IllegalStateException("DisguiseParser should not be called async!");
|
2020-03-25 03:52:23 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
if (sender instanceof Player) {
|
|
|
|
DisguiseUtilities.setCommandsUsed();
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
if (!permissions.hasPermissions()) {
|
2018-09-07 04:35:38 +02:00
|
|
|
throw new DisguiseParseException(LibsMsg.NO_PERM);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.length == 0) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_ARGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// How many args to skip due to the disugise being constructed
|
|
|
|
// Time to start constructing the disguise.
|
|
|
|
// We will need to check between all 3 kinds of disguises
|
|
|
|
int toSkip = 1;
|
|
|
|
ArrayList<String> usedOptions = new ArrayList<>();
|
|
|
|
Disguise disguise = null;
|
2018-10-23 23:13:13 +02:00
|
|
|
DisguisePerm disguisePerm;
|
2020-04-15 14:55:35 +02:00
|
|
|
String name;
|
2023-04-23 14:02:46 +02:00
|
|
|
boolean hasSetCustomName = false;
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
if (args[0].startsWith("@")) {
|
|
|
|
if (sender.hasPermission("libsdisguises.disguise.disguiseclone")) {
|
2020-09-14 05:01:05 +02:00
|
|
|
disguise = DisguiseUtilities.getClonedDisguise(args[0].toLowerCase(Locale.ENGLISH));
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
if (disguise == null) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_REF, args[0]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_REF);
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
disguisePerm = new DisguisePerm(disguise.getType());
|
2020-04-15 14:55:35 +02:00
|
|
|
name = disguise.getDisguiseName();
|
2023-04-23 14:02:46 +02:00
|
|
|
hasSetCustomName = disguise.isCustomDisguiseName();
|
2018-10-23 23:13:13 +02:00
|
|
|
|
|
|
|
if (disguisePerm.isUnknown()) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_CANT_DISG_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disguisePerm.getEntityType() == null) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_CANT_LOAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!permissions.isAllowedDisguise(disguisePerm)) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.NO_PERM_DISGUISE);
|
|
|
|
}
|
2018-09-07 04:35:38 +02:00
|
|
|
} else {
|
2018-10-23 23:13:13 +02:00
|
|
|
disguisePerm = getDisguisePerm(args[0]);
|
2020-04-15 14:55:35 +02:00
|
|
|
|
|
|
|
if (disguisePerm == null) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_DISG_NO_EXIST, args[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
name = disguisePerm.toReadable();
|
|
|
|
|
|
|
|
if (disguisePerm.getType().isCustom()) {
|
2020-04-18 22:23:35 +02:00
|
|
|
ModdedEntity ent = ModdedManager.getModdedEntity(disguisePerm.toReadable());
|
2020-04-15 14:55:35 +02:00
|
|
|
|
|
|
|
if (ent == null) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_CANT_DISG_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:19:03 +02:00
|
|
|
disguise = new ModdedDisguise(ent);
|
2023-04-23 14:02:46 +02:00
|
|
|
hasSetCustomName = true;
|
2020-04-15 14:55:35 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:07:53 +02:00
|
|
|
Entry<DisguisePerm, String> customDisguise = DisguiseConfig.getRawCustomDisguise(args[0]);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
if (customDisguise != null) {
|
2022-04-03 09:37:50 +02:00
|
|
|
// TODO Doesn't this mean we can't add args to our custom disguise on /disguise?
|
|
|
|
// Need to add user defined args for the custom disguise
|
2019-03-05 05:46:47 +01:00
|
|
|
args = DisguiseUtilities.split(customDisguise.getValue());
|
2020-04-15 14:55:35 +02:00
|
|
|
name = customDisguise.getKey().toReadable();
|
2023-04-23 14:02:46 +02:00
|
|
|
hasSetCustomName = true;
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
2021-05-27 10:57:39 +02:00
|
|
|
args = parsePlaceholders(args, sender == null ? target : sender, target);
|
2019-03-05 05:46:47 +01:00
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
if (disguisePerm.isUnknown()) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_CANT_DISG_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disguisePerm.getEntityType() == null) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_CANT_LOAD);
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:13:13 +02:00
|
|
|
if (!permissions.isAllowedDisguise(disguisePerm)) {
|
2018-09-07 04:35:38 +02:00
|
|
|
throw new DisguiseParseException(LibsMsg.NO_PERM_DISGUISE);
|
|
|
|
}
|
|
|
|
|
2022-02-17 08:44:17 +01:00
|
|
|
HashMap<String, HashMap<String, Boolean>> disguiseOptions = DisguisePermissions.getDisguiseOptions(sender, permNode, disguisePerm);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
if (disguise == null) {
|
2023-04-23 14:02:46 +02:00
|
|
|
WatcherMethod[] watcherMethods = ParamInfoManager.getDisguiseWatcherMethods(disguisePerm.getWatcherClass(), true);
|
|
|
|
String method = null;
|
|
|
|
Object param = null;
|
|
|
|
|
|
|
|
if (args.length > 1) {
|
|
|
|
String[] argArray = args;
|
|
|
|
|
|
|
|
if (Arrays.stream(watcherMethods).noneMatch(m -> m.getName().equalsIgnoreCase(argArray[1]))) {
|
|
|
|
for (ExtraDisguiseParam extra : extraDisguiseParams) {
|
|
|
|
if (!extra.isApplicable(disguisePerm.getType(), args[1])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
method = extra.getParameterMethod();
|
|
|
|
|
|
|
|
try {
|
|
|
|
param = extra.createParametervalue(sender, args[1]);
|
|
|
|
} catch (DisguiseParseException ex) {
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_EXPECTED_RECEIVED, extra.getParamInfo().getDescriptiveName(), args[1],
|
|
|
|
TranslateType.DISGUISE_OPTIONS.reverseGet(TranslateType.DISGUISE_OPTIONS.reverseGet(method)));
|
|
|
|
}
|
|
|
|
|
|
|
|
extra.checkParameterPermission(sender, permissions, disguiseOptions, usedOptions, disguisePerm, param);
|
|
|
|
toSkip++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
if (disguisePerm.isPlayer()) {
|
|
|
|
// If he is doing a player disguise
|
|
|
|
if (args.length == 1) {
|
|
|
|
// He needs to give the player name
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_SUPPLY_PLAYER);
|
|
|
|
} else {
|
2023-04-23 14:02:46 +02:00
|
|
|
if (method == null) {
|
|
|
|
param = "Nameless Player";
|
|
|
|
} else if (!"setName".equalsIgnoreCase(method)) {
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"Expected setName to be defined, this is an internal error, not a user error. Method was " + method);
|
2020-05-07 11:27:07 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
// Construct the player disguise
|
2023-04-23 14:02:46 +02:00
|
|
|
disguise = new PlayerDisguise((String) param);
|
2020-04-21 05:00:36 +02:00
|
|
|
|
2023-04-23 14:02:46 +02:00
|
|
|
// Prevent this being set later
|
|
|
|
method = null;
|
|
|
|
|
|
|
|
if (!hasSetCustomName) {
|
2020-04-21 05:00:36 +02:00
|
|
|
name = ((PlayerDisguise) disguise).getName();
|
|
|
|
}
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
2023-04-23 14:02:46 +02:00
|
|
|
} else if (disguisePerm.isMob()) {
|
|
|
|
// Its a mob, use the mob constructor
|
|
|
|
disguise = new MobDisguise(disguisePerm.getType());
|
|
|
|
} else if (disguisePerm.isMisc()) {
|
|
|
|
// Its a misc, we are going to use the MiscDisguise constructor.
|
|
|
|
disguise = new MiscDisguise(disguisePerm.getType());
|
|
|
|
}
|
2020-04-15 14:55:35 +02:00
|
|
|
|
2023-04-23 14:02:46 +02:00
|
|
|
if (method != null && param != null) {
|
|
|
|
WatcherMethod m = null;
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2023-04-23 14:02:46 +02:00
|
|
|
for (WatcherMethod method1 : watcherMethods) {
|
|
|
|
if (!method1.getName().equalsIgnoreCase(method)) {
|
|
|
|
continue;
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
2023-04-23 14:02:46 +02:00
|
|
|
|
|
|
|
if (!method1.getParam().isAssignableFrom(param.getClass())) {
|
|
|
|
continue;
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
2023-04-23 14:02:46 +02:00
|
|
|
|
|
|
|
m = method1;
|
|
|
|
break;
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 14:02:46 +02:00
|
|
|
if (m == null) {
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_CANT_LOAD_DETAILS, args[1], method);
|
|
|
|
}
|
2020-04-21 05:00:36 +02:00
|
|
|
|
2023-04-23 14:02:46 +02:00
|
|
|
MethodHandle handle = m.getMethod();
|
2020-08-23 10:11:59 +02:00
|
|
|
|
2023-04-23 14:02:46 +02:00
|
|
|
if (FlagWatcher.class.isAssignableFrom(m.getWatcherClass())) {
|
|
|
|
handle = handle.bindTo(disguise.getWatcher());
|
2018-09-07 04:35:38 +02:00
|
|
|
} else {
|
2023-04-23 14:02:46 +02:00
|
|
|
handle = handle.bindTo(disguise);
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
2023-04-23 14:02:46 +02:00
|
|
|
|
|
|
|
handle.invoke(param);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasSetCustomName && !disguisePerm.isPlayer()) {
|
|
|
|
name = disguise.getDisguiseName();
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:55:35 +02:00
|
|
|
disguise.setDisguiseName(name);
|
2023-04-23 14:02:46 +02:00
|
|
|
disguise.setCustomDisguiseName(hasSetCustomName);
|
2020-04-15 14:55:35 +02:00
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
// Copy strings to their new range
|
|
|
|
String[] newArgs = new String[args.length - toSkip];
|
|
|
|
System.arraycopy(args, toSkip, newArgs, 0, args.length - toSkip);
|
|
|
|
|
2020-01-12 05:26:44 +01:00
|
|
|
callMethods(sender, disguise, permissions, disguisePerm, usedOptions, newArgs, permNode);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2020-12-14 22:02:47 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
// Alright. We've constructed our disguise.
|
|
|
|
return disguise;
|
|
|
|
}
|
|
|
|
|
2020-12-14 22:02:47 +01:00
|
|
|
public static void callMethods(CommandSender sender, Disguise disguise, DisguisePermissions disguisePermission, DisguisePerm disguisePerm,
|
2021-05-20 20:23:01 +02:00
|
|
|
Collection<String> usedOptions, String[] args, String permNode) throws Throwable {
|
2021-09-10 16:33:01 +02:00
|
|
|
WatcherMethod[] methods = ParamInfoManager.getDisguiseWatcherMethods(disguise.getWatcher().getClass(), true);
|
2018-09-07 04:35:38 +02:00
|
|
|
List<String> list = new ArrayList<>(Arrays.asList(args));
|
2022-02-17 08:44:17 +01:00
|
|
|
HashMap<String, HashMap<String, Boolean>> disguiseOptions = DisguisePermissions.getDisguiseOptions(sender, permNode, disguisePerm);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
for (int argIndex = 0; argIndex < args.length; argIndex++) {
|
|
|
|
// This is the method name they provided
|
|
|
|
String methodNameProvided = list.remove(0);
|
|
|
|
// Translate the name they provided, to a name we recognize
|
|
|
|
String methodNameJava = TranslateType.DISGUISE_OPTIONS.reverseGet(methodNameProvided);
|
|
|
|
// The method we'll use
|
2021-05-20 20:23:01 +02:00
|
|
|
WatcherMethod methodToUse = null;
|
2018-09-07 04:35:38 +02:00
|
|
|
Object valueToSet = null;
|
|
|
|
DisguiseParseException parseException = null;
|
|
|
|
|
2021-05-21 16:42:56 +02:00
|
|
|
if (!list.isEmpty() && methodNameProvided.equalsIgnoreCase("setCustomData") && (sender == null || sender.isOp())) {
|
|
|
|
argIndex++;
|
|
|
|
String data = list.remove(0);
|
|
|
|
Map<String, String> deserial = DisguiseUtilities.getGson().fromJson(data, LinkedHashMap.class);
|
|
|
|
|
|
|
|
for (Entry<String, String> entry : deserial.entrySet()) {
|
|
|
|
String val = entry.getValue();
|
|
|
|
|
|
|
|
if (!val.contains(":")) {
|
|
|
|
disguise.addCustomData(entry.getKey(), null);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String className = val.substring(0, val.indexOf(":"));
|
|
|
|
val = val.substring(className.length() + 1);
|
|
|
|
|
|
|
|
disguise.addCustomData(entry.getKey(), DisguiseUtilities.getGson().fromJson(val, DisguiseMethods.parseType(className)));
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
for (WatcherMethod method : methods) {
|
2018-09-07 04:35:38 +02:00
|
|
|
if (!method.getName().equalsIgnoreCase(methodNameJava)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-19 00:57:39 +01:00
|
|
|
ParamInfo paramInfo = ParamInfoManager.getParamInfo(method);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Store how many args there were before calling the param
|
|
|
|
int argCount = list.size();
|
|
|
|
|
|
|
|
if (argCount < paramInfo.getMinArguments()) {
|
2020-12-14 22:02:47 +01:00
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_OPTION_VALUE, TranslateType.DISGUISE_OPTIONS.reverseGet(method.getName()));
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
valueToSet = paramInfo.fromString(list);
|
|
|
|
|
|
|
|
if (valueToSet == null && !paramInfo.canReturnNull()) {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip ahead as many args as were consumed on successful parse
|
|
|
|
argIndex += argCount - list.size();
|
|
|
|
|
|
|
|
methodToUse = method;
|
|
|
|
// We've found a method which will accept a valid value, break
|
|
|
|
break;
|
2020-08-23 10:11:59 +02:00
|
|
|
} catch (DisguiseParseException ex) {
|
2018-09-23 01:02:53 +02:00
|
|
|
parseException = ex;
|
2023-04-23 14:02:46 +02:00
|
|
|
} catch (Exception ex) {
|
2020-08-23 10:11:59 +02:00
|
|
|
parseException =
|
2022-01-17 04:43:03 +01:00
|
|
|
new DisguiseParseException(LibsMsg.PARSE_EXPECTED_RECEIVED, paramInfo.getDescriptiveName(), list.isEmpty() ? null : list.get(0),
|
|
|
|
TranslateType.DISGUISE_OPTIONS.reverseGet(method.getName()));
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (methodToUse == null) {
|
|
|
|
if (parseException != null) {
|
|
|
|
throw parseException;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_OPTION_NA, methodNameProvided);
|
|
|
|
}
|
|
|
|
|
2020-09-14 05:01:05 +02:00
|
|
|
if (!usedOptions.contains(methodToUse.getName().toLowerCase(Locale.ENGLISH))) {
|
|
|
|
usedOptions.add(methodToUse.getName().toLowerCase(Locale.ENGLISH));
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
2020-02-19 00:57:39 +01:00
|
|
|
doCheck(sender, disguisePermission, disguisePerm, usedOptions);
|
|
|
|
|
2020-01-22 21:42:01 +01:00
|
|
|
if (!disguiseOptions.isEmpty()) {
|
2020-01-12 05:26:44 +01:00
|
|
|
String stringValue = ParamInfoManager.toString(valueToSet);
|
|
|
|
|
2022-02-17 08:44:17 +01:00
|
|
|
if (!DisguisePermissions.hasPermissionOption(disguiseOptions, methodToUse.getName(), stringValue)) {
|
2020-12-14 22:02:47 +01:00
|
|
|
throw new DisguiseParseException(LibsMsg.PARSE_NO_PERM_PARAM, stringValue, disguisePerm.toReadable());
|
2020-01-12 05:26:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 22:02:47 +01:00
|
|
|
if (DisguiseConfig.isArmorstandsName() && ((methodToUse.getName().equals("setName") && disguise.isPlayerDisguise()) ||
|
2022-01-17 04:43:03 +01:00
|
|
|
(DisguiseConfig.isOverrideCustomNames() && methodToUse.getName().equals("setCustomName"))) &&
|
|
|
|
!sender.hasPermission("libsdisguises.multiname")) {
|
2020-05-07 11:27:07 +02:00
|
|
|
valueToSet = DisguiseUtilities.quoteNewLine((String) valueToSet);
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:01 +02:00
|
|
|
MethodHandle handle = methodToUse.getMethod();
|
|
|
|
|
|
|
|
if (FlagWatcher.class.isAssignableFrom(methodToUse.getWatcherClass())) {
|
|
|
|
handle = handle.bindTo(disguise.getWatcher());
|
2018-09-07 04:35:38 +02:00
|
|
|
} else {
|
2021-05-20 20:23:01 +02:00
|
|
|
handle = handle.bindTo(disguise);
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
2021-05-20 20:23:01 +02:00
|
|
|
|
|
|
|
handle.invoke(valueToSet);
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|