Tidy up a load of command-related stuff

master
Ciaran Gultnieks 2014-04-06 15:28:00 +01:00
parent 41aec77b13
commit 967d7322c3
1 changed files with 242 additions and 223 deletions

View File

@ -9,17 +9,17 @@ if moddebug then dbg=moddebug.dbg("people") else dbg={v1=function() end,v2=funct
-- actually exists.
local get_person = function(args)
if not args then return nil, nil, nil end
if not args then return nil, nil, nil end
person_name, args = string.match(args, "^([^ ]+)(.*)")
person_name, args = string.match(args, "^([^ ]+)(.*)")
if person_name and not people.is_valid_name(person_name) then
return nil, nil, nil
end
local person
if person_name then person = people.people[person_name] end
if args then args = string.sub(args, 2) end
return person_name, person, args
if person_name and not people.is_valid_name(person_name) then
return nil, nil, nil
end
local person
if person_name then person = people.people[person_name] end
if args then args = string.sub(args, 2) end
return person_name, person, args
end
@ -35,240 +35,263 @@ end
local subcmd = {}
subcmd.help = {
params = "[<subcmd>]",
desc = "You're being silly, aren't you?",
exec = function(playername, args)
params = "[<subcmd>]",
desc = "You're being silly, aren't you?",
exec = function(playername, args)
if args and args ~= "" then
if not subcmd[args] then
return "No such subcommand"
end
return "/people "..args.." "..subcmd[args].params.." - "..
subcmd[args].desc, true
end
if args and args ~= "" then
if not subcmd[args] then
return "No such subcommand"
end
return "/people "..args.." "..subcmd[args].params.." - "..
subcmd[args].desc, true
end
local msg = "Subcommands (use /people help <subcmd> for more):"
for c, _ in pairs(subcmd) do
msg = msg.." "..c
local msg = "Subcommands (use /people help <subcmd> for more):"
for c, _ in pairs(subcmd) do
msg = msg.." "..c
end
return msg, true
end
return msg, true
end
}
subcmd.create = {
params = "<name>",
desc = "Create a person with the given name at your current location",
exec = function(playername, args)
params = "<name>",
desc = "Create a person with the given name at your current location",
exec = function(playername, args)
local person, person_name
person_name, person, args = get_person(args)
if not person_name then
return "Name needs to be specified"
if not minetest.check_player_privs(playername, {server=true}) then
return "Only admins can create people"
end
local person, person_name
person_name, person, args = get_person(args)
if not person_name then
return "Name needs to be specified"
end
if person then
return "Person "..person_name.." already exists"
end
local player = minetest.get_player_by_name(playername)
if not player then
return "people create can only be used by a player"
end
local pos = player:getpos()
local obj = minetest.add_entity(pos, "people:person")
if not obj then
return "Failed to add_entity"
end
local ent = obj:get_luaentity()
ent.name = person_name
ent.owner = playername
people.people_add(ent)
return "Created "..person_name, true
end
if person then
return "Person "..person_name.." already exists"
end
local player = minetest.get_player_by_name(playername)
if not player then
return "people create can only be used by a player"
end
local pos = player:getpos()
local obj = minetest.add_entity(pos, "people:person")
if not obj then
return "Failed to add_entity"
end
local ent = obj:get_luaentity()
ent.name = person_name
ent.owner = playername
people.people_add(ent)
return "Created "..person_name, true
end
}
subcmd.delete = {
params = "<name>",
desc = "Delete the person with the given name",
exec = function(playername, args)
params = "<name>",
desc = "Delete the person with the given name",
exec = function(playername, args)
local person, person_name
person_name, person, args = get_person(args)
if not person then
return "Specify a valid person"
local person, person_name
person_name, person, args = get_person(args)
if not person then
return "Specify a valid person"
end
local ent = people.people[person_name].entity
if not ent then
return nil, false, true
end
if not minetest.check_player_privs(playername, {server=true}) then
if playername ~= ent.owner then
return person_name.." isn't yours, so so can't do that"
end
end
ent.object:remove()
people.people[person_name] = nil
return "Deleted "..person_name, true
end
local ent = people.people[person_name].entity
if not ent then
return nil, false, true
end
ent.object:remove()
people.people[person_name] = nil
return "Deleted "..person_name, true
end
}
subcmd.summon = {
params = "<name>",
desc = "Move the person with the given name to your current location",
exec = function(playername, args)
params = "<name>",
desc = "Move the person with the given name to your current location",
exec = function(playername, args)
local person, person_name
person_name, person, args = get_person(args)
if not person then
return "Specify a valid person"
local person, person_name
person_name, person, args = get_person(args)
if not person then
return "Specify a valid person"
end
local player = minetest.get_player_by_name(playername)
if not player then
return "people summon can only be used by a player"
end
local pos = player:getpos()
local ent = people.people[person_name].entity
if not ent then
return nil, false, true
end
ent.object:setpos(pos)
return "Summoned "..person_name, true
end
local player = minetest.get_player_by_name(playername)
if not player then
return "people summon can only be used by a player", false
end
local pos = player:getpos()
local ent = people.people[person_name].entity
if not ent then
return nil, false, true
end
ent.object:setpos(pos)
return "Summoned "..person_name, true
end
}
subcmd.where = {
params = "<name>",
desc = "Report the location and status of the person with the given name",
exec = function(playername, args)
params = "<name>",
desc = "Report the location and status of the person with the given name",
exec = function(playername, args)
local person, person_name
person_name, person, args = get_person(args)
if not person then
return "Specify a valid person"
local person, person_name
person_name, person, args = get_person(args)
if not person then
return "Specify a valid person"
end
local ent = people.people[person_name].entity
if not ent then
return person_name.." is inactive at "..minetest.pos_to_string(people.people[person_name].pos), true
end
return person_name.." is at "..minetest.pos_to_string(ent.object:getpos()), true
end
local ent = people.people[person_name].entity
if not ent then
return person_name.." is inactive at "..minetest.pos_to_string(people.people[person_name].pos), true
end
return person_name.." is at "..minetest.pos_to_string(ent.object:getpos()), true
end
}
subcmd.setowner = {
params = "<name> <owner>",
desc = "Set the owner of the given person to the given player",
exec = function(playername, person, person_name, args)
params = "<name> <owner>",
desc = "Set the owner of the given person to the given player",
exec = function(playername, args)
local person, person_name
person_name, person, args = get_person(args)
if not person then
return "Specify a valid person"
local person, person_name
person_name, person, args = get_person(args)
if not person then
return "Specify a valid person"
end
local ent = people.people[person_name].entity
if not ent then
return nil, false, true
end
if not minetest.check_player_privs(playername, {server=true}) then
if playername ~= ent.owner then
return person_name.." isn't yours, so so can't do that"
end
end
if not minetest.get_player_by_name(args) then
return "No such new owner '"..args.."'"
end
ent.owner = args
return "Set owner of "..person_name.. " to "..args, true
end
local ent = people.people[person_name].entity
if not ent then
return nil, false, true
end
if not minetest.get_player_by_name(args) then
return "No such new owner"
end
ent.owner = args
return "Set owner of "..person_name.. " to "..args, true
end
}
subcmd.list = {
params = "",
desc = "List all people in the world",
exec = function(playername, args)
params = "",
desc = "List all people in the world",
exec = function(playername, args)
local count = 0
for k, v in pairs(people.people) do
local msg = k.." : "
if v.entity then
msg = msg.."active at "..minetest.pos_to_string(v.entity.object:getpos())
else
if not v.waketime then
msg = msg.."inactive (for unknown time!?)"
else
msg = msg.."inactive for another "..v.waketime - minetest.get_gametime().."s at "..minetest.pos_to_string(v.pos)
if not minetest.check_player_privs(playername, {server=true}) then
return "Only admins can list people"
end
end
minetest.chat_send_player(playername, msg)
count = count + 1
local count = 0
for k, v in pairs(people.people) do
local msg = k.." : "
if v.entity then
msg = msg.."active at "..minetest.pos_to_string(v.entity.object:getpos())
else
if not v.waketime then
msg = msg.."inactive (for unknown time!?)"
else
msg = msg.."inactive for another "..v.waketime - minetest.get_gametime().."s at "..minetest.pos_to_string(v.pos)
end
end
minetest.chat_send_player(playername, msg)
count = count + 1
end
return count.." people", true
end
return count.." people", true
end
}
subcmd.tell = {
params = "<name> <message>",
desc = "Send a message to the given person",
exec = function(playername, args)
if args == "help" then
end
params = "<name> <message>",
desc = "Send a message to the given person",
exec = function(playername, args)
if args == "help" then
end
local person, person_name
person_name, person, args = get_person(args)
if not person then
return "Specify a valid person"
end
local ent = people.people[person_name].entity
if not ent then
return nil, false, true
end
local person, person_name
person_name, person, args = get_person(args)
if not person then
return "Specify a valid person"
end
local ent = people.people[person_name].entity
if not ent then
return nil, false, true
end
if playername ~= ent.owner then
return person.name.." doesn't listen to you", false
end
if playername ~= ent.owner then
return person.name.." doesn't listen to you"
end
local sender = minetest.get_player_by_name(playername)
if not sender then return end
ent.on_tell(ent, sender, args)
end
local sender = minetest.get_player_by_name(playername)
if not sender then return end
ent.on_tell(ent, sender, args)
end
}
subcmd.skin = {
params = "(<name> <skin>)|(<list>)",
desc = "Set a person's skin, or list available skins",
exec = function(playername, args)
params = "(<name> <skin>)|(<list>)",
desc = "Set a person's skin, or list available skins",
exec = function(playername, args)
local person, person_name
person_name, person, args = get_person(args)
if not args then
return "More arguments needed", false
end
if not minetest.get_modpath("skins") then
return "Skins mod is not installed", false
end
if person_name == "list" then
local texlist = ""
for _, skin in ipairs(skins.list) do
if string.match(skin, "^character_") then
if texlist ~= "" then texlist = texlist.." " end
texlist = texlist..string.sub(skin, 11)
local person, person_name
person_name, person, args = get_person(args)
if not args then
return "More arguments needed"
end
end
return "Available skins: "..texlist, true
end
if not person then
return "Specify a valid person, or 'list'", false
end
if not args then
return "Specify skin", false
end
if not minetest.get_modpath("skins") then
return "Skins mod is not installed"
end
local ent = person.entity
if not ent then
return nil, false, true
end
if person_name == "list" then
local texlist = ""
for _, skin in ipairs(skins.list) do
if string.match(skin, "^character_") then
if texlist ~= "" then texlist = texlist.." " end
texlist = texlist..string.sub(skin, 11)
end
end
return "Available skins: "..texlist, true
end
local req = "character_"..args
for _, skin in ipairs(skins.list) do
if skin == req then
ent.props.textures = {req..".png"}
ent:update_props()
return "Skin updated", true
end
end
return "No such skin as '"..args.."'", false
if not person then
return "Specify a valid person, or 'list'"
end
if not args then
return "Specify skin"
end
end
local ent = person.entity
if not ent then
return nil, false, true
end
if not minetest.check_player_privs(playername, {server=true}) then
if playername ~= ent.owner then
return person_name.." isn't yours, so so can't do that"
end
end
local req = "character_"..args
for _, skin in ipairs(skins.list) do
if skin == req then
ent.props.textures = {req..".png"}
ent:update_props()
return "Skin updated", true
end
end
return "No such skin as '"..args.."'"
end
}
--- Handle a chat command.
@ -277,41 +300,37 @@ subcmd.skin = {
-- and was subsequently activated.
people.do_command = function(name, param)
if not minetest.check_player_privs(name, {server=true}) then
return "People commands are currently all restricted to admins", false
end
local cmd, args
cmd, args = string.match(param, "^([^ ]+)(.*)")
if not cmd then return subcmd.help.exec() end
local cmd, args
cmd, args = string.match(param, "^([^ ]+)(.*)")
if not cmd then return subcmd.help.exec() end
if subcmd[cmd] then
if args then args = string.sub(args, 2) end
reply, success, queue_and_activate = subcmd[cmd].exec(name, args)
if queue_and_activate then
-- Queue a command to happen when the entity wakes up,
-- and make it wake up!
local person
_, person, _ = get_person(args)
person.waketime = minetest.get_gametime()
local add = {name, param}
if person.wakecmds then
table.insert(person.wakecmds, add)
else
person.wakecmds = {add}
end
return "Person is inactivate - activating...", true
if subcmd[cmd] then
if args then args = string.sub(args, 2) end
reply, success, queue_and_activate = subcmd[cmd].exec(name, args)
if queue_and_activate then
-- Queue a command to happen when the entity wakes up,
-- and make it wake up!
local person
_, person, _ = get_person(args)
person.waketime = minetest.get_gametime()
local add = {name, param}
if person.wakecmds then
table.insert(person.wakecmds, add)
else
person.wakecmds = {add}
end
return "Person is inactivate - activating...", true
end
return reply, success
end
return reply, success
end
return "No such people command '"..cmd.."' - see 'people help'", false
return "No such people command '"..cmd.."' - see 'people help'"
end
minetest.register_chatcommand("people", {
params = "<cmd> [name] [args]",
description = "Commands for working with the people module. '/people help' for help.",
func = people.do_command
params = "<cmd> [name] [args]",
description = "Commands for working with the people module. '/people help' for help.",
func = people.do_command
})
minetest.register_chatcommand("tell", {