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
-- Chat commands
local function handle_command(name, param)
local params = {}
for p in string.gmatch(param, "[^%s]+") do
table.insert(params, p)
end
local not_admin = not minetest.get_player_privs(name)[factions.priv]
local action = params[1]
if action == "create" then
local chat = {}
if do_mtt then factions.chat = chat end
function chat.create(name, params, not_admin)
local faction_name = params[2]
local password = params[3]
if not faction_name then
@ -215,7 +211,9 @@ local function handle_command(name, param)
factions.register_faction(faction_name, name, password)
return true, S("Registered @1.", faction_name)
end
elseif action == "disband" then
end
function chat.disband(name, params, not_admin)
local password = params[2]
if not password then
return false, S("Missing password.")
@ -244,7 +242,9 @@ local function handle_command(name, param)
factions.disband_faction(faction_name)
return true, S("Disbanded @1.", faction_name)
end
elseif action == "list" then
end
function chat.list(name, params, not_admin)
local faction_list = {}
for k in pairs(facts) do
table.insert(faction_list, k)
@ -255,7 +255,9 @@ local function handle_command(name, param)
return true, S("Factions (@1): @2.",
table.getn(faction_list), table.concat(faction_list, ", "))
end
elseif action == "info" then
end
function chat.info(name, params, not_admin)
local faction_name = params[2]
if not faction_name then
local player_factions = factions.get_player_factions(name)
@ -287,7 +289,9 @@ local function handle_command(name, param)
table.concat(fmembers, ", "))
return true, summary
end
elseif action == "player_info" then
end
function chat.player_info(name, params, not_admin)
local player_name = params[2] or name
if not player_name then
return false, S("Missing player name.")
@ -325,7 +329,9 @@ local function handle_command(name, param)
end
return true, summary
end
elseif action == "join" then
end
function chat.join(name, params, not_admin)
local faction_name = params[2]
local password = params[3]
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.")
end
end
elseif action == "leave" then
end
function chat.leave(name, params, not_admin)
local player_factions = factions.get_player_factions(name)
local number_factions = player_factions and table.getn(player_factions) or 0
local faction_name = params[2]
@ -374,7 +382,9 @@ local function handle_command(name, param)
return false, S("Error leaving faction.")
end
end
elseif action == "kick" then
end
function chat.kick(name, params, not_admin)
local target = params[2]
if not target then
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)
end
end
elseif action == "passwd" then
end
function chat.passwd(name, params, not_admin)
local password = params[2]
if not password then
return false, S("Missing password.")
@ -436,7 +448,9 @@ local function handle_command(name, param)
return false, S("Failed to change password.")
end
end
elseif action == "chown" then
end
function chat.chown(name, params, not_admin)
local target = params[2]
local password = params[3]
local faction_name = params[4]
@ -471,7 +485,9 @@ local function handle_command(name, param)
return false, S("Failed to transfer ownership.")
end
end
elseif action == "invite" then
end
function chat.invite(name, params, not_admin)
if not_admin then
return false, S(
"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)
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.")
end
local not_admin = not minetest.get_player_privs(name)[factions.priv]
return chat[action:lower()](name, params, not_admin)
end
minetest.register_chatcommand("factions", {