[Solved] Disable plugin (disable itself)

Hi!

I want to know the best solution for when during initialization, and something goes wrong in that process. This will most likely make the plugin unusable and throw millions of exceptions.

How do I disable/unload the plugin when a fail during initialization happens? I did not find anything in the PluginManager regarding this.

Thanks,
Yrlish

Unfortunately sponge does not allow disabling a plugin…

Any good ideas for a workaround?

Easy, do your checks and have a simple boolean “enabled” that after the plugin init, you go and say “hey, were we enabled properly? No? Okay, keep that boolean false and don’t init listeners etc”.

Then third parties can check your API of “enabled” and go from there.

Sounds like a good idea, thanks! Will look into how to implement this properly. :slight_smile:

Disabling plugins while the server is running has the potential to create a myriad of issues which is why its not supported. I recommend implenenting a sort of “disabled mode” where the core functionality of your plugin doesnt work. (I.e. if the config fails to load, dont register commands, etc)

Unregister all your listeners and commands should work nice as well.

1 Like

The block of code I use:

private void disable() {
    game.getEventManager().unregisterPluginListeners(this);
    game.getCommandManager().getOwnedBy(this).forEach(game.getCommandManager()::removeMapping);
    game.getScheduler().getScheduledTasks(this).forEach(Task::cancel);
}

I’d prefer that the plugin crashes the server. It is more obvious for the user that something went wrong.

1 Like

I would not prefer a plugin that crashs my production server and corrupts my worlds …
I would prefer a plugin that shutdown the server with an error message

Thanks for your replies!

I will most likely go with a similar solution that @pie_flavor presented. Unregister listeners, remove commands, cancels all scheduled tasks and log that the plugin was disabled with the reason why.

Ah, I thought we were talking about the plugin startup phase.

I would agree with you in the dev environment, but yeah- not really ideal for production.