Pass messages by return value

This commit is contained in:
ShadowNinja
2014-05-29 11:04:37 -04:00
parent 4e1aef5491
commit 02905caaeb
2 changed files with 127 additions and 190 deletions

View File

@ -5,35 +5,31 @@ minetest.register_chatcommand("protect", {
privs = {[areas.self_protection_privilege]=true},
func = function(name, param)
if param == "" then
minetest.chat_send_player(name, 'Invalid usage, see /help protect')
return
return false, "Invalid usage, see /help protect."
end
local pos1, pos2 = areas:getPos1(name), areas:getPos2(name)
if pos1 and pos2 then
pos1, pos2 = areas:sortPos(pos1, pos2)
else
minetest.chat_send_player(name, 'You need to select an area first')
return
return false, "You need to select an area first."
end
minetest.log("action", "/protect invoked, owner="..name..
" areaname="..param..
" startpos="..minetest.pos_to_string(pos1)..
" endpos=" ..minetest.pos_to_string(pos2))
" AreaName="..param..
" StartPos="..minetest.pos_to_string(pos1)..
" EndPos=" ..minetest.pos_to_string(pos2))
local canAdd, errMsg = areas:canPlayerAddArea(pos1, pos2, name)
if not canAdd then
minetest.chat_send_player(name,
"You can't protect that area: "
..errMsg)
return
return false, "You can't protect that area: "..errMsg
end
local id = areas:add(name, param, pos1, pos2, nil)
areas:save()
minetest.chat_send_player(name, "Area protected. ID: "..id)
end})
return true, "Area protected. ID: "..id
end
})
minetest.register_chatcommand("set_owner", {
@ -43,40 +39,38 @@ minetest.register_chatcommand("set_owner", {
.." area to any existing area",
privs = {areas=true},
func = function(name, param)
local found, _, ownername, areaname = param:find('^([^ ]+) (.+)$')
local ownerName, areaName = param:match('^(%S+)%s(.+)$')
if not found then
minetest.chat_send_player(name, "Incorrect usage, see /help set_owner")
return
if not ownerName then
return false, "Incorrect usage, see /help set_owner."
end
local pos1, pos2 = areas:getPos1(name), areas:getPos2(name)
if pos1 and pos2 then
pos1, pos2 = areas:sortPos(pos1, pos2)
else
minetest.chat_send_player(name, "You need to select an area first")
return
return false, "You need to select an area first."
end
if not areas:player_exists(ownername) then
minetest.chat_send_player(name, "The player \""
..ownername.."\" does not exist")
return
if not areas:player_exists(ownerName) then
return false, "The player \""
..ownerName.."\" does not exist."
end
minetest.log("action", name.." runs /set_owner. Owner = "..ownername..
" AreaName = "..areaname..
minetest.log("action", name.." runs /set_owner. Owner = "..ownerName..
" AreaName = "..areaName..
" StartPos = "..minetest.pos_to_string(pos1)..
" EndPos = " ..minetest.pos_to_string(pos2))
local id = areas:add(ownername, areaname, pos1, pos2, nil)
local id = areas:add(ownerName, areaName, pos1, pos2, nil)
areas:save()
minetest.chat_send_player(ownername,
minetest.chat_send_player(ownerName,
"You have been granted control over area #"..
id..". Type /list_areas to show your areas.")
minetest.chat_send_player(name, "Area protected. ID: "..id)
end})
return true, "Area protected. ID: "..id
end
})
minetest.register_chatcommand("add_owner", {
@ -84,10 +78,9 @@ minetest.register_chatcommand("add_owner", {
description = "Give a player access to a sub-area beetween two"
.." positions that have already been protected,"
.." Use set_owner if you don't want the parent to be set.",
privs = {},
func = function(name, param)
local found, _, pid, ownername, areaname
= param:find('^(%d+) ([^ ]+) (.+)$')
local pid, ownerName, areaName
= param:match('^(%d+) ([^ ]+) (.+)$')
if not found then
minetest.chat_send_player(name, "Incorrect usage, see /help add_owner")
@ -98,18 +91,15 @@ minetest.register_chatcommand("add_owner", {
if pos1 and pos2 then
pos1, pos2 = areas:sortPos(pos1, pos2)
else
minetest.chat_send_player(name, 'You need to select an area first')
return
return false, "You need to select an area first."
end
if not areas:player_exists(ownername) then
minetest.chat_send_player(name, 'The player "'
..ownername..'" does not exist')
return
if not areas:player_exists(ownerName) then
return false, "The player \""..ownerName.."\" does not exist."
end
minetest.log("action", name.." runs /add_owner. Owner = "..ownername..
" AreaName = "..areaname.." ParentID = "..pid..
minetest.log("action", name.." runs /add_owner. Owner = "..ownerName..
" AreaName = "..areaName.." ParentID = "..pid..
" StartPos = "..pos1.x..","..pos1.y..","..pos1.z..
" EndPos = " ..pos2.x..","..pos2.y..","..pos2.z)
@ -117,59 +107,51 @@ minetest.register_chatcommand("add_owner", {
pid = tonumber(pid)
if (not areas:isAreaOwner(pid, name)) or
(not areas:isSubarea(pos1, pos2, pid)) then
minetest.chat_send_player(name,
"You can't protect that area")
return
return false, "You can't protect that area."
end
local id = areas:add(ownername, areaname, pos1, pos2, pid)
local id = areas:add(ownerName, areaName, pos1, pos2, pid)
areas:save()
minetest.chat_send_player(ownername,
minetest.chat_send_player(ownerName,
"You have been granted control over area #"..
id..". Type /list_areas to show your areas.")
minetest.chat_send_player(name, "Area protected. ID: "..id)
end})
return true, "Area protected. ID: "..id
end
})
minetest.register_chatcommand("rename_area", {
params = "<ID> <newName>",
description = "Rename a area that you own",
privs = {},
func = function(name, param)
local found, _, id, newName = param:find("^(%d+) (.+)$")
local id, newName = param:match("^(%d+)%s(.+)$")
if not found then
minetest.chat_send_player(name,
"Invalid usage, see /help rename_area")
return
return false, "Invalid usage, see /help rename_area."
end
id = tonumber(id)
if not id then
minetest.chat_send_player(name, "That area doesn't exist.")
return
return false, "That area doesn't exist."
end
if not areas:isAreaOwner(id, name) then
minetest.chat_send_player(name, "You don't own that area.")
return
return true, "You don't own that area."
end
areas.areas[id].name = newName
areas:save()
minetest.chat_send_player(name, "Area renamed.")
end})
return true, "Area renamed."
end
})
minetest.register_chatcommand("find_areas", {
params = "<regexp>",
description = "Find areas using a Lua regular expression",
privs = {},
func = function(name, param)
if param == "" then
minetest.chat_send_player(name,
"A regular expression is required.")
return
return false, "A regular expression is required."
end
-- Check expression for validity
@ -177,157 +159,136 @@ minetest.register_chatcommand("find_areas", {
("Test [1]: Player (0,0,0) (0,0,0)"):find(param)
end
if not pcall(testRegExp) then
minetest.chat_send_player(name,
"Invalid regular expression.")
return
return false, "Invalid regular expression."
end
local found = false
local matches = {}
for id, area in pairs(areas.areas) do
if areas:isAreaOwner(id, name) and
areas:toString(id):find(param) then
minetest.chat_send_player(name, areas:toString(id))
found = true
table.insert(matches, areas:toString(id))
end
end
if not found then
minetest.chat_send_player(name, "No matches found")
if #matches > 1 then
return true, table.concat(matches, "\n")
else
return true, "No matches found."
end
end})
end
})
minetest.register_chatcommand("list_areas", {
params = "",
description = "List your areas, or all areas if you are an admin.",
privs = {},
func = function(name, param)
local admin = minetest.check_player_privs(name, {areas=true})
if admin then
minetest.chat_send_player(name,
"Showing all areas.")
else
minetest.chat_send_player(name,
"Showing your areas.")
end
local areaStrings = {}
for id, area in pairs(areas.areas) do
if admin or areas:isAreaOwner(id, name) then
minetest.chat_send_player(name,
areas:toString(id))
table.insert(areaStrings, areas:toString(id))
end
end
end})
if #areaStrings == 0 then
return true, "No visible areas."
end
return true, table.concat(areaStrings, "\n")
end
})
minetest.register_chatcommand("recursive_remove_areas", {
params = "<id>",
description = "Recursively remove areas using an id",
privs = {},
func = function(name, param)
local id = tonumber(param)
if not id then
minetest.chat_send_player(name,
"Invalid usage, see"
.." /help recursive_remove_areas")
return
return false, "Invalid usage, see"
.." /help recursive_remove_areas"
end
if not areas:isAreaOwner(id, name) then
minetest.chat_send_player(name, "Area "..id
.." does not exist or is"
.." not owned by you.")
return
return false, "Area "..id.." does not exist or is"
.." not owned by you."
end
areas:remove(id, true)
areas:save()
minetest.chat_send_player(name, "Removed area "..id
.." and it's sub areas.")
end})
return true, "Removed area "..id.." and it's sub areas."
end
})
minetest.register_chatcommand("remove_area", {
params = "<id>",
description = "Remove an area using an id",
privs = {},
func = function(name, param)
local id = tonumber(param)
if not id then
minetest.chat_send_player(name,
"Invalid usage, see /help remove_area")
return
return false, "Invalid usage, see /help remove_area"
end
if not areas:isAreaOwner(id, name) then
minetest.chat_send_player(name, "Area "..id
.." does not exist or"
.." is not owned by you")
return
return false, "Area "..id.." does not exist or"
.." is not owned by you."
end
areas:remove(id)
areas:save()
minetest.chat_send_player(name, 'Removed area '..id)
end})
return true, "Removed area "..id
end
})
minetest.register_chatcommand("change_owner", {
params = "<id> <NewOwner>",
description = "Change the owner of an area using its id",
privs = {},
params = "<ID> <NewOwner>",
description = "Change the owner of an area using it's ID",
func = function(name, param)
local found, _, id, new_owner =
param:find('^(%d+) ([^ ]+)$')
local id, newOwner = param:match("^(%d+)%s(%S+)$")
if not found then
minetest.chat_send_player(name,
"Invalid usage,"
.." see /help change_area_owner")
return
if not id then
return false, "Invalid usage, see"
.." /help change_owner."
end
if not areas:player_exists(new_owner) then
minetest.chat_send_player(name, 'The player "'
..new_owner..'" does not exist')
return
if not areas:player_exists(newOwner) then
return false, "The player \""..newOwner
.."\" does not exist."
end
id = tonumber(id)
if not areas:isAreaOwner(id, name) then
minetest.chat_send_player(name,
"Area "..id.." does not exist"
.." or is not owned by you.")
return
return false, "Area "..id.." does not exist"
.." or is not owned by you."
end
areas.areas[id].owner = new_owner
areas.areas[id].owner = newOwner
areas:save()
minetest.chat_send_player(name, 'Owner changed.')
minetest.chat_send_player(new_owner,
name..'" has given you control over an area.')
end})
minetest.chat_send_player(newOwner,
("%s has given you control over the area %q (ID %d).")
:format(name, areas[id].name, id))
return true, "Owner changed."
end
})
minetest.register_chatcommand("area_open", {
params = "<id>",
description = "Toggle an area open (anyone can interact) or not",
privs = {},
params = "<ID>",
description = "Toggle an area open (anyone can interact) or closed",
func = function(name, param)
local id = tonumber(param)
if not id then
minetest.chat_send_player(name,
"Invalid usage, see /help area_open")
return
return false, "Invalid usage, see /help area_open."
end
if not areas:isAreaOwner(id, name) then
minetest.chat_send_player(name,
"Area "..id.." does not exist"
.." or is not owned by you.")
return
return false, "Area "..id.." does not exist"
.." or is not owned by you."
end
local open = not areas.areas[id].open
-- Save false as nil to avoid inflating the DB.
areas.areas[id].open = open or nil
areas:save()
minetest.chat_send_player(name, "Area "..(open and "opened" or "closed")..".")
end})
return true, ("Area %s."):format(open and "opened" or "closed")
end
})