From 67b9d57ac2c14ffe16db10a4f98ba90c7bc9d932 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 10 Apr 2020 17:19:43 +1200 Subject: [PATCH] Improved tab complete for placeholders and ItemStack[], fixed items unable to parse empty string --- .../disguise/utilities/params/ParamInfo.java | 22 +++++++--- .../params/types/base/ParamInfoString.java | 2 + .../types/custom/ParamInfoGameProfile.java | 2 + .../types/custom/ParamInfoItemStack.java | 6 ++- .../types/custom/ParamInfoItemStackArray.java | 41 +++++++++++-------- .../translations/TranslateFiller.java | 4 +- 6 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfo.java b/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfo.java index c7f3e14f..ec914e89 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfo.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfo.java @@ -3,10 +3,7 @@ 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; +import java.util.*; /** * Created by libraryaddict on 7/09/2018. @@ -68,7 +65,15 @@ public abstract class ParamInfo { } public void setOtherValues(String... otherValues) { - this.otherValues = otherValues; + if (this.otherValues != null) { + this.otherValues = Arrays.copyOf(this.otherValues, this.otherValues.length + otherValues.length); + + for (int i = 0; i < otherValues.length; i++) { + this.otherValues[this.otherValues.length - (otherValues.length - i)] = otherValues[i]; + } + } else { + this.otherValues = otherValues; + } } public boolean canReturnNull() { @@ -140,6 +145,13 @@ public abstract class ParamInfo { } public Set getEnums(String tabComplete) { + if (getOtherValues() != null) { + HashSet set = new HashSet<>(getValues().keySet()); + set.addAll(Arrays.asList(getOtherValues())); + + return set; + } + return getValues().keySet(); } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoString.java b/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoString.java index b5464247..32fc9dc3 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoString.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoString.java @@ -9,6 +9,8 @@ import org.bukkit.ChatColor; public class ParamInfoString extends ParamInfo { public ParamInfoString(Class paramClass, String name, String description) { super(paramClass, name, description); + + setOtherValues("%user-name%", "%target-name%"); } @Override diff --git a/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoGameProfile.java b/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoGameProfile.java index 930712c4..f1713bb3 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoGameProfile.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoGameProfile.java @@ -11,6 +11,8 @@ import me.libraryaddict.disguise.utilities.params.ParamInfo; public class ParamInfoGameProfile extends ParamInfo { public ParamInfoGameProfile(Class paramClass, String name, String description) { super(paramClass, name, description); + + setOtherValues("%user-skin%", "%target-skin%"); } @Override diff --git a/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoItemStack.java b/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoItemStack.java index 1803ca10..15f4c722 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoItemStack.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoItemStack.java @@ -24,7 +24,7 @@ public class ParamInfoItemStack extends ParamInfoEnum { Enum[] possibleValues) { super(paramClass, name, valueType, description, possibleValues); - setOtherValues("null", "glow"); + setOtherValues("null", "%held-item%", "%offhand-item%", "%helmet%", "%chestplate%", "%leggings%", "%boots%"); } @Override @@ -97,7 +97,9 @@ public class ParamInfoItemStack extends ParamInfoEnum { } protected static ItemStack parseToItemstack(String string) { - if (string.startsWith("{") && string.endsWith("}")) { + if (string.isEmpty()) { + return null; + } else if (string.startsWith("{") && string.endsWith("}")) { try { return DisguiseUtilities.getGson().fromJson(string, ItemStack.class); } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoItemStackArray.java b/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoItemStackArray.java index ee1fff6f..18e57e94 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoItemStackArray.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/params/types/custom/ParamInfoItemStackArray.java @@ -1,8 +1,11 @@ package me.libraryaddict.disguise.utilities.params.types.custom; +import com.google.gson.Gson; import me.libraryaddict.disguise.utilities.DisguiseUtilities; +import org.apache.commons.lang.StringUtils; import org.bukkit.inventory.ItemStack; +import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.Set; @@ -13,6 +16,8 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack { public ParamInfoItemStackArray(Class paramClass, String name, String valueType, String description, Enum[] possibleValues) { super(paramClass, name, valueType, description, possibleValues); + + setOtherValues("%armor%"); } @Override @@ -22,16 +27,21 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack { @Override public Set getEnums(String tabComplete) { - String beginning = tabComplete.substring(0, tabComplete.contains(",") ? tabComplete.lastIndexOf(",") + 1 : 0); - String end = tabComplete.substring(tabComplete.contains(",") ? tabComplete.lastIndexOf(",") + 1 : 0); + ArrayList split = split(tabComplete); Set toReturn = new LinkedHashSet<>(); + if (split == null || split.stream().anyMatch(s -> s.equalsIgnoreCase("%armor%"))) { + return toReturn; + } + + String lastEntry = split.remove(split.size() - 1); + for (String material : super.getEnums(null)) { - if (!material.toLowerCase().startsWith(end.toLowerCase())) + if (!split.isEmpty() && !material.toLowerCase().startsWith(lastEntry.toLowerCase())) continue; - toReturn.add(beginning + material); + toReturn.add(StringUtils.join(split, ",") + (split.isEmpty() ? "" : ",") + material); } return toReturn; @@ -75,9 +85,9 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack { } } - String[] split = split(string); + ArrayList split = split(string); - if (split == null || split.length != 4) { + if (split == null || split.size() != 4) { return null; } @@ -85,30 +95,29 @@ 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.get(a)); } return items; } - private static String[] split(String string) { - String[] split = new String[4]; + private ArrayList split(String string) { + ArrayList split = new ArrayList<>(); char[] chars = string.toCharArray(); boolean quote = false; int depth = 0; - int splitNo = 0; StringBuilder builder = new StringBuilder(); for (int i = 0; i < chars.length; i++) { - if (splitNo > 3 || depth < 0) { + if (split.size() > 3 || depth < 0) { return null; } char c = chars[i]; if (!quote && depth == 0 && c == ',') { - split[splitNo++] = builder.toString(); + split.add(builder.toString()); builder = new StringBuilder(); continue; } @@ -122,9 +131,7 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack { if (c == '"') { quote = !quote; - } - - if (!quote) { + } else if (!quote) { if (c == '{' || c == '[') { depth++; } else if (c == '}' || c == ']') { @@ -133,11 +140,11 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack { } } - if (splitNo != 3 || quote || depth != 0) { + if (quote || depth != 0) { return null; } - split[splitNo] = builder.toString(); + split.add(builder.toString()); return split; } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/translations/TranslateFiller.java b/src/main/java/me/libraryaddict/disguise/utilities/translations/TranslateFiller.java index d209988e..32a030ef 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/translations/TranslateFiller.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/translations/TranslateFiller.java @@ -36,12 +36,12 @@ public class TranslateFiller { } } - if (info.getOtherValues() != null) { + /*if (info.getOtherValues() != null) { for (String e : info.getOtherValues()) { TranslateType.DISGUISE_OPTIONS_PARAMETERS .save(e, "Used for the disguise option " + info.getRawName()); } - } + }*/ } for (DisguiseType type : DisguiseType.values()) {