Make waypoints optional and use external datastorage mod

This also fixes waypoints not showing up on join.
This commit is contained in:
ShadowNinja 2014-07-06 17:39:20 -04:00
parent 0d777362f3
commit 527812fa96
4 changed files with 126 additions and 189 deletions

View File

@ -1,83 +0,0 @@
datastorage={}
datastorage["!registered_players"]={}
datastorage.save_data = function(table_pointer)
local data = minetest.serialize( datastorage[table_pointer] )
local path = minetest.get_worldpath().."/datastorage_"..table_pointer..".data"
local file = io.open( path, "w" )
if( file ) then
file:write( data )
file:close()
return true
else return nil
end
end
datastorage.load_data = function(table_pointer)
local path = minetest.get_worldpath().."/datastorage_"..table_pointer..".data"
local file = io.open( path, "r" )
if( file ) then
local data = file:read("*all")
datastorage[table_pointer] = minetest.deserialize( data )
file:close()
return true
else return nil
end
end
datastorage.get_container = function (player, key)
local player_name = player:get_player_name()
local container = datastorage[player_name]
if container[key] == nil then
container[key] = {}
end
datastorage.save_data(player_name)
return container[key]
end
-- forced save of all player's data
datastorage.save_container = function (player)
local player_name = player:get_player_name()
datastorage.save_data(player_name)
end
-- Init
if datastorage.load_data("!registered_players") == nil then
datastorage["!registered_players"]={}
datastorage.save_data("!registered_players")
end
minetest.register_on_joinplayer(function(player)
local player_name = player:get_player_name()
local registered = nil
for __,tab in ipairs(datastorage["!registered_players"]) do
if tab["player_name"] == player_name then registered = true break end
end
if registered == nil then
local new={}
new["player_name"]=player_name
table.insert(datastorage["!registered_players"],new)
datastorage[player_name]={}
datastorage.save_data("!registered_players")
datastorage.save_data(player_name)
else
datastorage.load_data(player_name)
end
end
)
minetest.register_on_leaveplayer(function(player)
local player_name = player:get_player_name()
datastorage.save_data(player_name)
datastorage[player_name] = nil
end
)
minetest.register_on_shutdown(function()
for __,tab in ipairs(datastorage["!registered_players"]) do
if datastorage[tab["player_name"]] == nil then break end
datastorage.save_data(tab["player_name"])
end
end
)

View File

@ -1,2 +1,4 @@
creative? creative?
intllib? intllib?
datastorage?

View File

@ -38,7 +38,6 @@ if creative_inventory then
end end
end end
dofile(modpath.."/datastorage.lua")
dofile(modpath.."/group.lua") dofile(modpath.."/group.lua")
dofile(modpath.."/api.lua") dofile(modpath.."/api.lua")
dofile(modpath.."/internal.lua") dofile(modpath.."/internal.lua")
@ -46,4 +45,7 @@ dofile(modpath.."/callbacks.lua")
dofile(modpath.."/register.lua") dofile(modpath.."/register.lua")
dofile(modpath.."/bags.lua") dofile(modpath.."/bags.lua")
dofile(modpath.."/item_names.lua") dofile(modpath.."/item_names.lua")
dofile(modpath.."/waypoints.lua") if minetest.get_modpath("datastorage") then
dofile(modpath.."/waypoints.lua")
end

View File

