From bf677c732de329b038e8fd4819f434d39375f558 Mon Sep 17 00:00:00 2001 From: Nils Gereke Date: Sun, 22 Sep 2024 12:59:22 +0200 Subject: [PATCH] fix: method missmatched arguments --- .../net/imprex/zip/common/ReflectionUtil.java | 42 ++++++++++++++++++ zip-nms/zip-nms-v1_21_R1/pom.xml | 10 ++--- .../zip/nms/v1_21_R1/ZipNmsManager.java | 44 ++++++++++++++++--- .../java/net/imprex/zip/BackpackListener.java | 3 +- 4 files changed, 88 insertions(+), 11 deletions(-) diff --git a/zip-common/src/main/java/net/imprex/zip/common/ReflectionUtil.java b/zip-common/src/main/java/net/imprex/zip/common/ReflectionUtil.java index d82461f..66fee75 100644 --- a/zip-common/src/main/java/net/imprex/zip/common/ReflectionUtil.java +++ b/zip-common/src/main/java/net/imprex/zip/common/ReflectionUtil.java @@ -3,6 +3,9 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; public class ReflectionUtil { @@ -104,6 +107,45 @@ public static Method getMethod(Class clazz, String name, Class... paramete } } + public static Method searchMethod(Class clazz, Class returnType, Class... parameterTypes) { + return findMethod(clazz, returnType, parameterTypes, new HashSet>()); + } + + private static Method findMethod(Class clazz, Class returnType, Class[] parameterTypes, Set> visitedClasses) { + if (clazz == null || visitedClasses.contains(clazz)) { + return null; + } + visitedClasses.add(clazz); + + System.out.println(clazz.getSimpleName()); + for (Method method : clazz.getDeclaredMethods()) { + if (method.getName().contains("Profile")) { + System.out.println(method.getName()); + for (Class type : method.getParameterTypes()) { + System.out.println(method.getName() + " " + type.getSimpleName()); + } + } + if (method.getReturnType().equals(returnType) && Arrays.equals(method.getParameterTypes(), parameterTypes)) { + method.setAccessible(true); + return method; + } + } + + Method superClassMethod = findMethod(clazz.getSuperclass(), returnType, parameterTypes, visitedClasses); + if (superClassMethod != null) { + return superClassMethod; + } + + for (Class interfaceClass : clazz.getInterfaces()) { + Method interfaceMethod = findMethod(interfaceClass, returnType, parameterTypes, visitedClasses); + if (interfaceMethod != null) { + return interfaceMethod; + } + } + + return null; + } + public static Constructor getConstructor(Class clazz, Class... parameterTypes) { try { return clazz.getConstructor(parameterTypes); diff --git a/zip-nms/zip-nms-v1_21_R1/pom.xml b/zip-nms/zip-nms-v1_21_R1/pom.xml index 98ceb3a..6d42049 100644 --- a/zip-nms/zip-nms-v1_21_R1/pom.xml +++ b/zip-nms/zip-nms-v1_21_R1/pom.xml @@ -21,7 +21,7 @@ org.spigotmc spigot - 1.21-R0.1-SNAPSHOT + 1.21.1-R0.1-SNAPSHOT remapped-mojang provided @@ -42,10 +42,10 @@ remap-obf - org.spigotmc:minecraft-server:1.21-R0.1-SNAPSHOT:txt:maps-mojang + org.spigotmc:minecraft-server:1.21.1-R0.1-SNAPSHOT:txt:maps-mojang true - org.spigotmc:spigot:1.21-R0.1-SNAPSHOT:jar:remapped-mojang + org.spigotmc:spigot:1.21.1-R0.1-SNAPSHOT:jar:remapped-mojang true remapped-obf @@ -60,9 +60,9 @@ ${project.build.directory}/${project.artifactId}-${project.version}-remapped-obf.jar - org.spigotmc:minecraft-server:1.21-R0.1-SNAPSHOT:csrg:maps-spigot + org.spigotmc:minecraft-server:1.21.1-R0.1-SNAPSHOT:csrg:maps-spigot - org.spigotmc:spigot:1.21-R0.1-SNAPSHOT:jar:remapped-obf + org.spigotmc:spigot:1.21.1-R0.1-SNAPSHOT:jar:remapped-obf diff --git a/zip-nms/zip-nms-v1_21_R1/src/main/java/net/imprex/zip/nms/v1_21_R1/ZipNmsManager.java b/zip-nms/zip-nms-v1_21_R1/src/main/java/net/imprex/zip/nms/v1_21_R1/ZipNmsManager.java index 8ebcea6..9318691 100644 --- a/zip-nms/zip-nms-v1_21_R1/src/main/java/net/imprex/zip/nms/v1_21_R1/ZipNmsManager.java +++ b/zip-nms/zip-nms-v1_21_R1/src/main/java/net/imprex/zip/nms/v1_21_R1/ZipNmsManager.java @@ -2,11 +2,13 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; +import java.util.function.BiConsumer; import org.bukkit.Material; import org.bukkit.craftbukkit.v1_21_R1.CraftRegistry; @@ -25,12 +27,11 @@ import net.minecraft.nbt.NbtAccounter; import net.minecraft.nbt.NbtIo; import net.minecraft.nbt.Tag; +import net.minecraft.world.item.component.ResolvableProfile; public class ZipNmsManager implements NmsManager { - private static final Class CRAFTMETASKULL_CLASS = ReflectionUtil.getCraftBukkitClass("inventory.CraftMetaSkull"); - private static final Method CRAFTMETASKULL_SET_PROFILE = ReflectionUtil.getMethod(CRAFTMETASKULL_CLASS, - "setProfile", GameProfile.class); + private static final BiConsumer SET_PROFILE; private static final RegistryAccess DEFAULT_REGISTRY = CraftRegistry.getMinecraftRegistry(); @@ -38,6 +39,38 @@ public class ZipNmsManager implements NmsManager { static { NBT_EMPTY_ITEMSTACK.putString("id", "minecraft:air"); + + BiConsumer setProfile = (meta, profile) -> { + throw new NullPointerException("Unable to find 'setProfile' method!"); + }; + + Class craftMetaSkullClass = new ItemStack(Material.PLAYER_HEAD) + .getItemMeta() + .getClass(); + + Method setResolvableProfileMethod = ReflectionUtil.searchMethod(craftMetaSkullClass, void.class, ResolvableProfile.class); + if (setResolvableProfileMethod != null) { + setProfile = (meta, profile) -> { + try { + setResolvableProfileMethod.invoke(meta, new ResolvableProfile(profile)); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + }; + } else { + Method setProfileMethod = ReflectionUtil.searchMethod(craftMetaSkullClass, void.class, GameProfile.class); + if (setProfileMethod != null) { + setProfile = (meta, profile) -> { + try { + setProfileMethod.invoke(meta, profile); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + }; + } + } + + SET_PROFILE = setProfile; } public byte[] nbtToBinary(CompoundTag compound) { @@ -103,9 +136,10 @@ public void setSkullProfile(SkullMeta meta, String texture) { try { GameProfile gameProfile = new GameProfile(UUID.randomUUID(), ""); gameProfile.getProperties().put("textures", new Property("textures", texture)); - CRAFTMETASKULL_SET_PROFILE.invoke(meta, gameProfile); + + SET_PROFILE.accept(meta, gameProfile); } catch (Exception e) { - throw new ClassCastException("Error by setting skull profile"); + e.printStackTrace(); } } diff --git a/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java b/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java index 14a4c6c..e439ffd 100644 --- a/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java +++ b/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java @@ -83,7 +83,8 @@ public void onInventoryClose(InventoryCloseEvent event) { @EventHandler(ignoreCancelled = false) public void onPlayerDropItem(PlayerDropItemEvent event) { - Backpack backpack = this.backpackHandler.getBackpack(event.getItemDrop().getItemStack()); + ItemStack item = event.getItemDrop().getItemStack(); + Backpack backpack = this.backpackHandler.getBackpack(item); if (backpack != null) { event.getPlayer().closeInventory(); }