Archiviert
13
0

Attempt to handle snapshot versions by assuming Minecraft version.

The snapshot version contains a release date, so we'll simply compare
that against a known release date of a Minecraft version. It it's 
later, we know it is at least a minor version above, and vice versa.
Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-09-28 16:25:30 +02:00
Ursprung 2001c15132
Commit 1b1f36c5d6
7 geänderte Dateien mit 579 neuen und 349 gelöschten Zeilen

Datei anzeigen

@ -81,12 +81,17 @@ public class ProtocolLibrary extends JavaPlugin {
/** /**
* The minimum version ProtocolLib has been tested with. * The minimum version ProtocolLib has been tested with.
*/ */
private static final String MINIMUM_MINECRAFT_VERSION = "1.0.0"; public static final String MINIMUM_MINECRAFT_VERSION = "1.0.0";
/** /**
* The maximum version ProtocolLib has been tested with, * The maximum version ProtocolLib has been tested with,
*/ */
private static final String MAXIMUM_MINECRAFT_VERSION = "1.6.2"; public static final String MAXIMUM_MINECRAFT_VERSION = "1.6.2";
/**
* The date (with ISO 8601) when the most recent version was released.
*/
public static final String MINECRAFT_LAST_RELEASE_DATE = "2013-07-08";
/** /**
* The number of milliseconds per second. * The number of milliseconds per second.

Datei anzeigen

@ -17,10 +17,13 @@
package com.comphenix.protocol.utility; package com.comphenix.protocol.utility;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.Server; import org.bukkit.Server;
import com.comphenix.protocol.ProtocolLibrary;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.collect.ComparisonChain; import com.google.common.collect.ComparisonChain;
@ -35,7 +38,7 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
/** /**
* Regular expression used to parse version strings. * Regular expression used to parse version strings.
*/ */
private static final String VERSION_PATTERN = ".*\\(.*MC.\\s*((?:\\d+\\.)*\\d)\\s*\\)"; private static final String VERSION_PATTERN = ".*\\(.*MC.\\s*([a-zA-z0-9\\-\\.]+)\\s*\\)";
private final int major; private final int major;
private final int minor; private final int minor;
@ -44,6 +47,9 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
// The development stage // The development stage
private final String development; private final String development;
// Snapshot?
private final SnapshotVersion snapshot;
/** /**
* Determine the current Minecraft version. * Determine the current Minecraft version.
* @param server - the Bukkit server that will be used to examine the MC version. * @param server - the Bukkit server that will be used to examine the MC version.
@ -53,17 +59,53 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
} }
/** /**
* Construct a version object from the format major.minor.build. * Construct a version object from the format major.minor.build, or the snapshot format.
* @param versionOnly - the version in text form. * @param versionOnly - the version in text form.
*/ */
public MinecraftVersion(String versionOnly) { public MinecraftVersion(String versionOnly) {
this(versionOnly, true);
}
/**
* Construct a version format from the standard release version or the snapshot verison.
* @param versionOnly - the version.
* @param parseSnapshot - TRUE to parse the snapshot, FALSE otherwise.
*/
private MinecraftVersion(String versionOnly, boolean parseSnapshot) {
String[] section = versionOnly.split("-"); String[] section = versionOnly.split("-");
int[] numbers = parseVersion(section[0]); SnapshotVersion snapshot = null;
int[] numbers = new int[3];
try {
numbers = parseVersion(section[0]);
} catch (NumberFormatException cause) {
// Skip snapshot parsing
if (!parseSnapshot)
throw cause;
try {
// Determine if the snapshot is newer than the current release version
snapshot = new SnapshotVersion(section[0]);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
MinecraftVersion latest = new MinecraftVersion(ProtocolLibrary.MAXIMUM_MINECRAFT_VERSION, false);
boolean newer = snapshot.getSnapshotDate().compareTo(
format.parse(ProtocolLibrary.MINECRAFT_LAST_RELEASE_DATE)) > 0;
numbers[0] = latest.getMajor();
numbers[1] = latest.getMinor() + (newer ? 1 : -1);
numbers[2] = 0;
} catch (Exception e) {
throw new IllegalStateException("Cannot parse " + section[0], e);
}
}
this.major = numbers[0]; this.major = numbers[0];
this.minor = numbers[1]; this.minor = numbers[1];
this.build = numbers[2]; this.build = numbers[2];
this.development = section.length > 1 ? section[1] : null; this.development = section.length > 1 ? section[1] : (snapshot != null ? "snapshot" : null);
this.snapshot = snapshot;
} }
/** /**
@ -88,6 +130,7 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
this.minor = minor; this.minor = minor;
this.build = build; this.build = build;
this.development = development; this.development = development;
this.snapshot = null;
} }
private int[] parseVersion(String version) { private int[] parseVersion(String version) {
@ -136,6 +179,22 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
return development; return development;
} }
/**
* Retrieve the snapshot version, or NULL if this is a release.
* @return The snapshot version.
*/
public SnapshotVersion getSnapshot() {
return snapshot;
}
/**
* Determine if this version is a snapshot.
* @return The snapshot version.
*/
public boolean isSnapshot() {
return snapshot != null;
}
/** /**
* Retrieve the version String (major.minor.build) only. * Retrieve the version String (major.minor.build) only.
* @return A normal version string. * @return A normal version string.
@ -144,7 +203,8 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
if (getDevelopmentStage() == null) if (getDevelopmentStage() == null)
return String.format("%s.%s.%s", getMajor(), getMinor(), getBuild()); return String.format("%s.%s.%s", getMajor(), getMinor(), getBuild());
else else
return String.format("%s.%s.%s-%s", getMajor(), getMinor(), getBuild(), getDevelopmentStage()); return String.format("%s.%s.%s-%s%s", getMajor(), getMinor(), getBuild(),
getDevelopmentStage(), isSnapshot() ? snapshot : "");
} }
@Override @Override
@ -158,6 +218,7 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
compare(getBuild(), o.getBuild()). compare(getBuild(), o.getBuild()).
// No development String means it's a release // No development String means it's a release
compare(getDevelopmentStage(), o.getDevelopmentStage(), Ordering.natural().nullsLast()). compare(getDevelopmentStage(), o.getDevelopmentStage(), Ordering.natural().nullsLast()).
compare(getSnapshot(), o.getSnapshot()).
result(); result();
} }
@ -207,4 +268,13 @@ public class MinecraftVersion implements Comparable<MinecraftVersion> {
throw new IllegalStateException("Cannot parse version String '" + text + "'"); throw new IllegalStateException("Cannot parse version String '" + text + "'");
} }
} }
/**
* Parse the given server version into a Minecraft version.
* @param serverVersion - the server version.
* @return The resulting Minecraft version.
*/
public static MinecraftVersion fromServerVersion(String serverVersion) {
return new MinecraftVersion(extractVersion(serverVersion));
}
} }

