mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-11-01 00:10:32 +01:00
Create NMSVersion for better backwards compatibility.
This commit is contained in:
parent
785bd5bfcf
commit
e674056b3a
@ -1,5 +1,7 @@
|
||||
package net.ess3.nms.refl;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.HashBasedTable;
|
||||
import com.google.common.collect.Table;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -10,8 +12,16 @@ import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ReflUtil {
|
||||
public static final NMSVersion V1_8_R1 = NMSVersion.fromString("v1_8_R1");
|
||||
public static final NMSVersion V1_8_R2 = NMSVersion.fromString("v1_8_R2");
|
||||
public static final NMSVersion V1_8_R3 = NMSVersion.fromString("v1_8_R3");
|
||||
public static final NMSVersion V1_9_R1 = NMSVersion.fromString("v1_9_R1");
|
||||
public static final NMSVersion V1_10_R1 = NMSVersion.fromString("v1_10_R1");
|
||||
private static NMSVersion nmsVersionObject;
|
||||
private static String nmsVersion;
|
||||
|
||||
public static String getNMSVersion() {
|
||||
@ -23,6 +33,13 @@ public class ReflUtil {
|
||||
return nmsVersion;
|
||||
}
|
||||
|
||||
public static NMSVersion getNmsVersionObject() {
|
||||
if (nmsVersionObject == null) {
|
||||
nmsVersionObject = NMSVersion.fromString(getNMSVersion());
|
||||
}
|
||||
return nmsVersionObject;
|
||||
}
|
||||
|
||||
public static Class<?> getNMSClass(String className) {
|
||||
return getClassCached("net.minecraft.server." + getNMSVersion() + "." + className);
|
||||
}
|
||||
@ -200,4 +217,104 @@ public class ReflUtil {
|
||||
return Arrays.deepHashCode(params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* https://gist.github.com/SupaHam/dad1db6406596c5f8e4b221ff473831c
|
||||
*
|
||||
* @author SupaHam (<a href="https://github.com/SupaHam">https://github.com/SupaHam</a>)
|
||||
*/
|
||||
public static class NMSVersion implements Comparable<NMSVersion> {
|
||||
private static final Pattern VERSION_PATTERN = Pattern.compile("^v(\\d+)_(\\d+)_R(\\d+)");
|
||||
private final int major;
|
||||
private final int minor;
|
||||
private final int release;
|
||||
|
||||
public static NMSVersion fromString(String string) {
|
||||
Preconditions.checkNotNull(string, "string cannot be null.");
|
||||
Matcher matcher = VERSION_PATTERN.matcher(string);
|
||||
Preconditions.checkArgument(matcher.matches(), string + " is not in valid version format. e.g. v1_10_R1");
|
||||
return new NMSVersion(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3)));
|
||||
}
|
||||
|
||||
private NMSVersion(int major, int minor, int release) {
|
||||
this.major = major;
|
||||
this.minor = minor;
|
||||
this.release = release;
|
||||
}
|
||||
|
||||
public boolean isHigherThan(NMSVersion o) {
|
||||
return compareTo(o) > 0;
|
||||
}
|
||||
|
||||
public boolean isHigherThanOrEqualTo(NMSVersion o) {
|
||||
return compareTo(o) >= 0;
|
||||
}
|
||||
|
||||
public boolean isLowerThan(NMSVersion o) {
|
||||
return compareTo(o) < 0;
|
||||
}
|
||||
|
||||
public boolean isLowerThanOrEqualTo(NMSVersion o) {
|
||||
return compareTo(o) <= 0;
|
||||
}
|
||||
|
||||
public int getMajor() {
|
||||
return major;
|
||||
}
|
||||
|
||||
public int getMinor() {
|
||||
return minor;
|
||||
}
|
||||
|
||||
public int getRelease() {
|
||||
return release;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
NMSVersion that = (NMSVersion) o;
|
||||
return major == that.major &&
|
||||
minor == that.minor &&
|
||||
release == that.release;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(major, minor, release);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "v" + major + "_" + minor + "_R" + release;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(NMSVersion o) {
|
||||
if (major < o.major) {
|
||||
return -1;
|
||||
} else if (major > o.major) {
|
||||
return 1;
|
||||
} else { // equal major
|
||||
if (minor < o.minor) {
|
||||
return -1;
|
||||
} else if (minor > o.minor) {
|
||||
return 1;
|
||||
} else { // equal minor
|
||||
if (release < o.release) {
|
||||
return -1;
|
||||
} else if (release > o.release) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0; // o is the same version as this.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,51 @@
|
||||
package net.ess3.nms.refl;
|
||||
|
||||
import net.ess3.nms.refl.ReflUtil.NMSVersion;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NMSVersionTest {
|
||||
|
||||
@Test
|
||||
public void testMajor() throws Exception {
|
||||
NMSVersion v2_9_R1 = NMSVersion.fromString("v2_9_R1");
|
||||
|
||||
Assert.assertEquals(2, v2_9_R1.getMajor());
|
||||
Assert.assertEquals(9, v2_9_R1.getMinor());
|
||||
Assert.assertEquals(1, v2_9_R1.getRelease());
|
||||
|
||||
Assert.assertEquals(v2_9_R1.toString(), "v2_9_R1");
|
||||
|
||||
Assert.assertTrue(v2_9_R1.isHigherThan(NMSVersion.fromString("v1_10_R1")));
|
||||
Assert.assertTrue(v2_9_R1.isHigherThanOrEqualTo(NMSVersion.fromString("v1_9_R1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinor() throws Exception {
|
||||
NMSVersion v1_10_R1 = NMSVersion.fromString("v1_10_R1");
|
||||
|
||||
Assert.assertEquals(1, v1_10_R1.getMajor());
|
||||
Assert.assertEquals(10, v1_10_R1.getMinor());
|
||||
Assert.assertEquals(1, v1_10_R1.getRelease());
|
||||
|
||||
Assert.assertEquals(v1_10_R1.toString(), "v1_10_R1");
|
||||
|
||||
Assert.assertTrue(NMSVersion.fromString("v1_9_R1").isLowerThan(v1_10_R1));
|
||||
Assert.assertTrue(NMSVersion.fromString("v1_9_R1").isLowerThanOrEqualTo(v1_10_R1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRelease() throws Exception {
|
||||
NMSVersion v1_9_R2 = NMSVersion.fromString("v1_9_R2");
|
||||
|
||||
Assert.assertEquals(1, v1_9_R2.getMajor());
|
||||
Assert.assertEquals(9, v1_9_R2.getMinor());
|
||||
Assert.assertEquals(2, v1_9_R2.getRelease());
|
||||
|
||||
Assert.assertEquals(v1_9_R2.toString(), "v1_9_R2");
|
||||
Assert.assertEquals(v1_9_R2, NMSVersion.fromString("v1_9_R2"));
|
||||
|
||||
Assert.assertTrue(v1_9_R2.isHigherThan(NMSVersion.fromString("v1_9_R1")));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user