init.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. --[[
  2. Copyright 2017 Auke Kok <sofar@foo-projects.org>
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject
  9. to the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  13. KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. ]]--
  20. local def = 300 -- server must be empty for this long
  21. local int = 15 -- check interval in seconds
  22. local to = def
  23. local function do_shutdown()
  24. local i, _ = next(minetest.get_connected_players())
  25. if not i then
  26. to = to - int
  27. else
  28. to = def
  29. minetest.after(int, do_shutdown)
  30. return
  31. end
  32. if to < 0 then
  33. minetest.log("action", "autoshutdown")
  34. minetest.request_shutdown()
  35. else
  36. minetest.after(int, do_shutdown)
  37. end
  38. end
  39. minetest.register_chatcommand("autoshutdown", {
  40. params = "autoshutdown server",
  41. description = "shut the server down after the last player leaves",
  42. privs = {server = true},
  43. func = function(name, param)
  44. do_shutdown()
  45. return true, "autoshutdown issued"
  46. end,
  47. })