diff --git a/build.gradle.kts b/build.gradle.kts index 8a049f55..b1b7c42a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,6 +13,7 @@ version = property("version") @Suppress("UnstableApiUsage") fun Project.nextGitTag(): String { val latestTag = providers.exec { + workingDir(project.projectDir) commandLine("git", "describe", "--tags", "--abbrev=0") }.standardOutput.asText.get().trim() diff --git a/inventory-framework-platform-bukkit/src/main/java/me/devnatan/inventoryframework/runtime/thirdparty/McVersion.java b/inventory-framework-platform-bukkit/src/main/java/me/devnatan/inventoryframework/runtime/thirdparty/McVersion.java index 276e1f13..9f00ebb2 100644 --- a/inventory-framework-platform-bukkit/src/main/java/me/devnatan/inventoryframework/runtime/thirdparty/McVersion.java +++ b/inventory-framework-platform-bukkit/src/main/java/me/devnatan/inventoryframework/runtime/thirdparty/McVersion.java @@ -1,33 +1,36 @@ package me.devnatan.inventoryframework.runtime.thirdparty; import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.bukkit.Bukkit; public class McVersion implements Comparable { + private static final Pattern LEADING_VERSION = + Pattern.compile("(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?"); + private static final McVersion CURRENT_VERSION; static { - final int currentMajor = Integer.parseInt(Bukkit.getBukkitVersion().split("\\.")[0]); - final int currentMinor = - Integer.parseInt(Bukkit.getBukkitVersion().split("\\.")[1].split("-")[0]); - boolean hasPatch = countColons(Bukkit.getBukkitVersion()) == 3; - final int currentPatch = hasPatch - ? Integer.parseInt(Bukkit.getBukkitVersion().split("\\.")[2].split("-")[0]) - : 0; - - CURRENT_VERSION = new McVersion(currentMajor, currentMinor, currentPatch); - } - - private static int countColons(final String string) { - int count = 0; - char[] arr = string.toCharArray(); - for (int i = 0; i < string.length(); i++) { - if (arr[i] == '.') { - count++; - } + CURRENT_VERSION = parse(Bukkit.getBukkitVersion()); + } + + /** + * Reads only the leading run of dot-separated numeric segments (major[.minor[.patch]]), + * so a build/commit suffix appended by a non-standard server fork (e.g. "26.2.build.17406-6bc38be") + * is ignored instead of throwing a {@link NumberFormatException} out of a static initializer. + */ + private static McVersion parse(final String version) { + final Matcher matcher = LEADING_VERSION.matcher(version); + if (!matcher.lookingAt()) { + return new McVersion(1, 0, 0); } - return count; + + final int major = Integer.parseInt(matcher.group(1)); + final int minor = matcher.group(2) != null ? Integer.parseInt(matcher.group(2)) : 0; + final int patch = matcher.group(3) != null ? Integer.parseInt(matcher.group(3)) : 0; + return new McVersion(major, minor, patch); } private final int major;