refactor handle_command for mtt

It could've been done by only exposing handle_command, but this is
cleaner for future maintenance as tasks are well separated.
This commit is contained in:
Luke aka SwissalpS 2024-09-16 01:03:57 +02:00
parent b6f312b9db
commit a27dcfd62c

@ -193,14 +193,10 @@ function factions.leave_faction(fname, player_name)
end end
-- Chat commands -- Chat commands
local function handle_command(name, param) local chat = {}
local params = {} if do_mtt then factions.chat = chat end
for p in string.gmatch(param, "[^%s]+") do
table.insert(params, p) function chat.create(name, params, not_admin)
end
local not_admin = not minetest.get_player_privs(name)[factions.priv]
local action = params[1]
if action == "create" then
local faction_name = params[2] local faction_name = params[2]
local password = params[3] local password = params[3]
if not faction_name then if not faction_name then
@ -215,7 +211,9 @@ local function handle_command(name, param)
factions.register_faction(faction_name, name, password) factions.register_faction(faction_name, name, password)
return true, S("Registered @1.", faction_name) return true, S("Registered @1.", faction_name)
end end
elseif action == "disband" then end
function chat.disband(name, params, not_admin)
local password = params[2] local password = params[2]
if not password then if not password then
return false, S("Missing password.") return false, S("Missing password.")
@ -244,7 +242,9 @@ local function handle_command(name, param)
factions.disband_faction(faction_name) factions.disband_faction(faction_name)
return true, S("Disbanded @1.", faction_name) return true, S("Disbanded @1.", faction_name)
end end
elseif action == "list" then end
function chat.list(name, params, not_admin)
local faction_list = {} local faction_list = {}
for k in pairs(facts) do for k in pairs(facts) do
table.insert(faction_list, k) table.insert(faction_list, k)
@ -255,7 +255,9 @@ local function handle_command(name, param)
return true, S("Factions (@1): @2.", return true, S("Factions (@1): @2.",
table.getn(faction_list), table.concat(faction_list, ", ")) table.getn(faction_list), table.concat(faction_list, ", "))
end end
elseif action == "info" then end
function chat.info(name, params, not_admin)
local faction_name = params[2] local faction_name = params[2]
if not faction_name then if not faction_name then
local player_factions = factions.get_player_factions(name) local player_factions = factions.get_player_factions(name)
@ -287,7 +289,9 @@ local function handle_command(name, param)
table.concat(fmembers, ", ")) table.concat(fmembers, ", "))
return true, summary return true, summary
end end
elseif action == "player_info" then end
function chat.player_info(name, params, not_admin)
local player_name = params[2] or name local player_name = params[2] or name
if not player_name then if not player_name then
return false, S("Missing player name.") return false, S("Missing player name.")
@ -325,7 +329,9 @@ local function handle_command(name, param)
end end
return true, summary return true, summary
end end
elseif action == "join" then end
function chat.join(name, params, not_admin)
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_factions(name) then if factions.mode_unique_faction and factions.get_player_factions(name) then
@ -345,7 +351,9 @@ local function handle_command(name, param)
return false, S("Error joining faction.") return false, S("Error joining faction.")
end end
end end
elseif action == "leave" then end
function chat.leave(name, params, not_admin)
local player_factions = factions.get_player_factions(name) local player_factions = factions.get_player_factions(name)
local number_factions = player_factions and table.getn(player_factions) or 0 local number_factions = player_factions and table.getn(player_factions) or 0
local faction_name = params[2] local faction_name = params[2]
@ -374,7 +382,9 @@ local function handle_command(name, param)
return false, S("Error leaving faction.") return false, S("Error leaving faction.")
end end
end end
elseif action == "kick" then end
function chat.kick(name, params, not_admin)
local target = params[2] local target = params[2]
if not target then if not target then
return false, S("Missing player name.") return false, S("Missing player name.")
@ -408,7 +418,9 @@ local function handle_command(name, param)
return false, S("Error kicking @1 from faction.", target) return false, S("Error kicking @1 from faction.", target)
end end
end end
elseif action == "passwd" then end
function chat.passwd(name, params, not_admin)
local password = params[2] local password = params[2]
if not password then if not password then
return false, S("Missing password.") return false, S("Missing password.")
@ -436,7 +448,9 @@ local function handle_command(name, param)
return false, S("Failed to change password.") return false, S("Failed to change password.")
end end
end end
elseif action == "chown" then end
function chat.chown(name, params, not_admin)
local target = params[2] local target = params[2]
local password = params[3] local password = params[3]
local faction_name = params[4] local faction_name = params[4]
@ -471,7 +485,9 @@ local function handle_command(name, param)
return false, S("Failed to transfer ownership.") return false, S("Failed to transfer ownership.")
end end
end end
elseif action == "invite" then end
function chat.invite(name, params, not_admin)
if not_admin then if not_admin then
return false, S( return false, S(
"Permission denied: You can't use this command, @1 priv is needed.", "Permission denied: You can't use this command, @1 priv is needed.",
@ -503,9 +519,20 @@ 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
else end
local function handle_command(name, param)
local params = {}
for p in string.gmatch(param, "[^%s]+") do
table.insert(params, p)
end
local action = params[1]
if not action or not chat[action:lower()] then
return false, S("Unknown subcommand. Run '/help factions' for help.") return false, S("Unknown subcommand. Run '/help factions' for help.")
end end
local not_admin = not minetest.get_player_privs(name)[factions.priv]
return chat[action:lower()](name, params, not_admin)
end end
minetest.register_chatcommand("factions", { minetest.register_chatcommand("factions", {