Various fixes

- Include recipes with matching groups in usages
- Fix group_to_item()
- Fix custom recipes not showing in usages
This commit is contained in:
pauloue 2019-01-08 01:25:22 +01:00 committed by Jean-Patrick Guerrero
parent d1ee125a1a
commit 7f6dd430ce

209
init.lua

@ -9,15 +9,12 @@ local datas = {searches = {}}
local progressive_mode = mt.settings:get_bool("craftguide_progressive_mode") local progressive_mode = mt.settings:get_bool("craftguide_progressive_mode")
local sfinv_only = mt.settings:get_bool("craftguide_sfinv_only") local sfinv_only = mt.settings:get_bool("craftguide_sfinv_only")
local get_recipe, get_recipes = mt.get_craft_recipe, mt.get_all_craft_recipes
local get_result, show_formspec = mt.get_craft_result, mt.show_formspec
local reg_items = mt.registered_items local reg_items = mt.registered_items
local get_result = mt.get_craft_result
craftguide.path = mt.get_modpath("craftguide") local show_formspec = mt.show_formspec
-- Intllib -- Intllib
local S = dofile(craftguide.path .. "/intllib.lua") local S = dofile(mt.get_modpath("craftguide") .. "/intllib.lua")
craftguide.intllib = S
-- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage: -- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage:
-- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1 -- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1
@ -43,14 +40,6 @@ local group_stereotypes = {
mesecon_conductor_craftable = "mesecons:wire_00000000_off", mesecon_conductor_craftable = "mesecons:wire_00000000_off",
} }
local function extract_groups(str)
if str:sub(1,6) ~= "group:" then
return
end
return str:sub(7):split(",")
end
local function __func() local function __func()
return debug.getinfo(2, "n").name return debug.getinfo(2, "n").name
end end
@ -88,6 +77,34 @@ craftguide.register_craft({
items = {"default:stone"}, items = {"default:stone"},
}) })
local function get_recipe(output)
local recipe = mt.get_craft_recipe(output)
if recipe.items then
return recipe
end
for i = 1, #craftguide.custom_crafts do
local custom_craft = craftguide.custom_crafts[i]
if custom_craft.output:match("%S*") == output then
return custom_craft
end
end
return {}
end
local function get_recipes(output)
local recipes = mt.get_all_craft_recipes(output) or {}
for i = 1, #craftguide.custom_crafts do
local custom_craft = craftguide.custom_crafts[i]
if custom_craft.output:match("%S*") == output then
recipes[#recipes + 1] = custom_craft
end
end
return recipes
end
local color_codes = { local color_codes = {
red = "#FF0000", red = "#FF0000",
yellow = "#FFFF00", yellow = "#FFFF00",
@ -120,11 +137,11 @@ local function in_table(T)
end end
local function group_to_items(group) local function group_to_items(group)
local items_with_group, counter = {}, 0 local items_with_group, c = {}, 0
for name, def in pairs(reg_items) do for name, def in pairs(reg_items) do
if def.groups[group:sub(7)] then if def.groups[group:sub(7)] then
counter = counter + 1 c = c + 1
items_with_group[counter] = name items_with_group[c] = name
end end
end end
@ -135,23 +152,38 @@ local function item_in_inv(inv, item)
return inv:contains_item("main", item) return inv:contains_item("main", item)
end end
local function group_to_item(item) local function extract_groups(str)
if item:sub(1,6) == "group:" then return str:sub(7):split(",")
local itemsub = item:sub(7)
if group_stereotypes[itemsub] then
item = group_stereotypes[itemsub]
elseif reg_items["default:" .. itemsub] then
item = item:gsub("group:", "default:")
else
for name, def in pairs(reg_items) do
if def.groups[item:match("[^,:]+$")] then
item = name
end
end end
local function item_has_groups(item_groups, groups)
for i = 1, #groups do
local group = groups[i]
if not item_groups[group] then
return
end end
end end
return item:sub(1,6) == "group:" and "" or item return true
end
local function groups_to_item(groups)
if #groups == 1 then
local group = groups[1]
if group_stereotypes[group] then
return group_stereotypes[group]
elseif reg_items["default:" .. group] then
return "default:" .. group
end
end
for name, def in pairs(reg_items) do
if item_has_groups(def.groups, groups) then
return name
end
end
return ""
end end
local function get_tooltip(item, recipe_type, cooktime, groups) local function get_tooltip(item, recipe_type, cooktime, groups)
@ -223,20 +255,21 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
local rows = ceil(maxn(items) / width) local rows = ceil(maxn(items) / width)
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 width > GRID_LIMIT or rows > GRID_LIMIT then
fs[#fs + 1] = fmt("label[%f,%f;%s]", fs[#fs + 1] = fmt("label[%f,%f;%s]",
(iX / 2) - 2, (iX / 2) - 2,
iY + 2.2, iY + 2.2,
S("Recipe is too big to be displayed (@1x@2)", width, rows)) S("Recipe is too big to be displayed (@1x@2)", width, rows))
return concat(fs) return concat(fs)
else end
for i, v in pairs(items) do
for i, item in pairs(items) do
local X = ceil((i - 1) % width + xoffset - width) - local X = ceil((i - 1) % width + xoffset - width) -
(sfinv_only and 0 or 0.2) (sfinv_only and 0 or 0.2)
local Y = ceil(i / width + (iY + 2) - min(2, rows)) local Y = ceil(i / width + (iY + 2) - min(2, rows))
if recipe_type ~= "cooking" and (width > 3 or rows > 3) then if width > 3 or rows > 3 then
BUTTON_SIZE = width > 3 and 3 / width or 3 / rows BUTTON_SIZE = width > 3 and 3 / width or 3 / rows
s_btn_size = BUTTON_SIZE s_btn_size = BUTTON_SIZE
X = BUTTON_SIZE * (i % width) + xoffset - 2.65 X = BUTTON_SIZE * (i % width) + xoffset - 2.65
@ -247,25 +280,28 @@ local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
rightest = X rightest = X
end end
local groups = extract_groups(v) local groups
if item:sub(1,6) == "group:" then
groups = extract_groups(item)
item = groups_to_item(groups)
end
local label = groups and "\nG" or "" local label = groups and "\nG" or ""
local item_r = group_to_item(v) local tltp = get_tooltip(item, recipe_type, cooktime, groups)
local tltp = get_tooltip(item_r, recipe_type, cooktime, groups)
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]", fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
X, X,
Y + (sfinv_only and 0.7 or 0.2), Y + (sfinv_only and 0.7 or 0.2),
BUTTON_SIZE, BUTTON_SIZE,
BUTTON_SIZE, BUTTON_SIZE,
item_r, item,
item_r:match("%S*"), item:match("%S*"),
label) label)
fs[#fs + 1] = tltp fs[#fs + 1] = tltp
end end
BUTTON_SIZE = 1.1 BUTTON_SIZE = 1.1
end
local custom_recipe = craftguide.craft_types[recipe_type] local custom_recipe = craftguide.craft_types[recipe_type]
if recipe_type == "cooking" or custom_recipe or if recipe_type == "cooking" or custom_recipe or
@ -442,7 +478,7 @@ local show_fs = function(player, player_name)
end end
local function recipe_in_inv(inv, item_name, recipes_f) local function recipe_in_inv(inv, item_name, recipes_f)
local recipes = recipes_f or get_recipes(item_name) or {} local recipes = recipes_f or get_recipes(item_name)
local show_item_recipes = {} local show_item_recipes = {}
for i = 1, #recipes do for i = 1, #recipes do
@ -525,54 +561,17 @@ local function init_datas(user, name)
end end
end end
local function add_custom_recipes(item, recipes)
for j = 1, #craftguide.custom_crafts do
local craft = craftguide.custom_crafts[j]
if craft.output:match("%S*") == item then
recipes[#recipes + 1] = {
type = craft.type,
width = craft.width,
items = craft.items,
output = craft.output,
}
end
end
return recipes
end
local function get_init_items() local function get_init_items()
local items_list, c = {}, 0 local items_list, c = {}, 0
local function list(name)
c = c + 1
items_list[c] = name
end
for name, def in pairs(reg_items) do for name, def in pairs(reg_items) do
local is_fuel = get_fueltime(name) > 0 local is_fuel = get_fueltime(name) > 0
if (not (def.groups.not_in_craft_guide == 1 or if not (def.groups.not_in_craft_guide == 1 or
def.groups.not_in_creative_inventory == 1)) and def.groups.not_in_creative_inventory == 1) and
(get_recipe(name).items or is_fuel) and (get_recipe(name).items or is_fuel) and
def.description and def.description ~= "" then def.description and def.description ~= "" then
list(name) c = c + 1
end items_list[c] = name
end
for i = 1, #craftguide.custom_crafts do
local craft = craftguide.custom_crafts[i]
local output = craft.output:match("%S*")
local listed
for j = 1, #items_list do
local listed_item = items_list[j]
if output == listed_item then
listed = true
break
end
end
if not listed then
list(output)
end end
end end
@ -580,31 +579,35 @@ local function get_init_items()
datas.init_items = items_list datas.init_items = items_list
end end
mt.register_on_mods_loaded(function() mt.register_on_mods_loaded(get_init_items)
get_init_items()
end) local function item_in_recipe(item, recipe)
local item_groups = reg_items[item].groups
for _, recipe_item in pairs(recipe.items) do
if recipe_item == item then
return true
elseif recipe_item:sub(1,6) == "group:" then
local groups = extract_groups(recipe_item)
if item_has_groups(item_groups, groups) then
return true
end
end
end
end
local function get_item_usages(item) local function get_item_usages(item)
local usages = {} local usages = {}
for name, def in pairs(reg_items) do for name, def in pairs(reg_items) do
if not (def.groups.not_in_craft_guide == 1 or if not (def.groups.not_in_craft_guide == 1 or
def.groups.not_in_creative_inventory == 1) and def.groups.not_in_creative_inventory == 1) and
get_recipe(name).items and def.description and def.description ~= "" then get_recipe(name).items and
def.description and def.description ~= "" then
local recipes = get_recipes(name) local recipes = get_recipes(name)
for i = 1, #recipes do for i = 1, #recipes do
local recipe = recipes[i] local recipe = recipes[i]
local items = recipe.items if item_in_recipe(item, recipe) then
usages[#usages + 1] = recipe
for j = 1, #items do
if items[j] == item then
usages[#usages + 1] = {
type = recipe.type,
items = items,
width = recipe.width,
output = recipe.output,
}
break
end
end end
end end
end end
@ -672,13 +675,8 @@ 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:find("%s") then
item = item:match("%S*")
end
local is_fuel = get_fueltime(item) > 0 local is_fuel = get_fueltime(item) > 0
local recipes = get_recipes(item) or {} local recipes = get_recipes(item)
recipes = add_custom_recipes(item, recipes)
local no_recipes = not next(recipes) local no_recipes = not next(recipes)
if no_recipes and not is_fuel then if no_recipes and not is_fuel then
@ -877,8 +875,7 @@ if not progressive_mode then
reset_datas(data) reset_datas(data)
local is_fuel = get_fueltime(node_name) > 0 local is_fuel = get_fueltime(node_name) > 0
local recipes = get_recipes(node_name) or {} local recipes = get_recipes(node_name)
recipes = add_custom_recipes(node_name, recipes)
local no_recipes = not next(recipes) local no_recipes = not next(recipes)
if no_recipes and not is_fuel then if no_recipes and not is_fuel then