Solved Messing around with reflection...

Discussion in 'Plugin Development' started by DarkBladee12, Apr 12, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    DarkBladee12

    Hey guys I am currently trying to use reflection instead of NMS code, but it seems that it isn't working and I don't understand why :/ Here's my code:

    Code:
    public Object getItemStack(ItemStack i) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            Class<?> craftItemStack = Class.forName(plugin.getServer().getClass().getPackage().getName() + ".inventory.CraftItemStack");
            for (Method m : craftItemStack.getMethods()) {
                if (m.getName().equals("asNMSCopy") && m.getParameterTypes().length == 1) {
                    return m.invoke(i); //Line 292
                }
            }
            return null;
        }
    That's the error I get:
    Code:
    2013-04-12 15:51:12 [SEVERE] java.lang.IllegalArgumentException: wrong number of arguments
    2013-04-12 15:51:12 [SEVERE]    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2013-04-12 15:51:12 [SEVERE]    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    2013-04-12 15:51:12 [SEVERE]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    2013-04-12 15:51:12 [SEVERE]    at java.lang.reflect.Method.invoke(Unknown Source)
    2013-04-12 15:51:12 [SEVERE]    at com.DarkBlade12.EnchantPlus.Listener.EnchantingListener.getItemStack(EnchantingListener.java:292)
    2013-04-12 15:51:12 [SEVERE]    at com.DarkBlade12.EnchantPlus.Listener.EnchantingListener.showCurrentlyEnchanting(EnchantingListener.java:308)
    2013-04-12 15:51:12 [SEVERE]    at com.DarkBlade12.EnchantPlus.Listener.EnchantingListener.onInventoryClick(EnchantingListener.java:82)
    2013-04-12 15:51:12 [SEVERE]    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2013-04-12 15:51:12 [SEVERE]    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    2013-04-12 15:51:12 [SEVERE]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    2013-04-12 15:51:12 [SEVERE]    at java.lang.reflect.Method.invoke(Unknown Source)
    2013-04-12 15:51:12 [SEVERE]    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    2013-04-12 15:51:12 [SEVERE]    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    2013-04-12 15:51:12 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    2013-04-12 15:51:12 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.PlayerConnection.a(PlayerConnection.java:1177)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.Packet102WindowClick.handle(SourceFile:31)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.NetworkManager.b(NetworkManager.java:292)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.PlayerConnection.d(PlayerConnection.java:110)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.ServerConnection.b(SourceFile:35)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.DedicatedServerConnection.b(SourceFile:30)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.MinecraftServer.r(MinecraftServer.java:580)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.DedicatedServer.r(DedicatedServer.java:225)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.MinecraftServer.q(MinecraftServer.java:476)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:409)
    2013-04-12 15:51:12 [SEVERE]    at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
     
  2. the first argument at invoke is excepted the instance where the methode is excuted on, not the first argument to pass
     
    DarkBladee12 likes this.
  3. Try this.

    return m.invoke(this, i);

    ferrybig
    Ah, just a moment faster than me :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  4. Offline

    DarkBladee12

Thread Status:
Not open for further replies.

Share This Page