Solved Having problems with invoking.

Discussion in 'Plugin Development' started by MCForger, Nov 18, 2012.

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

    MCForger

    Okay so this is my code for my custom bat class to be implemented:
    Code:
                Method a = net.minecraft.server.EntityTypes.class.getDeclaredMethod("a", args);
                a.setAccessible(true);
                a.invoke(SpecialBats.class, "Bat", 65);
    For some reason this does not work and this is what I get onEnable:
    Code:
    [MC-Alliance] Enabling MC-Alliance v0.6
    2012-11-18 15:47:13 [SEVERE] java.lang.IllegalArgumentException: wrong number of arguments
    2012-11-18 15:47:13 [SEVERE]    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2012-11-18 15:47:13 [SEVERE]    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    2012-11-18 15:47:13 [SEVERE]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    2012-11-18 15:47:13 [SEVERE]    at java.lang.reflect.Method.invoke(Unknown Source)
    2012-11-18 15:47:13 [SEVERE]    at com.mcforger.mcalliance.MCAlliance.onEnable(MCAlliance.java:28)
    2012-11-18 15:47:13 [SEVERE]    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    2012-11-18 15:47:13 [SEVERE]    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:374)
    2012-11-18 15:47:13 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    2012-11-18 15:47:13 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:270)
    2012-11-18 15:47:13 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:252)
    2012-11-18 15:47:13 [SEVERE]    at net.minecraft.server.MinecraftServer.j(MinecraftServer.java:320)
    2012-11-18 15:47:13 [SEVERE]    at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:299)
    2012-11-18 15:47:13 [SEVERE]    at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:258)
    2012-11-18 15:47:13 [SEVERE]    at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:147)
    2012-11-18 15:47:13 [SEVERE]    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:398)
    2012-11-18 15:47:13 [SEVERE]    at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
     
    
    But when I looked at at the Minecraft Server Source Code Bats # is 65?
    Code:
    a(EntityBat.class, "Bat", 65, 4996656, 986895);
    Any Ideas?

    fireblast709
    Have any ideas on whats wrong?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  2. Offline

    Tirelessly

    a.invoke(SpecialBats.class, "Bat", 65);
    2012-11-18 15:47:13 [SEVERE] java.lang.IllegalArgumentException: wrong number of arguments

    a(EntityBat.class, "Bat", 65, 4996656, 986895);

    Looks like you answered your own question...
     
  3. Offline

    MCForger

    No the problem is when I put the other numbers after the 65 I still get the same error or if I keep it just at 65.
     
  4. Offline

    Tirelessly

    Well you clearly can't just keep it at the 65..
     
  5. Offline

    MCForger

    Right but what should it be then? The other numbers after 65 are not working.
    md_5
    Have any ideas?
     
  6. Offline

    fireblast709

    have you tried just replacing the class?
    Code:java
    1. a(SpecialBats.class, "Bat", 65, 4996656, 986895);
    ?
     
  7. Offline

    MCForger

    Yes. And same error.

    Anybody?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  8. Offline

    md_5

    Read the error, you have the wrong number of arguments, that method takes 5 args.
    Don't bump...
     
  9. Offline

    MCForger

    Alright sorry but just doesnt make sense.
     
  10. Offline

    fireblast709

    MCForger this should at least give you another error, or you are calling the wrong method...
    what version of CB are you running this with?
     
  11. Offline

    md_5

    How does it not make sense? The method clearly requires 5 arguments, you are calling it with much less.
     
  12. Offline

    fireblast709

    Well after searching there are 2 methods, and the 5-argument one uses the 3-argument one and adds eggs.

    MCForger I do not know what you tried to put in args, but this is probably the correct way to use the reflection class Method: (this replaces bats with your special bats, and replaces the eggs as well. Though, untested)
    Code:java
    1. Method a = net.minecraft.server.EntityTypes.class.getDeclaredMethod("a", Class.class, String.class, int.class, int.class, int.class);
    2. a.setAccessible(true);
    3. a.invoke(SpecialBats.class, "Bat", 65, 4996656, 986895);
     
  13. Offline

    MCForger

    fireblast709
    So I tried it your way which is what I already did except I did something like this:
    Code:
                @SuppressWarnings("rawtypes")
                Class[] args = new Class[5];
                args[0] = Class.class;
                args[1] = String.class;
                args[2] = int.class;
                args[3] = int.class;
                args[4] = int.class;
                MCAllianceChat.logInfo(args[0].toString());
                MCAllianceChat.logInfo(args[1].toString());
                MCAllianceChat.logInfo(args[2].toString());
                MCAllianceChat.logInfo(args[3].toString());
                MCAllianceChat.logInfo(args[4].toString());
     
                Method a = net.minecraft.server.EntityTypes.class
                        .getDeclaredMethod("a", args);
                a.setAccessible(true);
                a.invoke(SpecialBats.class, "Bat", 65, 4996656, 986895);
    Which is the samething. But I am getting the same error:
    Code:
    2012-11-19 18:47:04 [INFO] [MC-Alliance] Enabling MC-Alliance v0.6
    2012-11-19 18:47:04 [SEVERE] java.lang.IllegalArgumentException: wrong number of arguments
    2012-11-19 18:47:04 [SEVERE]    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2012-11-19 18:47:04 [SEVERE]    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    2012-11-19 18:47:04 [SEVERE]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    2012-11-19 18:47:04 [SEVERE]    at java.lang.reflect.Method.invoke(Unknown Source)
    2012-11-19 18:47:04 [SEVERE]    at com.mcforger.mcalliance.MCAlliance.onEnable(MCAlliance.java:38)
    2012-11-19 18:47:04 [SEVERE]    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    2012-11-19 18:47:04 [SEVERE]    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:374)
    2012-11-19 18:47:04 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    2012-11-19 18:47:04 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:270)
    2012-11-19 18:47:04 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:252)
    2012-11-19 18:47:04 [SEVERE]    at net.minecraft.server.MinecraftServer.j(MinecraftServer.java:320)
    2012-11-19 18:47:04 [SEVERE]    at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:299)
    2012-11-19 18:47:04 [SEVERE]    at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:258)
    2012-11-19 18:47:04 [SEVERE]    at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:147)
    2012-11-19 18:47:04 [SEVERE]    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:398)
    =(
    2012-11-19 18:47:04 [SEVERE]    at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    This is my special bat class:
    Code:
    package com.mcforger.mcalliance;
     
    import net.minecraft.server.World;
     
    import org.bukkit.entity.Bat;
     
    public class SpecialBats extends net.minecraft.server.EntityBat
    {
     
        public SpecialBats(World world)
        {
            super(world);
        }
     
        @Override
        public void bl()
        {
            @SuppressWarnings("unused")
            Bat bat = (Bat) this.getBukkitEntity();
            super.bl();
        }
    }
    
    Also using one of the Dev builds for 1.4.4
     
  14. Offline

    fireblast709

    you give it as an array, but you need to specify the classes seperately like I do
     
  15. Offline

    one4me

    MCForger
    Like md_5 said a while ago, as well as the Exception also telling you, you're not using the correct number of arguments. Because the first parameter of invoke() is the Object you want to invoke the method on, you're currently trying to invoke a("Bat", 65, 4996656, 986895) on SpecialBats.class. Since this is a static method you can use null as the first parameter, otherwise you would use the an instance of the class.
    Try:
    Code:
    a.invoke(null, SpecialBats.class, "Bat", 65, 4996656, 986895);
    
     
  16. Offline

    fireblast709

    damnit you ninja'd me :p (I just remembered that as well xD)
     
  17. Offline

    MCForger

    one4me
    md_5
    fireblast709
    Thanks for helping and I didnt understand I guess I was tired and stressed. I will try out your method now one4me.

    Code works and fireblast I used my that works to as well as yours.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page