-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathVersionMatcher.java
More file actions
91 lines (83 loc) · 3.59 KB
/
VersionMatcher.java
File metadata and controls
91 lines (83 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package net.wesjd.anvilgui.version;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
/**
* Matches the server's NMS version to its {@link VersionWrapper}
*
* @author Wesley Smith
* @since 1.2.1
*/
public class VersionMatcher {
/** Maps a Minecraft version string to the corresponding revision string */
private static final Map<String, String> VERSION_TO_REVISION = new HashMap<String, String>() {
{
this.put("1.20", "1_20_R1");
this.put("1.20.1", "1_20_R1");
this.put("1.20.2", "1_20_R2");
this.put("1.20.3", "1_20_R3");
this.put("1.20.4", "1_20_R3");
this.put("1.20.5", "1_20_R4");
this.put("1.20.6", "1_20_R4");
this.put("1.21", "1_21_R1");
this.put("1.21.1", "1_21_R1");
this.put("1.21.2", "1_21_R2");
this.put("1.21.3", "1_21_R2");
this.put("1.21.4", "1_21_R3");
this.put("1.21.5", "1_21_R4");
this.put("1.21.6", "1_21_R5");
}
};
/* This needs to be updated to reflect the newest available version wrapper */
private static final String FALLBACK_REVISION = "1_21_R5";
/**
* Matches the server version to it's {@link VersionWrapper}
*
* @return The {@link VersionWrapper} for this server
* @throws IllegalStateException If the version wrapper failed to be instantiated or is unable to be found
*/
public VersionWrapper match() {
String craftBukkitPackage = Bukkit.getServer().getClass().getPackage().getName();
String rVersion;
if (!craftBukkitPackage.contains(".v")) { // cb package not relocated (i.e. paper 1.20.5+)
final String version = Bukkit.getBukkitVersion().split("-")[0];
rVersion = VERSION_TO_REVISION.getOrDefault(version, FALLBACK_REVISION);
} else {
rVersion = craftBukkitPackage.split("\\.")[3].substring(1);
}
boolean isMojMap = isMojangMapped(rVersion);
try {
return (VersionWrapper)
getWrapperClass(rVersion, isMojMap).getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException exception) {
throw new IllegalStateException("AnvilGUI does not support server version \"" + rVersion + "\"", exception);
} catch (ReflectiveOperationException exception) {
throw new IllegalStateException("Failed to instantiate version wrapper for version " + rVersion, exception);
}
}
private Class<?> getWrapperClass(String version, boolean isMojMap) throws ClassNotFoundException {
String pkg = getClass().getPackage().getName();
if (isMojMap) { // if mojang-mapped server, use MojangWrapper
try {
return Class.forName(pkg + ".MojangWrapper" + version);
} catch (ClassNotFoundException ignored) {
}
}
// then try usual wrapper
return Class.forName(pkg + ".Wrapper" + version);
}
private static boolean isMojangMapped(String version) {
// firstly check for paper
try {
Class.forName("com.destroystokyo.paper.ParticleBuilder");
} catch (ClassNotFoundException ignored) {
return false;
}
// then check version
final String[] versionNumbers = version.replace("R", "").split("_");
int major = Integer.parseInt(versionNumbers[1]);
int minor = versionNumbers.length > 2 ? Integer.parseInt(versionNumbers[2]) : 0;
if (major == 20 && minor == 4) return true; // 1.20.5/6
return major > 20; // >= 1.21
}
}