A rewrite of the node_ownership Minetest mod with many new features. https://content.minetest.net/packages/ShadowNinja/areas/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.6KB

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