Skip to content

Instantly share code, notes, and snippets.

@PilzAdam
Created September 14, 2012 19:57
Show Gist options
  • Save PilzAdam/3724328 to your computer and use it in GitHub Desktop.
Save PilzAdam/3724328 to your computer and use it in GitHub Desktop.
minetest.register_chatcommand("spawn", {
params = "",
description = "Rewspawn at spawnpoint",
privs = {},
func = function(name, param)
local file = io.open(minetest.get_worldpath().."/spawn", "r")
if not file then
minetest.chat_send_player(name, "No spawnpoint found")
return
end
local line = file:read("*line")
file:close()
local pos = minetest.string_to_pos(string.sub(line, 1, string.find(line, ")")))
if not pos or type(pos) ~= "table" then
minetest.chat_send_player(name, "Failed to load position: "..line)
return
end
minetest.env:get_player_by_name(name):setpos(pos)
minetest.chat_send_player(name, "Respawned")
end
})
minetest.register_chatcommand("setspawn", {
params = "<X>,<Y>,<Z>",
description = "Set spawnpoint spawnpoint",
privs = {server=true},
func = function(name, param)
local tmp = param
if string.find(tmp, ",") then
local x = tonumber(string.sub(tmp, 1, string.find(tmp, ",")-1))
tmp = string.sub(tmp, string.find(tmp, ",")+1)
if string.find(tmp, ",") then
local y = tonumber(string.sub(tmp, 1, string.find(tmp, ",")-1))
tmp = string.sub(tmp, string.find(tmp, ",")+1)
local z = tonumber(tmp)
end
end
if not x or not y or not z then
minetest.chat_send_player(name, "Invalid parameters ("..param..")")
return
end
local file = io.open(minetest.get_worldpath().."/spawn", "w")
if not file then
minetest.chat_send_player(name, "IO error")
return
end
file:write(minetest.pos_to_string({x=x, y=y, z=z}))
file:close()
minetest.chat_send_player(name, "Spawnpoint set")
end
})
minetest.register_on_respawnplayer(function(player)
local file = io.open(minetest.get_worldpath().."/spawn", "r")
if not file then
return
end
local line = file:read("*line")
file:close()
local pos = minetest.string_to_pos(string.sub(line, 1, string.find(line, ")")))
if not pos or type(pos) ~= "table" then
return
end
player:setpos(pos)
return true
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment