diff --git a/Images-Common/pom.xml b/Images-Common/pom.xml index 45e166d..cb3bb67 100644 --- a/Images-Common/pom.xml +++ b/Images-Common/pom.xml @@ -19,11 +19,17 @@ provided - com.comphenix.protocol + net.dmulloy2 ProtocolLib 5.1.0 provided + + junit + junit + 4.13.2 + test + - \ No newline at end of file + diff --git a/Images-Common/src/main/java/com/andavin/util/BukkitVersionParser.java b/Images-Common/src/main/java/com/andavin/util/BukkitVersionParser.java new file mode 100644 index 0000000..594428c --- /dev/null +++ b/Images-Common/src/main/java/com/andavin/util/BukkitVersionParser.java @@ -0,0 +1,43 @@ +/* + * MIT License + * + * Copyright (c) 2020 Mark + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.andavin.util; + +import java.util.regex.Pattern; + +final class BukkitVersionParser { + + private static final Pattern PAPER_BUILD_SUFFIX = Pattern.compile("\\.build(?:\\.\\d+)?$"); + + private BukkitVersionParser() { + } + + static String normalize(String bukkitVersion) { + + int qualifierIndex = bukkitVersion.indexOf('-'); + String version = qualifierIndex == -1 ? + bukkitVersion : + bukkitVersion.substring(0, qualifierIndex); + return PAPER_BUILD_SUFFIX.matcher(version).replaceFirst(""); + } +} diff --git a/Images-Common/src/main/java/com/andavin/util/MinecraftVersion.java b/Images-Common/src/main/java/com/andavin/util/MinecraftVersion.java index 2ebdde7..a49c25d 100644 --- a/Images-Common/src/main/java/com/andavin/util/MinecraftVersion.java +++ b/Images-Common/src/main/java/com/andavin/util/MinecraftVersion.java @@ -429,16 +429,10 @@ private static String findMinorVersion() { return packageName.substring(minorIndex); } - String bukkitVersion = server.getBukkitVersion(); - return matchVersion(bukkitVersion.substring(0, bukkitVersion.indexOf('-'))); + return matchVersion(BukkitVersionParser.normalize(server.getBukkitVersion())); } private static String matchVersion(String version) { - - if (version.endsWith(".build")) { // Paper experimental versions - version = version.substring(0, version.length() - ".build".length()); - } - switch (version) { case "1.21": case "1.21.1": diff --git a/Images-Common/src/test/java/com/andavin/util/BukkitVersionParserTest.java b/Images-Common/src/test/java/com/andavin/util/BukkitVersionParserTest.java new file mode 100644 index 0000000..3921d39 --- /dev/null +++ b/Images-Common/src/test/java/com/andavin/util/BukkitVersionParserTest.java @@ -0,0 +1,51 @@ +/* + * MIT License + * + * Copyright (c) 2020 Mark + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.andavin.util; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class BukkitVersionParserTest { + + @Test + public void normalizesNumberedPaperBuildVersion() { + assertEquals("26.1.2", BukkitVersionParser.normalize("26.1.2.build.2592-stable")); + } + + @Test + public void normalizesPaperBuildVersionWithoutNumber() { + assertEquals("26.1.2", BukkitVersionParser.normalize("26.1.2.build")); + } + + @Test + public void normalizesStandardBukkitVersion() { + assertEquals("1.21.11", BukkitVersionParser.normalize("1.21.11-R0.1-SNAPSHOT")); + } + + @Test + public void preservesPlainMinecraftVersion() { + assertEquals("26.1.2", BukkitVersionParser.normalize("26.1.2")); + } +} diff --git a/Images-Core/pom.xml b/Images-Core/pom.xml index 0d95f6f..03e07f8 100644 --- a/Images-Core/pom.xml +++ b/Images-Core/pom.xml @@ -51,7 +51,7 @@ provided - com.comphenix.protocol + net.dmulloy2 ProtocolLib 5.1.0 provided @@ -203,4 +203,4 @@ - \ No newline at end of file +