From bacec849db3135908aae7a1cdbd963bceab95791 Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Mon, 8 Aug 2016 18:51:17 +0200 Subject: [PATCH] Added convert function and command --- chatcommands.lua | 22 +++++++++++++++------- factions.lua | 44 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/chatcommands.lua b/chatcommands.lua index 2b8c778..ff776d0 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -443,12 +443,7 @@ factions.register_command("where", { infaction = false, on_success = function(player, faction, pos, chunkpos, args) local chunk = factions.chunks[chunkpos] - minetest.chat_send_player(player, "You are standing on chunk "..chunkpos) - if not chunk then - minetest.chat_send_player(player, "This chunk is free.") - else - minetest.chat_send_player(player, "This chunk belongs to "..chunk) - end + minetest.chat_send_player(player, "You are standing on chunk "..chunkpos..", part of "..chunk or "Wilderness"..",") return true end }) @@ -463,7 +458,6 @@ factions.register_command("help", { factions.register_command("spawn", { description = "Shows your faction's spawn", - infaction = true, on_success = function(player, faction, pos, chunkpos, args) if faction.spawn then minetest.chat_send_player(player, "Spawn is at ("..table.concat(faction.spawn, ", ")..")") @@ -474,6 +468,20 @@ factions.register_command("spawn", { end }) +factions.register_command("convert", { + description = "Load factions in the old format", + infaction = false, + global_privileges = {"faction_admin"}, + format = {"string"}, + on_success = function(player, faction, pos, chunkpos, args) + if factions.convert(args.strings[1]) then + minetest.chat_send_player(player, "Factions successfully converted.") + else + minetest.chat_send_player(player, "Error.") + end + end +}) + ------------------------------------------------------------------------------- -- name: cmdhandler(playername,parameter) -- diff --git a/factions.lua b/factions.lua index 5bcfb1f..fbb5e99 100644 --- a/factions.lua +++ b/factions.lua @@ -275,14 +275,6 @@ function factions.Faction.on_delete_rank(self, rank, newrank) end --?????????????? -function factions.fix_powercap(name) - factions.data.factions[name].powercap = #factions.dynamic_data.membertable[name] + 10 -end ---?????????????? - -function factions.get_chunk(pos) - return factions.chunks[factions.get_chunkpos(pos)] -end function factions.get_chunk_pos(pos) return math.floor(pos.x / 16.)..","..math.floor(pos.z / 16.) @@ -384,6 +376,42 @@ function factions.load() file:close() end end + +function factions.convert(filename) + local file, error = io.open(factions_worldid .. "/" .. filename, "r") + if not file then + minetest.chat_send_all("Cannot load file "..filename..". "..error) + return false + end + local raw_data = file:read("*a") + local data = minetest.deserialize(raw_data) + local factionsmod = data.factionsmod + local objects = data.objects + for faction, attrs in pairs(factionsmod) do + local newfac = factions.new_faction(faction) + newfac:add_player(attrs.owner, "leader") + for player, _ in pairs(attrs.adminlist) do + if not newfac.players[player] then + newfac:add_player(player, "moderator") + end + end + for player, _ in pairs(attrs.invitations) do + newfac:invite_player(player) + end + for i in ipairs(attrs.chunk) do + local chunkpos = table.concat(attrs.chunk[i],",") + newfac:claim_chunk(chunkpos) + end + end + for player, attrs in pairs(objects) do + local facname = attrs.factionsmod + local faction = factions.factions[facname] + if faction then + faction:add_player(player) + end + end + return true +end minetest.register_on_dieplayer( function(player)