2020-02-19 00:57:39 +01:00
|
|
|
package me.libraryaddict.disguise.utilities.params.types.custom;
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2019-12-31 07:35:21 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
2020-04-10 07:19:43 +02:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
2018-09-07 04:35:38 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
2020-04-10 07:19:43 +02:00
|
|
|
import java.util.ArrayList;
|
2018-09-07 04:35:38 +02:00
|
|
|
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);
|
2020-04-10 07:19:43 +02:00
|
|
|
|
|
|
|
setOtherValues("%armor%");
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 00:20:21 +02:00
|
|
|
@Override
|
|
|
|
public boolean canReturnNull() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
@Override
|
|
|
|
public Set<String> getEnums(String tabComplete) {
|
2020-04-10 07:19:43 +02:00
|
|
|
ArrayList<String> split = split(tabComplete);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
|
|
|
Set<String> toReturn = new LinkedHashSet<>();
|
|
|
|
|
2020-04-10 07:19:43 +02:00
|
|
|
if (split == null || split.stream().anyMatch(s -> s.equalsIgnoreCase("%armor%"))) {
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
String lastEntry = split.remove(split.size() - 1);
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
for (String material : super.getEnums(null)) {
|
2020-04-10 07:19:43 +02:00
|
|
|
if (!split.isEmpty() && !material.toLowerCase().startsWith(lastEntry.toLowerCase()))
|
2018-09-07 04:35:38 +02:00
|
|
|
continue;
|
|
|
|
|
2020-04-10 07:19:43 +02:00
|
|
|
toReturn.add(StringUtils.join(split, ",") + (split.isEmpty() ? "" : ",") + material);
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:35:21 +01:00
|
|
|
@Override
|
|
|
|
public String toString(Object object) {
|
2020-01-02 05:10:36 +01:00
|
|
|
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;
|
2019-12-31 07:35:21 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 04:35:38 +02:00
|
|
|
@Override
|
|
|
|
public Object fromString(String string) {
|
2020-02-15 07:56:06 +01:00
|
|
|
if (string.startsWith("[") && string.endsWith("]")) {
|
2019-12-31 07:35:21 +01:00
|
|
|
try {
|
|
|
|
return DisguiseUtilities.getGson().fromJson(string, ItemStack[].class);
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 07:19:43 +02:00
|
|
|
ArrayList<String> split = split(string);
|
2018-09-07 04:35:38 +02:00
|
|
|
|
2020-04-10 07:19:43 +02:00
|
|
|
if (split == null || split.size() != 4) {
|
2018-09-07 04:35:38 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse to itemstack array
|
|
|
|
ItemStack[] items = new ItemStack[4];
|
|
|
|
|
|
|
|
for (int a = 0; a < 4; a++) {
|
2020-04-10 07:19:43 +02:00
|
|
|
items[a] = parseToItemstack(split.get(a));
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|
2020-02-19 00:57:39 +01:00
|
|
|
|
2020-04-10 07:19:43 +02:00
|
|
|
private ArrayList<String> split(String string) {
|
|
|
|
ArrayList<String> split = new ArrayList<>();
|
2020-03-23 00:09:30 +01:00
|
|
|
|
|
|
|
char[] chars = string.toCharArray();
|
|
|
|
boolean quote = false;
|
|
|
|
int depth = 0;
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
|
|
|
for (int i = 0; i < chars.length; i++) {
|
2020-04-10 07:19:43 +02:00
|
|
|
if (split.size() > 3 || depth < 0) {
|
2020-03-23 00:09:30 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
char c = chars[i];
|
|
|
|
|
|
|
|
if (!quote && depth == 0 && c == ',') {
|
2020-04-10 07:19:43 +02:00
|
|
|
split.add(builder.toString());
|
2020-03-23 00:09:30 +01:00
|
|
|
builder = new StringBuilder();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.append(c);
|
|
|
|
|
|
|
|
if (c == '\\' && i + 1 < chars.length) {
|
|
|
|
builder.append(chars[++i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '"') {
|
|
|
|
quote = !quote;
|
2020-04-10 07:19:43 +02:00
|
|
|
} else if (!quote) {
|
2020-03-23 00:09:30 +01:00
|
|
|
if (c == '{' || c == '[') {
|
|
|
|
depth++;
|
|
|
|
} else if (c == '}' || c == ']') {
|
|
|
|
depth--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 07:19:43 +02:00
|
|
|
if (quote || depth != 0) {
|
2020-03-23 00:09:30 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-10 07:19:43 +02:00
|
|
|
split.add(builder.toString());
|
2020-03-23 00:09:30 +01:00
|
|
|
|
|
|
|
return split;
|
|
|
|
}
|
|
|
|
|
2020-02-19 00:57:39 +01:00
|
|
|
/**
|
|
|
|
* Is the values it returns all it can do?
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean isCustomValues() {
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-07 04:35:38 +02:00
|
|
|
}
|