From e816310da84c005f9fd0a544043db8c455a0e976 Mon Sep 17 00:00:00 2001 From: stepech Date: Fri, 2 Jul 2021 01:07:44 +0200 Subject: [PATCH] Fix mcmmo picking wrong version on builds withou patch number (#4555) * Fix server versioning * Switch to regex when getting server version * add comment * Poor beginner mistakes Co-authored-by: TheBusyBiscuit Co-authored-by: TheBusyBiscuit --- .../util/platform/PlatformManager.java | 53 +++++-------------- 1 file changed, 13 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/util/platform/PlatformManager.java b/src/main/java/com/gmail/nossr50/util/platform/PlatformManager.java index 78d604e11..989eb3da6 100644 --- a/src/main/java/com/gmail/nossr50/util/platform/PlatformManager.java +++ b/src/main/java/com/gmail/nossr50/util/platform/PlatformManager.java @@ -6,9 +6,9 @@ import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.Locale; -import java.util.stream.Collectors; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @@ -47,18 +47,19 @@ public class PlatformManager { private @NotNull MinecraftGameVersion determineGameVersion(String platformVersionString) { int major = 0, minor = 0, patch = 0; - String[] splitVersion = platformVersionString.split("\\.", 3); - mcMMO.p.getLogger().info("Platform String: " + platformVersionString); - //TODO: this is very hacky and probably isn't reliable - //Grab all consecutive digits - major = getSubsequentDigits(splitVersion[0].toCharArray(), 0); - minor = getSubsequentDigits(splitVersion[1].toCharArray(), 0); - //Not all versions of Minecraft have a patch digit - //If the first character isn't a digit it's not a patch number and its some crap we don't care about - if(splitVersion.length > 2 && Character.isDigit(splitVersion[2].toCharArray()[0])) - patch = getSubsequentDigits(splitVersion[2].toCharArray(), 0); + // Gets two numbers separated by . and optional third number after next dot. Must end with - or _ + Matcher versionMatch = Pattern.compile("(\\d+)\\.(\\d+)(?:\\.(\\d+))?[-_].*").matcher(platformVersionString); + + if (versionMatch.find()) { + major = Integer.parseInt(versionMatch.group(1)); + minor = Integer.parseInt(versionMatch.group(2)); + + if (versionMatch.group(3) != null) { + patch = Integer.parseInt(versionMatch.group(3)); + } + } mcMMO.p.getLogger().info("Minecraft version determined to be - " + major + "." @@ -68,34 +69,6 @@ public class PlatformManager { return new MinecraftGameVersion(major, minor, patch); } - /** - * Get all consecutive digits in a char array from position - * @param charArray target char array - * @param position starting position - * @return all consecutive digits from position - */ - private int getSubsequentDigits(char[] charArray, int position) { - ArrayList digitArrayList = new ArrayList<>(); - - do { - if(Character.isDigit(charArray[position])) { - digitArrayList.add(charArray[position]); - position++; - } else { - break; - } - } while (position < charArray.length); - - //Convert List -> String - String digits = digitArrayList - .stream() - .map(String::valueOf) - .collect(Collectors.joining()); - - //Copy value - return Integer.parseInt(digits); - } - //TODO: Rewrite this properly once we actually support a not-bukkit platform private @NotNull ServerSoftwareType determinePlatformType() { if(Bukkit.getVersion().toLowerCase(Locale.ENGLISH).contains("paper"))