areas/legacy.lua

109 lines
2.7 KiB
Lua
Raw Normal View History

2013-09-03 01:16:14 +02:00
-- This file contains functions to convert from
-- the old areas format and other compatability code.
2020-03-08 22:15:00 +01:00
local S = minetest.get_translator("areas")
2013-09-03 01:16:14 +02:00
minetest.register_chatcommand("legacy_load_areas", {
2020-03-08 22:15:00 +01:00
params = S("<version>"),
description = S("Loads, converts, and saves the areas from"
.." a legacy save file."),
2014-07-13 01:10:26 +02:00
privs = {areas=true, server=true},
2013-09-03 01:16:14 +02:00
func = function(name, param)
2020-03-08 22:15:00 +01:00
minetest.chat_send_player(name, S("Converting areas…"))
local version = tonumber(param)
if version == 0 then
2019-04-03 19:24:02 +02:00
local err = areas:node_ownership_load()
if err then
2020-03-08 22:15:00 +01:00
minetest.chat_send_player(name, S("Error loading legacy file: @1", err))
return
end
else
2020-03-08 22:15:00 +01:00
minetest.chat_send_player(name, S("Invalid version number. (0 allowed)"))
2013-09-03 01:16:14 +02:00
return
end
2020-03-08 22:15:00 +01:00
minetest.chat_send_player(name, S("Legacy file loaded."))
2013-09-03 01:16:14 +02:00
for k, area in pairs(areas.areas) do
-- New position format
area.pos1 = {x=area.x1, y=area.y1, z=area.z1}
area.pos2 = {x=area.x2, y=area.y2, z=area.z2}
2013-09-03 01:16:14 +02:00
area.x1, area.y1, area.z1,
area.x2, area.y2, area.z2 =
2013-09-03 01:16:14 +02:00
nil, nil, nil, nil, nil, nil
-- Area positions sorting
2014-07-12 22:37:54 +02:00
areas:sortPos(area.pos1, area.pos2)
2013-09-03 01:16:14 +02:00
-- Add name
2020-03-08 22:15:00 +01:00
area.name = S("unnamed")
-- Remove ID
area.id = nil
2013-09-03 01:16:14 +02:00
end
2020-03-08 22:15:00 +01:00
minetest.chat_send_player(name, S("Table format updated."))
2013-09-03 01:16:14 +02:00
areas:save()
2020-03-08 22:15:00 +01:00
minetest.chat_send_player(name, S("Converted areas saved. Done."))
2014-07-12 22:37:54 +02:00
end
})
2013-09-03 01:16:14 +02:00
function areas:node_ownership_load()
2013-09-03 01:16:14 +02:00
local filename = minetest.get_worldpath().."/owners.tbl"
2019-04-03 19:24:02 +02:00
local tables, err
2013-09-03 01:16:14 +02:00
tables, err = loadfile(filename)
if err then
return err
end
tables = tables()
for idx = 1, #tables do
local tolinkv, tolinki = {}, {}
for i, v in pairs(tables[idx]) do
if type(v) == "table" and tables[v[1]] then
table.insert(tolinkv, {i, tables[v[1]]})
end
if type(i) == "table" and tables[i[1]] then
table.insert(tolinki, {i, tables[i[1]]})
end
end
-- link values, first due to possible changes of indices
for _, v in ipairs(tolinkv) do
tables[idx][v[1]] = v[2]
end
-- link indices
for _, v in ipairs(tolinki) do
tables[idx][v[2]], tables[idx][v[1]] = tables[idx][v[1]], nil
end
end
self.areas = tables[1]
end
-- Returns the name of the first player that owns an area
function areas.getNodeOwnerName(pos)
for id, area in pairs(areas:getAreasAtPos(pos)) do
return area.owner
2013-09-03 01:16:14 +02:00
end
return false
end
-- Checks if a node is owned by you
function areas.isNodeOwner(pos, name)
2014-07-13 01:10:26 +02:00
if minetest.check_player_privs(name, areas.adminPrivs) then
2013-09-03 01:16:14 +02:00
return true
end
for id, area in pairs(areas:getAreasAtPos(pos)) do
if name == area.owner then
return true
2013-09-03 01:16:14 +02:00
end
end
return false
end
2013-11-15 00:26:57 +01:00
-- Checks if an area has an owner
function areas.hasOwner(pos)
for id, area in pairs(areas:getAreasAtPos(pos)) do
return true
2013-11-15 00:26:57 +01:00
end
return false
end