Hooking my API

Discussion in 'Plugin Development' started by DeMaggo, Jan 10, 2014.

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

    DeMaggo

    Greetings everyone,

    updating one of my plugins, I added an API.
    This API offers basic features related to the plugin and allows to register a custom object as an eventhandler.
    These eventhandlers have to implement a few methods. I defined these methods in an Interface:


    Code:java
    1. package demaggo.MegaCreeps.API;
    2.  
    3.  
    4. import org.bukkit.event.entity.CreatureSpawnEvent;
    5. import org.bukkit.event.entity.EntityDeathEvent;
    6.  
    7. import demaggo.MegaCreeps.MegaCreepInstance;
    8. import demaggo.MegaCreeps.MegaCreepSetup;
    9.  
    10. public interface MegaCreepsInterface {
    11.  
    12. public void handleDeathEvent(MegaCreepInstance m, EntityDeathEvent e);
    13.  
    14.  
    15. /*
    16. * modSetupLevel
    17. * modSetup
    18. * handleSpawnEvent
    19. */
    20.  
    21. public int modSetupLevel(CreatureSpawnEvent e, int oldlevel);
    22.  
    23. public MegaCreepSetup modSetup(CreatureSpawnEvent e, MegaCreepSetup oldsetup);
    24.  
    25.  
    26. public void handleSpawnEvent(MegaCreepInstance m);
    27.  
    28. }

    I also added the required functions to retrieve the API and so on.

    Next step: I compiled the plugin, used it in the build path of another plugin and tried to hook it.

    The class to implement the interface:
    Code:java
    1. public class Megacreepmod implements MegaCreepsInterface


    The method that supposed to hook the plugin:

    Code:java
    1. private boolean setupMCHook(){
    2.  
    3. MegaCreepsAPI api=null;
    4.  
    5. Plugin p=getServer().getPluginManager().getPlugin("MegaCreeps");
    6.  
    7. if (p!=null){
    8.  
    9. demaggo.MegaCreeps.Main.getAPI().registerModSetupLevel(new Megacreepmod(), "elite");
    10. }
    11.  
    12. return api!=null;
    13. }


    I tried multiple ways, but even though it was compiled with the original plugin, I get this error:

    Code:
    org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: demaggo/MegaCreeps/API/MegaCreepsInterface
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:184) ~[craftbukkit.jar:git-Spigot-1150]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:306) ~[craftbukkit.jar:git-Spigot-1150]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230) [craftbukkit.jar:git-Spigot-1150]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.java:259) [craftbukkit.jar:git-Spigot-1150]
        at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:133) [craftbukkit.jar:git-Spigot-1150]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:420) [craftbukkit.jar:git-Spigot-1150]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Spigot-1150]
    Caused by: java.lang.NoClassDefFoundError: demaggo/MegaCreeps/API/MegaCreepsInterface
        at java.lang.Class.forName0(Native Method) ~[?:1.7.0_25]
        at java.lang.Class.forName(Class.java:270) ~[?:1.7.0_25]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:173) ~[craftbukkit.jar:git-Spigot-1150]
        ... 6 more
    Caused by: java.lang.ClassNotFoundException: demaggo.MegaCreeps.API.MegaCreepsInterface
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366) ~[?:1.7.0_25]
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355) ~[?:1.7.0_25]
        at java.security.AccessController.doPrivileged(Native Method) ~[?:1.7.0_25]
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354) ~[?:1.7.0_25]
        at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader.java:80) ~[craftbukkit.jar:git-Spigot-1150]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:53) ~[craftbukkit.jar:git-Spigot-1150]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.7.0_25]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.7.0_25]
        at java.lang.Class.forName0(Native Method) ~[?:1.7.0_25]
        at java.lang.Class.forName(Class.java:270) ~[?:1.7.0_25]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:173) ~[craftbukkit.jar:git-Spigot-1150]
        ... 6 more
    How comes Eclipse has no compilation problems and finds all required classes while the Interface is unknown during runtime?
     
  2. DeMaggo
    I didn't look through your code, but I'd just like to point out one minor mistake in your text...
    You might want to choose your words better.
     
    NathanWolf and Garris0n like this.
  3. Offline

    Not2EXceL

    Whats your plugin yml look like? Or is your "API" even a plugin?

    Also lol at "public" modifiers in an interface
     
  4. Offline

    werter318

    Not2EXceL Why? It's not weird or anything to put them there...

    EDIT: I mean, obviously I know every method in an interface is public, but still, alot of programmers put them there.
     
  5. Offline

    Not2EXceL

    werter318 Its completely unecessary as interface method declaration is implicitly public and abstract.

    It's allowed, but its a bad practice.
    JLS:
     
  6. Offline

    werter318

  7. Offline

    Not2EXceL

    werter318 This is my last post. You simply cannot just take out "as a matter of style" from the said context. It changes the entirety of the statement. The JLS addresses the redundancy issue since people do that, but its not something that should be done or practiced according to the Java Language Specifications.
     
  8. Offline

    DeMaggo

    Can we please get to the topic? Why is everyone crying about nonsense?
    The API is a bonus function in my plugin. You simply pick the main class and call Main.getAPI() for full access, at least I thought so. The hooking plugin never gets loaded because it says it misses a class that should be there since it gets imported, compiled against AND is also there in the actual Main plugin.
     
  9. Offline

    NathanWolf

    Does the hooking plugin (lol) have your plugin defined as a dependency in its plugin.yml file?
     
    DeMaggo likes this.
  10. Offline

    Not2EXceL

    DeMaggo If you're using another plugin to access the Main.getAPI() method inside the plugin with the API, you need to add it as a dependency in plugin.yml. (or softdependency depending on what your doing)

    and it wasn't nonsense. nonsense is xtrollxdudex XD
     
  11. Offline

    DeMaggo

    I added a soft dependency and it worked well. Thumbs up for NathanWolf.
    If it wasn't for your attitude and spamming, you'd get one as well, Not2EXceL.
     
    NathanWolf likes this.
Thread Status:
Not open for further replies.

Share This Page