From 93cdc9b9dc240aef70ec5016552c7f36ee019865 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 9 Apr 2012 23:29:55 +0300 Subject: [PATCH] Support static_spawnpoint setting --- builtin/builtin.lua | 1 + builtin/chatcommands.lua | 2 +- builtin/misc.lua | 32 ++++++++++++++++++++++++++++++++ builtin/static_spawn.lua | 33 +++++++++++++++++++++++++++++++++ doc/lua_api.txt | 2 ++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 builtin/static_spawn.lua diff --git a/builtin/builtin.lua b/builtin/builtin.lua index 10acd0f5..13c1c09d 100644 --- a/builtin/builtin.lua +++ b/builtin/builtin.lua @@ -19,4 +19,5 @@ dofile(minetest.get_modpath("__builtin").."/misc.lua") dofile(minetest.get_modpath("__builtin").."/privileges.lua") dofile(minetest.get_modpath("__builtin").."/auth.lua") dofile(minetest.get_modpath("__builtin").."/chatcommands.lua") +dofile(minetest.get_modpath("__builtin").."/static_spawn.lua") diff --git a/builtin/chatcommands.lua b/builtin/chatcommands.lua index 047cfc69..a7061e3a 100644 --- a/builtin/chatcommands.lua +++ b/builtin/chatcommands.lua @@ -322,7 +322,7 @@ minetest.register_chatcommand("set", { privs = {server=true}, func = function(name, param) local arg, setname, setvalue = string.match(param, "(-[n]) ([^ ]+) (.+)") - if arg and arg == "n" and setname and setvalue then + if arg and arg == "-n" and setname and setvalue then minetest.setting_set(setname, setvalue) minetest.chat_send_player(name, setname.." = "..setvalue) return diff --git a/builtin/misc.lua b/builtin/misc.lua index 89f90dfb..b3b1ec6e 100644 --- a/builtin/misc.lua +++ b/builtin/misc.lua @@ -67,3 +67,35 @@ function minetest.get_node_group(name, group) return minetest.get_item_group(name, group) end +function minetest.string_to_pos(value) + local p = {} + p.x, p.y, p.z = string.match(value, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$") + if p.x and p.y and p.z then + p.x = tonumber(p.x) + p.y = tonumber(p.y) + p.z = tonumber(p.z) + return p + end + local p = {} + p.x, p.y, p.z = string.match(value, "^%( *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *%)$") + if p.x and p.y and p.z then + p.x = tonumber(p.x) + p.y = tonumber(p.y) + p.z = tonumber(p.z) + return p + end + return nil +end + +assert(minetest.string_to_pos("10.0, 5, -2").x == 10) +assert(minetest.string_to_pos("( 10.0, 5, -2)").z == -2) +assert(minetest.string_to_pos("asd, 5, -2)") == nil) + +function minetest.setting_get_pos(name) + local value = minetest.setting_get(name) + if not value then + return nil + end + return minetest.string_to_pos(value) +end + diff --git a/builtin/static_spawn.lua b/builtin/static_spawn.lua new file mode 100644 index 00000000..e8c107d8 --- /dev/null +++ b/builtin/static_spawn.lua @@ -0,0 +1,33 @@ +-- Minetest: builtin/static_spawn.lua + +local function warn_invalid_static_spawnpoint() + if minetest.setting_get("static_spawnpoint") and + not minetest.setting_get_pos("static_spawnpoint") then + minetest.log('error', "The static_spawnpoint setting is invalid: \"".. + minetest.setting_get("static_spawnpoint").."\"") + end +end + +warn_invalid_static_spawnpoint() + +local function put_player_in_spawn(obj) + warn_invalid_static_spawnpoint() + local static_spawnpoint = minetest.setting_get_pos("static_spawnpoint") + if not static_spawnpoint then + return false + end + minetest.log('action', "Moving "..obj:get_player_name().. + " to static spawnpoint at ".. + minetest.pos_to_string(static_spawnpoint)) + obj:setpos(static_spawnpoint) + return true +end + +minetest.register_on_newplayer(function(obj) + put_player_in_spawn(obj) +end) + +minetest.register_on_respawnplayer(function(obj) + return put_player_in_spawn(obj) +end) + diff --git a/doc/lua_api.txt b/doc/lua_api.txt index aabc673f..9bb0dfe9 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -514,6 +514,7 @@ string:trim() ^ eg. string.trim("\n \t\tfoo bar\t ") == "foo bar" minetest.pos_to_string({x=X,y=Y,z=Z}) -> "(X,Y,Z)" ^ Convert position to a printable string +minetest.string_to_pos(string) -> position minetest namespace reference ----------------------------- @@ -576,6 +577,7 @@ Setting-related: minetest.setting_set(name, value) minetest.setting_get(name) -> string or nil minetest.setting_getbool(name) -> boolean value or nil +minetest.setting_get_pos(name) -> position or nil minetest.add_to_creative_inventory(itemstring) Authentication: -- 2.30.2