legacy.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. local 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. local tables, err
  44. tables, err = loadfile(filename)
  45. if err then
  46. return err
  47. end
  48. tables = tables()
  49. for idx = 1, #tables do
  50. local tolinkv, tolinki = {}, {}
  51. for i, v in pairs(tables[idx]) do
  52. if type(v) == "table" and tables[v[1]] then
  53. table.insert(tolinkv, {i, tables[v[1]]})
  54. end
  55. if type(i) == "table" and tables[i[1]] then
  56. table.insert(tolinki, {i, tables[i[1]]})
  57. end
  58. end
  59. -- link values, first due to possible changes of indices
  60. for _, v in ipairs(tolinkv) do
  61. tables[idx][v[1]] = v[2]
  62. end
  63. -- link indices
  64. for _, v in ipairs(tolinki) do
  65. tables[idx][v[2]], tables[idx][v[1]] = tables[idx][v[1]], nil
  66. end
  67. end
  68. self.areas = tables[1]
  69. end
  70. -- Returns the name of the first player that owns an area
  71. function areas.getNodeOwnerName(pos)
  72. for id, area in pairs(areas:getAreasAtPos(pos)) do
  73. return area.owner
  74. end
  75. return false
  76. end
  77. -- Checks if a node is owned by you
  78. function areas.isNodeOwner(pos, name)
  79. if minetest.check_player_privs(name, areas.adminPrivs) then
  80. return true
  81. end
  82. for id, area in pairs(areas:getAreasAtPos(pos)) do
  83. if name == area.owner then
  84. return true
  85. end
  86. end
  87. return false
  88. end
  89. -- Checks if an area has an owner
  90. function areas.hasOwner(pos)
  91. for id, area in pairs(areas:getAreasAtPos(pos)) do
  92. return true
  93. end
  94. return false
  95. end
  96. IsPlayerNodeOwner = areas.isNodeOwner
  97. GetNodeOwnerName = areas.getNodeOwnerName
  98. HasOwner = areas.hasOwner
  99. -- This is entirely untested and may break in strange and new ways.
  100. if areas.config.legacy_table then
  101. owner_defs = setmetatable({}, {
  102. __index = function(table, key)
  103. local a = rawget(areas.areas, key)
  104. if not a then return a end
  105. local b = {}
  106. for k, v in pairs(a) do b[k] = v end
  107. b.x1, b.y1, b.z1 = b.pos1.x, b.pos1.y, b.pos1.z
  108. b.x2, b.y1, b.z2 = b.pos2.x, b.pos2.y, b.pos2.z
  109. b.pos1, b.pos2 = nil, nil
  110. b.id = key
  111. return b
  112. end,
  113. __newindex = function(table, key, value)
  114. local a = value
  115. a.pos1, a.pos2 = {x=a.x1, y=a.y1, z=a.z1},
  116. {x=a.x2, y=a.y2, z=a.z2}
  117. a.x1, a.y1, a.z1, a.x2, a.y2, a.z2 =
  118. nil, nil, nil, nil, nil, nil
  119. a.name = a.name or "unnamed"
  120. a.id = nil
  121. return rawset(areas.areas, key, a)
  122. end
  123. })
  124. end