Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

Commit

Permalink
Allow for escaped curly braces and quotes to exist in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
leviem1 committed Feb 2, 2021
1 parent 5043bcd commit 3a7292b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,22 @@ public String getCommand(String name) {
StringBuilder newString = new StringBuilder();

for (int i = 0, j = 0; i < command.length(); i++) {
if (command.charAt(i) == tokenStr.charAt(j)) {
if (command.charAt(i) == tokenStr.charAt(j) && (i == 0 || command.charAt(i - 1) != '\\')) {
positions[j] = i;
j++;
}

if (j == positions.length) {
String segment = command.substring(last, positions[0]);
String segment = command.substring(last, positions[0]).replaceAll("\\\\\\{", "{")
.replaceAll("\\\\}", "}");
newString.append(segment).append(name);
last = i + 1;
j = 0;
}
}

newString.append(command, last, command.length());
newString.append(command.substring(last).replaceAll("\\\\\\{", "{")
.replaceAll("\\\\}", "}"));

return newString.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,16 @@ private List<String> parseForStrings(String[] args) {

for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (sb == null && c == '"') {
if (sb == null && c == '"' && (i == 0 || text.charAt(i - 1) != '\\')) {
sb = new StringBuilder();
} else if (sb != null) {
if (c == '"') {
if (c == '"' && text.charAt(i - 1) != '\\') {
String str = sb.toString();
if (str.length() > 0) {
strings.add(str);
}
sb = null;
} else {
} else if (i == text.length() - 1 || text.charAt(i) != '\\' || text.charAt(i + 1) != '"') {
sb.append(c);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/snowypeaksystems/mobactions/util/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Message implements IMessage {

int tokens = 0;
for (int i = 0, j = 0; i < message.length(); i++) {
if (message.charAt(i) == tokenStr.charAt(j)) {
if (message.charAt(i) == tokenStr.charAt(j) && (i == 0 || message.charAt(i - 1) != '\\')) {
j++;
}

Expand Down Expand Up @@ -47,15 +47,16 @@ private String replaceAndColorize(String... args) {
StringBuilder newString = new StringBuilder();
StringBuilder formatCodes = new StringBuilder();
for (int i = 0, j = 0; i < message.length(); i++) {
if (message.charAt(i) == tokenStr.charAt(j)) {
if (message.charAt(i) == tokenStr.charAt(j) && (i == 0 || message.charAt(i - 1) != '\\')) {
positions[j] = i;
j++;
}

if (j == positions.length) {
if (args.length > tokens) {
String tokenFormat = message.substring(positions[0] + 1, positions[1]);
String segment = message.substring(last, positions[0]);
String segment = message.substring(last, positions[0]).replaceAll("\\\\\\{", "{")
.replaceAll("\\\\}", "}");
String previousFormat = ChatColor.getLastColors(segment);
formatCodes.append(previousFormat);
newString.append(segment);
Expand All @@ -76,7 +77,8 @@ private String replaceAndColorize(String... args) {
}
}

newString.append(message, last, message.length());
newString.append(message.substring(last).replaceAll("\\\\\\{", "{")
.replaceAll("\\\\}", "}"));

return newString.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ void replace() {

data = new CommandData("{}st {}st", "");
assertEquals("test test", data.getCommand("te"));
}

data = new CommandData("{} \\{\\} {}", "");
assertEquals("test {} test", data.getCommand("test"));

data = new CommandData("\\{\\} {} \\{\\}", "");
assertEquals("{} test {}", data.getCommand("test"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ void replace() {
assertThrows(IllegalArgumentException.class, message::replace);
assertThrows(IllegalArgumentException.class, message::replace);

message = new Message("{} \\{\\} {}");
assertEquals("test {} test", message.replace("test", "test"));
assertThrows(IllegalArgumentException.class, message::replace);

message = new Message("\\{\\} {} \\{\\}");
assertEquals("{} test {}", message.replace("test"));

message = new Message("&ctest {} test");
assertEquals(
ChatColor.translateAlternateColorCodes('&', "&ctest test test"),
Expand Down

0 comments on commit 3a7292b

Please sign in to comment.