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
})

76
pos.lua
View File

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