Skip to content

Getting the plugin instance

games647 edited this page Mar 29, 2015 · 6 revisions

Adding it to the plugin.yml

In order to access the ScoreboardStats in the onEnable() method, you need to tell Bukkit to load your plugin after ScoreboardStats. You can do this either with softdepend or depend in your plugin.yml.

Softdepend means that your plugin can still work without ScoreboardStats. Depend would check if the the plugin is available and if not your plugin won't be enabled. (See BukkitWiki)

...
softdepend:
    - Plugin1
    - ScoreboardStats
    - Plugin2
...
Or
...
softdepend: [Plugin1, ScoreboardStats, Plugin3]
...

Get the instance

Static method since Minecraft 1.7.2
    private void getPluginInstance() {
        //You should check if the plugin is available first 
        //otherwise it would throw a NoClassDefFoundError
        if (Bukkit.getPluginManager().isPluginEnabled("ScoreboardStats")) {
            //Added in 1.7.2
            ScoreboardStats scoreboardStats = JavaPlugin
                    .getPlugin(ScoreboardStats.class);
        }
    }

Old but still compatible solution
    private void getPluginInstance() {
        //returns null if plugin wasn't found
        Plugin plugin = Bukkit.getPluginManager().getPlugin("ScoreboardStats");
        //you should check this before casting
        ScoreboardStats scoreboardstats = (ScoreboardStats) plugin;
    }