Use of string.format instead of concat

This commit is contained in:
Jean-Patrick Guerrero 2019-01-02 18:57:35 +01:00
parent 4c02a5e5f5
commit b980dccaee

153
init.lua

@ -24,6 +24,7 @@ craftguide.intllib = S
local remove, maxn, sort, concat = table.remove, table.maxn, table.sort, table.concat local remove, maxn, sort, concat = table.remove, table.maxn, table.sort, table.concat
local vector_add, vector_mul = vector.add, vector.multiply local vector_add, vector_mul = vector.add, vector.multiply
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
local fmt = string.format
local DEFAULT_SIZE = 10 local DEFAULT_SIZE = 10
local MIN_LIMIT, MAX_LIMIT = 10, 12 local MIN_LIMIT, MAX_LIMIT = 10, 12
@ -102,7 +103,7 @@ local function get_fueltime(item)
end end
local function reset_datas(data) local function reset_datas(data)
data.show_usage = false data.show_usage = nil
data.filter = "" data.filter = ""
data.input = nil data.input = nil
data.pagenum = 1 data.pagenum = 1
@ -197,10 +198,16 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
local fs = {} local fs = {}
fs[#fs + 1] = "button[" .. (iX - (sfinv_only and 2.2 or 2.6)) .. "," .. fs[#fs + 1] = fmt("button[%f,%f;%f,%f;%s;%s %u %s %u]",
(iY + (sfinv_only and 3.9 or 3.3)) .. ";2.2,1;alternate;" .. iX - (sfinv_only and 2.2 or 2.6),
(show_usage and S("Usage") or S("Recipe")) .. " " .. iY + (sfinv_only and 3.9 or 3.3),
S("@1 of @2", recipe_num, #recipes) .. "]" 2.2,
1,
"alternate",
show_usage and S("Usage") or S("Recipe"),
recipe_num,
S("of"),
#recipes)
local recipe_type = recipes[recipe_num].type local recipe_type = recipes[recipe_num].type
local items = recipes[recipe_num].items local items = recipes[recipe_num].items
@ -217,8 +224,10 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
local rightest, s_btn_size = 0 local rightest, s_btn_size = 0
if recipe_type ~= "cooking" and (width > GRID_LIMIT or rows > GRID_LIMIT) then if recipe_type ~= "cooking" and (width > GRID_LIMIT or rows > GRID_LIMIT) then
fs[#fs + 1] = "label[" .. ((iX / 2) - 2) .. "," .. (iY + 2.2) .. ";" .. fs[#fs + 1] = fmt("label[%f,%f;%s]",
S("Recipe is too big to be displayed (@1x@2)", width, rows) .. "]" (iX / 2) - 2,
iY + 2.2,
S("Recipe is too big to be displayed (@1x@2)", width, rows))
return concat(fs) return concat(fs)
else else
@ -241,12 +250,18 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
local groups = extract_groups(v) local groups = extract_groups(v)
local label = groups and "\nG" or "" local label = groups and "\nG" or ""
local item_r = group_to_item(v) local item_r = group_to_item(v)
local tltip = get_tooltip(item_r, recipe_type, cooktime, groups) local tltp = get_tooltip(item_r, recipe_type, cooktime, groups)
fs[#fs + 1] = "item_image_button[" .. X .. "," .. fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
(Y + (sfinv_only and 0.7 or 0.2)) .. ";" .. X,
BUTTON_SIZE .. "," .. BUTTON_SIZE .. ";" .. item_r .. Y + (sfinv_only and 0.7 or 0.2),
";" .. item_r:match("%S*") .. ";" .. label .. "]" .. tltip BUTTON_SIZE,
BUTTON_SIZE,
item_r,
item_r:match("%S*"),
label)
fs[#fs + 1] = tltp
end end
BUTTON_SIZE = 1.1 BUTTON_SIZE = 1.1
@ -257,17 +272,22 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
(recipe_type == "normal" and width == 0) then (recipe_type == "normal" and width == 0) then
local icon = recipe_type == "cooking" and "furnace" or "shapeless" local icon = recipe_type == "cooking" and "furnace" or "shapeless"
local coords = (rightest + 1.2) .. "," ..
(iY + (sfinv_only and 2.2 or 1.7)) ..
";0.5,0.5;"
fs[#fs + 1] = "image[" .. coords .. fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
(custom_recipe and custom_recipe.icon or rightest + 1.2,
"craftguide_" .. icon .. ".png^[resize:16x16") .. "]" iY + (sfinv_only and 2.2 or 1.7),
0.5,
0.5,
custom_recipe and custom_recipe.icon or
"craftguide_" .. icon .. ".png^[resize:16x16")
fs[#fs + 1] = "tooltip[" .. coords .. fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]",
(custom_recipe and custom_recipe.description or rightest + 1.2,
recipe_type:gsub("^%l", string.upper)) .. "]" iY + (sfinv_only and 2.2 or 1.7),
0.5,
0.5,
custom_recipe and custom_recipe.description or
recipe_type:gsub("^%l", string.upper))
end end
local output = recipes[recipe_num].output local output = recipes[recipe_num].output
@ -276,32 +296,47 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
local arrow_X = rightest + (s_btn_size or BUTTON_SIZE) local arrow_X = rightest + (s_btn_size or BUTTON_SIZE)
local output_X = arrow_X + 0.9 local output_X = arrow_X + 0.9
fs[#fs + 1] = "image[" .. arrow_X .. "," .. fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
(iY + (sfinv_only and 2.85 or 2.35)) .. arrow_X,
";0.9,0.7;craftguide_arrow.png]" iY + (sfinv_only and 2.85 or 2.35),
0.9,
0.7,
"craftguide_arrow.png")
if output == "BURN" then if output == "BURN" then
fs[#fs + 1] = "image[" .. output_X .. "," .. fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
(iY + (sfinv_only and 2.68 or 2.18)) .. output_X,
";1.1,1.1;craftguide_fire.png]" iY + (sfinv_only and 2.68 or 2.18),
1.1,
1.1,
"craftguide_fire.png")
else else
fs[#fs + 1] = "item_image_button[" .. output_X .. "," .. fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;]",
(iY + (sfinv_only and 2.7 or 2.2)) .. ";" .. output_X,
BUTTON_SIZE .. "," .. BUTTON_SIZE .. ";" .. iY + (sfinv_only and 2.7 or 2.2),
output .. ";" .. output_s .. ";]" BUTTON_SIZE,
BUTTON_SIZE,
output,
output_s)
end end
fs[#fs + 1] = get_tooltip(output_s) fs[#fs + 1] = get_tooltip(output_s)
local output_is_fuel = get_fueltime(output) > 0 local output_is_fuel = get_fueltime(output) > 0
if output_is_fuel then if output_is_fuel then
fs[#fs + 1] = "image[" .. (output_X + 1) .. "," .. fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
(iY + (sfinv_only and 2.83 or 2.33)) .. output_X + 1,
";0.6,0.4;craftguide_arrow.png]" iY + (sfinv_only and 2.83 or 2.33),
0.6,
0.4,
"craftguide_arrow.png")
fs[#fs + 1] = "image[" .. (output_X + 1.6) .. "," .. fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
(iY + (sfinv_only and 2.68 or 2.18)) .. output_X + 1.6,
";0.6,0.6;craftguide_fire.png]" iY + (sfinv_only and 2.68 or 2.18),
0.6,
0.6,
"craftguide_fire.png")
end end
return concat(fs) return concat(fs)
@ -321,7 +356,6 @@ local function get_formspec(player_name)
local fs = {} local fs = {}
if not sfinv_only then if not sfinv_only then
fs[#fs + 1] = "size[" .. (data.iX - 0.35) .. "," .. (iY + 4) .. ";]" fs[#fs + 1] = "size[" .. (data.iX - 0.35) .. "," .. (iY + 4) .. ";]"
fs[#fs + 1] = "background[1,1;1,1;craftguide_bg.png;true]"
fs[#fs + 1] = "tooltip[size_inc;" .. S("Increase window size") .. "]" fs[#fs + 1] = "tooltip[size_inc;" .. S("Increase window size") .. "]"
fs[#fs + 1] = "tooltip[size_dec;" .. S("Decrease window size") .. "]" fs[#fs + 1] = "tooltip[size_dec;" .. S("Decrease window size") .. "]"
fs[#fs + 1] = "image_button[" .. (data.iX * 0.47) .. fs[#fs + 1] = "image_button[" .. (data.iX * 0.47) ..
@ -331,6 +365,7 @@ local function get_formspec(player_name)
end end
fs[#fs + 1] = [[ fs[#fs + 1] = [[
"background[1,1;1,1;craftguide_bg.png;true]"
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;] image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;] image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;]
field_close_on_enter[filter;false] field_close_on_enter[filter;false]
@ -342,8 +377,8 @@ local function get_formspec(player_name)
fs[#fs + 1] = "tooltip[next;" .. S("Next page") .. "]" fs[#fs + 1] = "tooltip[next;" .. S("Next page") .. "]"
fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 2.6 or 3.1)) .. fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 2.6 or 3.1)) ..
",0.12;0.8,0.8;craftguide_prev_icon.png;prev;]" ",0.12;0.8,0.8;craftguide_prev_icon.png;prev;]"
fs[#fs + 1] = "label[" .. (data.iX - (sfinv_only and 1.7 or 2.2)) .. ",0.22;" .. fs[#fs + 1] = "label[" .. (data.iX - (sfinv_only and 1.7 or 2.2)) ..
colorize(data.pagenum) .. " / " .. data.pagemax .. "]" ",0.22;" .. colorize(data.pagenum) .. " / " .. data.pagemax .. "]"
fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 0.7 or 1.2) - fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 0.7 or 1.2) -
(data.iX >= 11 and 0.08 or 0)) .. (data.iX >= 11 and 0.08 or 0)) ..
",0.12;0.8,0.8;craftguide_next_icon.png;next;]" ",0.12;0.8,0.8;craftguide_next_icon.png;next;]"
@ -352,8 +387,10 @@ local function get_formspec(player_name)
local xoffset = data.iX / 2.15 local xoffset = data.iX / 2.15
if not next(data.items) then if not next(data.items) then
fs[#fs + 1] = "label[" .. ((data.iX / 2) - 1) .. fs[#fs + 1] = fmt("label[%f,%f;%s]",
",2;" .. S("No item to show") .. "]" (data.iX / 2) - 1,
2,
S("No item to show"))
end end
local first_item = (data.pagenum - 1) * ipp local first_item = (data.pagenum - 1) * ipp
@ -366,10 +403,13 @@ local function get_formspec(player_name)
local X = i % data.iX local X = i % data.iX
local Y = (i % ipp - X) / data.iX + 1 local Y = (i % ipp - X) / data.iX + 1
fs[#fs + 1] = "item_image_button[" .. fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;]",
(X - (sfinv_only and 0 or (X * 0.05))) .. "," .. X - (sfinv_only and 0 or (X * 0.05)),
Y .. ";" .. BUTTON_SIZE .. "," .. BUTTON_SIZE .. ";" .. Y,
name .. ";" .. name .. "_inv;]" BUTTON_SIZE,
BUTTON_SIZE,
name,
name)
end end
if data.input and reg_items[data.input] then if data.input and reg_items[data.input] then
@ -417,6 +457,7 @@ local function recipe_in_inv(inv, item_name, recipes_f)
end end
end end
end end
if not group_in_inv and not item_in_inv(inv, item) then if not group_in_inv and not item_in_inv(inv, item) then
show_item_recipes[i] = false show_item_recipes[i] = false
end end
@ -631,9 +672,7 @@ local function get_fields(player, ...)
else for item in pairs(fields) do else for item in pairs(fields) do
if item:find(":") then if item:find(":") then
if item:sub(-4) == "_inv" then if item:find("%s") then
item = item:sub(1,-5)
elseif item:find("%s") then
item = item:match("%S*") item = item:match("%S*")
end end
@ -647,12 +686,12 @@ local function get_fields(player, ...)
end end
if item ~= data.input then if item ~= data.input then
data.show_usage = false data.show_usage = nil
else else
data.show_usage = not data.show_usage data.show_usage = not data.show_usage
end end
if not progressive_mode and (is_fuel and no_recipes) then if not progressive_mode and is_fuel and no_recipes then
data.show_usage = true data.show_usage = true
end end
@ -669,7 +708,7 @@ local function get_fields(player, ...)
end end
if not next(data.usages) then if not next(data.usages) then
data.show_usage = false data.show_usage = nil
end end
elseif progressive_mode then elseif progressive_mode then
@ -695,6 +734,7 @@ end
if sfinv_only then if sfinv_only then
sfinv.register_page("craftguide:craftguide", { sfinv.register_page("craftguide:craftguide", {
title = "Craft Guide", title = "Craft Guide",
get = function(self, player, context) get = function(self, player, context)
local player_name = player:get_player_name() local player_name = player:get_player_name()
return sfinv.make_formspec( return sfinv.make_formspec(
@ -703,6 +743,7 @@ if sfinv_only then
get_formspec(player_name) get_formspec(player_name)
) )
end, end,
on_enter = function(self, player, context) on_enter = function(self, player, context)
local player_name = player:get_player_name() local player_name = player:get_player_name()
local data = datas[player_name] local data = datas[player_name]
@ -711,6 +752,7 @@ if sfinv_only then
init_datas(player, player_name) init_datas(player, player_name)
end end
end, end,
on_player_receive_fields = function(self, player, context, fields) on_player_receive_fields = function(self, player, context, fields)
get_fields(player, fields) get_fields(player, fields)
end, end,
@ -757,10 +799,12 @@ else
wall_bottom = {-0.4375, -0.5, -0.3125, 0.4375, -0.4375, 0.3125}, wall_bottom = {-0.4375, -0.5, -0.3125, 0.4375, -0.4375, 0.3125},
wall_side = {-0.5, -0.3125, -0.4375, -0.4375, 0.3125, 0.4375} wall_side = {-0.5, -0.3125, -0.4375, -0.4375, 0.3125, 0.4375}
}, },
on_construct = function(pos) on_construct = function(pos)
local meta = mt.get_meta(pos) local meta = mt.get_meta(pos)
meta:set_string("infotext", S("Crafting Guide Sign")) meta:set_string("infotext", S("Crafting Guide Sign"))
end, end,
on_rightclick = function(pos, node, user, itemstack) on_rightclick = function(pos, node, user, itemstack)
on_use(itemstack, user) on_use(itemstack, user)
end end
@ -794,10 +838,10 @@ else
sfinv_buttons.register_button("craftguide", { sfinv_buttons.register_button("craftguide", {
title = S("Crafting Guide"), title = S("Crafting Guide"),
tooltip = S("Shows a list of available crafting recipes, cooking recipes and fuels"), tooltip = S("Shows a list of available crafting recipes, cooking recipes and fuels"),
image = "craftguide_book.png",
action = function(player) action = function(player)
on_use(nil, player) on_use(nil, player)
end, end,
image = "craftguide_book.png",
}) })
end end
end end
@ -823,7 +867,8 @@ if not progressive_mode then
end end
if not node_name then if not node_name then
return false, colorize("[craftguide] ", "red") .. S("No node pointed") return false, colorize("[craftguide] ", "red") ..
S("No node pointed")
elseif not datas[name] then elseif not datas[name] then
init_datas(player, name) init_datas(player, name)
end end