invite: cleanup and tweaks

- reduced needed indents
- remove explicit nil check where not needed
- use get_player_factions() instead of depricated get_player_faction()
and reduce calls of it
- tweaked join_faction()
- adds check if player already is in that faction
This commit is contained in:
Luke aka SwissalpS 2024-09-15 07:31:01 +02:00
parent f130b50565
commit 67436bffcf

@ -172,7 +172,7 @@ function factions.set_password(fname, password)
end end
function factions.join_faction(fname, player) function factions.join_faction(fname, player)
if facts[fname] == nil or not minetest.player_exists(player) then if not (facts[fname] and minetest.player_exists(player)) then
return false return false
end end
facts[fname].members[player] = true facts[fname].members[player] = true
@ -465,22 +465,30 @@ local function handle_command(name, param)
end end
end end
elseif action == "invite" then elseif action == "invite" then
if not minetest.get_player_privs(name)[factions.priv] then if not is_admin then
return false, S("Permission denied: You can't use this command, @1 priv is needed.", factions.priv) return false, S(
else "Permission denied: You can't use this command, @1 priv is needed.",
factions.priv
)
end
local target = params[2] local target = params[2]
local faction_name = params[3] local faction_name = params[3]
if not target then if not target then
return false, S("Missing player name.") return false, S("Missing player name.")
elseif not faction_name then elseif not faction_name then
return false, S("Missing faction name.") return false, S("Missing faction name.")
elseif facts[faction_name] == nil then elseif not facts[faction_name] then
return false, S("Faction @1 doesn't exist.", faction_name) return false, S("Faction @1 doesn't exist.", faction_name)
elseif not minetest.player_exists(target) then elseif not minetest.player_exists(target) then
return false, S("Player @1 doesn't exist.", target) return false, S("Player @1 doesn't exist.", target)
elseif factions.mode_unique_faction and factions.get_player_faction(target) ~= nil then end
local player_factions = factions.get_player_factions(target)
if player_factions and facts[faction_name].members[target] then
return false, S("Player @1 is already in faction @2.", return false, S("Player @1 is already in faction @2.",
target, factions.get_player_faction(target)) target, faction_name)
elseif player_factions and factions.mode_unique_faction then
return false, S("Player @1 is already in faction @2.",
target, player_factions[1])
else else
if factions.join_faction(faction_name, target) then if factions.join_faction(faction_name, target) then
return true, S("@1 is now a member of faction @2.", target, faction_name) return true, S("@1 is now a member of faction @2.", target, faction_name)
@ -488,7 +496,6 @@ local function handle_command(name, param)
return false, S("Error adding @1 to @2.", target, faction_name) return false, S("Error adding @1 to @2.", target, faction_name)
end end
end end
end
else else
return false, S("Unknown subcommand. Run '/help factions' for help.") return false, S("Unknown subcommand. Run '/help factions' for help.")
end end