Skip to content

Commit

Permalink
fix(tsl): ${message} escaping incorrectly
Browse files Browse the repository at this point in the history
- Fixed ${message} escaping incorrectly.
- Added ${message_unescaped} for non-string usages
- Fixed EXECUTE attempting to execute DISPLAYING words too
  • Loading branch information
iGoodie committed Jun 22, 2020
1 parent 24fdbe6 commit 2ec1bb8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -5,7 +5,7 @@ org.gradle.daemon=false

mod_id=twitchspawn
mod_group=net.programmer.igoodie
mod_version=1.6.0
mod_version=1.6.1

minecraft_version=1.14.4
forge_version=28.0.83
Expand Down
Expand Up @@ -24,7 +24,7 @@ public ExecuteAction(List<String> words) throws TSLSyntaxError {
if (!actionWords.stream().allMatch(word -> word.startsWith("/")))
throw new TSLSyntaxError("Every command must start with '/' character");

this.commands = new LinkedList<>(words);
this.commands = new LinkedList<>(actionWords);
}

@Override
Expand Down
Expand Up @@ -44,6 +44,9 @@ public static String fromArgs(String expression, EventArguments args) {
if (expression.equals("message"))
return JSONUtils.escape(args.message);

if (expression.equals("message_unescaped"))
return args.message;

if (expression.equals("title"))
return args.rewardTitle;

Expand Down
Expand Up @@ -49,22 +49,12 @@ public static void forEach(JSONArray array, Consumer<JSONObject> consumer) {
public static String escape(String jsonString) {
StringBuilder escapedString = new StringBuilder();

boolean isEscaping = false;

for (char character : jsonString.toCharArray()) {
if (character == '\\' && !isEscaping) {
isEscaping = true;
escapedString.append(character);
continue;
}

if (character == '\'' || character == '\"' || character == '\\') {
if (!isEscaping)
escapedString.append("\\");
if (character == '\'' || character == '"' || character == '\\') {
escapedString.append("\\");
}

escapedString.append(character);
isEscaping = false;
}

return escapedString.toString();
Expand Down
Expand Up @@ -86,13 +86,17 @@ public void pencentageBelow100Test() {
@Test
@DisplayName("should escape JSON strings successfully.")
public void jsonStringEscapistTest() throws JSONException {
String original = "!drop \"\"\"\"\"\"''''\\\\\\\\'\\\\\\\"\\";
// !drop " \" \ \\ \' \\' \\\
String original = "!drop \" \\\" \\ \\\\ \\' \\\\' \\\\\\";
String escaped = JSONUtils.escape(original);

String jsonString = String.format("{text:\'%s\'}", escaped);
String jsonString = String.format("{text:'%s'}", escaped);
System.out.println("Original : " + original);
System.out.println("Formatted: " + escaped);

JSONObject json = new JSONObject(jsonString);
System.out.println("Parsed: " + json);
System.out.println("Text : " + json.getString("text"));
}

}

0 comments on commit 2ec1bb8

Please sign in to comment.