From eaf4e7eeef9997fe2b42da7fa7831475570cf992 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Mon, 23 Mar 2020 12:09:30 +1300 Subject: [PATCH] Split itemstack[] by commas correctly, fixes #434 --- .../types/custom/ParamInfoItemStackArray.java | 56 ++++++++++++++++++- 1 file changed, 53 insertions(+), 3 deletions(-) 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 aced1d66..ee1fff6f 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 @@ -75,10 +75,9 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack { } } - // TODO Replace this with better - String[] split = string.split(",", -1); + String[] split = split(string); - if (split.length != 4) { + if (split == null || split.length != 4) { return null; } @@ -92,6 +91,57 @@ public class ParamInfoItemStackArray extends ParamInfoItemStack { return items; } + private static String[] split(String string) { + String[] split = new String[4]; + + 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) { + return null; + } + + char c = chars[i]; + + if (!quote && depth == 0 && c == ',') { + split[splitNo++] = builder.toString(); + builder = new StringBuilder(); + continue; + } + + builder.append(c); + + if (c == '\\' && i + 1 < chars.length) { + builder.append(chars[++i]); + continue; + } + + if (c == '"') { + quote = !quote; + } + + if (!quote) { + if (c == '{' || c == '[') { + depth++; + } else if (c == '}' || c == ']') { + depth--; + } + } + } + + if (splitNo != 3 || quote || depth != 0) { + return null; + } + + split[splitNo] = builder.toString(); + + return split; + } + /** * Is the values it returns all it can do? */