api.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. -------------------------
  2. -- Sky Layers: API
  3. -- Git: https://gitlab.com/rautars/skylayer
  4. -- License: MIT
  5. -- Credits: rautars
  6. -------------------------
  7. -- include skylayer core functions
  8. local modpath = minetest.get_modpath("skylayer");
  9. local core = dofile(modpath.."/core.lua")
  10. skylayer = {}
  11. -- set flag for enable / disable skylayer
  12. skylayer.is_enabled = function(enabled)
  13. core.settings.enabled = enabled
  14. end
  15. skylayer.add_layer = function(player_name, layer)
  16. if layer == nil or layer.name == nil then
  17. minetest.log("error", "Incorrect skylayer definition")
  18. return
  19. end
  20. local player_data = core.get_player_data(player_name)
  21. if player_data == nil then
  22. player_data = core.create_new_player_data(player_name)
  23. end
  24. if player_data == nil then
  25. minetest.log("error", "Fail to add skylayer to player '" .. player_name .. "'")
  26. return
  27. end
  28. layer.added_time = os.time()
  29. layer.updated = false
  30. layer.update_interval = layer.update_interval and layer.update_interval or core.settings.update_interval
  31. table.insert(player_data.skylayers, layer)
  32. end
  33. skylayer.remove_layer = function(player_name, layer_name)
  34. local player_data = core.get_player_data(player_name)
  35. if player_data == nil or player_data.skylayers == nil then
  36. return
  37. end
  38. if #player_data.skylayers == 0 then
  39. return
  40. end
  41. for k, layer in ipairs(player_data.skylayers) do
  42. if layer.name == layer_name then
  43. table.remove(player_data.skylayers, k)
  44. if #player_data.skylayers == 0 then
  45. local player = core.get_player_by_name(player_name)
  46. if player ~= nil then
  47. core.reset_sky(player)
  48. end
  49. end
  50. return
  51. end
  52. end
  53. end