Skip to content

Commit

Permalink
fix: method missmatched arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
NgLoader committed Sep 22, 2024
1 parent 7ada618 commit bf677c7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
42 changes: 42 additions & 0 deletions zip-common/src/main/java/net/imprex/zip/common/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<Class<?>>());
}

private static Method findMethod(Class<?> clazz, Class<?> returnType, Class<?>[] parameterTypes, Set<Class<?>> 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);
Expand Down
10 changes: 5 additions & 5 deletions zip-nms/zip-nms-v1_21_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.21-R0.1-SNAPSHOT</version>
<version>1.21.1-R0.1-SNAPSHOT</version>
<classifier>remapped-mojang</classifier>
<scope>provided</scope>
</dependency>
Expand All @@ -42,10 +42,10 @@
<id>remap-obf</id>
<configuration>
<srgIn>
org.spigotmc:minecraft-server:1.21-R0.1-SNAPSHOT:txt:maps-mojang</srgIn>
org.spigotmc:minecraft-server:1.21.1-R0.1-SNAPSHOT:txt:maps-mojang</srgIn>
<reverse>true</reverse>
<remappedDependencies>
org.spigotmc:spigot:1.21-R0.1-SNAPSHOT:jar:remapped-mojang</remappedDependencies>
org.spigotmc:spigot:1.21.1-R0.1-SNAPSHOT:jar:remapped-mojang</remappedDependencies>
<remappedArtifactAttached>true</remappedArtifactAttached>
<remappedClassifierName>remapped-obf</remappedClassifierName>
</configuration>
Expand All @@ -60,9 +60,9 @@
<inputFile>
${project.build.directory}/${project.artifactId}-${project.version}-remapped-obf.jar</inputFile>
<srgIn>
org.spigotmc:minecraft-server:1.21-R0.1-SNAPSHOT:csrg:maps-spigot</srgIn>
org.spigotmc:minecraft-server:1.21.1-R0.1-SNAPSHOT:csrg:maps-spigot</srgIn>
<remappedDependencies>
org.spigotmc:spigot:1.21-R0.1-SNAPSHOT:jar:remapped-obf</remappedDependencies>
org.spigotmc:spigot:1.21.1-R0.1-SNAPSHOT:jar:remapped-obf</remappedDependencies>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,19 +27,50 @@
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<SkullMeta, GameProfile> SET_PROFILE;

private static final RegistryAccess DEFAULT_REGISTRY = CraftRegistry.getMinecraftRegistry();

private static final CompoundTag NBT_EMPTY_ITEMSTACK = new CompoundTag();

static {
NBT_EMPTY_ITEMSTACK.putString("id", "minecraft:air");

BiConsumer<SkullMeta, GameProfile> 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) {
Expand Down Expand Up @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit bf677c7

Please sign in to comment.