简体   繁体   中英

How to enable a bukkit plugin first?

I have an interesting problem where I want to enable a specific Bukkit plugin first before any other plugins are enabled. This has proven a difficult task. I can't use the plugin.yml dependancy options because those assume I know what plugins are installed on any given server. I don't care if it doesn't load first, but I do need it to enable first.

I have tried several methods to accomplish this but with no luck:

Attempt 1:

static{

    try {
        Bukkit.getPluginManager().loadPlugin(plug);
        Bukkit.getPluginManager().enablePlugin(plugin);
    } catch (UnknownDependencyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidPluginException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidDescriptionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Allow me to explain. The static seems to run before most anything, including plugin loadups. This means I also have to define when it loads as well. This normally wouldn't be a problem, except non static API like getDataFolder() for the file Path doesn't work.

public static File plug = new File("/plugins/Debugger");

So unless I'm doing my Paths wrong, I have no clue why this throws an Exception.

NOTE: Yes, I have tried multiple different paths such as "plugins/Debugger" or "Debugger.jar" and ECT.

Method 2:

public void onLoad(){
        Bukkit.getPluginManager().enablePlugin(plugin);
        console.info("[Debugger] loaded first!");
    }

This seemed too good to be true and this method actually seemed to get me closer to solving my problem. This Method is called whenever the plugin loads, so by enabling the plugin within the onLoad Method, it actually caused the plugin to enable first; But there were issues when loading:

[00:15:08] [Server thread/ERROR]: null initializing Debugger v1.0.0 (Is it up to date?)
java.lang.NullPointerException
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:396) ~[craftbukkit.jar:git-Bukkit-0ebb9c7]
    at me.doublehelix457.Debugger.Debugger.onLoad(Debugger.java:20) ~[?:?]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.loadPlugins(CraftServer.java:299) [craftbukkit.jar:git-Bukkit-0ebb9c7]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.reload(CraftServer.java:723) [craftbukkit.jar:git-Bukkit-0ebb9c7]
    at org.bukkit.Bukkit.reload(Bukkit.java:548) [craftbukkit.jar:git-Bukkit-0ebb9c7]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [craftbukkit.jar:git-Bukkit-0ebb9c7]

Somehow despite the weird null initializing Debugger (Debugger is the test plugin name) the plugin still managed to enable first?

So the line it is referring to is Bukkit.getPluginManager().enablePlugin(plugin);

Doing some research online I noticed certain API like getServer() was not working within that Method and I believe that maybe this means that Bukkit or PluginManager may not exist yet.

If that's the case, then is there a workaround to this?

That said, I'm willing to take improvements on my current attempts or even try new ones, whatever gets the job done. Please do not ask "Why do you need to enable the plugin first?" I should mention that this version of bukkit is on 1.10.

Any HELPFUL advice would be much appreciated.

So now I feel really stupid. Turns out all I had to do was make my plugin variable non-static and define it. All that trouble just to change public static Debugger plugin; to public Debugger plugin = this;

This change works with Method 2.

Well. I hope that this benefits someone as to how they can enable a plugin first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM