-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathVersionHelper.java
More file actions
58 lines (44 loc) · 1.92 KB
/
VersionHelper.java
File metadata and controls
58 lines (44 loc) · 1.92 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
package at.helpch.chatchat.util;
import at.helpch.chatchat.api.exception.ChatChatException;
import com.google.common.primitives.Ints;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Class for detecting server version for legacy support :(
* @author Matt
*/
public final class VersionHelper {
public static final String NMS_VERSION = getNmsVersion();
public static final int CURRENT_VERSION = getCurrentVersion();
public static final int V1_19_0 = 1190;
/**
* Gets the current server version
*
* @return A protocol like number representing the version, for example 1.16.5 - 1165
*/
private static int getCurrentVersion() {
// No need to cache since will only run once
final Matcher matcher = Pattern.compile("(?<version>\\d+\\.\\d+)(?<patch>\\.\\d+)?").matcher(Bukkit.getBukkitVersion());
final StringBuilder stringBuilder = new StringBuilder();
if (matcher.find()) {
stringBuilder.append(matcher.group("version").replace(".", ""));
final String patch = matcher.group("patch");
if (patch == null) stringBuilder.append("0");
else stringBuilder.append(patch.replace(".", ""));
}
//noinspection UnstableApiUsage
final Integer version = Ints.tryParse(stringBuilder.toString());
// Should never fail
if (version == null) throw new ChatChatException("Could not retrieve server version!");
return version;
}
private static String getNmsVersion() {
final String version = Bukkit.getServer().getClass().getPackage().getName();
return version.substring(version.lastIndexOf('.') + 1);
}
public static Class<?> craftClass(@NotNull final String name) throws ClassNotFoundException {
return Class.forName("org.bukkit.craftbukkit." + NMS_VERSION + "." + name);
}
}