more tweaks

- join: check if already member
- leave: checks if user is in given faction at all
- kick: early abort if no player provided
- create: early abort if no faction or password are provided
- create: use get_player_factions() instead of get_player_faction()
- create: reduce explicit nil checks
- disband: early abort if missing password
- disband: reduce param-count-checks and use table.getn()
- info: reduce explicit nil checks and use table.getn()
- passwd: early abort if no password provided
- in general remove explicit nil-checks where not needed
This commit is contained in:
Luke aka SwissalpS 2024-09-15 08:23:12 +02:00
parent 67436bffcf
commit f2380e0bb1
3 changed files with 39 additions and 29 deletions

@ -49,7 +49,7 @@ function factions.get_facts()
end end
function factions.player_is_in_faction(fname, player_name) function factions.player_is_in_faction(fname, player_name)
if not minetest.player_exists(player_name) or facts[fname] == nil then if not minetest.player_exists(player_name) or not facts[fname] then
return false return false
end end
return facts[fname].members[player_name] return facts[fname].members[player_name]
@ -104,14 +104,14 @@ function factions.get_administered_factions(name)
end end
function factions.get_owner(fname) function factions.get_owner(fname)
if facts[fname] == nil then if not facts[fname] then
return false return false
end end
return facts[fname].owner return facts[fname].owner
end end
function factions.chown(fname, owner) function factions.chown(fname, owner)
if facts[fname] == nil then if not facts[fname] then
return false return false
end end
facts[fname].owner = owner facts[fname].owner = owner
@ -120,7 +120,7 @@ function factions.chown(fname, owner)
end end
function factions.register_faction(fname, founder, pw) function factions.register_faction(fname, founder, pw)
if facts[fname] ~= nil then if facts[fname] then
return false return false
end end
facts[fname] = { facts[fname] = {
@ -134,7 +134,7 @@ function factions.register_faction(fname, founder, pw)
end end
function factions.disband_faction(fname) function factions.disband_faction(fname)
if facts[fname] == nil then if not facts[fname] then
return false return false
end end
facts[fname] = nil facts[fname] = nil
@ -156,14 +156,14 @@ end
function factions.get_password(fname) function factions.get_password(fname)
minetest.log("warning", "Deprecated use of factions.get_password(). " minetest.log("warning", "Deprecated use of factions.get_password(). "
.. "Please update to using factions.valid_password() instead.") .. "Please update to using factions.valid_password() instead.")
if facts[fname] == nil then if not facts[fname] then
return false return false
end end
return facts[fname].password return facts[fname].password
end end
function factions.set_password(fname, password) function factions.set_password(fname, password)
if facts[fname] == nil then if not facts[fname] then
return false return false
end end
facts[fname].password256 = factions.hash_password(password) facts[fname].password256 = factions.hash_password(password)
@ -200,13 +200,13 @@ local function handle_command(name, param)
if action == "create" then if action == "create" then
local faction_name = params[2] local faction_name = params[2]
local password = params[3] local password = params[3]
if factions.mode_unique_faction and factions.get_player_faction(name) ~= nil then if not faction_name then
return false, S("You are already in a faction.")
elseif faction_name == nil then
return false, S("Missing faction name.") return false, S("Missing faction name.")
elseif password == nil then elseif not password then
return false, S("Missing password.") return false, S("Missing password.")
elseif facts[faction_name] ~= nil then elseif factions.mode_unique_faction and factions.get_player_factions(name) then
return false, S("You are already in a faction.")
elseif facts[faction_name] then
return false, S("Faction @1 already exists.", faction_name) return false, S("Faction @1 already exists.", faction_name)
else else
factions.register_faction(faction_name, name, password) factions.register_faction(faction_name, name, password)
@ -214,19 +214,18 @@ local function handle_command(name, param)
end end
elseif action == "disband" then elseif action == "disband" then
local password = params[2] local password = params[2]
local faction_name = nil if not password then
return false, S("Missing password.")
end
local faction_name = params[3]
local own_factions = factions.get_administered_factions(name) local own_factions = factions.get_administered_factions(name)
local number_factions = own_factions and #own_factions or 0 local number_factions = own_factions and table.getn(own_factions) or 0
if not is_admin and number_factions == 0 then if not is_admin and number_factions == 0 then
return false, S("You don't own any factions.") return false, S("You don't own any factions.")
elseif #params == 1 or not password then elseif not faction_name and number_factions == 1 then
return false, S("Missing password.")
elseif #params == 2 and number_factions == 1 then
faction_name = own_factions[1] faction_name = own_factions[1]
elseif #params >= 3 then
faction_name = params[3]
end end
if faction_name == nil then if not faction_name then
return false, S( return false, S(
"You are the owner of multiple factions, you have to choose one of them: @1.", "You are the owner of multiple factions, you have to choose one of them: @1.",
table.concat(own_factions, ", ") table.concat(own_factions, ", ")
@ -255,11 +254,11 @@ local function handle_command(name, param)
end end
elseif action == "info" then elseif action == "info" then
local faction_name = params[2] local faction_name = params[2]
if faction_name == nil then if not faction_name then
local player_factions = factions.get_player_factions(name) local player_factions = factions.get_player_factions(name)
if not player_factions then if not player_factions then
return true, S("No factions found.") return true, S("No factions found.")
elseif #player_factions == 1 then elseif table.getn(player_factions) == 1 then
faction_name = player_factions[1] faction_name = player_factions[1]
else else
return false, S( return false, S(
@ -268,7 +267,7 @@ local function handle_command(name, param)
) )
end end
end end
if facts[faction_name] == nil then if 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)
else else
local fmembers = {} local fmembers = {}
@ -332,6 +331,8 @@ local function handle_command(name, param)
return false, S("Missing faction name.") return false, S("Missing faction name.")
elseif not facts[faction_name] 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 facts[faction_name].members[name] then
return false, S("You are already in faction @1.")
elseif not factions.valid_password(faction_name, password) then elseif not factions.valid_password(faction_name, password) then
return false, S("Permission denied: Wrong password.") return false, S("Permission denied: Wrong password.")
else else
@ -361,6 +362,8 @@ local function handle_command(name, param)
return false, S("Faction @1 doesn't exist.", faction_name) return false, S("Faction @1 doesn't exist.", faction_name)
elseif factions.get_owner(faction_name) == name then elseif factions.get_owner(faction_name) == name then
return false, S("You cannot leave your own faction, change owner or disband it.") return false, S("You cannot leave your own faction, change owner or disband it.")
elseif not facts[faction_name].members[name] then
return false, S("You aren't part of faction @1.", faction_name)
else else
if factions.leave_faction(faction_name, name) then if factions.leave_faction(faction_name, name) then
return true, S("Left @1.", faction_name) return true, S("Left @1.", faction_name)
@ -370,6 +373,9 @@ local function handle_command(name, param)
end end
elseif action == "kick" then elseif action == "kick" then
local target = params[2] local target = params[2]
if not target then
return false, S("Missing player name.")
end
local faction_name = params[3] local faction_name = params[3]
local own_factions = factions.get_administered_factions(name) local own_factions = factions.get_administered_factions(name)
local number_factions = own_factions and table.getn(own_factions) or 0 local number_factions = own_factions and table.getn(own_factions) or 0
@ -383,9 +389,7 @@ local function handle_command(name, param)
table.concat(own_factions, ", ") table.concat(own_factions, ", ")
) )
end end
if not target then if not is_admin and factions.get_owner(faction_name) ~= name then
return false, S("Missing player name.")
elseif not is_admin and factions.get_owner(faction_name) ~= name then
return false, S("Permission denied: You are not the owner of that faction, " return false, S("Permission denied: You are not the owner of that faction, "
.. "and don't have the @1 privilege.", factions.priv) .. "and don't have the @1 privilege.", factions.priv)
elseif not facts[faction_name].members[target] then elseif not facts[faction_name].members[target] then
@ -403,6 +407,9 @@ local function handle_command(name, param)
end end
elseif action == "passwd" then elseif action == "passwd" then
local password = params[2] local password = params[2]
if not password then
return false, S("Missing password.")
end
local faction_name = params[3] local faction_name = params[3]
local own_factions = factions.get_administered_factions(name) local own_factions = factions.get_administered_factions(name)
local number_factions = own_factions and table.getn(own_factions) or 0 local number_factions = own_factions and table.getn(own_factions) or 0
@ -416,9 +423,6 @@ local function handle_command(name, param)
table.concat(own_factions, ", ") table.concat(own_factions, ", ")
) )
end end
if not password then
return false, S("Missing password.")
end
if not is_admin and factions.get_owner(faction_name) ~= name then if not is_admin and factions.get_owner(faction_name) ~= name then
return false, S("Permission denied: You are not the owner of that faction, " return false, S("Permission denied: You are not the owner of that faction, "
.. "and don't have the @1 privilege.", factions.priv) .. "and don't have the @1 privilege.", factions.priv)

@ -74,3 +74,6 @@ You cannot kick the owner of a faction, use '/factions chown <player> <password>
You cannot leave your own faction, change owner or disband it.=Vous ne pouvez pas quitter votre propre faction, changez le propriétaire ou dissolvez la. You cannot leave your own faction, change owner or disband it.=Vous ne pouvez pas quitter votre propre faction, changez le propriétaire ou dissolvez la.
No factions found.= No factions found.=
You aren't part of faction @1.=
You are already in faction @1.=

@ -74,3 +74,6 @@ You cannot kick the owner of a faction, use '/factions chown <player> <password>
You cannot leave your own faction, change owner or disband it.= You cannot leave your own faction, change owner or disband it.=
No factions found.= No factions found.=
You aren't part of faction @1.=
You are already in faction @1.=