Fixes some bugs

Fixes a problem in color parsing where a &# code would throw an exception.
Makes sure to remove non-numerical characters in plugin versions to prevent an exception.
This commit is contained in:
Kristian Knarvik 2022-11-07 00:58:55 +01:00
parent fac01fd807
commit 84a2669022
2 changed files with 19 additions and 3 deletions

View File

@ -79,13 +79,17 @@ public final class ColorHelper {
}
Pattern pattern;
if (requireAmpersandInHexColors) {
pattern = Pattern.compile("(&#[a-fA-F0-9]{6})");
pattern = Pattern.compile("&(#[a-fA-F0-9]{6})");
} else {
pattern = Pattern.compile("(&?#[a-fA-F0-9]{6})");
pattern = Pattern.compile("&?(#[a-fA-F0-9]{6})");
}
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group()));
try {
message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group()));
} catch (IllegalArgumentException ignored) {
//Something was matched that wasn't a proper color after all. Ignore it.
}
}
return message;
}

View File

@ -86,6 +86,8 @@ public final class UpdateChecker {
* @return <p>True if the new version is higher than the old one</p>
*/
public static boolean isVersionHigher(String oldVersion, String newVersion) {
oldVersion = removeNonNumericCharacters(oldVersion);
newVersion = removeNonNumericCharacters(newVersion);
String[] oldVersionParts = oldVersion.split("\\.");
String[] newVersionParts = newVersion.split("\\.");
int versionLength = Math.max(oldVersionParts.length, newVersionParts.length);
@ -99,4 +101,14 @@ public final class UpdateChecker {
return false;
}
/**
* Removes all characters except numbers and the "." character from the given version string
*
* @param versionString <p>The version string to clean</p>
* @return <p>The string with non-numeric characters replaced</p>
*/
private static String removeNonNumericCharacters(String versionString) {
return versionString.replaceAll("[^0-9.]", "");
}
}