From 14ca1b1e3648bdb65cbbccf71f1b34171792e950 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 23 Sep 2018 11:02:53 +1200 Subject: [PATCH] Disguise params extra information --- .../disguise/disguisetypes/MetaIndex.java | 11 +- .../watchers/AreaEffectCloudWatcher.java | 17 ++- .../watchers/FallingBlockWatcher.java | 15 +- .../disguise/utilities/LibsMsg.java | 7 + .../utilities/parser/DisguiseParser.java | 8 +- .../utilities/parser/params/ParamInfo.java | 5 +- .../parser/params/ParamInfoTypes.java | 12 +- .../parser/params/types/ParamInfoEnum.java | 3 +- .../params/types/custom/ParamInfoColor.java | 22 ++- .../types/custom/ParamInfoItemStack.java | 6 +- .../types/custom/ParamInfoItemStackArray.java | 2 +- .../types/custom/ParamInfoParticle.java | 129 ++++++++++++++++++ 12 files changed, 204 insertions(+), 33 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoParticle.java diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/MetaIndex.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/MetaIndex.java index a4e1c965..fe68526c 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/MetaIndex.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/MetaIndex.java @@ -1,10 +1,7 @@ package me.libraryaddict.disguise.disguisetypes; -import com.comphenix.protocol.wrappers.BlockPosition; +import com.comphenix.protocol.wrappers.*; import com.comphenix.protocol.wrappers.EnumWrappers.Direction; -import com.comphenix.protocol.wrappers.Vector3F; -import com.comphenix.protocol.wrappers.WrappedBlockData; -import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.nbt.NbtBase; import com.comphenix.protocol.wrappers.nbt.NbtFactory; import com.comphenix.protocol.wrappers.nbt.NbtType; @@ -42,8 +39,8 @@ public class MetaIndex { /** * The type of particle to display */ - public static MetaIndex AREA_EFFECT_PARTICLE = new MetaIndex<>(AreaEffectCloudWatcher.class, 3, - Particle.SPELL_MOB); + public static MetaIndex AREA_EFFECT_PARTICLE = new MetaIndex<>(AreaEffectCloudWatcher.class, 3, + WrappedParticle.create(Particle.SPELL_MOB, null)); /** * The size of the area @@ -607,7 +604,7 @@ public class MetaIndex { /** * @param watcherClass - A FlagWatcher class - * @param flagNo - The meta index number + * @param flagNo - The meta index number * @return The MetaIndex which corresponds to that FlagWatcher at that index */ public static MetaIndex getMetaIndex(Class watcherClass, int flagNo) { diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/AreaEffectCloudWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/AreaEffectCloudWatcher.java index 76742ff2..e1ec6392 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/AreaEffectCloudWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/AreaEffectCloudWatcher.java @@ -1,5 +1,6 @@ package me.libraryaddict.disguise.disguisetypes.watchers; +import com.comphenix.protocol.wrappers.WrappedParticle; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.disguisetypes.MetaIndex; @@ -49,12 +50,24 @@ public class AreaEffectCloudWatcher extends FlagWatcher { sendData(MetaIndex.AREA_EFFECT_IGNORE_RADIUS); } - public void setParticleType(Particle particle) { + public void setParticle(WrappedParticle particle) { setData(MetaIndex.AREA_EFFECT_PARTICLE, particle); sendData(MetaIndex.AREA_EFFECT_PARTICLE); } - public Particle getParticleType() { + public void setParticle(Particle particle) { + setParticle(WrappedParticle.create(particle, null)); + } + + public void setParticle(Particle particle, T particleData) { + setParticle(WrappedParticle.create(particle, particleData)); + } + + public WrappedParticle getParticle() { return getData(MetaIndex.AREA_EFFECT_PARTICLE); } + + public Particle getParticleType() { + return getParticle().getParticle(); + } } diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/FallingBlockWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/FallingBlockWatcher.java index 0f31630b..7459ab92 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/FallingBlockWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/FallingBlockWatcher.java @@ -1,15 +1,14 @@ package me.libraryaddict.disguise.disguisetypes.watchers; -import org.bukkit.Material; -import org.bukkit.inventory.ItemStack; - import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.utilities.DisguiseUtilities; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; public class FallingBlockWatcher extends FlagWatcher { - private ItemStack block; + private ItemStack block = new ItemStack(Material.STONE); public FallingBlockWatcher(Disguise disguise) { super(disguise); @@ -18,7 +17,7 @@ public class FallingBlockWatcher extends FlagWatcher { @Override public FallingBlockWatcher clone(Disguise disguise) { FallingBlockWatcher watcher = (FallingBlockWatcher) super.clone(disguise); - watcher.setBlock(getBlock()); + watcher.setBlock(getBlock().clone()); return watcher; } @@ -28,12 +27,12 @@ public class FallingBlockWatcher extends FlagWatcher { } public void setBlock(ItemStack block) { - this.block = block; - if (block == null || block.getType() == null || block.getType() == Material.AIR) { - block.setType(Material.STONE); + block = new ItemStack(Material.STONE); } + this.block = block; + if (DisguiseAPI.isDisguiseInUse(getDisguise()) && getDisguise().getWatcher() == this) { DisguiseUtilities.refreshTrackers(getDisguise()); } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/LibsMsg.java b/src/main/java/me/libraryaddict/disguise/utilities/LibsMsg.java index 58e318e7..9efcf5d3 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/LibsMsg.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/LibsMsg.java @@ -137,6 +137,13 @@ public enum LibsMsg { PARSE_EXPECTED_RECEIVED( ChatColor.RED + "Expected " + ChatColor.GREEN + "%s" + ChatColor.RED + ", received " + ChatColor.GREEN + "%s" + ChatColor.RED + " instead for " + ChatColor.GREEN + "%s"), + PARSE_PARTICLE_BLOCK(ChatColor.RED + "Expected " + ChatColor.GREEN + "%s:Material" + ChatColor.RED + ", received " + + ChatColor.GREEN + "%s" + ChatColor.RED + " instead"), + PARSE_PARTICLE_ITEM(ChatColor.RED + "Expected " + ChatColor.GREEN + "%s:Material,Amount?,Glow?" + ChatColor.RED + + ", received " + ChatColor.GREEN + "%s" + ChatColor.RED + " instead"), + PARSE_PARTICLE_REDSTONE( + ChatColor.RED + "Expected " + ChatColor.GREEN + "%s:Color,Size.0?" + ChatColor.RED + ", received " + + ChatColor.GREEN + "%s" + ChatColor.RED + " instead"), PARSE_NO_ARGS("No arguments defined"), PARSE_NO_OPTION_VALUE(ChatColor.RED + "No value was given for the option %s"), PARSE_NO_PERM_NAME(ChatColor.RED + "Error! You don't have permission to use that name!"), diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java index ea6e451d..529112d1 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java @@ -615,8 +615,9 @@ public class DisguiseParser { } // Construct the disguise - if (disguisePerm.getType() == DisguiseType.DROPPED_ITEM) { - disguise = new MiscDisguise(itemStack); + if (disguisePerm.getType() == DisguiseType.DROPPED_ITEM || + disguisePerm.getType() == DisguiseType.FALLING_BLOCK) { + disguise = new MiscDisguise(disguisePerm.getType(), itemStack); } else { disguise = new MiscDisguise(disguisePerm.getType(), miscId, miscData); } @@ -682,6 +683,9 @@ public class DisguiseParser { // We've found a method which will accept a valid value, break break; } + catch (DisguiseParseException ex) { + parseException = ex; + } catch (Exception ignored) { parseException = new DisguiseParseException(LibsMsg.PARSE_EXPECTED_RECEIVED, paramInfo.getDescriptiveName(), list.isEmpty() ? null : list.get(0), diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfo.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfo.java index c6c1e4d0..8323fad3 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfo.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfo.java @@ -1,6 +1,7 @@ package me.libraryaddict.disguise.utilities.parser.params; import me.libraryaddict.disguise.utilities.TranslateType; +import me.libraryaddict.disguise.utilities.parser.DisguiseParseException; import java.util.HashMap; import java.util.List; @@ -74,9 +75,9 @@ public abstract class ParamInfo { return false; } - protected abstract Object fromString(String string); + protected abstract Object fromString(String string) throws DisguiseParseException; - public Object fromString(List arguments) { + public Object fromString(List arguments) throws DisguiseParseException { // Don't consume a string immediately, if it errors we need to check other param types String string = arguments.get(0); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoTypes.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoTypes.java index 7e786d0a..142a01e3 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoTypes.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/ParamInfoTypes.java @@ -2,6 +2,7 @@ package me.libraryaddict.disguise.utilities.parser.params; import com.comphenix.protocol.wrappers.BlockPosition; import com.comphenix.protocol.wrappers.WrappedGameProfile; +import com.comphenix.protocol.wrappers.WrappedParticle; import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.RabbitType; import me.libraryaddict.disguise.utilities.parser.params.types.ParamInfoEnum; @@ -54,7 +55,8 @@ public class ParamInfoTypes { "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")); - paramInfos.add(new ParamInfoEnum(Particle.class, "Particle", "The different particles of Minecraft")); + 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")); paramInfos.add(new ParamInfoEnum(DyeColor.class, "DyeColor", "Dye colors of many different colors")); paramInfos.add(new ParamInfoEnum(Horse.Style.class, "Horse Style", @@ -67,8 +69,8 @@ public class ParamInfoTypes { 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 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())); @@ -121,6 +123,10 @@ public class ParamInfoTypes { List list = new ArrayList<>(); for (Material material : Material.values()) { + if (material == Material.AIR || material == Material.CAVE_AIR || material == Material.VOID_AIR) { + continue; + } + try { Field field = Material.class.getField(material.name()); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/ParamInfoEnum.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/ParamInfoEnum.java index 60451323..526dfc45 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/ParamInfoEnum.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/ParamInfoEnum.java @@ -1,5 +1,6 @@ package me.libraryaddict.disguise.utilities.parser.params.types; +import me.libraryaddict.disguise.utilities.parser.DisguiseParseException; import me.libraryaddict.disguise.utilities.parser.params.ParamInfo; import java.util.Map; @@ -25,7 +26,7 @@ public class ParamInfoEnum extends ParamInfo { } @Override - protected Object fromString(String string) { + protected Object fromString(String string) throws DisguiseParseException { string = string.replace("_", ""); for (Map.Entry entry : getValues().entrySet()) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoColor.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoColor.java index 8f4f962b..48112ac0 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoColor.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoColor.java @@ -9,16 +9,23 @@ import java.util.Map; * Created by libraryaddict on 19/09/2018. */ public class ParamInfoColor extends ParamInfoEnum { + private static Map staticColors; + public ParamInfoColor(Class paramClass, String name, String description, Map possibleValues) { super(paramClass, name, description, possibleValues); + + staticColors = possibleValues; } - @Override - protected Object fromString(String string) { - Object enumValue = super.fromString(string); + protected static Color parseToColor(String string) { + string = string.replace("_", ""); - if (enumValue != null) { - return enumValue; + for (Map.Entry entry : staticColors.entrySet()) { + if (!entry.getKey().replace("_", "").equalsIgnoreCase(string)) { + continue; + } + + return (Color) entry.getValue(); } String[] split = string.split(","); @@ -31,4 +38,9 @@ public class ParamInfoColor extends ParamInfoEnum { return null; } + + @Override + protected Object fromString(String string) { + return parseToColor(string); + } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoItemStack.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoItemStack.java index ffc7d3c5..0616f913 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoItemStack.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoItemStack.java @@ -32,9 +32,11 @@ public class ParamInfoItemStack extends ParamInfoEnum { return parseToItemstack(string); } - protected ItemStack parseToItemstack(String string) { - String[] split = string.split(":", -1); + protected static ItemStack parseToItemstack(String string) { + 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; } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoItemStackArray.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoItemStackArray.java index 265a46dd..c6c5f11d 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoItemStackArray.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoItemStackArray.java @@ -43,7 +43,7 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack { ItemStack[] items = new ItemStack[4]; for (int a = 0; a < 4; a++) { - items[a] = parseToItemstack(split[a]); + items[a] = parseToItemstack(split[a].split(":")); } return items; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoParticle.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoParticle.java new file mode 100644 index 00000000..d57fc26b --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/params/types/custom/ParamInfoParticle.java @@ -0,0 +1,129 @@ +package me.libraryaddict.disguise.utilities.parser.params.types.custom; + +import com.comphenix.protocol.wrappers.WrappedBlockData; +import com.comphenix.protocol.wrappers.WrappedParticle; +import me.libraryaddict.disguise.utilities.LibsMsg; +import me.libraryaddict.disguise.utilities.parser.DisguiseParseException; +import me.libraryaddict.disguise.utilities.parser.params.types.ParamInfoEnum; +import org.apache.commons.lang3.StringUtils; +import org.bukkit.Color; +import org.bukkit.Material; +import org.bukkit.Particle; + +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 getEnums(String tabComplete) { + Set 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 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.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); + } +}