9
0
mirror of https://github.com/HibiscusMC/HibiscusCommons.git synced 2025-12-19 15:09:26 +00:00

feat: add ability for commons to run on versions not explicitly supported

This commit is contained in:
LoJoSho
2025-07-04 11:15:53 -05:00
parent 24a51684ba
commit ea02298e4e
3 changed files with 49 additions and 22 deletions

View File

@@ -6,11 +6,14 @@ import org.jetbrains.annotations.Nullable;
public enum MinecraftVersion { public enum MinecraftVersion {
v1_20_4, v1_20_4,
v1_20_6, v1_20_6,
v1_21,
v1_21_1, v1_21_1,
v1_21_2,
v1_21_3, v1_21_3,
v1_21_4, v1_21_4,
v1_21_5, v1_21_5,
v1_21_6, v1_21_6,
v1_21_7,
; ;
public boolean isHigher(MinecraftVersion other) { public boolean isHigher(MinecraftVersion other) {

View File

@@ -6,20 +6,21 @@ import org.bukkit.Bukkit;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class NMSHandlers { public class NMSHandlers {
private static final LinkedHashMap<MinecraftVersion, String> VERSION_MAP = new LinkedHashMap <>() {{ private static final LinkedHashMap<MinecraftVersion, MinecraftVersionInformation> VERSION_MAP = new LinkedHashMap <>() {{
put(MinecraftVersion.v1_20_4, "v1_20_R3"); put(MinecraftVersion.v1_20_6, new MinecraftVersionInformation("v1_20_R4", true));
// 1.20.5 is not supported; was imminently bumped to 1.20.6 put(MinecraftVersion.v1_21, new MinecraftVersionInformation("v1_21_R1", false)); // 1.20 is not supported; was imminently bumped to 1.21.1
put(MinecraftVersion.v1_20_6, "v1_20_R4"); put(MinecraftVersion.v1_21_1, new MinecraftVersionInformation("v1_21_R1", true));
// 1.20 is not supported; was imminently bumped to 1.21.1 put(MinecraftVersion.v1_21_2, new MinecraftVersionInformation("v1_21_R2", false)); // 1.20.2 is not supported; was imminently bumped to 1.21.3
put(MinecraftVersion.v1_21_1, "v1_21_R1"); put(MinecraftVersion.v1_21_3, new MinecraftVersionInformation("v1_21_R2", true));
// 1.20.2 is not supported; was imminently bumped to 1.21.3 put(MinecraftVersion.v1_21_4, new MinecraftVersionInformation("v1_21_R3", true));
put(MinecraftVersion.v1_21_3, "v1_21_R2"); put(MinecraftVersion.v1_21_5, new MinecraftVersionInformation("v1_21_R4", true));
put(MinecraftVersion.v1_21_4, "v1_21_R3"); put(MinecraftVersion.v1_21_6, new MinecraftVersionInformation("v1_21_R5", false));
put(MinecraftVersion.v1_21_5, "v1_21_R4"); put(MinecraftVersion.v1_21_7, new MinecraftVersionInformation("v1_21_R5", true));
put(MinecraftVersion.v1_21_6, "v1_21_R5");
}}; }};
private static NMSHandler handler; private static NMSHandler handler;
@@ -40,7 +41,7 @@ public class NMSHandlers {
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('-'));
MinecraftVersion enumVersion = MinecraftVersion.fromVersionString(minecraftVersion); MinecraftVersion enumVersion = MinecraftVersion.fromVersionString(minecraftVersion);
String packageVersion = VERSION_MAP.get(enumVersion); MinecraftVersionInformation 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.");
@@ -50,23 +51,37 @@ 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 (MinecraftVersion supportedVersion : VERSION_MAP.keySet()) { sendSupportedVersions();
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.");
throw new RuntimeException("Failed to detect the server version."); throw new RuntimeException("Failed to detect the server version.");
} }
for (String selectedVersion : VERSION_MAP.values()) { for (Map.Entry<MinecraftVersion, MinecraftVersionInformation> selectedVersion : VERSION_MAP.entrySet()) {
if (!selectedVersion.contains(packageVersion)) { String internalReference = selectedVersion.getValue().internalReference();
if (!internalReference.contains(packageVersion.internalReference())) {
continue; continue;
} }
//MessagesUtil.sendDebugMessages(packageVersion + " has been detected.", Level.INFO);
version = enumVersion; version = selectedVersion.getKey();
if (!selectedVersion.getValue().supported()) {
HibiscusCommonsPlugin.getInstance().getLogger().severe("Detected Deprecated Version!");
HibiscusCommonsPlugin.getInstance().getLogger().severe(" ");
HibiscusCommonsPlugin.getInstance().getLogger().severe("Package Version: " + packageVersion.internalReference());
HibiscusCommonsPlugin.getInstance().getLogger().severe("Is Supported: " + packageVersion.supported());
HibiscusCommonsPlugin.getInstance().getLogger().severe(" ");
HibiscusCommonsPlugin.getInstance().getLogger().severe("This version has no explicit support for it. There maybe errors that are unfixable.");
HibiscusCommonsPlugin.getInstance().getLogger().severe("Consider moving to a version with explicit support. ");
HibiscusCommonsPlugin.getInstance().getLogger().severe(" ");
HibiscusCommonsPlugin.getInstance().getLogger().severe("Supported versions:");
sendSupportedVersions();
HibiscusCommonsPlugin.getInstance().getLogger().severe(" ");
}
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.internalReference() + ".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.internalReference() + ".NMSPackets").getConstructor().newInstance();
handler = new NMSHandler(utilHandler, packetHandler); handler = new NMSHandler(utilHandler, packetHandler);
return; return;
} catch (ClassNotFoundException | InvocationTargetException | InstantiationException | } catch (ClassNotFoundException | InvocationTargetException | InstantiationException |
@@ -75,4 +90,13 @@ public class NMSHandlers {
} }
} }
} }
private static void sendSupportedVersions() {
for (Map.Entry<MinecraftVersion, MinecraftVersionInformation> entry : VERSION_MAP.entrySet()) {
if (!entry.getValue().supported()) continue;
HibiscusCommonsPlugin.getInstance().getLogger().severe(" - " + entry.getKey().toVersionString());
}
}
private record MinecraftVersionInformation(String internalReference, boolean supported) {}
} }

View File

@@ -6,7 +6,7 @@ import java.util.logging.Level;
public class MessagesUtil { public class MessagesUtil {
private static boolean debug = false; private static boolean debug = true;
public static void sendDebugMessages(String message) { public static void sendDebugMessages(String message) {