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)
|
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)
|
meta:set_string("waypoint_compass:label", label)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -31,7 +32,13 @@ end
|
|||||||
|
|
||||||
|
|
||||||
local function get_compass_meta_pos(meta)
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -110,6 +117,11 @@ end
|
|||||||
|
|
||||||
local function show_hud_waypoint(player, compass_item_meta)
|
local function show_hud_waypoint(player, compass_item_meta)
|
||||||
local player_name = player:get_player_name()
|
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
|
-- Show this waypoint
|
||||||
local waypoint_name = get_compass_meta_label(compass_item_meta)
|
local waypoint_name = get_compass_meta_label(compass_item_meta)
|
||||||
local waypoint_color = get_compass_meta_color(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",
|
text = "m",
|
||||||
precision= compass_precision,
|
precision= compass_precision,
|
||||||
number = waypoint_color,
|
number = waypoint_color,
|
||||||
world_pos = get_compass_meta_pos(compass_item_meta),
|
world_pos = waypoint_pos,
|
||||||
})
|
})
|
||||||
-- store HUD elemnt id to remove it later
|
-- store HUD elemnt id to remove it later
|
||||||
if not player_waypoints[player_name] then
|
if not player_waypoints[player_name] then
|
||||||
player_waypoints[player_name] = {}
|
player_waypoints[player_name] = {}
|
||||||
end
|
end
|
||||||
--print("hud_id add", hud_id)
|
--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
|
player_waypoints[player_name].hud_id = hud_id
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -143,8 +155,8 @@ local function update_hud_waypoint(player, itemstack, force)
|
|||||||
local meta = itemstack:get_meta()
|
local meta = itemstack:get_meta()
|
||||||
local waypoint_pos = get_compass_meta_pos(meta)
|
local waypoint_pos = get_compass_meta_pos(meta)
|
||||||
-- remove different waypoint if it exists
|
-- remove different waypoint if it exists
|
||||||
if player_waypoints[player_name] or force and
|
if player_waypoints[player_name] and waypoint_pos and
|
||||||
(player_waypoints[player_name].pos ~= waypoint_pos) then
|
((player_waypoints[player_name].pos ~= waypoint_pos) or force) then
|
||||||
hide_hud_waypoint(player)
|
hide_hud_waypoint(player)
|
||||||
end
|
end
|
||||||
-- show new waypoint
|
-- show new waypoint
|
||||||
@ -194,6 +206,7 @@ local function coords_from_string(str)
|
|||||||
return {x = tmp[1], y = tmp[2], z = tmp[3]}
|
return {x = tmp[1], y = tmp[2], z = tmp[3]}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local compass_dialog_context = {}
|
local compass_dialog_context = {}
|
||||||
|
|
||||||
local function show_basic_dialog(itemstack, player)
|
local function show_basic_dialog(itemstack, player)
|
||||||
@ -224,15 +237,14 @@ local function compass_use_callback(itemstack, user, pointed_thing)
|
|||||||
end
|
end
|
||||||
local meta = itemstack:get_meta()
|
local meta = itemstack:get_meta()
|
||||||
local owner = get_compass_meta_owner(meta)
|
local owner = get_compass_meta_owner(meta)
|
||||||
print("owner", owner)
|
local player_name = user:get_player_name()
|
||||||
if owner == "" then
|
if owner == "" then
|
||||||
-- set first user as owner
|
-- set first user as owner
|
||||||
print("set owner", user:get_player_name())
|
set_compass_meta_owner(meta, player_name)
|
||||||
set_compass_meta_owner(meta, user:get_player_name())
|
elseif owner ~= player_name then
|
||||||
elseif owner ~= user:get_player_name() then
|
|
||||||
-- already has owner
|
-- already has owner
|
||||||
-- TODO show message "You are not the owner" (or maybe limit editing to color change?)
|
-- 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
|
return itemstack
|
||||||
end
|
end
|
||||||
if user:get_player_control()["sneak"] then
|
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()]
|
local compass_item = compass_dialog_context[player:get_player_name()]
|
||||||
if not compass_item then
|
if not compass_item then
|
||||||
-- should not happen normally
|
-- 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
|
return
|
||||||
end
|
end
|
||||||
local meta = compass_item:get_meta()
|
local meta = compass_item:get_meta()
|
||||||
|
|
||||||
local color = tonumber("0x"..fields.color, 16)
|
local color = tonumber("0x"..fields.color, 16)
|
||||||
if color then
|
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)
|
set_compass_meta_color(meta, color)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -281,7 +296,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||||||
local success = player:set_wielded_item(compass_item)
|
local success = player:set_wielded_item(compass_item)
|
||||||
if not success then
|
if not success then
|
||||||
-- no idea why this can happen
|
-- 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
|
end
|
||||||
-- assume dialog is closed and reset context
|
-- assume dialog is closed and reset context
|
||||||
compass_dialog_context[player:get_player_name()] = nil
|
compass_dialog_context[player:get_player_name()] = nil
|
||||||
@ -293,7 +310,7 @@ end)
|
|||||||
minetest.register_tool(
|
minetest.register_tool(
|
||||||
"waypoint_compass:compass", {
|
"waypoint_compass:compass", {
|
||||||
description = "Waypoint compass\n(sneak+place to set point)",
|
description = "Waypoint compass\n(sneak+place to set point)",
|
||||||
short_description = "Waypoint compass",
|
--short_description = "Waypoint compass",
|
||||||
inventory_image = "Waypoint_compass_inventory_image.png",
|
inventory_image = "Waypoint_compass_inventory_image.png",
|
||||||
wield_scale = {x = 0.5, y = 0.5, z = 0.5},
|
wield_scale = {x = 0.5, y = 0.5, z = 0.5},
|
||||||
range = 2.0, -- TODO what's the good range?
|
range = 2.0, -- TODO what's the good range?
|
||||||
|
Loading…
Reference in New Issue
Block a user