2021-07-04 12:21:13 +00:00
local wea = worldeditadditions
2021-03-20 01:48:56 +00:00
--- Parses a map of key-value pairs into a table.
2021-05-30 00:40:18 +00:00
-- For example, "count 25000 speed 0.8 rate_erosion 0.006 doawesome true" would be parsed into
-- the following table: { count = 25000, speed = 0.8, rate_erosion = 0.006, doawesome = true }.
2021-07-04 12:21:13 +00:00
-- @param params_text string The string to parse.
2021-07-12 23:22:35 +00:00
-- @param keywords string[]? Optional. A list of keywords. Keywords can be present on their own without a value. If found, their value will be automatically set to bool true.
2021-03-20 01:48:56 +00:00
-- @returns table A table of key-value pairs parsed out from the given string.
2021-07-04 12:21:13 +00:00
function worldeditadditions . parse . map ( params_text , keywords )
2021-07-12 23:22:35 +00:00
if not keywords then keywords = { } end
2021-03-20 01:48:56 +00:00
local result = { }
2021-07-04 12:21:13 +00:00
local parts = wea.split ( params_text , " %s+ " , false )
2021-03-20 01:48:56 +00:00
local last_key = nil
2021-07-04 12:21:13 +00:00
local mode = " KEY "
2021-03-20 01:48:56 +00:00
for i , part in ipairs ( parts ) do
2021-07-04 12:21:13 +00:00
if mode == " VALUE " then
2021-03-20 01:48:56 +00:00
-- Try converting to a number to see if it works
local part_converted = tonumber ( part )
2021-07-03 21:53:16 +00:00
if part_converted == nil then part_converted = part end
2021-05-30 00:40:18 +00:00
-- Look for bools
if part_converted == " true " then part_converted = true end
if part_converted == " false " then part_converted = false end
2021-03-20 01:48:56 +00:00
result [ last_key ] = part
2021-07-04 12:21:13 +00:00
mode = " KEY "
2021-03-20 01:48:56 +00:00
else
last_key = part
2021-07-04 12:21:13 +00:00
-- Keyword support
if wea.table . contains ( keywords , last_key ) then
result [ last_key ] = true
else
mode = " VALUE "
end
2021-03-20 01:48:56 +00:00
end
end
return true , result
end