diff --git a/src/main/java/tech/sbdevelopment/mapreflectionapi/api/MapWrapper.java b/src/main/java/tech/sbdevelopment/mapreflectionapi/api/MapWrapper.java index 4a85872..85bd165 100644 --- a/src/main/java/tech/sbdevelopment/mapreflectionapi/api/MapWrapper.java +++ b/src/main/java/tech/sbdevelopment/mapreflectionapi/api/MapWrapper.java @@ -255,7 +255,18 @@ public class MapWrapper { private void sendItemFramePacket(Player player, int entityId, ItemStack stack, int mapId) { Object nmsStack = ReflectionUtil.callMethod(craftStackClass, "asNMSCopy", stack); - Object nbtObject = ReflectionUtil.callMethod(nmsStack, ReflectionUtil.supports(19) ? "v" : ReflectionUtil.supports(18) ? "u" : ReflectionUtil.supports(13) ? "getOrCreateTag" : "getTag"); + + String nbtObjectName; + if (ReflectionUtil.supports(19)) { //1.19 + nbtObjectName = "v"; + } else if (ReflectionUtil.supports(18)) { //1.18 + nbtObjectName = ReflectionUtil.VER_MINOR == 1 ? "t" : "u"; //1.18.1 = t, 1.18(.2) = u + } else if (ReflectionUtil.supports(13)) { //1.13 - 1.17 + nbtObjectName = "getOrCreateTag"; + } else { //1.12 + nbtObjectName = "getTag"; + } + Object nbtObject = ReflectionUtil.callMethod(nmsStack, nbtObjectName); if (!ReflectionUtil.supports(13) && nbtObject == null) { //1.12 has no getOrCreate, call create if null! Object tagCompound = ReflectionUtil.callConstructor(tagCompoundClass); @@ -271,9 +282,23 @@ public class MapWrapper { true ); - List list = new ArrayList<>(); - Object dataWatcherObject = ReflectionUtil.getDeclaredField(entityItemFrameClass, ReflectionUtil.supports(17) ? "ao" : ReflectionUtil.supports(14) ? "ITEM" : ReflectionUtil.supports(13) ? "e" : "c"); + String dataWatcherObjectName; + if (ReflectionUtil.supports(19)) { //1.19, same as 1.17 and 1.18(.2) + dataWatcherObjectName = "ao"; + } else if (ReflectionUtil.supports(18)) { //1.18 + dataWatcherObjectName = ReflectionUtil.VER_MINOR == 1 ? "ap" : "ao"; //1.18.1 = ap, 1.18(.2) = ao + } else if (ReflectionUtil.supports(17)) { //1.17 + dataWatcherObjectName = "ao"; + } else if (ReflectionUtil.supports(14)) { //1.14 - 1.16 + dataWatcherObjectName = "ITEM"; + } else if (ReflectionUtil.supports(13)) { //1.13 + dataWatcherObjectName = "e"; + } else { //1.12 + dataWatcherObjectName = "c"; + } + Object dataWatcherObject = ReflectionUtil.getDeclaredField(entityItemFrameClass, dataWatcherObjectName); Object dataWatcherItem = ReflectionUtil.callFirstConstructor(dataWatcherItemClass, dataWatcherObject, nmsStack); + List list = new ArrayList<>(); list.add(dataWatcherItem); ReflectionUtil.setDeclaredField(packet, "b", list); diff --git a/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/ReflectionUtil.java b/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/ReflectionUtil.java index ac91b23..a0473c0 100644 --- a/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/ReflectionUtil.java +++ b/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/ReflectionUtil.java @@ -105,6 +105,13 @@ public class ReflectionUtil { * @since 4.0.0 */ public static final int VER = Integer.parseInt(VERSION.substring(1).split("_")[1]); + /** + * The raw minor version number. + * E.g. {@code v1_18_R2} to {@code 2} + * + * @since 4.0.0 + */ + public static final int VER_MINOR = toInt(VERSION.substring(1).split("_")[2].substring(1), 0); /** * Mojang remapped their NMS in 1.17 https://www.spigotmc.org/threads/spigot-bungeecord-1-17.510208/#post-4184317 */ @@ -183,6 +190,18 @@ public class ReflectionUtil { return VER >= version; } + /** + * Checks whether the server version is equal or greater than the given version. + * + * @param major the major version to compare the server version with. + * @param minor the minor version to compare the server version with. + * @return true if the version is equal or newer, otherwise false. + * @since 4.0.0 + */ + public static boolean supports(int major, int minor) { + return VER >= major && VER_MINOR >= minor; + } + private static Class wrapperToPrimitive(Class clazz) { if (clazz == Boolean.class) return boolean.class; if (clazz == Integer.class) return int.class; @@ -483,4 +502,8 @@ public class ReflectionUtil { return this.version == 0 ? handle : this.handle; } } + + private static int toInt(String string, int def) { + return string.isBlank() ? def : Integer.parseInt(string); + } } \ No newline at end of file