Datei anzeigen

@ -0,0 +1,108 @@
package com.comphenix.protocol.utility;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.common.base.Objects;
import com.google.common.collect.ComparisonChain;
/**
* Used to parse a snapshot version.
* @author Kristian
*/
public class SnapshotVersion implements Comparable<SnapshotVersion> {
private static final Pattern SNAPSHOT_PATTERN = Pattern.compile("(\\d{2}w\\d{2})([a-z])");
private final String rawString;
private final Date snapshotDate;
private final int snapshotWeekVersion;
public SnapshotVersion(String version) {
Matcher matcher = SNAPSHOT_PATTERN.matcher(version.trim());
if (matcher.matches()) {
try {
this.snapshotDate = getDateFormat().parse(matcher.group(1));
this.snapshotWeekVersion = (int)matcher.group(2).charAt(0) - (int)'a';
this.rawString = version;
} catch (ParseException e) {
throw new IllegalArgumentException("Date implied by snapshot version is invalid.", e);
}
} else {
throw new IllegalArgumentException("Cannot parse " + version + " as a snapshot version.");
}
}
/**
* Retrieve the snapshot date parser.
* <p>
* We have to create a new instance of SimpleDateFormat every time as it is not thread safe.
* @return The date formatter.
*/
private static SimpleDateFormat getDateFormat() {
SimpleDateFormat format = new SimpleDateFormat("yy'w'ww", Locale.US);
format.setLenient(false);
return format;
}
/**
* Retrieve the snapshot version within a week, starting at zero.
* @return The weekly version
*/
public int getSnapshotWeekVersion() {
return snapshotWeekVersion;
}
/**
* Retrieve the week this snapshot was released.
* @return The week.
*/
public Date getSnapshotDate() {
return snapshotDate;
}
/**
* Retrieve the raw snapshot string (yy'w'ww[a-z]).
* @return The snapshot string.
*/
public String getSnapshotString() {
return rawString;
}
@Override
public int compareTo(SnapshotVersion o) {
if (o == null)
return 1;
return ComparisonChain.start().
compare(snapshotDate, o.getSnapshotDate()).
compare(snapshotWeekVersion, o.getSnapshotWeekVersion()).
result();
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj instanceof SnapshotVersion) {
SnapshotVersion other = (SnapshotVersion) obj;
return Objects.equal(snapshotDate, other.getSnapshotDate()) &&
snapshotWeekVersion == other.getSnapshotWeekVersion();
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(snapshotDate, snapshotWeekVersion);
}
@Override
public String toString() {
return rawString;
}
}

