diff --git a/init.lua b/init.lua index 200f763..5f63c9e 100644 --- a/init.lua +++ b/init.lua @@ -56,6 +56,10 @@ local function get_member_list(meta) return meta:get_string("members"):split(" ") end +local function get_faction_list(meta) + return meta:get_string("factions"):split(" ") +end + -- write member list table in protector meta as string local function set_member_list(meta, list) @@ -63,6 +67,10 @@ local function set_member_list(meta, list) meta:set_string("members", table.concat(list, " ")) end +local function set_faction_list(meta, list) + meta:set_string("factions", table.concat(list, " ")) +end + -- check for owner name local function is_owner(meta, name) @@ -74,38 +82,19 @@ end local function is_member(meta, name) - if factions_available and meta:get_int("faction_members") == 1 then - - if factions.version == nil then - - -- backward compatibility - if factions.get_player_faction(name) ~= nil - and factions.get_player_faction(meta:get_string("owner")) == - factions.get_player_faction(name) then - return true - end - else - -- is member if player and owner share at least one faction - local player_factions = factions.get_player_factions(name) - local owner = meta:get_string("owner") - - if player_factions ~= nil and player_factions ~= false then - - for _, f in ipairs(player_factions) do - - if factions.player_is_in_faction(f, owner) then - return true - end - end - end - end - end - for _, n in pairs(get_member_list(meta)) do if n == name then return true end end + if factions_available then + for _, faction in pairs(get_faction_list(meta)) do + if factions.player_is_in_faction(faction, name) then + return true + end + end + end + return false end @@ -119,9 +108,6 @@ local function add_member(meta, name) -- Constant (20) defined by player.h if name:len() > 25 then return end - -- does name already exist? - if is_owner(meta, name) or is_member(meta, name) then return end - local list = get_member_list(meta) if #list >= protector_max_share_count then return end @@ -131,6 +117,15 @@ local function add_member(meta, name) set_member_list(meta, list) end +local function add_faction(meta, name) + if name ~= string.match(name, "[%w_-]+") then return end + if name:len() > 25 then return end + local list = get_faction_list(meta) + if #list >= 4 then return end + table.insert(list, name) + set_faction_list(meta, list) +end + -- remove player name from table local function del_member(meta, name) @@ -148,6 +143,17 @@ local function del_member(meta, name) set_member_list(meta, list) end +local function del_faction(meta, name) + local list = get_faction_list(meta) + for i, n in pairs(list) do + if n == name then + table.remove(list, i) + break + end + end + set_faction_list(meta, list) +end + -- protector interface local function protector_formspec(meta) @@ -157,54 +163,33 @@ local function protector_formspec(meta) .. default.gui_bg_img .. "label[2.5,0;" .. F(S("-- Protector interface --")) .. "]" .. "label[0,1;" .. F(S("PUNCH node to show protected area")) .. "]" - .. "label[0,2;" .. F(S("Members:")) .. "]" + .. "label[0,1.5;" .. F(S("Members:")) .. "]" .. "button_exit[2.5,6.2;3,0.5;close_me;" .. F(S("Close")) .. "]" .. "field_close_on_enter[protector_add_member;false]" local members = get_member_list(meta) local npp = protector_max_share_count -- max users added to protector list - local i = 0 - local checkbox_faction = false - -- Display the checkbox only if the owner is member of at least 1 faction if factions_available then - - if factions.version == nil then - - -- backward compatibility - if factions.get_player_faction(meta:get_string("owner")) then - checkbox_faction = true - end - else - local player_factions = factions.get_player_factions(meta:get_string("owner")) - - if player_factions ~= nil and #player_factions >= 1 then - checkbox_faction = true - end - end - end - if checkbox_faction then - - formspec = formspec .. "checkbox[0,5;faction_members;" - .. F(S("Allow faction access")) - .. ";" .. (meta:get_int("faction_members") == 1 and - "true" or "false") .. "]" + formspec = formspec .. "label[0,4.25;" .. F(S("Factions:")) .. "]" + .. "field_close_on_enter[protector_add_faction;false]" if npp > 8 then npp = 8 end end + local i = 0 for n = 1, #members do if i < npp then -- show username formspec = formspec .. "button[" .. (i % 4 * 2) - .. "," .. math.floor(i / 4 + 3) + .. "," .. (math.floor(i / 4) + 2.5) .. ";1.5,.5;protector_member;" .. F(members[n]) .. "]" -- username remove button .. "button[" .. (i % 4 * 2 + 1.25) .. "," - .. math.floor(i / 4 + 3) + .. (math.floor(i / 4) + 2.5) .. ";.75,.5;protector_del_member_" .. F(members[n]) .. ";X]" end @@ -215,15 +200,38 @@ local function protector_formspec(meta) -- user name entry field formspec = formspec .. "field[" .. (i % 4 * 2 + 1 / 3) .. "," - .. (math.floor(i / 4 + 3) + 1 / 3) + .. (math.floor(i / 4) + 2.5 + 1 / 3) .. ";1.433,.5;protector_add_member;;]" -- username add button .."button[" .. (i % 4 * 2 + 1.25) .. "," - .. math.floor(i / 4 + 3) .. ";.75,.5;protector_submit;+]" + .. (math.floor(i / 4) + 2.5) .. ";.75,.5;protector_submit;+]" end + if factions_available then + local member_factions = get_faction_list(meta) + i = 0 + for n = 1, #member_factions do + if i < 4 then + formspec = formspec .. "button[" .. (i % 4 * 2) + .. "," .. math.floor(i / 4 + 5) + .. ";1.5,.5;protector_faction;" .. F(member_factions[n]) .. "]" + .. "button[" .. (i % 4 * 2 + 1.25) .. "," + .. math.floor(i / 4 + 5) + .. ";.75,.5;protector_del_faction_" .. F(member_factions[n]) .. ";X]" + end + i = i + 1 + end + if i < 4 then + formspec = formspec .. "field[" .. (i % 4 * 2 + 1 / 3) .. "," + .. (math.floor(i / 4 + 5) + 1 / 3) + .. ";1.433,.5;protector_add_faction;;]" + .."button[" .. (i % 4 * 2 + 1.25) .. "," + .. math.floor(i / 4 + 5) .. ";.75,.5;protector_submit_faction;+]" + end + end + return formspec end @@ -671,8 +679,18 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) local meta = minetest.get_meta(pos) ; if not meta then return end -- add faction members - if factions_available and fields.faction_members ~= nil then - meta:set_int("faction_members", fields.faction_members == "true" and 1 or 0) + if factions_available then + local add_faction_input = fields.protector_add_faction + if add_faction_input and add_faction_input ~= "" then + for _, i in pairs(add_faction_input:split(" ")) do + add_faction(meta, i) + end + end + for field, value in pairs(fields) do + if string.sub(field, 0, string.len("protector_del_faction_")) == "protector_del_faction_" then + del_faction(meta, string.sub(field, string.len("protector_del_faction_") + 1)) + end + end end -- add member [+]