From 360c1f50605fdcbc2a653ff0708326aca8179122 Mon Sep 17 00:00:00 2001 From: Ravis96 Date: Sun, 30 Jun 2024 19:49:12 +0200 Subject: [PATCH] Fix VersionProvider --- .../template/nms/api/VersionProvider.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/plugin-core/nms/api/src/main/java/cc/dreamcode/template/nms/api/VersionProvider.java b/plugin-core/nms/api/src/main/java/cc/dreamcode/template/nms/api/VersionProvider.java index 3fde5fa..1c9766d 100644 --- a/plugin-core/nms/api/src/main/java/cc/dreamcode/template/nms/api/VersionProvider.java +++ b/plugin-core/nms/api/src/main/java/cc/dreamcode/template/nms/api/VersionProvider.java @@ -9,6 +9,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Locale; +import java.util.Map; import java.util.TreeMap; @UtilityClass @@ -35,15 +36,27 @@ public static VersionAccessor getVersionAccessor() { } private static String getNmsVersion() { + + final String[] nmsVersionSplit = Bukkit.getServer().getClass().getPackage().getName().split("\\."); + if (nmsVersionSplit.length >= 4) { + final String nmsVersion = nmsVersionSplit[3]; + if (nmsVersion.startsWith("v")) { + return nmsVersion; + } + } + try { Method getDataVersion = UnsafeValues.class.getMethod("getDataVersion"); int dataVersion = (int) getDataVersion.invoke(Bukkit.getServer().getUnsafe()); - return NEWER_NMS_VERSION.floorEntry(dataVersion).getValue(); - } - catch (NoSuchMethodException ignored) { - return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; + Map.Entry entry = NEWER_NMS_VERSION.floorEntry(dataVersion); + + if (entry == null) { + throw new RuntimeException("Cannot find server version"); + } + + return entry.getValue(); } - catch (IllegalAccessException | InvocationTargetException e) { + catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException("Cannot find server version", e); } }