From 4b30ce46b62142bc8afa79c680fa27b28d3482db Mon Sep 17 00:00:00 2001 From: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com> Date: Sat, 20 Feb 2021 16:22:31 +0100 Subject: [PATCH 1/7] Update VariableString.java --- .../ch/njol/skript/lang/VariableString.java | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/VariableString.java b/src/main/java/ch/njol/skript/lang/VariableString.java index ee0e4849190..7d2fa52f7ca 100644 --- a/src/main/java/ch/njol/skript/lang/VariableString.java +++ b/src/main/java/ch/njol/skript/lang/VariableString.java @@ -167,6 +167,7 @@ public static VariableString newInstance(final String s) { /** * Tests whether a string is correctly quoted, i.e. only has doubled double quotes in it. + * Singular double quotes are only allowed between percentage signs. * * @param s The string * @param withQuotes Whether s must be surrounded by double quotes or not @@ -176,12 +177,22 @@ public static boolean isQuotedCorrectly(final String s, final boolean withQuotes if (withQuotes && (!s.startsWith("\"") || !s.endsWith("\""))) return false; boolean quote = false; + boolean percentage = false; for (int i = withQuotes ? 1 : 0; i < (withQuotes ? s.length() - 1 : s.length()); i++) { - if (s.charAt(i) != '"') { - if (quote) - return false; - } else { + if (percentage) { + if (s.charAt(i) == '%') + percentage = false; + + continue; + } + + if (quote && s.charAt(i) != '"') + return false; + + if (s.charAt(i) == '"') { quote = !quote; + } else if (s.charAt(i) == '%') { + percentage = true; } } return !quote; @@ -211,7 +222,7 @@ public static String unquote(final String s, final boolean surroundingQuotes) { */ @Nullable public static VariableString newInstance(final String orig, final StringMode mode) { - if (!isQuotedCorrectly(orig, false)) + if (mode != StringMode.VARIABLE_NAME && !isQuotedCorrectly(orig, false)) return null; final int n = StringUtils.count(orig, '%'); if (n % 2 != 0) { @@ -220,7 +231,26 @@ public static VariableString newInstance(final String orig, final StringMode mod } // We must not parse color codes yet, as JSON support would be broken :( - final String s = orig.replace("\"\"", "\""); + final String s; + if (mode != StringMode.VARIABLE_NAME) { + // Replace every double " character with a single ", except for those in expressions (between %) + StringBuilder stringBuilder = new StringBuilder(); + + boolean expression = false; + for (int i = 0; i < orig.length(); i++) { + char c = orig.charAt(i); + stringBuilder.append(c); + + if (c == '%') + expression = !expression; + + if (!expression && c == '"') + i++; + } + s = stringBuilder.toString(); + } else { + s = orig; + } final List string = new ArrayList<>(n / 2 + 2); // List of strings and expressions @@ -255,7 +285,6 @@ public static VariableString newInstance(final String orig, final StringMode mod } else { final RetainingLogHandler log = SkriptLogger.startRetainingLog(); try { - @SuppressWarnings("unchecked") final Expression expr = new SkriptParser("" + s.substring(c + 1, c2), SkriptParser.PARSE_EXPRESSIONS, ParseContext.DEFAULT).parseExpression(Object.class); if (expr == null) { log.printErrors("Can't understand this expression: " + s.substring(c + 1, c2)); From ae905b61cadf5fd22effb325c0009aeacfc9d4bd Mon Sep 17 00:00:00 2001 From: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com> Date: Sat, 20 Feb 2021 17:01:01 +0100 Subject: [PATCH 2/7] Update VariableString.java --- src/main/java/ch/njol/skript/lang/VariableString.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/lang/VariableString.java b/src/main/java/ch/njol/skript/lang/VariableString.java index 7d2fa52f7ca..dc7a381d637 100644 --- a/src/main/java/ch/njol/skript/lang/VariableString.java +++ b/src/main/java/ch/njol/skript/lang/VariableString.java @@ -808,7 +808,7 @@ public void change(final Event e, final @Nullable Object[] delta, final ChangeMo @Override public boolean getAnd() { - return false; + return true; } @Override From 0761bf30d3809f56c88583c98502c3d2d3ac5ad6 Mon Sep 17 00:00:00 2001 From: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com> Date: Sat, 20 Feb 2021 17:01:33 +0100 Subject: [PATCH 3/7] Update ExprEntities.sk --- src/test/skript/tests/syntaxes/expressions/ExprEntities.sk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/skript/tests/syntaxes/expressions/ExprEntities.sk b/src/test/skript/tests/syntaxes/expressions/ExprEntities.sk index be40ab07b80..e98cad46aa3 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprEntities.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprEntities.sk @@ -1,6 +1,6 @@ test "entities in chunk": spawn 10 sheep at spawn of world "world" wait 1 tick - assert size of all entities in chunk at spawn of world "world" >= 10 with "Size of all entities in spawn chunk is not > 10: %size of all entities in chunk at spawn of world ""world""%" + assert size of all entities in chunk at spawn of world "world" >= 10 with "Size of all entities in spawn chunk is not > 10: %size of all entities in chunk at spawn of world "world"%" delete all entities in chunk at spawn of world "world" - assert size of all entities in chunk at spawn of world "world" = 0 with "Size of all entities in spawn chunk != 0: %size of all entities in chunk at spawn of world ""world""%" \ No newline at end of file + assert size of all entities in chunk at spawn of world "world" = 0 with "Size of all entities in spawn chunk != 0: %size of all entities in chunk at spawn of world "world"%" From 2224d529cf71848b1b8ea6aaa9b7b11ae81d61c0 Mon Sep 17 00:00:00 2001 From: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com> Date: Sat, 20 Feb 2021 17:02:00 +0100 Subject: [PATCH 4/7] Create 590-escaping quotes is required in some places it shouldn't be.sk --- ...required in some places it shouldn't be.sk | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/test/skript/tests/regressions/590-escaping quotes is required in some places it shouldn't be.sk diff --git a/src/test/skript/tests/regressions/590-escaping quotes is required in some places it shouldn't be.sk b/src/test/skript/tests/regressions/590-escaping quotes is required in some places it shouldn't be.sk new file mode 100644 index 00000000000..a17b701daa7 --- /dev/null +++ b/src/test/skript/tests/regressions/590-escaping quotes is required in some places it shouldn't be.sk @@ -0,0 +1,19 @@ +function myFunction_five_nine_zero(s: string) :: string: + return {_s} + +test "double quote parsing": + #assert "Testing" is set with "simple string failed" + + #assert "Testing %1 + 1%" is set with "simple string with expression failed" + + #assert "Testing """ is set with "simple string with escaped quote failed" + + assert "Testing %length of "abc"%" is set with "string with expression with string ##1 failed" + #assert "%myFunction_five_nine_zero("Hello")% world" is "Hello world" with "string with expression with string ##2 failed" + + + #assert {_abc} is not set with "simple variable failed" + #assert {_abc%%} is not set with "simple variable with escaped percentage sign failed" + #assert {_abc%1 + 1%} is not set with "simple variable with expression failed" + + #assert {_%subtext of "test" from characters 1 to 1%} is not set with "variable with expression with string failed" From 6e733d2ba66b006bbb2458bc956150ac454f251f Mon Sep 17 00:00:00 2001 From: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com> Date: Sat, 20 Feb 2021 17:07:16 +0100 Subject: [PATCH 5/7] Update 590-escaping quotes is required in some places it shouldn't be.sk --- ...is required in some places it shouldn't be.sk | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/skript/tests/regressions/590-escaping quotes is required in some places it shouldn't be.sk b/src/test/skript/tests/regressions/590-escaping quotes is required in some places it shouldn't be.sk index a17b701daa7..d8552d32207 100644 --- a/src/test/skript/tests/regressions/590-escaping quotes is required in some places it shouldn't be.sk +++ b/src/test/skript/tests/regressions/590-escaping quotes is required in some places it shouldn't be.sk @@ -2,18 +2,18 @@ function myFunction_five_nine_zero(s: string) :: string: return {_s} test "double quote parsing": - #assert "Testing" is set with "simple string failed" + assert "Testing" is set with "simple string failed" - #assert "Testing %1 + 1%" is set with "simple string with expression failed" + assert "Testing %1 + 1%" is set with "simple string with expression failed" - #assert "Testing """ is set with "simple string with escaped quote failed" + assert "Testing """ is set with "simple string with escaped quote failed" assert "Testing %length of "abc"%" is set with "string with expression with string ##1 failed" - #assert "%myFunction_five_nine_zero("Hello")% world" is "Hello world" with "string with expression with string ##2 failed" + assert "%myFunction_five_nine_zero("Hello")% world" is "Hello world" with "string with expression with string ##2 failed" - #assert {_abc} is not set with "simple variable failed" - #assert {_abc%%} is not set with "simple variable with escaped percentage sign failed" - #assert {_abc%1 + 1%} is not set with "simple variable with expression failed" + assert {_abc} is not set with "simple variable failed" + assert {_abc%%} is not set with "simple variable with escaped percentage sign failed" + assert {_abc%1 + 1%} is not set with "simple variable with expression failed" - #assert {_%subtext of "test" from characters 1 to 1%} is not set with "variable with expression with string failed" + assert {_%subtext of "test" from characters 1 to 1%} is not set with "variable with expression with string failed" From 8bdcfbd125c74ca533a21284f02f93a521eb1452 Mon Sep 17 00:00:00 2001 From: TPGamesNL Date: Wed, 5 May 2021 13:46:08 +0200 Subject: [PATCH 6/7] Update files --- src/main/java/ch/njol/skript/lang/SkriptParser.java | 6 +++++- src/main/java/ch/njol/skript/lang/VariableString.java | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/SkriptParser.java b/src/main/java/ch/njol/skript/lang/SkriptParser.java index 47ee1ee9bfa..7903f385162 100644 --- a/src/main/java/ch/njol/skript/lang/SkriptParser.java +++ b/src/main/java/ch/njol/skript/lang/SkriptParser.java @@ -1351,11 +1351,15 @@ static int countUnescaped(final String pattern, final char c, final int start, f * @return Index of the end quote */ private static int nextQuote(final String s, final int from) { + boolean inExpression = false; for (int i = from; i < s.length(); i++) { - if (s.charAt(i) == '"') { + char c = s.charAt(i); + if (c == '"' && !inExpression) { if (i == s.length() - 1 || s.charAt(i + 1) != '"') return i; i++; + } else if (c == '%') { + inExpression = !inExpression; } } return -1; diff --git a/src/main/java/ch/njol/skript/lang/VariableString.java b/src/main/java/ch/njol/skript/lang/VariableString.java index c124564edca..7988a4b6bee 100644 --- a/src/main/java/ch/njol/skript/lang/VariableString.java +++ b/src/main/java/ch/njol/skript/lang/VariableString.java @@ -200,7 +200,7 @@ public static String unquote(String s, boolean surroundingQuotes) { * @return A new VariableString instance. */ @Nullable - public static VariableString newInstance(final String orig, final StringMode mode) { + public static VariableString newInstance(String orig, StringMode mode) { if (mode != StringMode.VARIABLE_NAME && !isQuotedCorrectly(orig, false)) return null; int n = StringUtils.count(orig, '%'); @@ -210,7 +210,7 @@ public static VariableString newInstance(final String orig, final StringMode mod } // We must not parse color codes yet, as JSON support would be broken :( - final String s; + String s; if (mode != StringMode.VARIABLE_NAME) { // Replace every double " character with a single ", except for those in expressions (between %) StringBuilder stringBuilder = new StringBuilder(); From 71958f58120180ee2076975f837e505da97c430e Mon Sep 17 00:00:00 2001 From: TPGamesNL Date: Wed, 5 May 2021 13:54:42 +0200 Subject: [PATCH 7/7] Update VariableString.java --- src/main/java/ch/njol/skript/lang/VariableString.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/lang/VariableString.java b/src/main/java/ch/njol/skript/lang/VariableString.java index 7988a4b6bee..c608e0ef221 100644 --- a/src/main/java/ch/njol/skript/lang/VariableString.java +++ b/src/main/java/ch/njol/skript/lang/VariableString.java @@ -305,7 +305,8 @@ public static VariableString newInstance(String orig, StringMode mode) { Object[] sa = string.toArray(); if (string.size() == 1 && string.get(0) instanceof Expression && ((Expression) string.get(0)).getReturnType() == String.class && - ((Expression) string.get(0)).isSingle()) { + ((Expression) string.get(0)).isSingle() && + mode == StringMode.MESSAGE) { String expr = ((Expression) string.get(0)).toString(null, false); Skript.warning(expr + " is already a text, so you should not put it in one (e.g. " + expr + " instead of " + "\"%" + expr.replace("\"", "\"\"") + "%\")"); }