Improve robustness of variable replacing in messages

- Once the limit is reached the argument replacer would still continue sometimes (if a the replacement itself triggers the limit and the inserted message increases the size of the message we are building and adds a parameter tag again, like %0%)
- The language tag replacer also stops earlier now, it did not have the above problem though
- Fixed FancyMessageFormat.insertMessage() not checking its parameters
- Message.get() with a limit will now return an empty message if the limit is reached to prevent problems like above.
- Closes #205
This commit is contained in:
Thijs Wiefferink 2016-08-07 00:09:30 +02:00
parent 374b68d8fa
commit 7421e685fa
2 changed files with 26 additions and 6 deletions

View File

@ -151,7 +151,13 @@ public class FancyMessageFormat {
* @param end The end of the variable to replace
*/
public static void insertMessage(List<String> message, List<String> insert, int line, int start, int end) {
if(insert == null || line < 0 || line >= message.size() || start < 0 || end < 0) {
return;
}
String lineContent = message.remove(line);
if(start > lineContent.length() || end > lineContent.length()) {
return;
}
if(isTaggedInteractive(lineContent)) {
lineContent = lineContent.replace("", "");
message.add(line, lineContent.substring(0, start)+convertToConsole(insert)+lineContent.substring(end));

View File

@ -86,6 +86,9 @@ public class Message {
* @return Message as a list
*/
private List<String> get(Limit limit) {
if(limit.reached()) {
return new ArrayList<>();
}
executeReplacements(limit);
return message;
}
@ -292,7 +295,7 @@ public class Message {
* - Else the parameter will replace its number surrounded with VARIABLESTART and VARIABLEEND
*/
private void replaceArgumentVariables(Limit limit) {
if(message == null || message.size() == 0 || replacements == null) {
if(message == null || message.size() == 0 || replacements == null || limit.reached()) {
return;
}
boolean result = false;
@ -305,8 +308,15 @@ public class Message {
} else if(param instanceof Message) {
Pattern variables = Pattern.compile(Pattern.quote(VARIABLESTART)+number+Pattern.quote(VARIABLEEND));
Matcher matches = variables.matcher(message.get(i));
if(matches.find()) {
FancyMessageFormat.insertMessage(message, ((Message)param).get(limit), i, matches.start(), matches.end());
if(matches.find()) { // Only replaces one occurance of the variable, others will be done next round
int startDiff = message.size()-i;
List<String> insertMessage = ((Message)param).get(limit);
if(limit.reached()) {
return;
}
FancyMessageFormat.insertMessage(message, insertMessage, i, matches.start(), matches.end());
// Skip to end of insert
i = message.size()-startDiff;
}
number++;
} else {
@ -322,7 +332,7 @@ public class Message {
* Replace all language variables in a message
*/
private void replaceLanguageVariables(Limit limit) {
if(message == null || message.size() == 0) {
if(message == null || message.size() == 0 || limit.reached()) {
return;
}
@ -352,8 +362,12 @@ public class Message {
// Insert message
int startDiff = message.size()-i;
FancyMessageFormat.insertMessage(message, insert.get(limit), i, matches.start(), matches.end());
// Skip to end of insert, language tags already replaced
List<String> insertMessage = insert.get(limit);
if(limit.reached()) {
return;
}
FancyMessageFormat.insertMessage(message, insertMessage, i, matches.start(), matches.end());
// Skip to end of insert
i = message.size()-startDiff;
}
}