Resource Detect plugin load at server start or unload at server stop

Discussion in 'Spigot Plugin Development' started by Nik2143, Jul 17, 2021.

  1. Spigot API hasn't an official way to detect if the plugin has been reloaded but there are some workaround that can be used to detect if the plugin has been loaded or unloaded from a plugin manager

    To detect if the plugin has been loaded from plugin manager can be used the field currentTick that is in CraftScheduler Class
    To get access to the field we can use Reflaction with this line
    Code (Java):
    Field tick = Bukkit.getScheduler().getClass().getDeclaredField("currentTick");
    the field currentTick is private so is needed to set it as accessible
    Code (Java):
    tick.setAccessible(true);
    Now you can get the value with this
    Code (Java):
    int tickvalue = (Integer) tick.get(Bukkit.getScheduler());
    and use the variable tickvalue to check if plugin has been loaded from a plugin manager
    If the value is -1 the plugin has been loaded on server start if it is higher the plugin has been loaded from a plugin manager or anyway after the saver has been started

    To detect if the plugin has been unloaded on server shutdown can be used the method isRunning in MinecraftServer class
    To get access to this method is possible to use NMS or Reflection, here will be seen the method with Reflection
    First is needed to get the MinecraftServer instance with the method getServer() in CraftServer
    Code (Java):
    Method getServerMethod = getServer().getClass().getMethod("getServer");
    Object minecraftServer = getServerMethod.invoke(Bukkit.getServer());
    Now is needed to get the method isRunning
    Code (Java):
    Method isRunningMethod = minecraftServer.getClass().getMethod("isRunning");
    and invoking it is possible to detect if the server is stopping
    Code (Java):
    boolean stopping = !(Boolean) isRunningMethod.invoke(minecraftServer);
    The value will be true if the server is stopping or false if it is still running (so the plugin has been unloaded from a plugin manager)
     
    #1 Nik2143, Jul 17, 2021
    Last edited: Jul 17, 2021
    • Useful Useful x 1
  2. I am pretty sure that these events are handled within the server.
    Whilst the Spigot.API is not directly responsible for these actions the Bukkit.API will be!
    Spigot is an extension of the Bukkit API.
    ie
    Plugin thisPlugin.depends().somePlugin();

    ReLoad Server.plugin events are never recommended!
     
    #2 Goldentoenail, Jul 17, 2021
    Last edited: Jul 17, 2021
  3. As i know spigot and bukkit API haven't a way to know if server is stopping same thing to know if the plugin has been loaded during server startup
     
  4. There is actually a trick you can use to detect if the server is reloading or if it is starting new. When a server reloads, all worlds will remain loaded. So by making your plugin launch before worlds load, you can simply check if there are worlds loaded in onEnable. If so, it is a reload, if not, it is a new start up.
    Simply put this in your plugin.yml to make your plugin enable before worlds load on startup:
    Code (YAML):
    load: STARTUP
    Then in your onEnable() you can check the following:
    Code (Java):
    if (!Bukkit.getWorlds().isEmpty()){
        // server has been reloaded.
    }
     
  5. I didn't knew about this way