Skip to content

Commit d6696e0

Browse files
committed
backport VersionUtils patch
1 parent 65e4eb9 commit d6696e0

File tree

2 files changed

+242
-5
lines changed

2 files changed

+242
-5
lines changed

pom.xml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
<exclude>META-INF/maven/**</exclude>
5454
</excludes>
5555
</filter>
56+
<filter>
57+
<artifact>me.hsgamer:hscore-bukkit-utils</artifact>
58+
<excludes>
59+
<exclude>me/hsgamer/hscore/bukkit/utils/VersionUtils*</exclude>
60+
</excludes>
61+
</filter>
5662
</filters>
5763
<relocations>
5864
<!-- HSCore -->
@@ -90,10 +96,6 @@
9096
<artifactId>maven-javadoc-plugin</artifactId>
9197
<version>3.10.0</version>
9298
<configuration>
93-
<links>
94-
<link>https://hsgamer.github.io/HSCore/</link>
95-
<link>https://projectunified.github.io/MineLib/</link>
96-
</links>
9799
<outputDirectory>${project.build.directory}/reports</outputDirectory>
98100
</configuration>
99101
<executions>
@@ -152,7 +154,7 @@
152154
</repository>
153155
<repository>
154156
<id>placeholderapi</id>
155-
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
157+
<url>https://repo.extendedclip.com/releases/</url>
156158
</repository>
157159
<repository>
158160
<id>minecraft-repo</id>
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
package me.hsgamer.hscore.bukkit.utils;
2+
3+
import org.bukkit.Bukkit;
4+
5+
import java.util.Optional;
6+
import java.util.regex.Matcher;
7+
import java.util.regex.Pattern;
8+
9+
/**
10+
* The helper class for server versions
11+
*/
12+
public final class VersionUtils {
13+
private static final int MAJOR_VERSION;
14+
private static final int MINOR_VERSION;
15+
private static final int PATCH_VERSION;
16+
private static final boolean IS_CRAFTBUKKIT_MAPPED;
17+
private static final String CRAFTBUKKIT_PACKAGE_VERSION;
18+
19+
static {
20+
Matcher versionMatcher = Pattern.compile("MC: (\\d+)\\.(\\d+)(\\.(\\d+))?").matcher(Bukkit.getVersion());
21+
if (versionMatcher.find()) {
22+
int majorVersion = Integer.parseInt(versionMatcher.group(1));
23+
int minorVersion = Integer.parseInt(versionMatcher.group(2));
24+
int patchVersion = Optional.ofNullable(versionMatcher.group(4)).filter(s -> !s.isEmpty()).map(Integer::parseInt).orElse(0);
25+
if (majorVersion == 1) {
26+
MAJOR_VERSION = minorVersion;
27+
MINOR_VERSION = patchVersion;
28+
PATCH_VERSION = 0;
29+
} else {
30+
MAJOR_VERSION = majorVersion;
31+
MINOR_VERSION = minorVersion;
32+
PATCH_VERSION = patchVersion;
33+
}
34+
} else {
35+
MAJOR_VERSION = -1;
36+
MINOR_VERSION = -1;
37+
PATCH_VERSION = -1;
38+
}
39+
40+
Matcher packageMatcher = Pattern.compile("v\\d+_\\d+_R\\d+").matcher(Bukkit.getServer().getClass().getPackage().getName());
41+
IS_CRAFTBUKKIT_MAPPED = packageMatcher.find();
42+
if (IS_CRAFTBUKKIT_MAPPED) {
43+
CRAFTBUKKIT_PACKAGE_VERSION = packageMatcher.group();
44+
} else {
45+
CRAFTBUKKIT_PACKAGE_VERSION = "";
46+
}
47+
}
48+
49+
private VersionUtils() {
50+
// EMPTY
51+
}
52+
53+
/**
54+
* Get the major version of the server
55+
*
56+
* @return the version
57+
*/
58+
public static int getMajorVersion() {
59+
return MAJOR_VERSION;
60+
}
61+
62+
/**
63+
* Get the minor version of the server
64+
*
65+
* @return the version
66+
*/
67+
public static int getMinorVersion() {
68+
return MINOR_VERSION;
69+
}
70+
71+
/**
72+
* Get the patch version of the server
73+
*
74+
* @return the version
75+
*/
76+
public static int getPatchVersion() {
77+
return PATCH_VERSION;
78+
}
79+
80+
/**
81+
* Compare the server version with the given version
82+
*
83+
* @param majorVersion the major version
84+
* @param minorVersion the minor version
85+
* @param patchVersion the patch version
86+
*
87+
* @return 0 if the versions are the same, -1 if the given version is lower, 1 if the given version is higher
88+
*/
89+
public static int compare(int majorVersion, int minorVersion, int patchVersion) {
90+
int compare = Integer.compare(majorVersion, MAJOR_VERSION);
91+
if (compare == 0) {
92+
compare = Integer.compare(minorVersion, MINOR_VERSION);
93+
}
94+
if (compare == 0) {
95+
compare = Integer.compare(patchVersion, PATCH_VERSION);
96+
}
97+
return compare;
98+
}
99+
100+
/**
101+
* Compare the server version with the given version
102+
*
103+
* @param majorVersion the major version
104+
* @param minorVersion the minor version
105+
*
106+
* @return 0 if the versions are the same, -1 if the given version is lower, 1 if the given version is higher
107+
*/
108+
public static int compare(int majorVersion, int minorVersion) {
109+
return compare(majorVersion, minorVersion, getPatchVersion());
110+
}
111+
112+
/**
113+
* Compare the server version with the given version
114+
*
115+
* @param majorVersion the major version
116+
*
117+
* @return 0 if the versions are the same, -1 if the given version is lower, 1 if the given version is higher
118+
*/
119+
public static int compare(int majorVersion) {
120+
return compare(majorVersion, getMinorVersion());
121+
}
122+
123+
/**
124+
* Check if the server major version is at least the given major version
125+
*
126+
* @param majorVersion the major version to check
127+
*
128+
* @return true if it is
129+
*/
130+
public static boolean isAtLeast(int majorVersion) {
131+
return compare(majorVersion) >= 0;
132+
}
133+
134+
/**
135+
* Check if the server version is at least the given version
136+
*
137+
* @param majorVersion the major version to check
138+
* @param minorVersion the minor version to check
139+
*
140+
* @return true if it is
141+
*/
142+
public static boolean isAtLeast(int majorVersion, int minorVersion) {
143+
return compare(majorVersion, minorVersion) >= 0;
144+
}
145+
146+
/**
147+
* Check if the server version is at the given version
148+
*
149+
* @param majorVersion the major version to check
150+
*
151+
* @return true if it is
152+
*/
153+
public static boolean isAt(int majorVersion) {
154+
return compare(majorVersion) == 0;
155+
}
156+
157+
/**
158+
* Check if the server version is at the given version
159+
*
160+
* @param majorVersion the major version to check
161+
* @param minorVersion the minor version to check
162+
*
163+
* @return true if it is
164+
*/
165+
public static boolean isAt(int majorVersion, int minorVersion) {
166+
return compare(majorVersion, minorVersion) == 0;
167+
}
168+
169+
/**
170+
* Check if the server version is newer than the given version
171+
*
172+
* @param majorVersion the major version to check
173+
*
174+
* @return true if it is
175+
*/
176+
public static boolean isNewerThan(int majorVersion) {
177+
return compare(majorVersion) > 0;
178+
}
179+
180+
/**
181+
* Check if the server version is newer than the given version
182+
*
183+
* @param majorVersion the major version to check
184+
* @param minorVersion the minor version to check
185+
*
186+
* @return true if it is
187+
*/
188+
public static boolean isNewerThan(int majorVersion, int minorVersion) {
189+
return compare(majorVersion, minorVersion) > 0;
190+
}
191+
192+
/**
193+
* Check if the server version is lower than the given version
194+
*
195+
* @param majorVersion the major version to check
196+
*
197+
* @return true if it is
198+
*/
199+
public static boolean isLowerThan(int majorVersion) {
200+
return compare(majorVersion) < 0;
201+
}
202+
203+
/**
204+
* Check if the server version is lower than the given version
205+
*
206+
* @param majorVersion the major version to check
207+
* @param minorVersion the minor version to check
208+
*
209+
* @return true if it is
210+
*/
211+
public static boolean isLowerThan(int majorVersion, int minorVersion) {
212+
return compare(majorVersion, minorVersion) < 0;
213+
}
214+
215+
/**
216+
* Check if the server is using CraftBukkit mappings.
217+
* CraftBukkit mappings are usually used in Spigot and old Paper versions.
218+
* It's useful to check whether the server is using CraftBukkit mappings (Spigot, old Paper) or new Paper mappings.
219+
* <a href="https://forums.papermc.io/threads/important-dev-psa-future-removal-of-cb-package-relocation.1106/">More info</a>
220+
*
221+
* @return true if it is
222+
*/
223+
public static boolean isCraftBukkitMapped() {
224+
return IS_CRAFTBUKKIT_MAPPED;
225+
}
226+
227+
/**
228+
* Get the CraftBukkit package version
229+
*
230+
* @return the CraftBukkit package version, or empty if {@link #isCraftBukkitMapped()} returns false
231+
*/
232+
public static String getCraftBukkitPackageVersion() {
233+
return CRAFTBUKKIT_PACKAGE_VERSION;
234+
}
235+
}

0 commit comments

Comments
 (0)