factions/chatcommands.lua

460 lines
13 KiB
Lua
Raw Normal View History

2016-07-30 05:02:22 +02:00
-------------------------------------------------------------------------------
2016-07-30 07:38:52 +02:00
-- factionsmod Mod by Sapier
2016-07-30 05:02:22 +02:00
--
-- License WTFPL
--
--! @file chatcommnd.lua
2016-07-30 07:38:52 +02:00
--! @brief factionsmod chat interface
2016-07-30 05:02:22 +02:00
--! @copyright Sapier
--! @author Sapier
--! @date 2013-05-08
--
-- Contact sapier a t gmx net
-------------------------------------------------------------------------------
2016-07-30 07:38:52 +02:00
--! @class factionsmod_chat
2016-07-30 05:02:22 +02:00
--! @brief chat interface class
2016-07-30 07:38:52 +02:00
factionsmod_chat = {}
2016-07-30 05:02:22 +02:00
-------------------------------------------------------------------------------
-- name: init()
--
--! @brief initialize chat interface
2016-07-30 07:38:52 +02:00
--! @memberof factionsmod_chat
2016-07-30 05:02:22 +02:00
--! @public
-------------------------------------------------------------------------------
2016-07-30 07:38:52 +02:00
function factionsmod_chat.init()
2016-07-30 05:02:22 +02:00
minetest.register_privilege("faction_user",
{
description = "this user is allowed to interact with faction mod",
give_to_singleplayer = true,
}
)
minetest.register_privilege("faction_admin",
{
2016-07-30 07:38:52 +02:00
description = "this user is allowed to create or delete factionsmod",
2016-07-30 05:02:22 +02:00
give_to_singleplayer = true,
}
)
2016-07-30 07:38:52 +02:00
minetest.register_chatcommand("factionsmod",
2016-07-30 05:02:22 +02:00
{
params = "<cmd> <parameter 1> .. <parameter n>",
description = "faction administration functions",
privs = { interact=true },
2016-07-30 07:38:52 +02:00
func = factionsmod_chat.cmdhandler,
2016-07-30 05:02:22 +02:00
}
)
minetest.register_chatcommand("af",
{
params = "text",
2016-07-30 07:38:52 +02:00
description = "send message to all factionsmod",
2016-07-30 05:02:22 +02:00
privs = { faction_user=true },
2016-07-30 07:38:52 +02:00
func = factionsmod_chat.allfactionsmod_chathandler,
2016-07-30 05:02:22 +02:00
}
)
minetest.register_chatcommand("f",
{
params = "<factionname> text",
description = "send message to a specific faction",
privs = { faction_user=true },
2016-07-30 07:38:52 +02:00
func = factionsmod_chat.chathandler,
2016-07-30 05:02:22 +02:00
}
)
end
-------------------------------------------------------------------------------
-- name: cmdhandler(playername,parameter)
--
--! @brief chat command handler
2016-07-30 07:38:52 +02:00
--! @memberof factionsmod_chat
2016-07-30 05:02:22 +02:00
--! @private
--
--! @param playername name
--! @param parameter data supplied to command
-------------------------------------------------------------------------------
2016-07-30 07:38:52 +02:00
function factionsmod_chat.cmdhandler(playername,parameter)
2016-07-30 05:02:22 +02:00
local player = minetest.env:get_player_by_name(playername)
local params = parameter:split(" ")
local cmd = params[1]
--handle common commands
if parameter == nil or
parameter == "" then
2016-07-30 07:38:52 +02:00
local playerfactionsmod = factionsmod.get_factionsmod(player)
2016-07-30 05:02:22 +02:00
2016-07-30 07:38:52 +02:00
local tosend = "factionsmod: " .. playername .. " factionsmod:"
for i,v in ipairs(playerfactionsmod) do
if i ~= #playerfactionsmod then
2016-07-30 05:02:22 +02:00
tosend = tosend .. " " .. v .. ","
else
tosend = tosend .. " " .. v
end
end
minetest.chat_send_player(playername, tosend, false)
return
end
if cmd == "claim" then
2016-07-30 22:33:10 +02:00
local playerfaction = factionsmod.get_factionsmod(player)
factionsmod.claim(playerfaction[1],player)
2016-07-30 05:02:22 +02:00
return
end
if cmd == "unclaim" then
2016-07-30 22:33:10 +02:00
local playerfaction = factionsmod.get_factionsmod(player)
factionsmod.unclaim(playerfaction[1],player)
2016-07-30 05:02:22 +02:00
return
end
2016-07-30 07:38:52 +02:00
--list all known factionsmod
2016-07-30 05:02:22 +02:00
if cmd == "list" then
2016-07-30 07:38:52 +02:00
local list = factionsmod.get_faction_list()
local tosend = "factionsmod: current available factionsmod:"
2016-07-30 05:02:22 +02:00
for i,v in ipairs(list) do
if i ~= #list then
tosend = tosend .. " " .. v .. ","
else
tosend = tosend .. " " .. v
end
end
minetest.chat_send_player(playername, tosend, false)
return
end
2016-07-30 07:38:52 +02:00
--show factionsmod mod version
2016-07-30 05:02:22 +02:00
if cmd == "version" then
2016-07-30 07:38:52 +02:00
minetest.chat_send_player(playername, "factionsmod: version " .. factionsmod_version , false)
2016-07-30 05:02:22 +02:00
return
end
--show description of faction
if cmd == "info" then
if params[2] ~= nil then
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: " .. params[2] .. ": " ..
factionsmod.get_description(params[2]), false)
2016-07-30 05:02:22 +02:00
return
end
end
if cmd == "leave" then
if params[2] ~= nil then
if params[3] ~= nil then
local toremove = minetest.env:get_player_by_name(params[3])
--allowed if faction_admin, admin of faction or player itself
2016-07-30 07:38:52 +02:00
if factionsmod.is_admin(params[2],playername) and
2016-07-30 05:02:22 +02:00
toremove ~= nil then
2016-07-30 07:38:52 +02:00
factionsmod.member_remove(params[2],toremove)
2016-07-30 05:02:22 +02:00
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: " .. params[3] .. " has been removed from "
2016-07-30 05:02:22 +02:00
.. params[2], false)
return
end
else
2016-07-30 07:38:52 +02:00
factionsmod.member_remove(params[2],player)
2016-07-30 05:02:22 +02:00
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: You have left " .. params[2], false)
2016-07-30 05:02:22 +02:00
return
end
end
end
--handle superadmin only commands
--if minetest.check_player_privs(playername,{ faction_admin=true }) then
2016-07-30 05:02:22 +02:00
--create new faction
if cmd == "create" then
if params[2] ~= nil then
2016-07-30 07:38:52 +02:00
if factionsmod.add_faction(params[2]) then
2016-07-30 05:02:22 +02:00
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: created faction " .. params[2],
2016-07-30 05:02:22 +02:00
false)
2016-07-30 07:38:52 +02:00
if factionsmod.member_add(params[2],minetest.env:get_player_by_name(playername)) then
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: " .. playername .. " joined faction " ..
params[2],
false)
end
2016-07-30 05:02:22 +02:00
return
else
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: FAILED to created faction " .. params[2],
2016-07-30 05:02:22 +02:00
false)
return
end
end
end
--end
2016-07-30 06:22:28 +02:00
2016-07-30 05:02:22 +02:00
if cmd == "join" then
if params[2] ~= nil then
--check for invitation
2016-07-30 07:38:52 +02:00
if factionsmod.is_invited(params[2],playername) then
if factionsmod.member_add(params[2],player) then
2016-07-30 05:02:22 +02:00
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: joined faction " ..
2016-07-30 05:02:22 +02:00
params[2],
false)
return
else
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: FAILED to join faction " ..
2016-07-30 05:02:22 +02:00
params[2],
false)
return
end
else
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: you are not allowed to join " .. params[2],
2016-07-30 05:02:22 +02:00
false)
return
end
end
end
--all following commands require at least two parameters
if params[2] ~= nil then
if minetest.check_player_privs(playername,{ faction_admin=true }) or
2016-07-30 07:38:52 +02:00
factionsmod.is_admin(params[2],playername) then
2016-07-30 05:02:22 +02:00
--delete faction
if cmd == "delete" then
2016-07-30 07:38:52 +02:00
if factionsmod.delete_faction(params[2]) then
2016-07-30 05:02:22 +02:00
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: deleted faction " .. params[2],
2016-07-30 05:02:22 +02:00
false)
return
else
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: FAILED to deleted faction " .. params[2],
2016-07-30 05:02:22 +02:00
false)
return
end
end
if cmd == "set_free" then
if params[3] ~= nil and
(params[3] == "true" or params[3] == "false")then
local value = false
if params[3] == "true" then
value = true
end
2016-07-30 07:38:52 +02:00
if factionsmod.set_free(params[2],value) then
2016-07-30 05:02:22 +02:00
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: free to join for " .. params[2] ..
2016-07-30 05:02:22 +02:00
" has been set to " .. params[3],
false)
else
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: FAILED to set free to join for " ..
2016-07-30 05:02:22 +02:00
params[2],
false)
end
end
end
--set player admin status
if cmd == "admin" then
if params[3] ~= nil and params[4] ~= nil and
(params[4] == "true" or params[4] == "false") then
local value = false
if params[4] == "true" then
value = true
end
2016-07-30 07:38:52 +02:00
if factionsmod.set_admin(params[2],params[3],value) then
2016-07-30 05:02:22 +02:00
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: adminstate of " .. params[3] ..
2016-07-30 05:02:22 +02:00
" has been set to " .. params[4],
false)
else
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: FAILED to set admin privileges for " ..
2016-07-30 05:02:22 +02:00
params[3],
false)
end
end
return
end
if cmd == "description" and
params[2] ~= nil and
params[3] ~= nil then
local desc = params[3]
for i=4, #params, 1 do
desc = desc .. " " .. params[i]
end
2016-07-30 07:38:52 +02:00
if factionsmod.set_description(params[2],desc) then
2016-07-30 05:02:22 +02:00
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: updated description of faction " ..
2016-07-30 05:02:22 +02:00
params[2],
false)
return
else
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: FAILED to update description of faction " ..
2016-07-30 05:02:22 +02:00
params[2],
false)
return
end
end
if cmd == "invite" and
params[2] ~= nil and
params[3] ~= nil then
2016-07-30 07:38:52 +02:00
if factionsmod.member_invite(params[2],params[3]) then
2016-07-30 05:02:22 +02:00
minetest.chat_send_player(params[3],
2016-07-30 07:38:52 +02:00
"factionsmod: " .. params[3] ..
2016-07-30 05:02:22 +02:00
" you have been invited to join faction " .. params[2],
false)
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: " .. params[3] ..
2016-07-30 05:02:22 +02:00
" has been invited to join faction " .. params[2],
false)
return
else
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: FAILED to invite " .. params[3] ..
2016-07-30 05:02:22 +02:00
" to join faction " .. params[2],
false)
return
end
end
end
end
2016-07-30 07:38:52 +02:00
factionsmod_chat.show_help(playername)
2016-07-30 05:02:22 +02:00
end
-------------------------------------------------------------------------------
2016-07-30 07:38:52 +02:00
-- name: allfactionsmod_chathandler(playername,parameter)
2016-07-30 05:02:22 +02:00
--
--! @brief chat handler
2016-07-30 07:38:52 +02:00
--! @memberof factionsmod_chat
2016-07-30 05:02:22 +02:00
--! @private
--
--! @param playername name
--! @param parameter data supplied to command
-------------------------------------------------------------------------------
2016-07-30 07:38:52 +02:00
function factionsmod_chat.allfactionsmod_chathandler(playername,parameter)
2016-07-30 05:02:22 +02:00
local player = minetest.env:get_player_by_name(playername)
if player ~= nil then
local recipients = {}
2016-07-30 07:38:52 +02:00
for faction,value in pairs(factionsmod.get_factionsmod(player)) do
for name,value in pairs(factionsmod.dynamic_data.membertable[faction]) do
2016-07-30 05:02:22 +02:00
local object_to_check = mientest.env:get_player_by_name(name)
if object_to_check ~= nil then
recipients[name] = true
end
end
end
for recipient,value in pairs(recipients) do
if recipient ~= playername then
minetest.chat_send_player(recipient,playername ..": " .. parameter,false)
end
end
return
end
2016-07-30 07:38:52 +02:00
factionsmod_chat.show_help(playername)
2016-07-30 05:02:22 +02:00
end
-------------------------------------------------------------------------------
-- name: chathandler(playername,parameter)
--
--! @brief chat handler
2016-07-30 07:38:52 +02:00
--! @memberof factionsmod_chat
2016-07-30 05:02:22 +02:00
--! @private
--
--! @param playername name
--! @param parameter data supplied to command
-------------------------------------------------------------------------------
2016-07-30 07:38:52 +02:00
function factionsmod_chat.chathandler(playername,parameter)
2016-07-30 05:02:22 +02:00
local player = minetest.env:get_player_by_name(playername)
if player ~= nil then
local line = parameter:split(" ")
local target_faction = line[1]
local text = line[2]
for i=3,#line,1 do
text = text .. " " .. line[i]
end
local valid_faction = false
2016-07-30 07:38:52 +02:00
for faction,value in pairs(factionsmod.get_factionsmod(player)) do
2016-07-30 05:02:22 +02:00
if target_faction == faction then
valid_faction = true
end
end
if faction ~= nil and valid_faction and
2016-07-30 07:38:52 +02:00
factionsmod.dynamic_data.membertable[faction] ~= nil then
for name,value in pairs(factionsmod.dynamic_data.membertable[faction]) do
2016-07-30 05:02:22 +02:00
local object_to_check = mientest.env:get_player_by_name(name)
2016-07-30 07:38:52 +02:00
factionsmod_chat.show_help(playername)
2016-07-30 05:02:22 +02:00
if object_to_check ~= nil and
name ~= playername then
minetest.chat_send_player(name,playername ..": " .. text,false)
end
end
else
minetest.chat_send_player(playername,
2016-07-30 07:38:52 +02:00
"factionsmod: you're not a member of " .. dump(faction),false)
2016-07-30 05:02:22 +02:00
end
return
end
2016-07-30 07:38:52 +02:00
factionsmod_chat.show_help(playername)
2016-07-30 05:02:22 +02:00
end
-------------------------------------------------------------------------------
-- name: show_help(playername,parameter)
--
--! @brief send help message to player
2016-07-30 07:38:52 +02:00
--! @memberof factionsmod_chat
2016-07-30 05:02:22 +02:00
--! @private
--
--! @param playername name
-------------------------------------------------------------------------------
2016-07-30 07:38:52 +02:00
function factionsmod_chat.show_help(playername)
2016-07-30 05:02:22 +02:00
local MSG = function(text)
minetest.chat_send_player(playername,text,false)
end
2016-07-30 07:38:52 +02:00
MSG("factionsmod mod")
2016-07-30 05:02:22 +02:00
MSG("Usage:")
MSG("\tUser commands:")
2016-07-30 07:38:52 +02:00
MSG("\t\t/factionsmod -> info on your current factionsmod")
MSG("\t\t/factionsmod info <factionname> -> show description of faction")
MSG("\t\t/factionsmod list -> show list of factionsmod")
MSG("\t\t/factionsmod leave <factionname> -> leave specified faction")
MSG("\t\t/factionsmod join <factionname> -> join specified faction")
MSG("\t\t/factionsmod version -> show version number of mod")
2016-07-30 05:02:22 +02:00
MSG("\tAdmin commands:")
2016-07-30 07:38:52 +02:00
MSG("\t\t/factionsmod create <factionname> -> create a new faction")
MSG("\t\t/factionsmod delete <factionname> -> delete a faction faction")
MSG("\t\t/factionsmod leave <factionname> <playername> -> remove player from faction")
MSG("\t\t/factionsmod invite <factionname> <playername> -> invite player to faction")
MSG("\t\t/factionsmod set_free <factionname> <value> -> set faction free to join")
MSG("\t\t/factionsmod admin <factionname> <playername> <value> -> make player admin of faction")
MSG("\t\t/factionsmod description <factionname> <text> -> set description for faction")
2016-07-30 22:33:10 +02:00
end