Cleanup waypoints code

This commit is contained in:
Jean-Patrick Guerrero 2021-11-08 20:41:47 +01:00
parent 2f612bb5da
commit d9f8b738e5
4 changed files with 32 additions and 33 deletions

View File

@ -77,7 +77,7 @@ i3.files.groups()
i3.files.inventory()
local storage = core.get_mod_storage()
local slz, dslz = i3.get("slz", "dslz")
local slz, dslz, str_to_pos, add_hud_waypoint = i3.get("slz", "dslz", "str_to_pos", "add_hud_waypoint")
i3.data = dslz(storage:get_string "data") or {}
@ -174,15 +174,7 @@ local function init_waypoints(player)
for _, v in ipairs(data.waypoints) do
if not v.hide then
local id = player:hud_add {
hud_elem_type = "waypoint",
name = v.name,
text = " m",
world_pos = v.pos,
number = v.color,
z_index = -300,
}
local id = add_hud_waypoint(player, v.name, str_to_pos(v.pos), v.color)
v.id = id
end
end

View File

@ -493,6 +493,17 @@ local function sort_inventory(player, data)
end
end
local function add_hud_waypoint(player, name, pos, color)
return player:hud_add {
hud_elem_type = "waypoint",
name = name,
text = " m",
world_pos = pos,
number = color,
z_index = -300,
}
end
-------------------------------------------------------------------------------
local _ = {
@ -533,12 +544,15 @@ local _ = {
spawn_item = spawn_item,
clean_name = clean_name,
safe_teleport = safe_teleport,
add_hud_waypoint = add_hud_waypoint,
-- Core functions
clr = core.colorize,
slz = core.serialize,
dslz = core.deserialize,
ESC = core.formspec_escape,
pos_to_str = core.pos_to_string,
str_to_pos = core.string_to_pos,
check_privs = core.check_player_privs,
-- Registered items

View File

@ -267,7 +267,7 @@ local function get_waypoint_fs(fs, data, player, yextra, ctn_len)
fs("tooltip", 0, y, ctn_len - 2.5, 0.65,
fmt("Name: %s\nPosition:%s", clr("#ff0", v.name),
core.pos_to_string(v.pos, 0):sub(2,-2):gsub("(%-*%d+)", clr("#ff0", " %1"))))
v.pos:sub(2,-2):gsub("(%-*%d*%.?%d+)", clr("#ff0", " %1"))))
local del = fmt("waypoint_%u_delete", i)
fs(fmt("style[%s;fgimg=%s;fgimg_hovered=%s;content_offset=0]", del, PNG.trash, PNG.trash_hover))

View File

@ -7,9 +7,10 @@ local fmt, find, match, sub, lower, split = i3.get("fmt", "find", "match", "sub"
local vec_new, vec_eq, vec_round = i3.get("vec_new", "vec_eq", "vec_round")
local sort, copy, insert, remove, indexof = i3.get("sort", "copy", "insert", "remove", "indexof")
local msg, is_fav = i3.get("msg", "is_fav")
local is_group, extract_groups, groups_to_items =
i3.get("is_group", "extract_groups", "groups_to_items")
local msg, is_fav, pos_to_str, str_to_pos, add_hud_waypoint =
i3.get("msg", "is_fav", "pos_to_str", "str_to_pos", "add_hud_waypoint")
local search, get_sorting_idx, sort_inventory, sort_by_category, get_recipes =
i3.get("search", "get_sorting_idx", "sort_inventory", "sort_by_category", "get_recipes")
local show_item, get_stack, clean_name, compressible, check_privs, safe_teleport =
@ -87,7 +88,7 @@ i3.new_tab {
remove(data.waypoints, id)
elseif action == "teleport" then
local pos = vec_new(waypoint.pos)
local pos = vec_new(str_to_pos(waypoint.pos))
safe_teleport(player, pos)
msg(name, fmt("Teleported to %s", clr("#ff0", waypoint.name)))
@ -98,14 +99,8 @@ i3.new_tab {
elseif action == "hide" then
if waypoint.hide then
local new_id = player:hud_add {
hud_elem_type = "waypoint",
name = waypoint.name,
text = " m",
world_pos = waypoint.pos,
number = waypoint.color,
z_index = -300,
}
local new_id = add_hud_waypoint(
player, waypoint.name, str_to_pos(waypoint.pos), waypoint.color)
waypoint.id = new_id
waypoint.hide = nil
@ -166,11 +161,11 @@ i3.new_tab {
return msg(name, "'home' privilege missing")
end
safe_teleport(player, core.string_to_pos(data.home))
safe_teleport(player, str_to_pos(data.home))
msg(name, S"Welcome back home!")
elseif fields.set_home then
data.home = core.pos_to_string(player:get_pos(), 1)
data.home = pos_to_str(player:get_pos(), 1)
elseif sb_inv and sub(sb_inv, 1, 3) == "CHG" then
data.scrbar_inv = tonumber(match(sb_inv, "%d+"))
@ -180,7 +175,7 @@ i3.new_tab {
local pos = player:get_pos()
for _, v in ipairs(data.waypoints) do
if vec_eq(vec_round(pos), vec_round(v.pos)) then
if vec_eq(vec_round(pos), vec_round(str_to_pos(v.pos))) then
return msg(name, "You already set a waypoint at this position")
end
end
@ -192,17 +187,15 @@ i3.new_tab {
end
local color = random(0xffffff)
local id = add_hud_waypoint(player, waypoint, pos, color)
local id = player:hud_add {
hud_elem_type = "waypoint",
name = waypoint,
text = " m",
world_pos = pos,
number = color,
z_index = -300,
}
insert(data.waypoints, {
name = waypoint,
pos = pos_to_str(pos, 1),
color = color,
id = id,
})
insert(data.waypoints, {name = waypoint, pos = pos, color = color, id = id})
data.scrbar_inv = data.scrbar_inv + 1000
end