Solved How to execute task before the plugin is stopped?

Discussion in 'Spigot Plugin Development' started by Xeonmeister, Feb 17, 2020.

  1. Im saving player data to a database and i wanted to make it so when the server stops then before kicking everyone it saves all the data. The problem is that the onDisable is called after the plugin is shut down and i can not schedule any tasks. Is there maybe an event or something that fires BEFORE the plugin is disabled?
  2. Maybe try to detect when someone runs the /stop or /reload command.
    1) You save your data
    2) You cancel it1
    3) You make the sender re execute the command or you do Bukkit.shutdown()

    I hope this works, since I haven't tried it
    #2 PainDeMie, Feb 17, 2020
    Last edited: Feb 17, 2020
  3. it doesnt make any sense to schedule a task when the server is shutting down
    • Agree Agree x 1
  4. onDisable is when the server is still online and the plugin is about to shut down.
  5. Override JavaPlugin#onDisable ?
  6. Nvm i just made a seperate function to be called when the plugin.isEnabled() is false. (By the time onDisable is called plugin.isEnabled() will already give you false).
  7. There are many solutions for this,
    Instead of looking for help with your attempted solution look for help with your actual problem (Saving player stats on disable)
    Running a "task" before server stop may not be the better solution for this.
    You can try:
    - Cache the player stats, so you can save them if the player is offline.
    - Save stats when player disconnects instead of server disable
    • Like Like x 1