Improve get_player_factions, get_owned_factions and get_administred_factions to return nil if there is no faction corresponding

This commit is contained in:
Supergoat666 2020-08-22 01:36:14 +02:00 committed by Gitea
parent cac9740440
commit 44ffad45b9

@ -71,9 +71,12 @@ function factions.get_player_factions(name)
if not minetest.player_exists(name) then if not minetest.player_exists(name) then
return false return false
end end
local player_factions = {} local player_factions = nil
for fname, fact in pairs(facts) do for fname, fact in pairs(facts) do
if fact.members[name] then if fact.members[name] then
if not player_factions then
player_factions = {}
end
table.insert(player_factions, fname) table.insert(player_factions, fname)
end end
end end
@ -81,9 +84,12 @@ function factions.get_player_factions(name)
end end
function factions.get_owned_factions(name) function factions.get_owned_factions(name)
local own_factions = {} local own_factions = nil
for fname, fact in pairs(facts) do for fname, fact in pairs(facts) do
if fact.owner == name then if fact.owner == name then
if not own_factions then
own_factions = {}
end
table.insert(own_factions, fname) table.insert(own_factions, fname)
end end
end end
@ -91,9 +97,12 @@ function factions.get_owned_factions(name)
end end
function factions.get_administered_factions(name) function factions.get_administered_factions(name)
local adm_factions = {} local adm_factions = nil
for fname, fact in pairs(facts) do for fname, fact in pairs(facts) do
if minetest.get_player_privs(name).playerfactions_admin or fact.owner == name then if minetest.get_player_privs(name).playerfactions_admin or fact.owner == name then
if not adm_factions then
adm_factions = {}
end
table.insert(adm_factions, fname) table.insert(adm_factions, fname)
end end
end end
@ -289,14 +298,20 @@ local function handle_command(name, param)
str_member = str_member..", "..v str_member = str_member..", "..v
end end
end end
for _,v in ipairs(factions.get_owned_factions(player_name)) do minetest.chat_send_player(name, S("@1 is in the following factions: @2", player_name, str_member, str_owner))
local owned_factions = factions.get_owned_factions(player_name)
if not owned_factions then
minetest.chat_send_player(name, S("She/He is the owner of no faction"))
else
for _,v in ipairs(owned_factions) do
if str_owner == "" then if str_owner == "" then
str_owner = str_owner..v str_owner = str_owner..v
else else
str_owner = str_owner..", "..v str_owner = str_owner..", "..v
end end
end end
minetest.chat_send_player(name, S("@1 is in the following factions: @2\nShe/He is the owner of the following factions: @3", player_name, str_member, str_owner)) minetest.chat_send_player(name, S("She/He is the owner of the following factions: @1", str_owner))
end
if minetest.get_player_privs(name).playerfactions_admin then if minetest.get_player_privs(name).playerfactions_admin then
minetest.chat_send_player(name, S("@1 has the playerfactions_admin privilege so she/he can admin every factions", player_name)) minetest.chat_send_player(name, S("@1 has the playerfactions_admin privilege so she/he can admin every factions", player_name))
end end