do not show unset position
This commit is contained in:
parent
cbc5cda07b
commit
e14d942193
43
init.lua
43
init.lua
@ -14,6 +14,7 @@ local compass_range = 180
|
||||
|
||||
|
||||
local function set_compass_meta_label(meta, label)
|
||||
meta:set_string("description", string.format("Waypoint compass to \"%s\"", label))
|
||||
meta:set_string("waypoint_compass:label", label)
|
||||
end
|
||||
|
||||
@ -31,7 +32,13 @@ end
|
||||
|
||||
|
||||
local function get_compass_meta_pos(meta)
|
||||
return minetest.deserialize(meta:get_string("waypoint_compass:position"))
|
||||
local pos_str = meta:get_string("waypoint_compass:position")
|
||||
if pos_str == "" then
|
||||
-- FIXME This is probably bad.
|
||||
return nil
|
||||
else
|
||||
return minetest.deserialize(meta:get_string("waypoint_compass:position"))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -110,6 +117,11 @@ end
|
||||
|
||||
local function show_hud_waypoint(player, compass_item_meta)
|
||||
local player_name = player:get_player_name()
|
||||
local waypoint_pos = get_compass_meta_pos(compass_item_meta)
|
||||
if waypoint_pos == nil then
|
||||
-- do not show unset compass position
|
||||
return
|
||||
end
|
||||
-- Show this waypoint
|
||||
local waypoint_name = get_compass_meta_label(compass_item_meta)
|
||||
local waypoint_color = get_compass_meta_color(compass_item_meta)
|
||||
@ -119,14 +131,14 @@ local function show_hud_waypoint(player, compass_item_meta)
|
||||
text = "m",
|
||||
precision= compass_precision,
|
||||
number = waypoint_color,
|
||||
world_pos = get_compass_meta_pos(compass_item_meta),
|
||||
world_pos = waypoint_pos,
|
||||
})
|
||||
-- store HUD elemnt id to remove it later
|
||||
if not player_waypoints[player_name] then
|
||||
player_waypoints[player_name] = {}
|
||||
end
|
||||
--print("hud_id add", hud_id)
|
||||
player_waypoints[player_name].pos = get_compass_meta_pos(compass_item_meta)
|
||||
player_waypoints[player_name].pos = waypoint_pos
|
||||
player_waypoints[player_name].hud_id = hud_id
|
||||
end
|
||||
|
||||
@ -143,8 +155,8 @@ local function update_hud_waypoint(player, itemstack, force)
|
||||
local meta = itemstack:get_meta()
|
||||
local waypoint_pos = get_compass_meta_pos(meta)
|
||||
-- remove different waypoint if it exists
|
||||
if player_waypoints[player_name] or force and
|
||||
(player_waypoints[player_name].pos ~= waypoint_pos) then
|
||||
if player_waypoints[player_name] and waypoint_pos and
|
||||
((player_waypoints[player_name].pos ~= waypoint_pos) or force) then
|
||||
hide_hud_waypoint(player)
|
||||
end
|
||||
-- show new waypoint
|
||||
@ -194,6 +206,7 @@ local function coords_from_string(str)
|
||||
return {x = tmp[1], y = tmp[2], z = tmp[3]}
|
||||
end
|
||||
|
||||
|
||||
local compass_dialog_context = {}
|
||||
|
||||
local function show_basic_dialog(itemstack, player)
|
||||
@ -224,15 +237,14 @@ local function compass_use_callback(itemstack, user, pointed_thing)
|
||||
end
|
||||
local meta = itemstack:get_meta()
|
||||
local owner = get_compass_meta_owner(meta)
|
||||
print("owner", owner)
|
||||
local player_name = user:get_player_name()
|
||||
if owner == "" then
|
||||
-- set first user as owner
|
||||
print("set owner", user:get_player_name())
|
||||
set_compass_meta_owner(meta, user:get_player_name())
|
||||
elseif owner ~= user:get_player_name() then
|
||||
set_compass_meta_owner(meta, player_name)
|
||||
elseif owner ~= player_name then
|
||||
-- already has owner
|
||||
-- TODO show message "You are not the owner" (or maybe limit editing to color change?)
|
||||
print("You are not the owner. Owner is " .. owner)
|
||||
minetest.chat_send_player(player_name, "You are not the owner of this compass. Owner is " .. owner .. ".")
|
||||
return itemstack
|
||||
end
|
||||
if user:get_player_control()["sneak"] then
|
||||
@ -260,13 +272,16 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
local compass_item = compass_dialog_context[player:get_player_name()]
|
||||
if not compass_item then
|
||||
-- should not happen normally
|
||||
minetest.log("error","[MOD] waypoint_compass: " .. player:get_player_name() .. " Closed compass dialog without opening it?")
|
||||
minetest.log("error","[MOD] waypoint_compass: " ..
|
||||
player:get_player_name() ..
|
||||
" Closed compass dialog without opening it?")
|
||||
return
|
||||
end
|
||||
local meta = compass_item:get_meta()
|
||||
|
||||
local color = tonumber("0x"..fields.color, 16)
|
||||
if color then
|
||||
color = math.max(math.min(color, 0xFFFFFF), 0x0) -- for some reason seems to work fine without this
|
||||
set_compass_meta_color(meta, color)
|
||||
end
|
||||
|
||||
@ -281,7 +296,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
local success = player:set_wielded_item(compass_item)
|
||||
if not success then
|
||||
-- no idea why this can happen
|
||||
minetest.log("error","[MOD] waypoint_compass: " .. player:get_player_name() .. " Failed to swap compass after editing it")
|
||||
minetest.log("error","[MOD] waypoint_compass: " ..
|
||||
player:get_player_name() ..
|
||||
" Failed to swap compass after editing it")
|
||||
end
|
||||
-- assume dialog is closed and reset context
|
||||
compass_dialog_context[player:get_player_name()] = nil
|
||||
@ -293,7 +310,7 @@ end)
|
||||
minetest.register_tool(
|
||||
"waypoint_compass:compass", {
|
||||
description = "Waypoint compass\n(sneak+place to set point)",
|
||||
short_description = "Waypoint compass",
|
||||
--short_description = "Waypoint compass",
|
||||
inventory_image = "Waypoint_compass_inventory_image.png",
|
||||
wield_scale = {x = 0.5, y = 0.5, z = 0.5},
|
||||
range = 2.0, -- TODO what's the good range?
|
||||
|
Loading…
Reference in New Issue
Block a user