Methods easily accesible by other plugins

Discussion in 'Plugin Development' started by Digi, Dec 24, 2011.

Thread Status:
Not open for further replies.
  1. I'm looking to make methods that are easily accessible by other plugins and shouldn't fail dramatically if the plugin doesn't exist... anyone got a good way ?

    EDIT: the shortest I could find is:

    include the .jar of the plugin
    import the main class
    getPlugin on plugin name
    check isEnabled()
    use plugin methods...

    Any shorter way ? :}
     
  2. Bukkit.getServer().getPluginManager().getPlugin("PluginName"); ?

    If it returns null plugin does not exist.
     
  3. Yes, I already have that in the list, 3rd.

    Anyway... I found a way using static but I dunno what are the possible side effects... it's simple enough tough:
    - import the plugin
    - use ClassName.myEnablePointer() as condition
    - then use everything else with ClassName.method(...)
    3 steps to use a imported plugin is just awesome.

    Now I'm curious how to make a API for static methods, I tried with abstract and interface but they won't accept static...
    Also, if I just use class and blank everything out to compile without warnings/errors, I get runtime error:
    Code:
    Found class package.ClassName, but interface was expected
     
  4. Offline

    halley

    Don't bother with static methods, someone calling your plugin gets your plugin instance with the getPlugin() already described. Just make methods that they CAN call public, and methods they SHOULD NOT call private or protected.
     
  5. Why should I require them of using getPlugin() when they can just use MainClass name ?

    And yes, I know what public/private/protected are, static has nothing to do with that AFAIK.
     
  6. Offline

    bergerkiller

    @Digi If you want to be 100% sure your plugin won't fail if the 'depending' plugin got changed, you can surround the sensitive code by a Throwable. For example:
    Code:
            if (MyWorlds.isSpoutEnabled) {
                try {
                    SpoutPlayer sp = SpoutManager.getPlayer(player);
                    if (sp.isSpoutCraftEnabled()) {
                        SpoutWeather w = SpoutWeather.NONE;
                        if (player.getWorld().hasStorm()) {
                            if (this.showRain && this.showSnow) {
                                w = SpoutWeather.RESET;
                            } else if (this.showRain) {
                                w = SpoutWeather.RAIN;
                            } else if (this.showSnow) {
                                w = SpoutWeather.SNOW;
                            }
                        }
                        SpoutManager.getBiomeManager().setPlayerWeather(SpoutManager.getPlayer(player), w);
                    }
                } catch (Throwable t) {
                    MyWorlds.isSpoutEnabled = false;
                    MyWorlds.log(Level.SEVERE, "An error occured while using Spout, Spout is no longer used in MyWorlds from now on:");
                    t.printStackTrace();
                }
            }
    As soon you notice that the plugin is heavily failing, you can cut it off and stop using it. Of course, if you expect less severe errors to occur that are not related to runtime errors, you can add multiple catches.
    Code:
    try {
        //sensitive coding
    } catch (Exception ex) {
        ex.printStackTrace(); //less severe errors
    } catch (Throwable t) {
        //runtime/other errors - kill it!
    }
     
  7. That's overkill code, I'm trying to imply the least amount of code required =)

    Anyway, I forgot that Spout uses the same thing... look, SpoutManager.get..., that's what I'm trying to achieve, a simple name to access the plugin, I've already done that but I can't seem to make a separate jar as the API for it, something that would be significantly less in size than the plugin, since you don't need to include... actually, java doesn't include everything anyway, so a separate API would be a waste of time... so, screw it, I'll learn interfaces and abstract another time.

    Thanks guys :p
     
  8. Hmm, it seems that static alone won't do if the plugin isn't there =) ... I guess the getPlugin() is the only way :/
     
  9. Offline

    bergerkiller

    @Digi you can't add a check in your plugin to check if your plugin is there, because that check wouldn't be there either then. All the other can do is check if the plugin is enabled and/or get the plugin instance. From there on he can do anything he wants.

    Having a static 'pluginExists' variable won't work, since this 'flag' isn't there when it doesn't exist.
     
  10. Yes, I realized that :} but getPlugin() is short enough.
     
Thread Status:
Not open for further replies.

Share This Page