From 5682a87c95e765eac13c311ee2170560bd2276d3 Mon Sep 17 00:00:00 2001 From: Almighty-Satan <43804777+Almighty-Satan@users.noreply.github.com> Date: Sat, 7 Oct 2023 16:59:16 +0200 Subject: [PATCH] Make VersionUtils load on Magma 1.20 --- .../cfw/version/CrucibleVersionAdapter.java | 2 +- .../version/OneNineteenVersionAdapter.java | 2 +- .../cfw/version/OneSevenVersionAdapter.java | 2 +- .../cfw/version/OneSixteenVersionAdapter.java | 2 +- .../cfw/version/OneTwelveVersionAdapter.java | 2 +- .../cfw/version/OneTwentyVersionAdapter.java | 5 ++++ .../varoplugin/cfw/version/VersionUtils.java | 25 +++++++++++++------ 7 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/varoplugin/cfw/version/CrucibleVersionAdapter.java b/src/main/java/de/varoplugin/cfw/version/CrucibleVersionAdapter.java index 74766ebd..c2b46977 100644 --- a/src/main/java/de/varoplugin/cfw/version/CrucibleVersionAdapter.java +++ b/src/main/java/de/varoplugin/cfw/version/CrucibleVersionAdapter.java @@ -39,7 +39,7 @@ protected void initServerProperties() throws ClassNotFoundException, NoSuchMetho @Override protected void initPlayer() throws NoSuchMethodException, SecurityException, NoSuchFieldException, ClassNotFoundException { - this.nmsPlayerClass = Class.forName("org.bukkit.craftbukkit." + VersionUtils.getNmsVersion() + ".entity.CraftPlayer").getMethod("getHandle").getReturnType(); + this.nmsPlayerClass = Class.forName(VersionUtils.getCraftBukkitPackage() + ".entity.CraftPlayer").getMethod("getHandle").getReturnType(); this.pingField = this.nmsPlayerClass.getField("field_71138_i"); this.connectionField = this.nmsPlayerClass.getField("field_71135_a"); this.sendPacketMethod = this.connectionField.getType().getMethod("func_147359_a", Class.forName("net.minecraft.network.Packet")); diff --git a/src/main/java/de/varoplugin/cfw/version/OneNineteenVersionAdapter.java b/src/main/java/de/varoplugin/cfw/version/OneNineteenVersionAdapter.java index c4d612f1..0d7c03ef 100644 --- a/src/main/java/de/varoplugin/cfw/version/OneNineteenVersionAdapter.java +++ b/src/main/java/de/varoplugin/cfw/version/OneNineteenVersionAdapter.java @@ -34,7 +34,7 @@ public class OneNineteenVersionAdapter extends OneSeventeenVersionAdapter { @Override public void forceClearWorlds() { try { - Field dedicatedServerField = Class.forName("org.bukkit.craftbukkit." + VersionUtils.getNmsVersion() + ".CraftServer").getDeclaredField("console"); + Field dedicatedServerField = Class.forName(VersionUtils.getCraftBukkitPackage() + ".CraftServer").getDeclaredField("console"); dedicatedServerField.setAccessible(true); Object dedicatedServer = dedicatedServerField.get(Bukkit.getServer()); Field worldServerField = dedicatedServer.getClass().getSuperclass().getDeclaredField(this.getWorldServerFieldName()); diff --git a/src/main/java/de/varoplugin/cfw/version/OneSevenVersionAdapter.java b/src/main/java/de/varoplugin/cfw/version/OneSevenVersionAdapter.java index fe9293e8..161ef16c 100644 --- a/src/main/java/de/varoplugin/cfw/version/OneSevenVersionAdapter.java +++ b/src/main/java/de/varoplugin/cfw/version/OneSevenVersionAdapter.java @@ -88,7 +88,7 @@ protected void initServerProperties() throws ClassNotFoundException, NoSuchMetho } protected void initEntity() throws NoSuchMethodException, SecurityException, ClassNotFoundException { - this.entityHandleMethod = Class.forName("org.bukkit.craftbukkit." + VersionUtils.getNmsVersion() + ".entity.CraftEntity").getMethod("getHandle"); + this.entityHandleMethod = Class.forName(VersionUtils.getCraftBukkitPackage() + ".entity.CraftEntity").getMethod("getHandle"); } protected void initPlayer() throws NoSuchMethodException, SecurityException, NoSuchFieldException, ClassNotFoundException { diff --git a/src/main/java/de/varoplugin/cfw/version/OneSixteenVersionAdapter.java b/src/main/java/de/varoplugin/cfw/version/OneSixteenVersionAdapter.java index face543a..90ce680a 100644 --- a/src/main/java/de/varoplugin/cfw/version/OneSixteenVersionAdapter.java +++ b/src/main/java/de/varoplugin/cfw/version/OneSixteenVersionAdapter.java @@ -34,7 +34,7 @@ public class OneSixteenVersionAdapter extends OneFourteenVersionAdapter { @Override public void forceClearWorlds() { try { - Field dedicatedServerField = Class.forName("org.bukkit.craftbukkit." + VersionUtils.getNmsVersion() + ".CraftServer").getDeclaredField("console"); + Field dedicatedServerField = Class.forName(VersionUtils.getCraftBukkitPackage() + ".CraftServer").getDeclaredField("console"); dedicatedServerField.setAccessible(true); Object dedicatedServer = dedicatedServerField.get(Bukkit.getServer()); Map worldServer = (Map) dedicatedServer.getClass().getField(this.getWorldServerFieldName()).get(dedicatedServer); diff --git a/src/main/java/de/varoplugin/cfw/version/OneTwelveVersionAdapter.java b/src/main/java/de/varoplugin/cfw/version/OneTwelveVersionAdapter.java index 310b8101..c6e675b0 100644 --- a/src/main/java/de/varoplugin/cfw/version/OneTwelveVersionAdapter.java +++ b/src/main/java/de/varoplugin/cfw/version/OneTwelveVersionAdapter.java @@ -40,7 +40,7 @@ class OneTwelveVersionAdapter extends OneNineVersionAdapter { @Override protected void initLocale() throws SecurityException, IllegalArgumentException { try { - this.localeMethod = Class.forName("org.bukkit.craftbukkit." + VersionUtils.getNmsVersion() + ".entity.CraftPlayer").getDeclaredMethod("getLocale"); + this.localeMethod = Class.forName(VersionUtils.getCraftBukkitPackage() + ".entity.CraftPlayer").getDeclaredMethod("getLocale"); } catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) { e.printStackTrace(); } diff --git a/src/main/java/de/varoplugin/cfw/version/OneTwentyVersionAdapter.java b/src/main/java/de/varoplugin/cfw/version/OneTwentyVersionAdapter.java index d0e450e9..32d028e2 100644 --- a/src/main/java/de/varoplugin/cfw/version/OneTwentyVersionAdapter.java +++ b/src/main/java/de/varoplugin/cfw/version/OneTwentyVersionAdapter.java @@ -28,6 +28,11 @@ public class OneTwentyVersionAdapter extends OneNineteenVersionAdapter { + @Override + protected void initXp() { + // nop + } + @Override public void setXpCooldown(Player player, int cooldown) { player.setExpCooldown(cooldown); diff --git a/src/main/java/de/varoplugin/cfw/version/VersionUtils.java b/src/main/java/de/varoplugin/cfw/version/VersionUtils.java index 44768b73..96740ab4 100644 --- a/src/main/java/de/varoplugin/cfw/version/VersionUtils.java +++ b/src/main/java/de/varoplugin/cfw/version/VersionUtils.java @@ -34,6 +34,7 @@ public class VersionUtils { private static final String FORGE_CLASS = "net.minecraftforge.common.MinecraftForge"; + private static final String craftBukkitPackage; private static final String nmsClass; private static final String nmsVersion; private static final boolean forgeSupport; @@ -50,18 +51,24 @@ public class VersionUtils { if (Bukkit.getServer() == null) { version = ServerVersion.UNSUPPORTED; serverSoftware = ServerSoftware.UNKNOWN; + craftBukkitPackage = null; nmsClass = null; nmsVersion = null; } else { - String base = "net.minecraft"; - nmsVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; - if (nmsVersion.startsWith("v1")) { - // 1.7 - 1.16 - nmsClass = base + ".server." + nmsVersion; + craftBukkitPackage = Bukkit.getServer().getClass().getPackage().getName(); + String[] packageSplit = craftBukkitPackage.split("\\."); + if (packageSplit.length > 3) { + nmsVersion = packageSplit[3]; + if (nmsVersion.startsWith("v1")) { + // 1.7 - 1.16 + nmsClass = base + ".server." + nmsVersion; + } else + // Thermos (1.17+ does not use this string at all) + nmsClass = base + ".server"; } else { - // Thermos (1.17+ does not use this string at all) nmsClass = base + ".server"; + nmsVersion = Bukkit.getServer().getBukkitVersion().split("-")[0].replace('.', '_'); } version = ServerVersion.getVersion(nmsVersion); serverSoftware = ServerSoftware.getServerSoftware(); @@ -82,11 +89,15 @@ static boolean isClassPresent(String clazz) { } } + static String getCraftBukkitPackage() { + return craftBukkitPackage; + } + static String getNmsClass() { return nmsClass; } - public static String getNmsVersion() { + static String getNmsVersion() { return nmsVersion; }