mirror of
https://github.com/HibiscusMC/HibiscusCommons.git
synced 2025-12-23 08:59:18 +00:00
feat: add better support for comparing Minecraft versions
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
package me.lojosho.hibiscuscommons.nms;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public enum MinecraftVersion {
|
||||||
|
v1_20_4,
|
||||||
|
v1_20_6,
|
||||||
|
v1_21_1,
|
||||||
|
v1_21_3,
|
||||||
|
v1_21_4;
|
||||||
|
|
||||||
|
public boolean isHigher(MinecraftVersion other) {
|
||||||
|
return this.ordinal() > other.ordinal();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isHigherOrEqual(MinecraftVersion other) {
|
||||||
|
return this.ordinal() >= other.ordinal();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isLowerOrEqual(MinecraftVersion other) {
|
||||||
|
return this.ordinal() <= other.ordinal();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isLower(MinecraftVersion other) {
|
||||||
|
return this.ordinal() < other.ordinal();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the enum into a usable string.
|
||||||
|
* @return Returns string of version (such as 1.21.4)
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public String toVersionString() {
|
||||||
|
// Remove the "v" prefix and replace underscores with dots
|
||||||
|
return name().substring(1).replace('_', '.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the enum from a version. Returns null if invalid version
|
||||||
|
* @param version A version number, such as 1.21.4
|
||||||
|
* @return Returns the enum, such as v1_21_4
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public static MinecraftVersion fromVersionString(@NotNull String version) {
|
||||||
|
String enumName = "v" + version.replace('.', '_');
|
||||||
|
for (MinecraftVersion v : values()) {
|
||||||
|
if (v.name().equals(enumName)) {
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,20 +9,20 @@ import java.util.LinkedHashMap;
|
|||||||
|
|
||||||
public class NMSHandlers {
|
public class NMSHandlers {
|
||||||
|
|
||||||
private static final LinkedHashMap<String, String> VERSION_MAP = new LinkedHashMap <>() {{
|
private static final LinkedHashMap<MinecraftVersion, String> VERSION_MAP = new LinkedHashMap <>() {{
|
||||||
put("1.20.4", "v1_20_R3");
|
put(MinecraftVersion.v1_20_4, "v1_20_R3");
|
||||||
// 1.20.5 is not supported; was imminently bumped to 1.20.6
|
// 1.20.5 is not supported; was imminently bumped to 1.20.6
|
||||||
put("1.20.6", "v1_20_R4");
|
put(MinecraftVersion.v1_20_6, "v1_20_R4");
|
||||||
// 1.20 is not supported; was imminently bumped to 1.21.1
|
// 1.20 is not supported; was imminently bumped to 1.21.1
|
||||||
put("1.21.1", "v1_21_R1");
|
put(MinecraftVersion.v1_21_1, "v1_21_R1");
|
||||||
// 1.20.2 is not supported; was imminently bumped to 1.21.3
|
// 1.20.2 is not supported; was imminently bumped to 1.21.3
|
||||||
put("1.21.3", "v1_21_R2");
|
put(MinecraftVersion.v1_21_3, "v1_21_R2");
|
||||||
put("1.21.4", "v1_21_R3");
|
put(MinecraftVersion.v1_21_4, "v1_21_R3");
|
||||||
}};
|
}};
|
||||||
|
|
||||||
private static NMSHandler handler;
|
private static NMSHandler handler;
|
||||||
@Getter
|
@Getter
|
||||||
private static String version;
|
private static MinecraftVersion version;
|
||||||
|
|
||||||
public static boolean isVersionSupported() {
|
public static boolean isVersionSupported() {
|
||||||
return getVersion() != null;
|
return getVersion() != null;
|
||||||
@@ -37,7 +37,8 @@ public class NMSHandlers {
|
|||||||
if (handler != null) return;
|
if (handler != null) return;
|
||||||
final String bukkitVersion = Bukkit.getServer().getBukkitVersion();
|
final String bukkitVersion = Bukkit.getServer().getBukkitVersion();
|
||||||
String minecraftVersion = bukkitVersion.substring(0, bukkitVersion.indexOf('-'));
|
String minecraftVersion = bukkitVersion.substring(0, bukkitVersion.indexOf('-'));
|
||||||
String packageVersion = VERSION_MAP.get(minecraftVersion);
|
MinecraftVersion enumVersion = MinecraftVersion.fromVersionString(minecraftVersion);
|
||||||
|
String packageVersion = VERSION_MAP.get(enumVersion);
|
||||||
|
|
||||||
if (packageVersion == null) {
|
if (packageVersion == null) {
|
||||||
HibiscusCommonsPlugin.getInstance().getLogger().severe("An error occurred while trying to detect the version of the server.");
|
HibiscusCommonsPlugin.getInstance().getLogger().severe("An error occurred while trying to detect the version of the server.");
|
||||||
@@ -47,8 +48,8 @@ public class NMSHandlers {
|
|||||||
HibiscusCommonsPlugin.getInstance().getLogger().severe("Detected Package Version: " + packageVersion);
|
HibiscusCommonsPlugin.getInstance().getLogger().severe("Detected Package Version: " + packageVersion);
|
||||||
HibiscusCommonsPlugin.getInstance().getLogger().severe(" ");
|
HibiscusCommonsPlugin.getInstance().getLogger().severe(" ");
|
||||||
HibiscusCommonsPlugin.getInstance().getLogger().severe("Supported versions:");
|
HibiscusCommonsPlugin.getInstance().getLogger().severe("Supported versions:");
|
||||||
for (String supportedVersion : VERSION_MAP.keySet()) {
|
for (MinecraftVersion supportedVersion : VERSION_MAP.keySet()) {
|
||||||
HibiscusCommonsPlugin.getInstance().getLogger().severe(" - " + supportedVersion);
|
HibiscusCommonsPlugin.getInstance().getLogger().severe(" - " + supportedVersion.toVersionString());
|
||||||
}
|
}
|
||||||
HibiscusCommonsPlugin.getInstance().getLogger().severe(" ");
|
HibiscusCommonsPlugin.getInstance().getLogger().severe(" ");
|
||||||
HibiscusCommonsPlugin.getInstance().getLogger().severe("Please update HibiscusCommons that supports this version.");
|
HibiscusCommonsPlugin.getInstance().getLogger().severe("Please update HibiscusCommons that supports this version.");
|
||||||
@@ -60,7 +61,7 @@ public class NMSHandlers {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//MessagesUtil.sendDebugMessages(packageVersion + " has been detected.", Level.INFO);
|
//MessagesUtil.sendDebugMessages(packageVersion + " has been detected.", Level.INFO);
|
||||||
version = packageVersion;
|
version = enumVersion;
|
||||||
try {
|
try {
|
||||||
NMSUtils utilHandler = (NMSUtils) Class.forName("me.lojosho.hibiscuscommons.nms." + packageVersion + ".NMSUtils").getConstructor().newInstance();
|
NMSUtils utilHandler = (NMSUtils) Class.forName("me.lojosho.hibiscuscommons.nms." + packageVersion + ".NMSUtils").getConstructor().newInstance();
|
||||||
NMSPackets packetHandler = (NMSPackets) Class.forName("me.lojosho.hibiscuscommons.nms." + packageVersion + ".NMSPackets").getConstructor().newInstance();
|
NMSPackets packetHandler = (NMSPackets) Class.forName("me.lojosho.hibiscuscommons.nms." + packageVersion + ".NMSPackets").getConstructor().newInstance();
|
||||||
|
|||||||
Reference in New Issue
Block a user