1 -- This file contains functions to convert from
2 -- the old areas format and other compatability code.
4 minetest
.register_chatcommand("legacy_load_areas", {
6 description
= "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
, "Converting areas...")
11 local version
= tonumber(param
)
13 err
= areas
:node_ownership_load()
15 minetest
.chat_send_player(name
, "Error loading legacy file: "..err
)
19 minetest
.chat_send_player(name
, "Invalid version number. (0 allowed)")
22 minetest
.chat_send_player(name
, "Legacy file loaded.")
24 for k
, area
in pairs(areas
.areas
) do
25 -- New position format
26 area
.pos1
= {x
=area
.x1
, y
=area
.y1
, z
=area
.z1
}
27 area
.pos2
= {x
=area
.x2
, y
=area
.y2
, z
=area
.z2
}
29 area
.x1
, area
.y1
, area
.z1
,
30 area
.x2
, area
.y2
, area
.z2
=
31 nil, nil, nil, nil, nil, nil
33 -- Area positions sorting
34 areas
:sortPos(area
.pos1
, area
.pos2
)
42 minetest
.chat_send_player(name
, "Table format updated.")
45 minetest
.chat_send_player(name
, "Converted areas saved. Done.")
49 function areas
:node_ownership_load()
50 local filename
= minetest
.get_worldpath().."/owners.tbl"
51 tables
, err
= loadfile(filename
)
57 for idx
= 1, #tables
do
58 local tolinkv
, tolinki
= {}, {}
59 for i
, v
in pairs(tables
[idx
]) do
60 if type(v
) == "table" and tables
[v
[1]]
then
61 table.insert(tolinkv
, {i
, tables
[v
[1]]
})
63 if type(i
) == "table" and tables
[i
[1]]
then
64 table.insert(tolinki
, {i
, tables
[i
[1]]
})
67 -- link values, first due to possible changes of indices
68 for _
, v
in ipairs(tolinkv
) do
69 tables
[idx
][v
[1]]
= v
[2]
72 for _
, v
in ipairs(tolinki
) do
73 tables
[idx
][v
[2]]
, tables
[idx
][v
[1]]
= tables
[idx
][v
[1]]
, nil
76 self
.areas
= tables
[1]
79 -- Returns the name of the first player that owns an area
80 function areas
.getNodeOwnerName(pos
)
81 for id
, area
in pairs(areas
:getAreasAtPos(pos
)) do
87 -- Checks if a node is owned by you
88 function areas
.isNodeOwner(pos
, name
)
89 if minetest
.check_player_privs(name
, areas
.adminPrivs
) then
92 for id
, area
in pairs(areas
:getAreasAtPos(pos
)) do
93 if name
== area
.owner
then
100 -- Checks if an area has an owner
101 function areas
.hasOwner(pos
)
102 for id
, area
in pairs(areas
:getAreasAtPos(pos
)) do
108 IsPlayerNodeOwner
= areas
.isNodeOwner
109 GetNodeOwnerName
= areas
.getNodeOwnerName
110 HasOwner
= areas
.hasOwner
112 -- This is entirely untested and may break in strange and new ways.
113 if areas
.config
.legacy_table
then
114 owner_defs
= setmetatable({}, {
115 __index
= function(table, key
)
116 local a
= rawget(areas
.areas
, key
)
117 if not a
then return a
end
119 for k
, v
in pairs(a
) do b
[k
] = v
end
120 b
.x1
, b
.y1
, b
.z1
= b
.pos1
.x
, b
.pos1
.y
, b
.pos1
.z
121 b
.x2
, b
.y1
, b
.z2
= b
.pos2
.x
, b
.pos2
.y
, b
.pos2
.z
122 b
.pos1
, b
.pos2
= nil, nil
126 __newindex
= function(table, key
, value
)
128 a
.pos1
, a
.pos2
= {x
=a
.x1
, y
=a
.y1
, z
=a
.z1
},
129 {x
=a
.x2
, y
=a
.y2
, z
=a
.z2
}
130 a
.x1
, a
.y1
, a
.z1
, a
.x2
, a
.y2
, a
.z2
=
131 nil, nil, nil, nil, nil, nil
132 a
.name
= a
.name
or "unnamed"
134 return rawset(areas
.areas
, key
, a
)