From d21ebed81da9bffa657ad7e9e5aad530ed63f442 Mon Sep 17 00:00:00 2001 From: lojosho Date: Tue, 18 Mar 2025 20:29:30 -0500 Subject: [PATCH] feat: add better support for comparing Minecraft versions --- .../hibiscuscommons/nms/MinecraftVersion.java | 54 +++++++++++++++++++ .../hibiscuscommons/nms/NMSHandlers.java | 23 ++++---- 2 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 common/src/main/java/me/lojosho/hibiscuscommons/nms/MinecraftVersion.java diff --git a/common/src/main/java/me/lojosho/hibiscuscommons/nms/MinecraftVersion.java b/common/src/main/java/me/lojosho/hibiscuscommons/nms/MinecraftVersion.java new file mode 100644 index 0000000..8e540fb --- /dev/null +++ b/common/src/main/java/me/lojosho/hibiscuscommons/nms/MinecraftVersion.java @@ -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; + } +} diff --git a/common/src/main/java/me/lojosho/hibiscuscommons/nms/NMSHandlers.java b/common/src/main/java/me/lojosho/hibiscuscommons/nms/NMSHandlers.java index 30cf84d..0bb951b 100644 --- a/common/src/main/java/me/lojosho/hibiscuscommons/nms/NMSHandlers.java +++ b/common/src/main/java/me/lojosho/hibiscuscommons/nms/NMSHandlers.java @@ -9,20 +9,20 @@ import java.util.LinkedHashMap; public class NMSHandlers { - private static final LinkedHashMap VERSION_MAP = new LinkedHashMap <>() {{ - put("1.20.4", "v1_20_R3"); + private static final LinkedHashMap VERSION_MAP = new LinkedHashMap <>() {{ + put(MinecraftVersion.v1_20_4, "v1_20_R3"); // 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 - 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 - put("1.21.3", "v1_21_R2"); - put("1.21.4", "v1_21_R3"); + put(MinecraftVersion.v1_21_3, "v1_21_R2"); + put(MinecraftVersion.v1_21_4, "v1_21_R3"); }}; private static NMSHandler handler; @Getter - private static String version; + private static MinecraftVersion version; public static boolean isVersionSupported() { return getVersion() != null; @@ -37,7 +37,8 @@ public class NMSHandlers { if (handler != null) return; final String bukkitVersion = Bukkit.getServer().getBukkitVersion(); 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) { 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(" "); HibiscusCommonsPlugin.getInstance().getLogger().severe("Supported versions:"); - for (String supportedVersion : VERSION_MAP.keySet()) { - HibiscusCommonsPlugin.getInstance().getLogger().severe(" - " + supportedVersion); + for (MinecraftVersion supportedVersion : VERSION_MAP.keySet()) { + HibiscusCommonsPlugin.getInstance().getLogger().severe(" - " + supportedVersion.toVersionString()); } HibiscusCommonsPlugin.getInstance().getLogger().severe(" "); HibiscusCommonsPlugin.getInstance().getLogger().severe("Please update HibiscusCommons that supports this version."); @@ -60,7 +61,7 @@ public class NMSHandlers { continue; } //MessagesUtil.sendDebugMessages(packageVersion + " has been detected.", Level.INFO); - version = packageVersion; + version = enumVersion; try { 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();