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

76
pos.lua
View File

@ -12,26 +12,20 @@ areas.pos1 = {}
areas.pos2 = {} areas.pos2 = {}
minetest.register_chatcommand("select_area", { minetest.register_chatcommand("select_area", {
params = "<id>", params = "<ID>",
description = "Select a area by id.", description = "Select a area by id.",
privs = {},
func = function(name, param) func = function(name, param)
local id = tonumber(param) local id = tonumber(param)
if not id then if not id then
minetest.chat_send_player(name, return false, "Invalid usage, see /help select_area."
"Invalid usage, see /help select_area.")
return
end end
if not areas.areas[id] then if not areas.areas[id] then
minetest.chat_send_player(name, return false, "The area "..id.." does not exist."
"The area "..id.." does not exist.")
return
end end
areas:setPos1(name, areas.areas[id].pos1) areas:setPos1(name, areas.areas[id].pos1)
areas:setPos2(name, areas.areas[id].pos2) areas:setPos2(name, areas.areas[id].pos2)
minetest.chat_send_player(name, return true, "Area "..id.." selected."
"Area "..id.." selected.")
end, end,
}) })
@ -47,24 +41,19 @@ minetest.register_chatcommand("area_pos1", {
if found then if found then
pos = {x=tonumber(x), y=tonumber(y), z=tonumber(z)} pos = {x=tonumber(x), y=tonumber(y), z=tonumber(z)}
elseif param == "" then elseif param == "" then
player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player then if player then
pos = player:getpos() pos = player:getpos()
else else
minetest.chat_send_player(name, return false, "Unable to get position."
"Unable to get position")
return
end end
else else
minetest.chat_send_player(name, return false, "Invalid usage, see /help area_pos1."
"Invalid usage, see /help area_pos1")
return
end end
pos = vector.round(pos) pos = vector.round(pos)
areas:setPos1(name, pos) areas:setPos1(name, pos)
minetest.chat_send_player(name, return true, "Area position 1 set to "
"Area position 1 set to " ..minetest.pos_to_string(pos)
..minetest.pos_to_string(pos))
end, end,
}) })
@ -72,7 +61,6 @@ minetest.register_chatcommand("area_pos2", {
params = "[X Y Z|X,Y,Z]", params = "[X Y Z|X,Y,Z]",
description = "Set area protection region position 2 to your" description = "Set area protection region position 2 to your"
.." location or the one specified", .." location or the one specified",
privs = {},
func = function(name, param) func = function(name, param)
local pos = nil local pos = nil
local found, _, x, y, z = param:find( local found, _, x, y, z = param:find(
@ -80,24 +68,19 @@ minetest.register_chatcommand("area_pos2", {
if found then if found then
pos = {x=tonumber(x), y=tonumber(y), z=tonumber(z)} pos = {x=tonumber(x), y=tonumber(y), z=tonumber(z)}
elseif param == "" then elseif param == "" then
player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player then if player then
pos = player:getpos() pos = player:getpos()
else else
minetest.chat_send_player(name, return false, "Unable to get position."
"Unable to get position")
return
end end
else else
minetest.chat_send_player(name, return false, "Invalid usage, see /help area_pos2."
"Invalid usage, see /help area_pos2")
return
end end
pos = vector.round(pos) pos = vector.round(pos)
areas:setPos2(name, pos) areas:setPos2(name, pos)
minetest.chat_send_player(name, return true, "Area position 2 set to "
"Area position 2 set to " ..minetest.pos_to_string(pos)
..minetest.pos_to_string(pos))
end, end,
}) })
@ -106,38 +89,31 @@ minetest.register_chatcommand("area_pos", {
params = "set/set1/set2/get", params = "set/set1/set2/get",
description = "Set area protection region, position 1, or position 2" description = "Set area protection region, position 1, or position 2"
.." by punching nodes, or display the region", .." by punching nodes, or display the region",
privs = {},
func = function(name, param) func = function(name, param)
if param == "set" then -- Set both area positions if param == "set" then -- Set both area positions
areas.set_pos[name] = "pos1" areas.set_pos[name] = "pos1"
minetest.chat_send_player(name, return true, "Select positions by punching two nodes."
"Select positions by punching two nodes")
elseif param == "set1" then -- Set area position 1 elseif param == "set1" then -- Set area position 1
areas.set_pos[name] = "pos1only" areas.set_pos[name] = "pos1only"
minetest.chat_send_player(name, return true, "Select position 1 by punching a node."
"Select position 1 by punching a node")
elseif param == "set2" then -- Set area position 2 elseif param == "set2" then -- Set area position 2
areas.set_pos[name] = "pos2" areas.set_pos[name] = "pos2"
minetest.chat_send_player(name, return true, "Select position 2 by punching a node."
"Select position 2 by punching a node")
elseif param == "get" then -- Display current area positions elseif param == "get" then -- Display current area positions
if areas.pos1[name] ~= nil then local pos1str, pos2str = "Position 1: ", "Position 2: "
minetest.chat_send_player(name, "Position 1: " if areas.pos1[name] then
..minetest.pos_to_string(areas.pos1[name])) pos1str = pos1str..minetest.pos_to_string(areas.pos1[name])
else else
minetest.chat_send_player(name, pos1str = pos1str.."<not set>"
"Position 1 not set")
end end
if areas.pos2[name] ~= nil then if areas.pos2[name] then
minetest.chat_send_player(name, "Position 2: " pos2str = pos2str..minetest.pos_to_string(areas.pos2[name])
..minetest.pos_to_string(areas.pos2[name]))
else else
minetest.chat_send_player(name, pos2str = pos2str.."<not set>"
"Position 2 not set")
end end
return true, pos1str.."\n"..pos2str
else else
minetest.chat_send_player(name, return false, "Unknown subcommand: "..param
"Unknown subcommand: "..param)
end end
end, end,
}) })