-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathVersionHelper.java
More file actions
93 lines (74 loc) · 2.53 KB
/
VersionHelper.java
File metadata and controls
93 lines (74 loc) · 2.53 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
92
93
package com.extendedclip.papi.expansion.player;
import com.google.common.primitives.Ints;
import org.bukkit.Bukkit;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.Player;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Matt (<a href="https://github.com/ipsk">@ipsk</a>)
*/
public final class VersionHelper {
private static final int VERSION = getCurrentVersion();
/**
* @see Damageable#getAbsorptionAmount()
*/
public static final boolean HAS_ABSORPTION_METHODS = VERSION >= 1_15_0;
/**
* @see Player#getPing()
*/
public static final boolean IS_1_17_OR_NEWER = VERSION >= 1_17_0;
/**
* @see Player#getLocale()
*/
public static final boolean IS_1_20_2_OR_NEWER = VERSION >= 1_20_2;
/**
* @see Player#getLocale()
*/
public static final boolean IS_1_20_4_OR_NEWER = VERSION >= 1_20_4;
/**
* @see Player#getLocale()
*/
public static final boolean IS_1_20_6_OR_NEWER = VERSION >= 1_20_6;
/**
* @see Player#getLocale()
*/
public static final boolean IS_MOJMAP = IS_1_20_6_OR_NEWER && doesClassExist("org.bukkit.craftbukkit.CraftServer");
private VersionHelper() { }
/**
* 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()) {
final String patch = matcher.group("patch");
stringBuilder
.append(matcher.group("version").replace(".", ""))
.append((patch == null) ? "0" : patch.replace(".", ""));
}
final Integer version = Ints.tryParse(stringBuilder.toString());
// Should never fail
if (version == null) {
throw new IllegalArgumentException("Could not retrieve server version!");
}
return version;
}
/**
* Detect specific class exists
*
* @param className
* @return true if the class exists, otherwise false
*/
private static boolean doesClassExist(String className) {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}