Reloading plugins from code

Hi! How to correct reload plugins from code, i.e. do the same as the “/sponge plugins reload” command?

I think there are two ways to achieve it.

  1. Listen to Sponge’s reload event which can be triggered by running /sponge plugins reload
    Example taken from Sponge docs:
import org.spongepowered.api.event.game.GameReloadEvent;

@Listener
public void reload(GameReloadEvent event) {
    // Do reload stuff
}
  1. Create your own reload command and reload what you want. I.e reload cache, classes’ or object’s fields etc.
1 Like

No, you did not understand. :slight_smile: I need to post this event in the core, as if I had called a command.
For example, something like Sponge.getPluginManager().reloadPlugins().

Well, then let’s wait for sombody that will able to help because I still don’t understand.

I’ve not tested but something like this should work:

Sponge.getEventManager().post(SpongeEventFactory.createGameReloadEvent(Sponge.getCauseStackManager().getCurrentCause()));
1 Like

This is a internal class of SpongeCommon. Internal classes are categorically unacceptable in the plugin code.

You’re wrong. org.spongepowered.api.event.SpongeEventFactory is part of SpongeAPI.

Very strange. Its not in the documentation, and the source code does not exist in the SpongeAPI repository. However, it is highlighted in the IDE.

That’s because there is no source code, this class is generated dynamically. :slight_smile:

2 Likes