Datei anzeigen

@ -22,9 +22,9 @@ import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import com.comphenix.protocol.utility.MinecraftVersion; import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.utility.SnapshotVersion;
public class MinecraftVersionTest { public class MinecraftVersionTest {
@Test @Test
public void testComparision() { public void testComparision() {
MinecraftVersion within = new MinecraftVersion(1, 2, 5); MinecraftVersion within = new MinecraftVersion(1, 2, 5);
@ -38,6 +38,12 @@ public class MinecraftVersionTest {
assertFalse(outside.compareTo(within) < 0 && outside.compareTo(highest) < 0); assertFalse(outside.compareTo(within) < 0 && outside.compareTo(highest) < 0);
} }
@Test
public void testSnapshotVersion() {
MinecraftVersion version = MinecraftVersion.fromServerVersion("git-Spigot-1119 (MC: 13w39b)");
assertEquals(version.getSnapshot(), new SnapshotVersion("13w39b"));
}
public void testParsing() { public void testParsing() {
assertEquals(MinecraftVersion.extractVersion("CraftBukkit R3.0 (MC: 1.4.3)"), "1.4.3"); assertEquals(MinecraftVersion.extractVersion("CraftBukkit R3.0 (MC: 1.4.3)"), "1.4.3");
assertEquals(MinecraftVersion.extractVersion("CraftBukkit Test Beta 1 (MC: 1.10.01 )"), "1.10.01"); assertEquals(MinecraftVersion.extractVersion("CraftBukkit Test Beta 1 (MC: 1.10.01 )"), "1.10.01");

Datei anzeigen

@ -0,0 +1,41 @@
package com.comphenix.protocol.utility;
import static org.junit.Assert.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.junit.Test;
public class SnapshotVersionTest {
@Test
public void testDates() {
SnapshotVersion a = new SnapshotVersion("12w50b");
SnapshotVersion b = new SnapshotVersion("13w05a");
expect(a.getSnapshotDate(), 12, 50);
expect(b.getSnapshotDate(), 13, 5);
// Test equality
assertEquals(a, new SnapshotVersion("12w50b"));
}
@Test(expected=IllegalArgumentException.class)
public void testDateParsingProblem() {
// This date is not valid
new SnapshotVersion("12w80a");
}
@Test(expected=IllegalArgumentException.class)
public void testMissingWeekVersion() {
new SnapshotVersion("13w05");
}
private void expect(Date date, int year, int week) {
Calendar calendar = Calendar.getInstance(Locale.US);
calendar.setTime(date);
assertEquals(year, calendar.get(Calendar.YEAR) % 100);
assertEquals(week, calendar.get(Calendar.WEEK_OF_YEAR));
}
}