Improved tab complete for placeholders and ItemStack[], fixed items unable to parse empty string

This commit is contained in:
libraryaddict 2020-04-10 17:19:43 +12:00
parent 64641c5390
commit 67b9d57ac2
No known key found for this signature in database
GPG Key ID: 052E4FBCD257AEA4
6 changed files with 51 additions and 26 deletions

View File

@ -3,10 +3,7 @@ package me.libraryaddict.disguise.utilities.params;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.translations.TranslateType; import me.libraryaddict.disguise.utilities.translations.TranslateType;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* Created by libraryaddict on 7/09/2018. * Created by libraryaddict on 7/09/2018.
@ -68,8 +65,16 @@ public abstract class ParamInfo {
} }
public void setOtherValues(String... otherValues) { public void setOtherValues(String... 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; this.otherValues = otherValues;
} }
}
public boolean canReturnNull() { public boolean canReturnNull() {
return false; return false;
@ -140,6 +145,13 @@ public abstract class ParamInfo {
} }
public Set<String> getEnums(String tabComplete) { public Set<String> getEnums(String tabComplete) {
if (getOtherValues() != null) {
HashSet<String> set = new HashSet<>(getValues().keySet());
set.addAll(Arrays.asList(getOtherValues()));
return set;
}
return getValues().keySet(); return getValues().keySet();
} }

View File

@ -9,6 +9,8 @@ import org.bukkit.ChatColor;
public class ParamInfoString extends ParamInfo { public class ParamInfoString extends ParamInfo {
public ParamInfoString(Class paramClass, String name, String description) { public ParamInfoString(Class paramClass, String name, String description) {
super(paramClass, name, description); super(paramClass, name, description);
setOtherValues("%user-name%", "%target-name%");
} }
@Override @Override

View File

@ -11,6 +11,8 @@ import me.libraryaddict.disguise.utilities.params.ParamInfo;
public class ParamInfoGameProfile extends ParamInfo { public class ParamInfoGameProfile extends ParamInfo {
public ParamInfoGameProfile(Class paramClass, String name, String description) { public ParamInfoGameProfile(Class paramClass, String name, String description) {
super(paramClass, name, description); super(paramClass, name, description);
setOtherValues("%user-skin%", "%target-skin%");
} }
@Override @Override

View File

@ -24,7 +24,7 @@ public class ParamInfoItemStack extends ParamInfoEnum {
Enum[] possibleValues) { Enum[] possibleValues) {
super(paramClass, name, valueType, description, possibleValues); super(paramClass, name, valueType, description, possibleValues);
setOtherValues("null", "glow"); setOtherValues("null", "%held-item%", "%offhand-item%", "%helmet%", "%chestplate%", "%leggings%", "%boots%");
} }
@Override @Override
@ -97,7 +97,9 @@ public class ParamInfoItemStack extends ParamInfoEnum {
} }
protected static ItemStack parseToItemstack(String string) { protected static ItemStack parseToItemstack(String string) {
if (string.startsWith("{") && string.endsWith("}")) { if (string.isEmpty()) {
return null;
} else if (string.startsWith("{") && string.endsWith("}")) {
try { try {
return DisguiseUtilities.getGson().fromJson(string, ItemStack.class); return DisguiseUtilities.getGson().fromJson(string, ItemStack.class);
} }

View File

@ -1,8 +1,11 @@
package me.libraryaddict.disguise.utilities.params.types.custom; package me.libraryaddict.disguise.utilities.params.types.custom;
import com.google.gson.Gson;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import org.apache.commons.lang.StringUtils;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -13,6 +16,8 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack {
public ParamInfoItemStackArray(Class paramClass, String name, String valueType, String description, public ParamInfoItemStackArray(Class paramClass, String name, String valueType, String description,
Enum[] possibleValues) { Enum[] possibleValues) {
super(paramClass, name, valueType, description, possibleValues); super(paramClass, name, valueType, description, possibleValues);
setOtherValues("%armor%");
} }
@Override @Override
@ -22,16 +27,21 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack {
@Override @Override
public Set<String> getEnums(String tabComplete) { public Set<String> getEnums(String tabComplete) {
String beginning = tabComplete.substring(0, tabComplete.contains(",") ? tabComplete.lastIndexOf(",") + 1 : 0); ArrayList<String> split = split(tabComplete);
String end = tabComplete.substring(tabComplete.contains(",") ? tabComplete.lastIndexOf(",") + 1 : 0);
Set<String> toReturn = new LinkedHashSet<>(); Set<String> 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)) { for (String material : super.getEnums(null)) {
if (!material.toLowerCase().startsWith(end.toLowerCase())) if (!split.isEmpty() && !material.toLowerCase().startsWith(lastEntry.toLowerCase()))
continue; continue;
toReturn.add(beginning + material); toReturn.add(StringUtils.join(split, ",") + (split.isEmpty() ? "" : ",") + material);
} }
return toReturn; return toReturn;
@ -75,9 +85,9 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack {
} }
} }
String[] split = split(string); ArrayList<String> split = split(string);
if (split == null || split.length != 4) { if (split == null || split.size() != 4) {
return null; return null;
} }
@ -85,30 +95,29 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack {
ItemStack[] items = new ItemStack[4]; ItemStack[] items = new ItemStack[4];
for (int a = 0; a < 4; a++) { for (int a = 0; a < 4; a++) {
items[a] = parseToItemstack(split[a]); items[a] = parseToItemstack(split.get(a));
} }
return items; return items;
} }
private static String[] split(String string) { private ArrayList<String> split(String string) {
String[] split = new String[4]; ArrayList<String> split = new ArrayList<>();
char[] chars = string.toCharArray(); char[] chars = string.toCharArray();
boolean quote = false; boolean quote = false;
int depth = 0; int depth = 0;
int splitNo = 0;
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (int i = 0; i < chars.length; i++) { for (int i = 0; i < chars.length; i++) {
if (splitNo > 3 || depth < 0) { if (split.size() > 3 || depth < 0) {
return null; return null;
} }
char c = chars[i]; char c = chars[i];
if (!quote && depth == 0 && c == ',') { if (!quote && depth == 0 && c == ',') {
split[splitNo++] = builder.toString(); split.add(builder.toString());
builder = new StringBuilder(); builder = new StringBuilder();
continue; continue;
} }
@ -122,9 +131,7 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack {
if (c == '"') { if (c == '"') {
quote = !quote; quote = !quote;
} } else if (!quote) {
if (!quote) {
if (c == '{' || c == '[') { if (c == '{' || c == '[') {
depth++; depth++;
} else if (c == '}' || c == ']') { } 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; return null;
} }
split[splitNo] = builder.toString(); split.add(builder.toString());
return split; return split;
} }

View File

@ -36,12 +36,12 @@ public class TranslateFiller {
} }
} }
if (info.getOtherValues() != null) { /*if (info.getOtherValues() != null) {
for (String e : info.getOtherValues()) { for (String e : info.getOtherValues()) {
TranslateType.DISGUISE_OPTIONS_PARAMETERS TranslateType.DISGUISE_OPTIONS_PARAMETERS
.save(e, "Used for the disguise option " + info.getRawName()); .save(e, "Used for the disguise option " + info.getRawName());
} }
} }*/
} }
for (DisguiseType type : DisguiseType.values()) { for (DisguiseType type : DisguiseType.values()) {