spawns.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. local world_path = minetest.get_worldpath()
  2. local org_file = world_path .. "/beds_spawns"
  3. local file = world_path .. "/beds_spawns"
  4. local bkwd = false
  5. -- check for PA's beds mod spawns
  6. local cf = io.open(world_path .. "/beds_player_spawns", "r")
  7. if cf ~= nil then
  8. io.close(cf)
  9. file = world_path .. "/beds_player_spawns"
  10. bkwd = true
  11. end
  12. function beds.read_spawns()
  13. local spawns = beds.spawn
  14. local input = io.open(file, "r")
  15. if input and not bkwd then
  16. repeat
  17. local x = input:read("*n")
  18. if x == nil then
  19. break
  20. end
  21. local y = input:read("*n")
  22. local z = input:read("*n")
  23. local name = input:read("*l")
  24. spawns[name:sub(2)] = {x = x, y = y, z = z}
  25. until input:read(0) == nil
  26. io.close(input)
  27. elseif input and bkwd then
  28. beds.spawn = minetest.deserialize(input:read("*all"))
  29. input:close()
  30. beds.save_spawns()
  31. os.rename(file, file .. ".backup")
  32. file = org_file
  33. end
  34. end
  35. beds.read_spawns()
  36. function beds.save_spawns()
  37. if not beds.spawn then
  38. return
  39. end
  40. local data = {}
  41. local output = io.open(org_file, "w")
  42. for k, v in pairs(beds.spawn) do
  43. table.insert(data, string.format("%.1f %.1f %.1f %s\n", v.x, v.y, v.z, k))
  44. end
  45. output:write(table.concat(data))
  46. io.close(output)
  47. end
  48. function beds.set_spawns()
  49. for name,_ in pairs(beds.player) do
  50. local player = minetest.get_player_by_name(name)
  51. local p = player:get_pos()
  52. -- but don't change spawn location if borrowing a bed
  53. if not minetest.is_protected(p, name) then
  54. beds.spawn[name] = p
  55. end
  56. end
  57. beds.save_spawns()
  58. end