spawns.lua 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_spawn(pos, name)
  49. if beds.player[name] then
  50. local player = minetest.get_player_by_name(name)
  51. if player and player:is_player() then
  52. beds.spawn[name] = pos
  53. end
  54. end
  55. beds.save_spawns()
  56. end