Formspec marker API fixes

- Fix marker count not properly incrementing
- Fix registration of ids with : in their names
This commit is contained in:
Hugues Ross 2020-04-10 17:19:21 -04:00
parent a964f42270
commit c27d96b89f

View File

@ -134,14 +134,31 @@ function cartographer.register_biome(name, textures, min_height, max_height)
}; };
end end
-- Format marker ids to allow their use as formspec element ids.
-- We're mostly concerned with guarding against the : character because it is
-- common for ids and has an alternate meaning in formspecs.
-- id: The id to format
--
-- Returns the formatted id
local function format_marker_id(id)
return id:gsub(":", "_");
end
-- Register a marker with textures to display -- Register a marker with textures to display
-- id: A string containing the id of the marker -- id: A string containing the id of the marker
-- name: A string containing the displayedname of the marker -- name: A string containing the displayedname of the marker
-- textures: A table of texture names. -- textures: A table of texture names.
-- These should correspond with detail levels, -- These should correspond with detail levels,
-- any detail level past the length of the table will return the last texture -- any detail level past the length of the table will return the last texture
--
-- Returns the final id that the marker was registered under
function cartographer.register_marker(id, name, textures) function cartographer.register_marker(id, name, textures)
if not _cartographer.marker_lookup then if not id then
return nil;
end
id = format_marker_id(id);
if not _cartographer.marker_lookup[id] then
_cartographer.marker_count = _cartographer.marker_count + 1; _cartographer.marker_count = _cartographer.marker_count + 1;
end end
@ -149,6 +166,8 @@ function cartographer.register_marker(id, name, textures)
name = name, name = name,
textures = textures, textures = textures,
}; };
return id;
end end
function cartographer.is_filled(map, x, z) function cartographer.is_filled(map, x, z)
@ -180,6 +199,11 @@ end
-- detail: The detail level -- detail: The detail level
-- Returns a string with a texture name, or nil if no matching marker was found. -- Returns a string with a texture name, or nil if no matching marker was found.
function cartographer.get_marker_texture(id, detail) function cartographer.get_marker_texture(id, detail)
if not id then
return nil;
end
id = format_marker_id(id);
local marker = _cartographer.marker_lookup[id]; local marker = _cartographer.marker_lookup[id];
if marker then if marker then