legacy.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. -- This file contains functions to convert from
  2. -- the old areas format and other compatability code.
  3. minetest.register_chatcommand("legacy_load_areas", {
  4. params = "<version>",
  5. description = "Loads, converts, and saves the areas from"
  6. .." a legacy save file.",
  7. privs = {areas=true, server=true},
  8. func = function(name, param)
  9. minetest.chat_send_player(name, "Converting areas...")
  10. local version = tonumber(param)
  11. if version == 0 then
  12. err = areas:node_ownership_load()
  13. if err then
  14. minetest.chat_send_player(name, "Error loading legacy file: "..err)
  15. return
  16. end
  17. else
  18. minetest.chat_send_player(name, "Invalid version number. (0 allowed)")
  19. return
  20. end
  21. minetest.chat_send_player(name, "Legacy file loaded.")
  22. for k, area in pairs(areas.areas) do
  23. -- New position format
  24. area.pos1 = {x=area.x1, y=area.y1, z=area.z1}
  25. area.pos2 = {x=area.x2, y=area.y2, z=area.z2}
  26. area.x1, area.y1, area.z1,
  27. area.x2, area.y2, area.z2 =
  28. nil, nil, nil, nil, nil, nil
  29. -- Area positions sorting
  30. areas:sortPos(area.pos1, area.pos2)
  31. -- Add name
  32. area.name = "unnamed"
  33. -- Remove ID
  34. area.id = nil
  35. end
  36. minetest.chat_send_player(name, "Table format updated.")
  37. areas:save()
  38. minetest.chat_send_player(name, "Converted areas saved. Done.")
  39. end
  40. })
  41. function areas:node_ownership_load()
  42. local filename = minetest.get_worldpath().."/owners.tbl"
  43. tables, err = loadfile(filename)
  44. if err then
  45. return err
  46. end
  47. tables = tables()
  48. for idx = 1, #tables do
  49. local tolinkv, tolinki = {}, {}
  50. for i, v in pairs(tables[idx]) do
  51. if type(v) == "table" and tables[v[1]] then
  52. table.insert(tolinkv, {i, tables[v[1]]})
  53. end
  54. if type(i) == "table" and tables[i[1]] then
  55. table.insert(tolinki, {i, tables[i[1]]})
  56. end
  57. end
  58. -- link values, first due to possible changes of indices
  59. for _, v in ipairs(tolinkv) do
  60. tables[idx][v[1]] = v[2]
  61. end
  62. -- link indices
  63. for _, v in ipairs(tolinki) do
  64. tables[idx][v[2]], tables[idx][v[1]] = tables[idx][v[1]], nil
  65. end
  66. end
  67. self.areas = tables[1]
  68. end
  69. -- Returns the name of the first player that owns an area
  70. function areas.getNodeOwnerName(pos)
  71. for id, area in pairs(areas:getAreasAtPos(pos)) do
  72. return area.owner
  73. end
  74. return false
  75. end
  76. -- Checks if a node is owned by you
  77. function areas.isNodeOwner(pos, name)
  78. if minetest.check_player_privs(name, areas.adminPrivs) then
  79. return true
  80. end
  81. for id, area in pairs(areas:getAreasAtPos(pos)) do
  82. if name == area.owner then
  83. return true
  84. end
  85. end
  86. return false
  87. end
  88. -- Checks if an area has an owner
  89. function areas.hasOwner(pos)
  90. for id, area in pairs(areas:getAreasAtPos(pos)) do
  91. return true
  92. end
  93. return false
  94. end
  95. IsPlayerNodeOwner = areas.isNodeOwner
  96. GetNodeOwnerName = areas.getNodeOwnerName
  97. HasOwner = areas.hasOwner
  98. -- This is entirely untested and may break in strange and new ways.
  99. if areas.config.legacy_table then
  100. owner_defs = setmetatable({}, {
  101. __index = function(table, key)
  102. local a = rawget(areas.areas, key)
  103. if not a then return a end
  104. local b = {}
  105. for k, v in pairs(a) do b[k] = v end
  106. b.x1, b.y1, b.z1 = b.pos1.x, b.pos1.y, b.pos1.z
  107. b.x2, b.y1, b.z2 = b.pos2.x, b.pos2.y, b.pos2.z
  108. b.pos1, b.pos2 = nil, nil
  109. b.id = key
  110. return b
  111. end,
  112. __newindex = function(table, key, value)
  113. local a = value
  114. a.pos1, a.pos2 = {x=a.x1, y=a.y1, z=a.z1},
  115. {x=a.x2, y=a.y2, z=a.z2}
  116. a.x1, a.y1, a.z1, a.x2, a.y2, a.z2 =
  117. nil, nil, nil, nil, nil, nil
  118. a.name = a.name or "unnamed"
  119. a.id = nil
  120. return rawset(areas.areas, key, a)
  121. end
  122. })
  123. end