From 6e406a48596bfd47ebe8663c8e91fd45aebb88a8 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 26 Jan 2024 17:21:07 +1300 Subject: [PATCH] Fix older builds of MC, and initial relocation support --- .../libraryaddict/disguise/LibsDisguises.java | 13 ++++- .../utilities/reflection/ClassMappings.java | 10 +++- .../utilities/reflection/NmsVersion.java | 49 +++++++++++----- .../reflection/ReflectionManager.java | 57 +++++++++++++------ 4 files changed, 95 insertions(+), 34 deletions(-) diff --git a/plugin/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/plugin/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index f7e859c9..5c04d593 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -205,7 +205,14 @@ public class LibsDisguises extends JavaPlugin { getLogger().info("File Name: " + getFile().getName()); - getLogger().info("Discovered nms version: " + ReflectionManager.getBukkitVersion() + " (" + ReflectionManager.getVersion() + ")"); + String nmsPackageName = ReflectionManager.getNmsPackage(); + + if (nmsPackageName.isEmpty()) { + nmsPackageName = "{Not package relocated}"; + } + + getLogger().info("Discovered nms version: (Package: " + nmsPackageName + ") (LD: " + ReflectionManager.getVersion() + ") (MC: " + + ReflectionManager.getMinecraftVersion() + ")"); getLogger().info("Jenkins Build: " + (isNumberedBuild() ? "#" : "") + getBuildNo()); @@ -222,8 +229,8 @@ public class LibsDisguises extends JavaPlugin { if (ReflectionManager.getVersion() == null) { getLogger().severe("You're using the wrong version of Lib's Disguises for your server! This is " + "intended for " + StringUtils.join( - Arrays.stream(NmsVersion.values()).filter(v -> v != NmsVersion.UNSUPPORTED).map(v -> v.name().replace("_", ".")) - .collect(Collectors.toList()), " & ") + "!"); + Arrays.stream(NmsVersion.values()).filter(v -> v != NmsVersion.UNSUPPORTED).map(v -> String.join(", ", v.getSupportedVersions())) + .collect(Collectors.toList()), ", ") + "!"); getPluginLoader().disablePlugin(this); return; } diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ClassMappings.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ClassMappings.java index 48399a02..d64169a6 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ClassMappings.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ClassMappings.java @@ -74,8 +74,16 @@ public class ClassMappings { "net.minecraft.world.level.block.state", "net.minecraft.world.level", "net.minecraft.world.phys", "org.bukkit.craftbukkit.$version$.block.data", "org.bukkit.craftbukkit.$version$", "org.bukkit.craftbukkit.$version$.entity", "org.bukkit.craftbukkit.$version$.inventory", "org.bukkit.craftbukkit.$version$.util"}; + String replaceStr = "$version$"; + String version = ReflectionManager.getNmsPackage(); + + // If there is no nms package, then replace the . as well so we don't have a "org.bukkit..server" package name situation + if (version.isEmpty()) { + replaceStr = "." + replaceStr; + } + for (int i = 0; i < s.length; i++) { - s[i] = s[i].replace("$version$", ReflectionManager.getBukkitVersion()); + s[i] = s[i].replace(replaceStr, version); } return s; diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/NmsVersion.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/NmsVersion.java index c6d44650..f806adb6 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/NmsVersion.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/NmsVersion.java @@ -1,23 +1,44 @@ package me.libraryaddict.disguise.utilities.reflection; +import lombok.Getter; + /** * Created by libraryaddict on 6/02/2020. */ public enum NmsVersion { - v1_12, - v1_13, - v1_14, - v1_15, - v1_16, - v1_17, - v1_18, - v1_19_R1, // 1.19.0, 1.19.1, 1.19.2 - v1_19_R2, // 1.19.3 - v1_19_R3, // 1.19.4 - v1_20_R1, // 1.20 & 1.20.1 - v1_20_R2, // 1.20.2 - v1_20_R3, // 1.20.3 - UNSUPPORTED; + v1_12("1.12", "1.12.1", "1.12.2"), + v1_13("1.13", "1.13.1", "1.13.2"), + v1_14("1.14", "1.14.1", "1.14.2", "1.14.3", "1.14.4"), + v1_15("1.15", "1.15.1", "1.15.2"), + v1_16("1.16", "1.16.1", "1.16.2", "1.16.3", "1.16.4", "1.16.5"), + v1_17("1.17", "1.17.1"), + v1_18("1.18", "1.18.1", "1.18.2"), + v1_19_R1("1.19.0", "1.19.1", "1.19.2"), + v1_19_R2("1.19.3"), + v1_19_R3("1.19.4"), + v1_20_R1("1.20", "1.20.1"), + v1_20_R2("1.20.2"), + v1_20_R3("1.20.3", "1.20.4"), + UNSUPPORTED("N/A"); + + @Getter + private final String[] supportedVersions; + + NmsVersion(String... minecraftVersions) { + this.supportedVersions = minecraftVersions; + } + + public boolean isMinecraftVersion(String minecraftVersion) { + for (String version : supportedVersions) { + if (!version.equals(minecraftVersion)) { + continue; + } + + return true; + } + + return false; + } /** * If this enum version is older, or the same version as the current running server diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java index 929a69e2..630f5ffc 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java @@ -122,11 +122,13 @@ import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; public class ReflectionManager { private static final HashMap soundCategories = new HashMap<>(); - private static String bukkitVersion; + private static String craftbukkitVersion; private static Method itemAsCraftCopyMethod; private static Method itemAsNmsCopyMethod; private static Method damageAndIdleSoundMethod; @@ -198,6 +200,7 @@ public class ReflectionManager { private static Method addEntityTracker; private static Method fillProfileProperties; private static MinecraftSessionService sessionService; + private static String minecraftVersion; public static void init() { try { @@ -217,6 +220,8 @@ public class ReflectionManager { if (nmsReflection != null) { sessionService = nmsReflection.getMinecraftSessionService(); } else { + getServerMethod = getCraftMethod("CraftServer", "getServer"); + Object minecraftServer = getMinecraftServer(); for (Method method : getNmsClass("MinecraftServer").getMethods()) { @@ -254,7 +259,6 @@ public class ReflectionManager { itemAsNmsCopyMethod = getCraftMethod(craftItemClass, "asNMSCopy", ItemStack.class); itemAsBukkitMethod = getCraftMethod(craftItemClass, "asBukkitCopy", getNmsClass("ItemStack")); - getServerMethod = getCraftMethod("CraftServer", "getServer"); getEnumArtMethod = getCraftMethod("CraftArt", "BukkitToNotch", Art.class); blockPositionConstructor = getNmsConstructor("BlockPosition", int.class, int.class, int.class); enumDirectionMethod = getNmsMethod("EnumDirection", "fromType2", int.class); @@ -340,7 +344,6 @@ public class ReflectionManager { for (Enum anEnum : (Enum[]) getNmsClass("SoundCategory").getEnumConstants()) { soundCategories.put((String) soundCategoryMethod.invoke(anEnum), anEnum); } - } catch (Exception e) { e.printStackTrace(); } @@ -850,19 +853,47 @@ public class ReflectionManager { } public static NmsVersion getVersion() { - if (version == null) { - getBukkitVersion(); + if (craftbukkitVersion == null) { + getCraftBukkitPackage(); } return version; } - public static String getBukkitVersion() { - if (bukkitVersion == null) { - bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3]; + public static String getMinecraftVersion() { + if (minecraftVersion == null) { + Matcher matcher = Pattern.compile(" \\(MC: ([^)]+?)\\)").matcher(Bukkit.getVersion()); + + if (!matcher.find()) { + throw new IllegalStateException("Lib's Disguises is unable to find and parse a ` (MC: 1.10.1)` version in Bukkit.getVersion()"); + } + + minecraftVersion = matcher.group(1); + } + + return minecraftVersion; + } + + @Deprecated + public static String getNmsPackage() { + String cbPackage = craftbukkitVersion; + String[] spl = cbPackage.split("\\."); + + if (spl.length != 4) { + return ""; + } + + return spl[3]; + } + + public static String getCraftBukkitPackage() { + if (craftbukkitVersion == null) { + craftbukkitVersion = Bukkit.getServer().getClass().getPackage().getName(); + + String mcVersion = getMinecraftVersion(); for (NmsVersion v : NmsVersion.values()) { - if (!getBukkitVersion().startsWith(v.name())) { + if (!v.isMinecraftVersion(mcVersion)) { continue; } @@ -870,7 +901,7 @@ public class ReflectionManager { } } - return bukkitVersion; + return craftbukkitVersion; } public static Class getCraftClass(String className) { @@ -1841,12 +1872,6 @@ public class ReflectionManager { return null; } - public static String getMinecraftVersion() { - String version = Bukkit.getVersion(); - version = version.substring(version.lastIndexOf(" ") + 1, version.length() - 1); - return version; - } - public static WrappedDataWatcherObject createDataWatcherObject(MetaIndex index, Object value) { if (value == null) { return null;