Add register for formspec

This commit is contained in:
An0n3m0us 2019-10-19 00:09:03 +01:00
parent b2bb6214db
commit 23923731bf

View file

@ -13,7 +13,7 @@ local formspec =
"size[8,4]" ..
"real_coordinates[true]" ..
"label[3,0.5;" .. S("Are you sure?") .. "]" ..
"label[0.5,1;" .. S("(This will override your previous home coordinates!)") .. "]" ..
"label[0.65,1;" .. S("(This will override your previous home coordinates!)") .. "]" ..
"button_exit[0.2,2.75;2,1;yes;Yes]" ..
"button_exit[5.6,2.75;2,1;no;No]"
@ -35,34 +35,32 @@ loadhomes()
sethome.set = function(name, pos)
local player = minetest.get_player_by_name(name)
if not player or not pos then
return false
minetest.chat_send_player(player:get_player_name(), "Player not found!")
end
player:set_attribute("sethome:home", minetest.pos_to_string(pos))
minetest.show_formspec(player:get_player_name(), "sethome:sethomedialog", formspec)
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "sethome:sethomedialog" then -- This is your form name
if fields.yes then
player:set_attribute("sethome:home", minetest.pos_to_string(pos))
-- remove `name` from the old storage file
local data = {}
local output = io.open(homes_file, "w")
if output then
homepos[name] = nil
for i, v in pairs(homepos) do
table.insert(data, string.format("%.1f %.1f %.1f %s\n", v.x, v.y, v.z, i))
end
output:write(table.concat(data))
io.close(output)
return true
end
end
-- remove `name` from the old storage file
local data = {}
local output = io.open(homes_file, "w")
if output then
homepos[name] = nil
for i, v in pairs(homepos) do
table.insert(data, string.format("%.1f %.1f %.1f %s\n", v.x, v.y, v.z, i))
end
end)
return true -- if the file doesn't exist - don't return an error.
output:write(table.concat(data))
io.close(output)
end
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "sethome:sethomedialog" then
if fields.yes then
sethome.set(player:get_player_name(), player:get_pos())
minetest.chat_send_player(player:get_player_name(), "Home set!")
end
end
end)
sethome.get = function(name)
local player = minetest.get_player_by_name(name)
local pos = minetest.string_to_pos(player:get_attribute("sethome:home"))
@ -111,9 +109,6 @@ minetest.register_chatcommand("sethome", {
func = function(name)
name = name or "" -- fallback to blank name if nil
local player = minetest.get_player_by_name(name)
if player and sethome.set(name, player:get_pos()) then
return true, S("Home set!")
end
return false, S("Player not found!")
minetest.show_formspec(player:get_player_name(), "sethome:sethomedialog", formspec)
end,
})