Read for information...

Now using ASM manipulation to remove invalid methods on load
Fixed imports
Fixed Chat Components being used in 1.12
Fixed tab complete showing args for disguise options you can't use
Disguise option permissions now demand a parameter to be the method name
Falling block disguises are now only usable with blocks
LibsDisguises command now tab completes the new options
Libs Disguises command lets you create a vanilla compatible item string
If a vehicle is disguised as a vehicle, don't give no gravity
Fixed horse disguise using almost random values for its flagwatcher settings
Renamed horse disguise setMouthOpen to setEating
Slightly better string for premium info jar location
Skip attributes packets if using older ProtocolLib jar
Don't cancel entity death if entity is dead
Improved disguise permissions checking
Fixed time parameter not being attributed properly
This commit is contained in:
libraryaddict
2020-02-19 12:57:39 +13:00
parent 668eec641e
commit 897a6629ae
65 changed files with 1205 additions and 513 deletions

View File

@@ -0,0 +1,152 @@
package me.libraryaddict.disguise.utilities.params;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.translations.TranslateType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by libraryaddict on 7/09/2018.
*/
public abstract class ParamInfo {
private Class paramClass;
private String descriptiveName;
private String name;
private Map<String, Object> possibleValues;
/**
* Used for translations, namely ItemStack and it's 'Glowing' and 'null' counterparts
*/
private String[] otherValues;
private String description;
public ParamInfo(Class paramClass, String name, String description) {
this(paramClass, name, name, description);
}
public ParamInfo(Class paramClass, String name, String descriptiveName, String description) {
this.name = name;
this.paramClass = paramClass;
this.descriptiveName = descriptiveName;
this.description = description;
}
public ParamInfo(Class paramClass, String name, String description, Enum[] possibleValues) {
this(paramClass, name, name, description, possibleValues);
}
public ParamInfo(Class paramClass, String name, String descriptiveName, String description, Enum[] possibleValues) {
this(paramClass, name, descriptiveName, description);
this.possibleValues = new HashMap<>();
for (Enum anEnum : possibleValues) {
this.getValues().put(anEnum.name(), anEnum);
}
}
public ParamInfo(Class paramClass, String name, String description, Map<String, Object> possibleValues) {
this(paramClass, name, name, description, possibleValues);
}
public ParamInfo(Class paramClass, String name, String descriptiveName, String description,
Map<String, Object> possibleValues) {
this(paramClass, name, descriptiveName, description);
this.possibleValues = new HashMap<>();
this.possibleValues.putAll(possibleValues);
}
public boolean canTranslateValues() {
return getValues() != null;
}
public String[] getOtherValues() {
return this.otherValues;
}
public void setOtherValues(String... otherValues) {
this.otherValues = otherValues;
}
public boolean canReturnNull() {
return false;
}
protected abstract Object fromString(String string) throws DisguiseParseException;
public abstract String toString(Object object);
public Object fromString(List<String> arguments) throws DisguiseParseException {
// Don't consume a string immediately, if it errors we need to check other param types
String string = arguments.get(0);
Object value = fromString(string);
// Throw error if null wasn't expected
if (value == null && !canReturnNull()) {
throw new IllegalArgumentException();
}
arguments.remove(0);
return value;
}
public int getMinArguments() {
return 1;
}
public boolean hasValues() {
return getValues() != null;
}
protected Class getParamClass() {
return paramClass;
}
public boolean isParam(Class paramClass) {
return getParamClass() == paramClass;
}
public String getName() {
return TranslateType.DISGUISE_OPTIONS_PARAMETERS.get(getRawName());
}
public String getDescriptiveName() {
return TranslateType.DISGUISE_OPTIONS_PARAMETERS.get(getRawDescriptiveName());
}
public String getRawName() {
return this.name;
}
public String getRawDescriptiveName() {
return descriptiveName;
}
public String getDescription() {
return TranslateType.DISGUISE_OPTIONS_PARAMETERS.get(getRawDescription());
}
public String getRawDescription() {
return description;
}
public Map<String, Object> getValues() {
return this.possibleValues;
}
public Set<String> getEnums(String tabComplete) {
return getValues().keySet();
}
/**
* Is the values it returns all it can do?
*/
public boolean isCustomValues() {
return true;
}
}

View File

@@ -0,0 +1,152 @@
package me.libraryaddict.disguise.utilities.params;
import lombok.Getter;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.disguisetypes.watchers.FallingBlockWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.params.types.custom.ParamInfoItemBlock;
import me.libraryaddict.disguise.utilities.watchers.DisguiseMethods;
import org.bukkit.ChatColor;
import org.bukkit.inventory.ItemStack;
import javax.annotation.Nullable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class ParamInfoManager {
private static List<ParamInfo> paramList;
private static DisguiseMethods disguiseMethods;
@Getter
private static ParamInfoItemBlock paramInfoItemBlock;
public static List<ParamInfo> getParamInfos() {
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(Method method) {
if (method.getDeclaringClass() == FallingBlockWatcher.class &&
method.getParameterTypes()[0] == ItemStack.class) {
return getParamInfoItemBlock();
}
return getParamInfo(method.getParameterTypes()[0]);
}
public static ParamInfo getParamInfo(Class c) {
for (ParamInfo info : getParamInfos()) {
if (!info.isParam(c)) {
continue;
}
return info;
}
return null;
}
public static ParamInfo getParamInfo(DisguisePerm disguiseType, String methodName) {
return getParamInfo(disguiseType.getType(), methodName);
}
public static ParamInfo getParamInfo(DisguiseType disguiseType, String methodName) {
for (Method method : getDisguiseWatcherMethods(disguiseType.getWatcherClass())) {
if (!method.getName().toLowerCase().equals(methodName.toLowerCase()))
continue;
return getParamInfo(method);
}
return null;
}
static {
ParamInfoTypes infoTypes = new ParamInfoTypes();
paramList = infoTypes.getParamInfos();
paramInfoItemBlock = infoTypes.getParamInfoBlock();
disguiseMethods = new DisguiseMethods();
//paramList.sort((o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getName(), o2.getName()));
}
public static Method[] getDisguiseWatcherMethods(@Nullable Class<? extends FlagWatcher> watcherClass) {
if (watcherClass == null) {
return new Method[0];
}
ArrayList<Method> methods = new ArrayList<>(disguiseMethods.getMethods(watcherClass));
// Order first by their declaring class, the top class (SheepWatcher) goes before (FlagWatcher)
// Order methods in the same watcher by their name from A to Z
methods.sort((m1, m2) -> {
int v1 = getValue(m1);
int v2 = getValue(m2);
if (v1 != v2) {
return v1 - v2;
}
return String.CASE_INSENSITIVE_ORDER.compare(m1.getName(), m2.getName());
});
// Add these last as it's what we want to present to be called the least
for (String methodName : new String[]{"setSelfDisguiseVisible", "setHideHeldItemFromSelf",
"setHideArmorFromSelf", "setHearSelfDisguise", "setHidePlayer", "setExpires"}) {
try {
methods.add(Disguise.class
.getMethod(methodName, methodName.equals("setExpires") ? long.class : boolean.class));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
if (watcherClass == PlayerWatcher.class) {
try {
methods.add(PlayerDisguise.class.getMethod("setNameVisible", boolean.class));
methods.add(PlayerDisguise.class.getMethod("setDynamicName", boolean.class));
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
return methods.toArray(new Method[0]);
}
/**
* Value of the method, used namely for ordering the more unique methods to a disguise
*/
public static int getValue(Method method) {
ChatColor methodColor = ChatColor.YELLOW;
Class<?> declaring = method.getDeclaringClass();
if (declaring == LivingWatcher.class) {
return 1;
} else if (!(FlagWatcher.class.isAssignableFrom(declaring)) || declaring == FlagWatcher.class) {
return 2;
}
return 0;
}
}

View File

@@ -0,0 +1,198 @@
package me.libraryaddict.disguise.utilities.params;
import com.comphenix.protocol.wrappers.BlockPosition;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.comphenix.protocol.wrappers.WrappedParticle;
import me.libraryaddict.disguise.disguisetypes.EntityPose;
import me.libraryaddict.disguise.disguisetypes.RabbitType;
import me.libraryaddict.disguise.utilities.params.types.ParamInfoEnum;
import me.libraryaddict.disguise.utilities.params.types.base.*;
import me.libraryaddict.disguise.utilities.params.types.custom.*;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import org.apache.commons.lang.StringUtils;
import org.bukkit.*;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MainHand;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.EulerAngle;
import java.lang.reflect.Field;
import java.util.*;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoTypes {
public ParamInfoItemBlock getParamInfoBlock() {
return new ParamInfoItemBlock(ItemStack.class, "ItemStack", "ItemStack (Material,Amount?,Glow?)",
"An ItemStack compromised of Material,Amount,Glow. Only requires Material", getMaterials());
}
/**
* Constructor values are listed here for continuity
*/
public List<ParamInfo> getParamInfos() {
List<ParamInfo> paramInfos = new ArrayList<>();
// Register enum types
//paramInfos.add(new ParamInfoEnum(AnimalColor.class, "Animal Color",
// "View all the colors you can use for an animal color"));
paramInfos
.add(new ParamInfoEnum(Art.class, "Art", "View all the paintings you can use for a painting disguise"));
paramInfos.add(new ParamInfoEnum(Horse.Color.class, "Horse Color",
"View all the colors you can use for a horses color"));
paramInfos.add(new ParamInfoEnum(Villager.Profession.class, "Villager Profession",
"View all the professions you can set on a Villager and Zombie Villager"));
if (NmsVersion.v1_14.isSupported()) {
paramInfos.add(new ParamInfoEnum(Villager.Type.class, "Villager Biome",
"View all the biomes you can set on a Villager and Zombie Villager"));
}
paramInfos.add(new ParamInfoEnum(BlockFace.class, "Direction", "Direction (North, East, South, West, Up, Down)",
"View the directions usable on player setSleeping and shulker direction",
Arrays.copyOf(BlockFace.values(), 6)));
paramInfos
.add(new ParamInfoEnum(RabbitType.class, "Rabbit Type", "View the kinds of rabbits you can turn into"));
paramInfos
.add(new ParamInfoEnum(TreeSpecies.class, "Tree Species", "View the different types of tree species"));
paramInfos.add(new ParamInfoEnum(MainHand.class, "Main Hand", "Set the main hand for an entity"));
paramInfos.add(new ParamInfoEnum(Llama.Color.class, "Llama Color",
"View all the colors you can use for a llama color"));
paramInfos.add(new ParamInfoEnum(Parrot.Variant.class, "Parrot Variant",
"View the different colors a parrot can be"));
if (NmsVersion.v1_13.isSupported()) {
paramInfos.add(new ParamInfoParticle(WrappedParticle.class, "Particle",
"The different particles of Minecraft", Particle.values(), getMaterials()));
paramInfos.add(new ParamInfoEnum(TropicalFish.Pattern.class, "Pattern", "Patterns of a tropical fish"));
} else {
paramInfos.add(new ParamInfoEnum(Particle.class, "Particle", "The different particles of Minecraft"));
}
paramInfos.add(new ParamInfoEnum(DyeColor.class, "DyeColor", "Dye colors of many different colors"));
paramInfos.add(new ParamInfoEnum(Horse.Style.class, "Horse Style",
"Horse style which is the patterns on the horse"));
if (NmsVersion.v1_14.isSupported()) {
paramInfos.add(new ParamInfoEnum(EntityPose.class, "EntityPose", "The pose the entity should strike"));
paramInfos.add(new ParamInfoEnum(Cat.Type.class, "Cat Type", "The type of cat"));
paramInfos.add(new ParamInfoEnum(Fox.Type.class, "Fox Type", "The type of fox"));
paramInfos.add(new ParamInfoEnum(Panda.Gene.class, "Panda Gene", "The panda gene type"));
paramInfos.add(new ParamInfoEnum(MushroomCow.Variant.class, "Mushroom Cow Variant",
"The different variants for mushroom cows"));
}
// Register custom types
paramInfos.add(new ParamInfoEulerAngle(EulerAngle.class, "Euler Angle", "Euler Angle (X,Y,Z)",
"Set the X,Y,Z directions on an armorstand"));
paramInfos.add(new ParamInfoColor(Color.class, "Color", "Colors that can also be defined through RGB",
getColors()));
paramInfos.add(new ParamInfoEnum(Material.class, "Material", "A material used for blocks and items",
getMaterials()));
paramInfos.add(new ParamInfoItemStack(ItemStack.class, "ItemStack", "ItemStack (Material,Amount?,Glow?)",
"An ItemStack compromised of Material,Amount,Glow. Only requires Material", getMaterials()));
paramInfos.add(new ParamInfoItemStackArray(ItemStack[].class, "ItemStack[]",
"Four ItemStacks (Material:Amount?:Glow?,Material:Amount?:Glow?..)",
"Four ItemStacks separated by a comma", getMaterials()));
paramInfos.add(new ParamInfoPotionEffect(PotionEffectType.class, "Potion Effect",
"View all the potion effects you can add", getPotions()));
paramInfos.add(new ParamInfoBlockPosition(BlockPosition.class, "Block Position", "Block Position (num,num,num)",
"Three numbers separated by a ,"));
paramInfos.add(new ParamInfoGameProfile(WrappedGameProfile.class, "GameProfile",
"Get the gameprofile here https://sessionserver.mojang" +
".com/session/minecraft/profile/PLAYER_UUID_GOES_HERE?unsigned=false"));
paramInfos.add(new ParamInfoTime(long.class, "Expiry Time",
"Set how long the disguise lasts, <Num><Time><Num>... where <Time> is (s/sec)(m/min)(h/hour)(d/day) " +
"etc. 30m20secs = 30 minutes, 20 seconds"));
// Register base types
Map<String, Object> booleanMap = new HashMap<>();
booleanMap.put("true", true);
booleanMap.put("false", false);
paramInfos.add(new ParamInfoBoolean("Boolean", "True/False", "True or False", booleanMap));
paramInfos.add(new ParamInfoString(String.class, "Text", "A line of text"));
paramInfos.add(new ParamInfoInteger("Number", "A whole number without decimals"));
paramInfos.add(new ParamInfoFloat("Number.0", "A number which can have decimal places"));
paramInfos.add(new ParamInfoDouble("Number.0", "A number which can have decimal places"));
return paramInfos;
}
private Map<String, Color> getColors() {
try {
Map<String, Color> map = new HashMap<>();
Class cl = Class.forName("org.bukkit.Color");
for (Field field : cl.getFields()) {
if (field.getType() != cl) {
continue;
}
map.put(field.getName(), (Color) field.get(null));
}
return map;
}
catch (ClassNotFoundException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
private Material[] getMaterials() {
List<Material> list = new ArrayList<>();
for (Material material : Material.values()) {
if (material.name().matches("([A-Z]+_)?AIR")) {
continue;
}
try {
Field field = Material.class.getField(material.name());
// Ignore all legacies materials
if (field.isAnnotationPresent(Deprecated.class)) {
continue;
}
list.add(material);
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
return list.toArray(new Material[0]);
}
private Map<String, Object> getPotions() {
Map<String, Object> map = new HashMap<>();
for (PotionEffectType effectType : PotionEffectType.values()) {
if (effectType == null)
continue;
map.put(toReadable(effectType.getName()), effectType);
}
return map;
}
private String toReadable(String string) {
String[] split = string.split("_");
for (int i = 0; i < split.length; i++) {
split[i] = split[i].substring(0, 1) + split[i].substring(1).toLowerCase();
}
return StringUtils.join(split, "_");
}
}

View File

@@ -0,0 +1,55 @@
package me.libraryaddict.disguise.utilities.params.types;
import me.libraryaddict.disguise.utilities.params.ParamInfo;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import java.util.Map;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoEnum extends ParamInfo {
public ParamInfoEnum(Class<? extends Enum> paramClass, String name, String description) {
super(paramClass, name, name, description, paramClass.getEnumConstants());
}
public ParamInfoEnum(Class paramClass, String name, String valueType, String description, Enum[] possibleValues) {
super(paramClass, name, valueType, description, possibleValues);
}
public ParamInfoEnum(Class paramClass, String name, String description, Enum[] possibleValues) {
super(paramClass, name, name, description, possibleValues);
}
public ParamInfoEnum(Class paramClass, String name, String description, Map<String, Object> possibleValues) {
super(paramClass, name, name, description, possibleValues);
}
@Override
protected Object fromString(String string) throws DisguiseParseException {
string = string.replace("_", "");
for (Map.Entry<String, Object> entry : getValues().entrySet()) {
if (!entry.getKey().replace("_", "").equalsIgnoreCase(string)) {
continue;
}
return entry.getValue();
}
return null;
}
@Override
public String toString(Object object) {
return object.toString();
}
/**
* Is the values it returns all it can do?
*/
@Override
public boolean isCustomValues() {
return false;
}
}

View File

@@ -0,0 +1,62 @@
package me.libraryaddict.disguise.utilities.params.types.base;
import me.libraryaddict.disguise.utilities.params.ParamInfo;
import me.libraryaddict.disguise.utilities.translations.TranslateType;
import java.util.List;
import java.util.Map;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoBoolean extends ParamInfo {
public ParamInfoBoolean(String name, String valueType, String description, Map<String, Object> possibleValues) {
super(Boolean.class, name, valueType, description, possibleValues);
}
@Override
public boolean isParam(Class classType) {
return classType == Boolean.class || classType == Boolean.TYPE;
}
@Override
public Object fromString(List<String> list) {
if (list.isEmpty()) {
return true;
}
String string = list.get(0);
if (string.equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("true"))) {
list.remove(0);
} else if (string.equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("false"))) {
list.remove(0);
return false;
}
return true;
}
@Override
protected Object fromString(String string) {
throw new IllegalStateException("This shouldn't be called");
}
@Override
public String toString(Object object) {
return object.toString();
}
@Override
public int getMinArguments() {
return 0;
}
/**
* Is the values it returns all it can do?
*/
@Override
public boolean isCustomValues() {
return false;
}
}

View File

@@ -0,0 +1,27 @@
package me.libraryaddict.disguise.utilities.params.types.base;
import me.libraryaddict.disguise.utilities.params.ParamInfo;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoDouble extends ParamInfo {
public ParamInfoDouble(String name, String description) {
super(null, name, description);
}
@Override
public boolean isParam(Class classType) {
return classType == Double.class || classType == Double.TYPE;
}
@Override
protected Object fromString(String string) {
return Double.parseDouble(string);
}
@Override
public String toString(Object object) {
return object.toString();
}
}

View File

@@ -0,0 +1,27 @@
package me.libraryaddict.disguise.utilities.params.types.base;
import me.libraryaddict.disguise.utilities.params.ParamInfo;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoFloat extends ParamInfo {
public ParamInfoFloat(String name, String description) {
super(Number.class, name, description);
}
@Override
public boolean isParam(Class classType) {
return classType == Float.class || classType == Float.TYPE;
}
@Override
protected Object fromString(String string) {
return Float.parseFloat(string);
}
@Override
public String toString(Object object) {
return object.toString();
}
}

View File

@@ -0,0 +1,27 @@
package me.libraryaddict.disguise.utilities.params.types.base;
import me.libraryaddict.disguise.utilities.params.ParamInfo;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoInteger extends ParamInfo {
public ParamInfoInteger(String name, String description) {
super(null, name, description);
}
@Override
public boolean isParam(Class classType) {
return classType == Integer.class || classType == Integer.TYPE;
}
@Override
protected Object fromString(String string) {
return Integer.parseInt(string);
}
@Override
public String toString(Object object) {
return object.toString();
}
}

View File

@@ -0,0 +1,23 @@
package me.libraryaddict.disguise.utilities.params.types.base;
import me.libraryaddict.disguise.utilities.params.ParamInfo;
import org.bukkit.ChatColor;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoString extends ParamInfo {
public ParamInfoString(Class paramClass, String name, String description) {
super(paramClass, name, description);
}
@Override
protected Object fromString(String string) {
return ChatColor.translateAlternateColorCodes('&', string);
}
@Override
public String toString(Object object) {
return ((String) object).replace(ChatColor.COLOR_CHAR + "", "&");
}
}

View File

@@ -0,0 +1,31 @@
package me.libraryaddict.disguise.utilities.params.types.custom;
import com.comphenix.protocol.wrappers.BlockPosition;
import me.libraryaddict.disguise.utilities.params.ParamInfo;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoBlockPosition extends ParamInfo {
public ParamInfoBlockPosition(Class paramClass, String name, String valueType, String description) {
super(paramClass, name, valueType, description);
}
@Override
protected Object fromString(String string) {
String[] split = string.split(",");
if (split.length != 3) {
return null;
}
return new BlockPosition(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
}
@Override
public String toString(Object object) {
BlockPosition position = (BlockPosition) object;
return String.format("%s,%s,%s", position.getX(), position.getY(), position.getZ());
}
}

View File

@@ -0,0 +1,71 @@
package me.libraryaddict.disguise.utilities.params.types.custom;
import me.libraryaddict.disguise.utilities.params.types.ParamInfoEnum;
import org.bukkit.Color;
import java.util.Map;
/**
* Created by libraryaddict on 19/09/2018.
*/
public class ParamInfoColor extends ParamInfoEnum {
private static Map<String, Color> staticColors;
public ParamInfoColor(Class paramClass, String name, String description, Map possibleValues) {
super(paramClass, name, description, possibleValues);
staticColors = (Map<String, Color>) possibleValues;
}
protected Color parseToColor(String string) {
string = string.replace("_", "");
for (Map.Entry<String, Color> entry : staticColors.entrySet()) {
if (!entry.getKey().replace("_", "").equalsIgnoreCase(string)) {
continue;
}
return entry.getValue();
}
String[] split = string.split(",");
if (split.length == 1) {
return Color.fromRGB(Integer.parseInt(split[0]));
} else if (split.length == 3) {
return Color.fromRGB(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
}
return null;
}
@Override
public String toString(Object object) {
Color color = (Color) object;
if (staticColors.containsValue(color)) {
for (String key : staticColors.keySet()) {
if (staticColors.get(key) != color) {
continue;
}
return key;
}
}
return String.format("%s,%s,%s", color.getRed(), color.getGreen(), color.getBlue());
}
@Override
protected Object fromString(String string) {
return parseToColor(string);
}
/**
* Is the values it returns all it can do?
*/
@Override
public boolean isCustomValues() {
return true;
}
}

View File

@@ -0,0 +1,31 @@
package me.libraryaddict.disguise.utilities.params.types.custom;
import me.libraryaddict.disguise.utilities.params.ParamInfo;
import org.bukkit.util.EulerAngle;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoEulerAngle extends ParamInfo {
public ParamInfoEulerAngle(Class paramClass, String name, String valueType, String description) {
super(paramClass, name, valueType, description);
}
@Override
protected Object fromString(String string) {
String[] split = string.split(",");
if (split.length != 3) {
return null;
}
return new EulerAngle(Double.parseDouble(split[0]), Double.parseDouble(split[1]), Double.parseDouble(split[2]));
}
@Override
public String toString(Object object) {
EulerAngle angle = (EulerAngle) object;
return String.format("%s,%s,%s", angle.getX(), angle.getY(), angle.getZ());
}
}

View File

@@ -0,0 +1,25 @@
package me.libraryaddict.disguise.utilities.params.types.custom;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.mojang.authlib.GameProfile;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.params.ParamInfo;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoGameProfile extends ParamInfo {
public ParamInfoGameProfile(Class paramClass, String name, String description) {
super(paramClass, name, description);
}
@Override
protected Object fromString(String string) {
return DisguiseUtilities.getGson().fromJson(string, WrappedGameProfile.class);
}
@Override
public String toString(Object object) {
return DisguiseUtilities.getGson().toJson(((WrappedGameProfile) object).getHandle(), GameProfile.class);
}
}

View File

@@ -0,0 +1,60 @@
package me.libraryaddict.disguise.utilities.params.types.custom;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
/**
* Created by libraryaddict on 16/02/2020.
*/
public class ParamInfoItemBlock extends ParamInfoItemStack {
public ParamInfoItemBlock(Class paramClass, String name, String valueType, String description,
Material[] possibleValues) {
super(paramClass, name, valueType, description,
Arrays.stream(possibleValues).filter(Material::isBlock).toArray(Material[]::new));
}
@Override
public Object fromString(String string) {
String[] split = string.split("[ -]", -1);
if (split.length > (NmsVersion.v1_13.isSupported() ? 1 : 3)) {
throw new IllegalArgumentException();
}
Material material = ReflectionManager.getMaterial(split[0].toLowerCase());
if (material == null) {
material = Material.getMaterial(split[0].toUpperCase());
}
if (material == null) {
throw new IllegalArgumentException();
}
ItemStack itemStack;
if (!NmsVersion.v1_13.isSupported() && split.length > 1 && split[split.length - 1].matches("[0-9]+")) {
itemStack = new ItemStack(material, 1, Short.parseShort(split[split.length - 1]));
} else {
itemStack = new ItemStack(material, 1);
}
if (!itemStack.getType().isBlock()) {
throw new IllegalArgumentException();
}
return itemStack;
}
/**
* Is the values it returns all it can do?
*/
@Override
public boolean isCustomValues() {
return false;
}
}

View File

@@ -0,0 +1,198 @@
package me.libraryaddict.disguise.utilities.params.types.custom;
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.params.types.ParamInfoEnum;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import me.libraryaddict.disguise.utilities.translations.TranslateType;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoItemStack extends ParamInfoEnum {
public ParamInfoItemStack(Class paramClass, String name, String valueType, String description,
Enum[] possibleValues) {
super(paramClass, name, valueType, description, possibleValues);
setOtherValues("null", "glow");
}
@Override
public boolean canTranslateValues() {
return false;
}
@Override
public boolean canReturnNull() {
return true;
}
@Override
public Object fromString(String string) {
return parseToItemstack(string);
}
@Override
public String toString(Object object) {
ItemStack item = (ItemStack) object;
ItemStack temp = new ItemStack(item.getType(), item.getAmount());
if (item.containsEnchantment(Enchantment.DURABILITY)) {
temp.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
}
if (temp.isSimilar(item)) {
String name = item.getType().name();
if (item.getAmount() != 1) {
name += ":" + item.getAmount();
}
if (item.containsEnchantment(Enchantment.DURABILITY)) {
name += ":" + TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("glow");
}
return name;
}
String itemName = ReflectionManager.getItemName(item.getType());
ArrayList<String> mcArray = new ArrayList<>();
if (NmsVersion.v1_13.isSupported() && item.hasItemMeta()) {
mcArray.add(itemName + DisguiseUtilities.serialize(NbtFactory.fromItemTag(item)));
} else {
mcArray.add(itemName);
}
if (item.getAmount() != 1) {
mcArray.add(String.valueOf(item.getAmount()));
}
if (!NmsVersion.v1_13.isSupported()) {
if (item.getDurability() != 0) {
mcArray.add(String.valueOf(item.getDurability()));
}
if (item.hasItemMeta()) {
mcArray.add(DisguiseUtilities.serialize(NbtFactory.fromItemTag(item)));
}
}
return StringUtils.join(mcArray, "-");
}
protected static ItemStack parseToItemstack(String string) {
if (string.startsWith("{") && string.endsWith("}")) {
try {
return DisguiseUtilities.getGson().fromJson(string, ItemStack.class);
}
catch (Exception ex) {
throw new IllegalArgumentException();
}
} else if (!string.matches("[a-zA-Z0-9_:,]+")) { // If it can't be simple parsed due to invalid chars
String[] split;
// If it matches /give @p stone {data}
if (string.matches("[^{]+?[ -]\\{.+?}")) {
split = string.substring(0, string.indexOf("{") - 1).split("[ -]");
split = Arrays.copyOf(split, split.length + 1);
split[split.length - 1] = string.substring(string.indexOf("{"));
} else if (string.matches("[^{ ]+?\\{.+?}( [0-9]+?)")) { // /give @p stone[data] <amount?>
split = new String[string.endsWith("}") ? 2 : 3];
split[0] = string.substring(0, string.indexOf("{"));
split[string.endsWith("}") ? 2 : 1] = string
.substring(string.indexOf("{"), string.lastIndexOf("}") + 1);
if (!string.endsWith("}")) {
split[1] = string.substring(string.lastIndexOf(" ") + 1);
}
} else {
split = string.split("[ -]");
}
Material material = ReflectionManager.getMaterial(split[0].toLowerCase());
if (material == null) {
material = Material.getMaterial(split[0].toUpperCase());
}
if (material == null) {
throw new IllegalArgumentException();
}
int amount = split.length > 1 && split[1].matches("[0-9]+") ? Integer.parseInt(split[1]) : 1;
ItemStack itemStack;
if (!NmsVersion.v1_13.isSupported() && split.length > 2 && split[2].matches("[0-9]+")) {
itemStack = new ItemStack(material, amount, Short.parseShort(split[2]));
} else {
itemStack = new ItemStack(material, amount);
}
if (split[split.length - 1].contains("{")) {
Bukkit.getUnsafe().modifyItemStack(itemStack, split[split.length - 1]);
}
return itemStack;
}
return parseToItemstack(string.split("[:,]")); // Split on colon or comma
}
protected static ItemStack parseToItemstack(String[] split) {
if (split[0].isEmpty() || split[0].equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("null"))) {
return null;
}
Material material = Material.getMaterial(split[0].toUpperCase());
if (material == null) {
throw new IllegalArgumentException();
}
Integer amount = null;
boolean enchanted = false;
for (int i = 1; i < split.length; i++) {
String s = split[i];
if (!enchanted && s.equalsIgnoreCase(TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("glow"))) {
enchanted = true;
} else if (s.matches("\\d+") && amount == null) {
amount = Integer.parseInt(s);
} else {
throw new IllegalArgumentException();
}
}
ItemStack itemStack = new ItemStack(material, amount == null ? 1 : amount);
if (enchanted) {
itemStack.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
}
return itemStack;
}
public boolean isParam(Class paramClass) {
return getParamClass().isAssignableFrom(paramClass);
}
/**
* Is the values it returns all it can do?
*/
@Override
public boolean isCustomValues() {
return true;
}
}

View File

@@ -0,0 +1,101 @@
package me.libraryaddict.disguise.utilities.params.types.custom;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import org.bukkit.inventory.ItemStack;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Created by libraryaddict on 7/09/2018.
*/
public class ParamInfoItemStackArray extends ParamInfoItemStack {
public ParamInfoItemStackArray(Class paramClass, String name, String valueType, String description,
Enum[] possibleValues) {
super(paramClass, name, valueType, description, possibleValues);
}
@Override
public boolean canReturnNull() {
return false;
}
@Override
public Set<String> getEnums(String tabComplete) {
String beginning = tabComplete.substring(0, tabComplete.contains(",") ? tabComplete.lastIndexOf(",") + 1 : 0);
String end = tabComplete.substring(tabComplete.contains(",") ? tabComplete.lastIndexOf(",") + 1 : 0);
Set<String> toReturn = new LinkedHashSet<>();
for (String material : super.getEnums(null)) {
if (!material.toLowerCase().startsWith(end.toLowerCase()))
continue;
toReturn.add(beginning + material);
}
return toReturn;
}
@Override
public String toString(Object object) {
ItemStack[] stacks = (ItemStack[]) object;
String returns = "";
for (int i = 0; i < stacks.length; i++) {
if (i > 0) {
returns += ",";
}
if (stacks[i] == null) {
continue;
}
String toString = super.toString(stacks[i]);
// If we can't parse to simple
if (toString.startsWith("{")) {
return DisguiseUtilities.getGson().toJson(object);
}
returns += toString;
}
return returns;
}
@Override
public Object fromString(String string) {
if (string.startsWith("[") && string.endsWith("]")) {
try {
return DisguiseUtilities.getGson().fromJson(string, ItemStack[].class);
}
catch (Exception ex) {
}
}
String[] split = string.split(",", -1);
if (split.length != 4) {
return null;
}
// Parse to itemstack array
ItemStack[] items = new ItemStack[4];
for (int a = 0; a < 4; a++) {
items[a] = parseToItemstack(split[a].split(":"));
}
return items;
}
/**
* Is the values it returns all it can do?
*/
@Override
public boolean isCustomValues() {
return true;
}
}

View File

@@ -0,0 +1,165 @@
package me.libraryaddict.disguise.utilities.params.types.custom;
import com.comphenix.protocol.wrappers.WrappedBlockData;
import com.comphenix.protocol.wrappers.WrappedParticle;
import me.libraryaddict.disguise.utilities.params.ParamInfoManager;
import me.libraryaddict.disguise.utilities.params.types.ParamInfoEnum;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* Created by libraryaddict on 19/09/2018.
*/
public class ParamInfoParticle extends ParamInfoEnum {
private Material[] materials;
public ParamInfoParticle(Class paramClass, String name, String description, Enum[] possibleValues,
Material[] materials) {
super(paramClass, name, description, possibleValues);
this.materials = materials;
}
public Set<String> getEnums(String tabComplete) {
Set<String> enums = getValues().keySet();
if (tabComplete.isEmpty()) {
return enums;
}
enums = new HashSet<>(enums);
tabComplete = tabComplete.toUpperCase();
for (Particle particle : new Particle[]{Particle.BLOCK_CRACK, Particle.BLOCK_DUST, Particle.ITEM_CRACK}) {
for (Material mat : materials) {
if (particle != Particle.ITEM_CRACK && !mat.isBlock()) {
continue;
}
String name = particle.name() + ":" + mat.name();
if (!name.startsWith(tabComplete)) {
continue;
}
enums.add(name);
}
}
return enums;
}
@Override
public String toString(Object object) {
WrappedParticle particle = (WrappedParticle) object;
Object data = particle.getData();
String returns = particle.getParticle().name();
if (data != null) {
if (data instanceof ItemStack) {
returns += "," + ((ItemStack) data).getType().name();
} else if (data instanceof WrappedBlockData) {
returns += "," + ((WrappedBlockData) data).getType().name();
} else if (data instanceof Particle.DustOptions) {
returns += "," +
ParamInfoManager.getParamInfo(Color.class).toString(((Particle.DustOptions) data).getColor());
if (((Particle.DustOptions) data).getSize() != 1f) {
returns += "," + ((Particle.DustOptions) data).getSize();
}
}
}
return returns;
}
@Override
public Object fromString(String string) throws DisguiseParseException {
String[] split = string.split("[:,]"); // Split on comma or colon
Particle particle = (Particle) super.fromString(split[0]);
if (particle == null) {
return null;
}
Object data = null;
switch (particle) {
case BLOCK_CRACK:
case BLOCK_DUST:
case FALLING_DUST:
Material material;
if (split.length != 2 || (material = Material.getMaterial(split[1])) == null || !material.isBlock()) {
throw new DisguiseParseException(LibsMsg.PARSE_PARTICLE_BLOCK, particle.name(), string);
}
data = WrappedBlockData.createData(material);
break;
case ITEM_CRACK:
if (split.length != 1) {
data = ParamInfoItemStack.parseToItemstack(Arrays.copyOfRange(split, 1, split.length));
}
if (data == null) {
throw new DisguiseParseException(LibsMsg.PARSE_PARTICLE_ITEM, particle.name(), string);
}
break;
case REDSTONE:
// If it can't be a RGB color or color name
// REDSTONE:BLUE - 2 args
// REDSTONE:BLUE,4 - 3 args
// REDSTONE:3,5,2 - 4 args
// REDSTONE:3,5,6,2 - 5 args
if (split.length < 2 || split.length > 5) {
throw new DisguiseParseException(LibsMsg.PARSE_PARTICLE_REDSTONE, particle.name(), string);
}
Color color = ((ParamInfoColor) ParamInfoManager.getParamInfo(Color.class)).parseToColor(
StringUtils.join(Arrays.copyOfRange(split, 1, split.length - (split.length % 2)), ","));
if (color == null) {
throw new DisguiseParseException(LibsMsg.PARSE_PARTICLE_REDSTONE, particle.name(), string);
}
float size;
if (split.length % 2 == 0) {
size = 1;
} else if (!split[split.length - 1].matches("[0-9.]+")) {
throw new DisguiseParseException(LibsMsg.PARSE_PARTICLE_REDSTONE, particle.name(), string);
} else {
size = Math.max(0.2f, Float.parseFloat(split[split.length - 1]));
}
data = new Particle.DustOptions(color, size);
break;
}
if (data == null && split.length > 1) {
return null;
}
return WrappedParticle.create(particle, data);
}
/**
* Is the values it returns all it can do?
*/
@Override
public boolean isCustomValues() {
return true;
}
}

View File

@@ -0,0 +1,25 @@
package me.libraryaddict.disguise.utilities.params.types.custom;
import me.libraryaddict.disguise.utilities.params.types.ParamInfoEnum;
import org.bukkit.potion.PotionEffectType;
import java.util.Map;
/**
* Created by libraryaddict on 16/02/2020.
*/
public class ParamInfoPotionEffect extends ParamInfoEnum {
public ParamInfoPotionEffect(Class paramClass, String name, String description,
Map<String, Object> possibleValues) {
super(paramClass, name, description, possibleValues);
}
public boolean isParam(Class paramClass) {
return PotionEffectType.class.isAssignableFrom(paramClass);
}
@Override
public String toString(Object object) {
return ((PotionEffectType) object).getName();
}
}

View File

@@ -0,0 +1,52 @@
package me.libraryaddict.disguise.utilities.params.types.custom;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.params.ParamInfo;
/**
* Created by libraryaddict on 6/03/2019.
*/
public class ParamInfoTime extends ParamInfo {
public ParamInfoTime(Class paramClass, String name, String description) {
super(paramClass, name, description);
}
@Override
public boolean isParam(Class classType) {
return classType == Long.class || classType == Long.TYPE;
}
@Override
protected Object fromString(String string) throws DisguiseParseException {
if (string.matches("[0-9]{13,}")) {
return Long.parseLong(string);
}
long time = DisguiseParser.parseStringToTime(string);
// If disguise expires X ticks afterwards
if (DisguiseConfig.isDynamicExpiry()) {
time *= 20;
} else if (!DisguiseConfig.isDynamicExpiry()) { // If disguise expires at a set time
time *= 1000; // Multiply for milliseconds
time += System.currentTimeMillis(); // Add current time to expiry time
}
return time;
}
@Override
public String toString(Object object) {
return object.toString();
}
/**
* Is the values it returns all it can do?
*/
@Override
public boolean isCustomValues() {
return true;
}
}