init.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. local mod_storage = minetest.get_mod_storage()
  2. health_adjuster = {}
  3. health_adjuster.players = {}
  4. local start_max_health = tonumber(minetest.settings:get("health_adjuster_start_max_health")) or 20
  5. local max_reachable = tonumber(minetest.settings:get("health_adjuster_max_reachable")) or 50
  6. -- Load palyers' saved data
  7. if mod_storage:get_string("data") ~= nil and mod_storage:get_string("data") ~= "return nil" and mod_storage:get_string("data") ~= "" then
  8. health_adjuster.players = minetest.deserialize(mod_storage:get_string("data"))
  9. minetest.log("info", "health_adjuster: Previously saved data loaded.")
  10. end
  11. local function save_players_data()
  12. mod_storage:set_string("data", minetest.serialize(health_adjuster.players))
  13. end
  14. health_adjuster.set_players_max_health = function(player, value)
  15. local name = player:get_player_name()
  16. if value > 0 then
  17. if value > max_reachable then
  18. value = max_reachable
  19. end
  20. health_adjuster.players[name] = value
  21. player:set_properties({
  22. hp_max = value,
  23. })
  24. save_players_data()
  25. end
  26. end
  27. minetest.register_on_joinplayer(function(player)
  28. local name = player:get_player_name()
  29. if health_adjuster.players[name] == nil then
  30. health_adjuster.players[name] = start_max_health
  31. save_players_data()
  32. else
  33. player:set_properties({
  34. hp_max = health_adjuster.players[name]
  35. })
  36. end
  37. end)