Solved Optional plugin dependency

Discussion in 'Plugin Development' started by mcoder, Feb 27, 2013.

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


    Hey everyone,

    I'm currently working on a plugin which hooks into WorldGuards API to extend the protections a bit. Everything ist working fine as ist is. Now I want to change it a bit, making the WorldGuard dependency optional, so at least some features can be used without WorldGuard aswell. The problem is, when loading the modified version of the plugin without having WorldGuard installed, it fails to load and register it's events because it can't find the WorldGuard classes (of course, they don't exist).
    "com/sk89q/worldguard/bukkit/WorldGuardPlugin does not exist."

    My question is: how do I solve this problem? Is there a way to tell the plugin not to check for this class when wg support is not enabled? Or maybe include the class files in my own plugin?

    Thanks in advance for any help or hints you can give me :)

    regards, mcoder
  2. Offline


    Do not include any WorldGuard class files!
    1. if (Bukkit.getPluginManager().getPlugin("WorldGuard") != null) {
    2. // world guard is on the server
    3. // only use worldguard methods here
    4. } else {
    5. // worldguard is not loaded on the server
    6. // do not use any worldguard methods
    7. }
  3. Offline

    Lolmewn Retired Staff

    The above, or use try{}catch(Exception e){} for checking if a class exists.
  4. Offline


    mcoder Use what Minecrell posted but store what getPlugin() returns and check against null like he did but also check if it's instanceof WorldGuard to be sure that it's got the same class you want to use.

    Try-catch would be a kinda slow and dirty way to do it and it's not required since you can do it faster and simplier.
  5. Offline


    I made a boolean like
    public boolean wg = false;
    then in the onEnable check if its there, then set wg to true; then in your commands that involve wg just check if wg is true. if it is do the command
    alexschrod likes this.
  6. Offline


    Thanks for all the replies!

    the problem is, that's exactly what I did. When the plugin is loaded, I'm checking if wg is enabled. I store this information in a boolean. Now I register my listener (playerInteractEvent). when it is triggered, it does some wg independent stuff, and then, if that wg Boolean is true the wg stuff. If not, it just skips this part.

    When I load it with world guard, everything works the way it should, when not it fails to register the listener with the above error message.

    I really don't want to use a try catch since that is not a very clean way to do this and besides, it would still not register that listener but simply catch the error message.
  7. Offline


    if a methode returns a type that is not inside your jar, like if you have a methode that returns WorldGuardPlugin, it wont work without the other plugin
  8. Offline


    Ah, there is the problem, thanks. I'll try to do all the wg stuff within one method, although that would be a rather long and confusing one. Is there really no way to solve this propperly?
  9. Offline


    If you don't use any WorldGuard stuff when WorldGuard is not loaded in the server, then you shouldn't get an error message when registering or using the listener I think.
  10. Offline


    Move everything which references any worldguard API into it's own class(es), and only instantiate that/those class(es) if you detect the worldguard plugin via getPlugin().

    For bonus points, write different implementations of the classes for when worldguard is/is not present, so you can retain some functionality, this is what I do for all third party code and any code which references CB or NMS.
  11. Offline


    Sorry about the late reply, but that sounds like a really good way to handle it. Thanks for the suggestions, seems obvious now, but it for sure wasn'r for me before :)

    regards, mcoder

    - solved -
Thread Status:
Not open for further replies.

Share This Page