Skip to content

Commit

Permalink
Fix formatting applying to wrong message parts (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed Apr 11, 2017
1 parent 5210c80 commit f0d22cd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
18 changes: 12 additions & 6 deletions src/main/java/ch/njol/skript/lang/VariableString.java
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,21 @@ public MessageComponent[] getMessageComponents(final Event e) {
ChatMessages.copyStyles(componentList.get(componentList.size() - 1), c2[0]); // Copy styles
componentList.addAll(Arrays.asList(c2));
} else {
final MessageComponent[] c2 = ChatMessages.parseToArray(Classes.toString(info.expr.getArray(e), flags, null));
ChatMessages.copyStyles(componentList.get(componentList.size() - 1), c2[0]); // Copy styles
componentList.addAll(Arrays.asList(c2));
final String str = Classes.toString(info.expr.getArray(e), flags, null);
MessageComponent last = componentList.get(componentList.size() - 1);
if (last != null)
last.text += str;
else
componentList.add(ChatMessages.plainText(str));
}
} else if (o instanceof Expression<?>) {
assert mode != StringMode.MESSAGE;
final MessageComponent[] c2 = ChatMessages.parseToArray(Classes.toString(((Expression<?>) o).getArray(e), true, mode));
ChatMessages.copyStyles(componentList.get(componentList.size() - 1), c2[0]); // Copy styles
componentList.addAll(Arrays.asList(c2));
final String str = Classes.toString(((Expression<?>) o).getArray(e), true, mode);
MessageComponent last = componentList.get(componentList.size() - 1);
if (last != null)
last.text += str;
else
componentList.add(ChatMessages.plainText(str));
}
} else { // String part, parsed already
// However, there might be variable URLs etc.
Expand Down
59 changes: 32 additions & 27 deletions src/main/java/ch/njol/skript/util/chat/ChatMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ public static List<MessageComponent> parse(String msg) {
}

ChatCode code = null;
String param = "";
VariableString varParam = null;

if (c == '>') { // Tag end
String tag = msg.substring(tagStart + 1, i);
tagMode = false;

String name;
String param = "";
VariableString varParam = null;
if (tag.contains(":")) {
String[] split = tag.split(":", 2);
name = split[0];
Expand All @@ -217,40 +217,35 @@ public static List<MessageComponent> parse(String msg) {
continue;
}

assert param != null;
if (code.colorCode != null) // Just update color code
current.color = code.colorCode;
else
code.updateComponent(current, param, varParam); // Call ChatCode update
} else if (nextColorChar && c < 256) { // Legacy color code
nextColorChar = false;
code = colorChars[c];
if (code != null) {
@SuppressWarnings("null")
@NonNull String param = null;
if (code.colorCode != null) // Just update color code
current.color = code.colorCode;
else
code.updateComponent(current, param, null); // Call ChatCode update
} else {
if (code == null) {
curStr.append(previous).append(c);
continue;
}
}

if (code != null && code.nextComponent()) { // Next chat component
String text = curStr.toString();
curStr = new StringBuilder();
assert text != null;
current.text = text;

MessageComponent old = current;
current = new MessageComponent();
if (code.equals(ChatCode.reset))
current.reset = true;
copyStyles(old, current);
if (code != null) {
if (code.nextComponent()) { // Next chat component
String text = curStr.toString();
curStr = new StringBuilder();
assert text != null;
current.text = text;

MessageComponent old = current;
current = new MessageComponent();
if (code.equals(ChatCode.reset))
current.reset = true;
copyStyles(old, current);

components.add(current);
}

components.add(current);
if (code.colorCode != null) // Just update color code
current.color = code.colorCode;
else
code.updateComponent(current, param, varParam); // Call ChatCode update
}
}
String text = curStr.toString();
Expand Down Expand Up @@ -321,4 +316,14 @@ public static void shareStyles(MessageComponent[] components) {
previous = c;
}
}

/**
* Constructs plain text only message component.
* @param str
*/
public static MessageComponent plainText(String str) {
MessageComponent component = new MessageComponent();
component.text = str;
return component;
}
}

0 comments on commit f0d22cd

Please sign in to comment.