@ -5,87 +5,104 @@ else
S = function(s) return s end S = function(s) return s end
end end
unified_inventory.hud_colors = { local hud_colors = {
{"#FFFFFF", 0xFFFFFF, S("White")}, {"#FFFFFF", 0xFFFFFF, S("White")},
{"#DBBB00", 0xf1d32c, S("Yellow")}, {"#DBBB00", 0xf1d32c, S("Yellow")},
{"#DD0000", 0xDD0000, S("Red")}, {"#DD0000", 0xDD0000, S("Red")},
{"#2cf136", 0x2cf136, S("Green")}, {"#2cf136", 0x2cf136, S("Green")},
{"#2c4df1", 0x2c4df1, S("Blue")}, {"#2c4df1", 0x2c4df1, S("Blue")},
} }
unified_inventory.hud_colors_max = #unified_inventory.hud_colors
local hud_colors_max = #hud_colors
-- Stores temporary player data (persists until player leaves)
local waypoints_temp = {}
unified_inventory.register_page("waypoints", { unified_inventory.register_page("waypoints", {
get_formspec = function(player) get_formspec = function(player)
local waypoints = datastorage.get_container (player, "waypoints") local player_name = player:get_player_name()
local waypoints = datastorage.get(player_name, "waypoints")
local formspec = "background[0,4.5;8,4;ui_main_inventory.png]" .. local formspec = "background[0,4.5;8,4;ui_main_inventory.png]" ..
"image[0,0;1,1;ui_waypoints_icon.png]" .. "image[0,0;1,1;ui_waypoints_icon.png]" ..
"label[1,0;" .. S("Waypoints") .. "]" "label[1,0;" .. S("Waypoints") .. "]"
-- Tabs buttons: -- Tabs buttons:
local i
for i = 1, 5, 1 do for i = 1, 5, 1 do
formspec = formspec .. formspec = formspec ..
"image_button[0.0," .. 0.2 + i * 0.7 .. ";.8,.8;" .. "image_button[0.0," .. 0.2 + i * 0.7 .. ";.8,.8;" ..
(i == waypoints.selected and "ui_blue_icon_background.png^" or "") .. (i == waypoints.selected and "ui_blue_icon_background.png^" or "") ..
"ui_" .. i .. "_icon.png;" .. "ui_" .. i .. "_icon.png;" ..
"select_waypoint" .. i .. ";]" .. "select_waypoint" .. i .. ";]" ..
"tooltip[select_waypoint" .. i .. ";" .. minetest.formspec_escape(S("Select Waypoint #%d"):format(i)).."]" "tooltip[select_waypoint" .. i .. ";"
.. minetest.formspec_escape(S("Select Waypoint #%d"):format(i)).."]"
end end
i = waypoints.selected local i = waypoints.selected or 1
local waypoint = waypoints[i] or {}
local temp = waypoints_temp[player_name][i] or {}
local default_name = "Waypoint "..i
-- Main buttons: -- Main buttons:
formspec = formspec .. formspec = formspec ..
"image_button[4.5,3.7;.8,.8;".. "image_button[4.5,3.7;.8,.8;"..
"ui_waypoint_set_icon.png;".. "ui_waypoint_set_icon.png;"..
"set_waypoint"..i..";]".. "set_waypoint"..i..";]"..
"tooltip[set_waypoint" .. i .. ";" .. minetest.formspec_escape(S("Set waypoint to current location")).."]" "tooltip[set_waypoint" .. i .. ";"
.. minetest.formspec_escape(S("Set waypoint to current location")).."]"
formspec = formspec .. formspec = formspec ..
"image_button[5.2,3.7;.8,.8;".. "image_button[5.2,3.7;.8,.8;"..
(waypoints[i].active and "ui_on_icon.png" or "ui_off_icon.png")..";".. (waypoint.active and "ui_on_icon.png" or "ui_off_icon.png")..";"..
"toggle_waypoint"..i..";]".. "toggle_waypoint"..i..";]"..
"tooltip[toggle_waypoint" .. i .. ";" .. minetest.formspec_escape(S("Make waypoint "..(waypoints[i].active and "invisible" or "visible"))).."]" "tooltip[toggle_waypoint" .. i .. ";"
.. minetest.formspec_escape(S("Make waypoint "
..(waypoint.active and "invisible" or "visible"))).."]"
formspec = formspec .. formspec = formspec ..
"image_button[5.9,3.7;.8,.8;".. "image_button[5.9,3.7;.8,.8;"..
(waypoints[i].display_pos and "ui_green_icon_background.png" or "ui_red_icon_background.png").."^ui_xyz_icon.png;".. (waypoint.display_pos and "ui_green_icon_background.png" or "ui_red_icon_background.png").."^ui_xyz_icon.png;"..
"toggle_display_pos" .. i .. ";]".. "toggle_display_pos" .. i .. ";]"..
"tooltip[toggle_display_pos" .. i .. ";" .. minetest.formspec_escape(S((waypoints[i].display_pos and "Disable" or "Enable").." display of waypoint coordinates")).."]" "tooltip[toggle_display_pos" .. i .. ";"
.. minetest.formspec_escape(S((waypoint.display_pos and "Disable" or "Enable")
.." display of waypoint coordinates")).."]"
formspec = formspec .. formspec = formspec ..
"image_button[6.6,3.7;.8,.8;".. "image_button[6.6,3.7;.8,.8;"..
"ui_circular_arrows_icon.png;".. "ui_circular_arrows_icon.png;"..
"toggle_color"..i..";]".. "toggle_color"..i..";]"..
"tooltip[toggle_color" .. i .. ";" .. minetest.formspec_escape(S("Change color of waypoint display")).."]" "tooltip[toggle_color" .. i .. ";"
.. minetest.formspec_escape(S("Change color of waypoint display")).."]"
formspec = formspec .. formspec = formspec ..
"image_button[7.3,3.7;.8,.8;".. "image_button[7.3,3.7;.8,.8;"..
"ui_pencil_icon.png;".. "ui_pencil_icon.png;"..
"rename_waypoint"..i..";]".. "rename_waypoint"..i..";]"..
"tooltip[rename_waypoint" .. i .. ";" .. minetest.formspec_escape(S("Edit waypoint name")).."]" "tooltip[rename_waypoint" .. i .. ";"
.. minetest.formspec_escape(S("Edit waypoint name")).."]"
-- Waypoint's info: -- Waypoint's info:
if waypoints[i].active then if waypoint.active then
formspec = formspec .. "label[1,0.8;"..S("Waypoint active").."]" formspec = formspec .. "label[1,0.8;"..S("Waypoint active").."]"
else else
formspec = formspec .. "label[1,0.8;"..S("Waypoint inactive").."]" formspec = formspec .. "label[1,0.8;"..S("Waypoint inactive").."]"
end end
if waypoints[i].edit then if temp.edit then
formspec = formspec .. formspec = formspec ..
"field[1.3,3.2;6,.8;rename_box" .. i .. ";;"..waypoints[i].name.."]" .. "field[1.3,3.2;6,.8;rename_box" .. i .. ";;"
..(waypoint.name or default_name).."]" ..
"image_button[7.3,2.9;.8,.8;".. "image_button[7.3,2.9;.8,.8;"..
"ui_ok_icon.png;".. "ui_ok_icon.png;"..
"confirm_rename"..i.. ";]".. "confirm_rename"..i.. ";]"..
"tooltip[confirm_rename" .. i .. ";" .. minetest.formspec_escape(S("Finish editing")).."]" "tooltip[confirm_rename" .. i .. ";"
.. minetest.formspec_escape(S("Finish editing")).."]"
end end
formspec = formspec .. "label[1,1.3;"..S("World position")..": " .. formspec = formspec .. "label[1,1.3;"..S("World position")..": " ..
minetest.pos_to_string(waypoints[i].world_pos) .. "]" .. minetest.pos_to_string(waypoint.world_pos or vector.new()) .. "]" ..
"label[1,1.8;"..S("Name")..": ".. waypoints[i].name .. "]" .. "label[1,1.8;"..S("Name")..": ".. (waypoint.name or default_name) .. "]" ..
"label[1,2.3;"..S("HUD text color")..": " .. "label[1,2.3;"..S("HUD text color")..": " ..
unified_inventory.hud_colors[waypoints[i].color][3] .. "]" hud_colors[waypoint.color or 1][3] .. "]"
return {formspec=formspec} return {formspec=formspec}
end, end,
@ -97,133 +114,132 @@ unified_inventory.register_button("waypoints", {
tooltip = S("Waypoints"), tooltip = S("Waypoints"),
}) })
unified_inventory.update_hud = function (player, waypoint) local function update_hud(player, waypoints, temp, i)
local waypoint = waypoints[i]
if not waypoint then return end
temp[i] = temp[i] or {}
temp = temp[i]
local pos = waypoint.world_pos or vector.new()
local name local name
if waypoint.display_pos then if waypoint.display_pos then
name = "(".. name = minetest.pos_to_string(pos)
waypoint.world_pos.x .. "," .. if waypoint.name then
waypoint.world_pos.y .. "," .. name = name..", "..waypoint.name
waypoint.world_pos.z .. ")"
if waypoint.name ~= "" then
name = name .. ", " ..
waypoint.name
end end
else else
name = waypoint.name name = waypoint.name or "Waypoint "..i
end end
if waypoint.hud then if temp.hud then
player:hud_remove(waypoint.hud) player:hud_remove(temp.hud)
end end
if waypoint.active then if waypoint.active then
waypoint.hud = player:hud_add({ temp.hud = player:hud_add({
hud_elem_type = "waypoint", hud_elem_type = "waypoint",
number = unified_inventory.hud_colors[waypoint.color][2] , number = hud_colors[waypoint.color or 1][2] ,
name = name, name = name,
text = "m", text = "m",
world_pos = waypoint.world_pos world_pos = pos
}) })
else else
waypoint.hud = nil temp.hud = nil
end end
end end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "" then if formname ~= "" then return end
return
end
local player_name = player:get_player_name()
local update_formspec = false local update_formspec = false
local update_hud = false local need_update_hud = false
local hit = false
local waypoints = datastorage.get_container (player, "waypoints") local waypoints = datastorage.get(player_name, "waypoints")
local temp = waypoints_temp[player_name]
for i = 1, 5, 1 do for i = 1, 5, 1 do
if fields["select_waypoint"..i] then if fields["select_waypoint"..i] then
hit = true
waypoints.selected = i waypoints.selected = i
update_formspec = true update_formspec = true
end end
if fields["toggle_waypoint"..i] then if fields["toggle_waypoint"..i] then
hit = true
waypoints[i] = waypoints[i] or {}
waypoints[i].active = not (waypoints[i].active) waypoints[i].active = not (waypoints[i].active)
update_hud = true need_update_hud = true
update_formspec = true update_formspec = true
end end
if fields["set_waypoint"..i] then if fields["set_waypoint"..i] then
hit = true
local pos = player:getpos() local pos = player:getpos()
pos.x = math.floor(pos.x) pos.x = math.floor(pos.x)
pos.y = math.floor(pos.y) pos.y = math.floor(pos.y)
pos.z = math.floor(pos.z) pos.z = math.floor(pos.z)
waypoints[i] = waypoints[i] or {}
waypoints[i].world_pos = pos waypoints[i].world_pos = pos
update_hud = true need_update_hud = true
update_formspec = true update_formspec = true
end end
if fields["rename_waypoint"..i] then if fields["rename_waypoint"..i] then
waypoints[i].edit = true hit = true
waypoints[i] = waypoints[i] or {}
temp[i].edit = true
update_formspec = true update_formspec = true
end end
if fields["toggle_display_pos"..i] then if fields["toggle_display_pos"..i] then
hit = true
waypoints[i] = waypoints[i] or {}
waypoints[i].display_pos = not waypoints[i].display_pos waypoints[i].display_pos = not waypoints[i].display_pos
update_hud = true need_update_hud = true
update_formspec = true update_formspec = true
end end
if fields["toggle_color"..i] then if fields["toggle_color"..i] then
local color = waypoints[i].color hit = true
waypoints[i] = waypoints[i] or {}
local color = waypoints[i].color or 1
color = color + 1 color = color + 1
if color > unified_inventory.hud_colors_max then if color > hud_colors_max then
color = 1 color = 1
end end
waypoints[i].color = color waypoints[i].color = color
update_hud = true need_update_hud = true
update_formspec = true update_formspec = true
end end
if fields["confirm_rename"..i] then if fields["confirm_rename"..i] then
waypoints[i].edit = false hit = true
waypoints[i] = waypoints[i] or {}
temp[i].edit = false
waypoints[i].name = fields["rename_box"..i] waypoints[i].name = fields["rename_box"..i]
update_hud = true need_update_hud = true
update_formspec = true update_formspec = true
end end
if need_update_hud then
if update_hud then update_hud(player, waypoints, temp, i)
unified_inventory.update_hud (player, waypoints[i])
end end
if update_formspec then if update_formspec then
unified_inventory.set_inventory_formspec(player, "waypoints") unified_inventory.set_inventory_formspec(player, "waypoints")
end end
if hit then return end
end end
end) end)
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local waypoints = datastorage.get_container (player, "waypoints") local player_name = player:get_player_name()
local need_save = false local waypoints = datastorage.get(player_name, "waypoints")
-- Create new waypoints data local temp = {}
for i = 1, 5, 1 do waypoints_temp[player_name] = temp
if waypoints[i] == nil then for i = 1, 5 do
need_save = true update_hud(player, waypoints, temp, i)
waypoints[i] = {
edit = false,
active = false,
display_pos = true,
color = 1,
name = "Waypoint ".. i,
world_pos = {x = 0, y = 0, z = 0},
}
end end
end
if need_save then datastorage.save_container (player) end
-- Initialize waypoints
minetest.after(0.5, function()
waypoints.selected = 1
for i = 1, 5, 1 do
waypoints[i].edit = false
unified_inventory.update_hud (player, waypoints[i])
end
end)
end) end)
minetest.register_on_leaveplayer(function(player)
waypoints_temp[player:get_player_name()] = nil
end)