Improved tab complete for placeholders and ItemStack[], fixed items unable to parse empty string
This commit is contained in:
parent
64641c5390
commit
67b9d57ac2
@ -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<String> getEnums(String tabComplete) {
|
||||
if (getOtherValues() != null) {
|
||||
HashSet<String> set = new HashSet<>(getValues().keySet());
|
||||
set.addAll(Arrays.asList(getOtherValues()));
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
return getValues().keySet();
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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<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);
|
||||
ArrayList<String> split = split(tabComplete);
|
||||
|
||||
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)) {
|
||||
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<String> 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<String> split(String string) {
|
||||
ArrayList<String> 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;
|
||||
}
|
||||
|
@ -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()) {
|
||||
|
Loading…
Reference in New Issue
Block a user