Make VersionUtil not explode when patch is not present

This commit is contained in:
md678685 2019-01-03 22:16:14 +00:00
parent 04ec013407
commit da89efb79f
2 changed files with 25 additions and 1 deletions

View File

@ -49,7 +49,16 @@ public class VersionUtil {
matcher = VERSION_PATTERN.matcher(v1_13_2_R01.toString());
Preconditions.checkArgument(matcher.matches(), string + " is not in valid version format. e.g. 1.8.8-R0.1");
}
return new BukkitVersion(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3)), Double.parseDouble(matcher.group(4)));
return from(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4));
}
private static BukkitVersion from(String major, String minor, String patch, String revision) {
if (patch.isEmpty()) patch = "0";
return new BukkitVersion(Integer.parseInt(major),
Integer.parseInt(minor),
Integer.parseInt(patch),
Double.parseDouble(revision));
}
private BukkitVersion(int major, int minor, int patch, double revision) {

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.VersionUtil;
import junit.framework.TestCase;
import org.bukkit.World.Environment;
import org.bukkit.plugin.InvalidDescriptionException;
@ -190,4 +191,18 @@ public class UtilTest extends TestCase {
b = new GregorianCalendar(2000, 3, 7, 10, 0, 0);
assertEquals("10 years 6 months 10 days", DateUtil.formatDateDiff(a, b));
}
public void testVer() {
VersionUtil.BukkitVersion v;
v = VersionUtil.BukkitVersion.fromString("1.13.2-R0.1");
assertEquals(v.getMajor(), 1);
assertEquals(v.getMinor(), 13);
assertEquals(v.getPatch(), 2);
assertEquals(v.getRevision(), 0.1);
v = VersionUtil.BukkitVersion.fromString("1.9-R1.4");
assertEquals(v.getMajor(), 1);
assertEquals(v.getMinor(), 9);
assertEquals(v.getPatch(), 0);
assertEquals(v.getRevision(), 1.4);
}
}