Code cleaning

This commit is contained in:
Jean-Patrick Guerrero 2019-09-09 21:55:51 +02:00
parent 12e35f80f0
commit 19b8e28afb

@ -130,6 +130,10 @@ local function is_func(x)
return type(x) == "function" return type(x) == "function"
end end
local function is_group(item)
return sub(item, 1, 6) == "group:"
end
local craft_types = {} local craft_types = {}
function craftguide.register_craft_type(name, def) function craftguide.register_craft_type(name, def)
@ -281,7 +285,7 @@ local function groups_item_in_recipe(item, recipe)
local item_groups = def.groups local item_groups = def.groups
for _, recipe_item in pairs(recipe.items) do for _, recipe_item in pairs(recipe.items) do
if sub(recipe_item, 1,6) == "group:" then if is_group(recipe_item) then
local groups = extract_groups(recipe_item) local groups = extract_groups(recipe_item)
if item_has_groups(item_groups, groups) then if item_has_groups(item_groups, groups) then
local usage = copy(recipe) local usage = copy(recipe)
@ -368,6 +372,7 @@ local function get_recipes(item, data, player)
end end
local no_recipes = not recipes or #recipes == 0 local no_recipes = not recipes or #recipes == 0
if no_recipes and not usages then if no_recipes and not usages then
return return
elseif usages and no_recipes then elseif usages and no_recipes then
@ -511,7 +516,7 @@ local function get_recipe_fs(data)
local groups local groups
if sub(item, 1,6) == "group:" then if is_group(item) then
groups = extract_groups(item) groups = extract_groups(item)
item = groups_to_items(groups) item = groups_to_items(groups)
end end
@ -788,12 +793,14 @@ local old_register_craft = core.register_craft
core.register_craft = function(recipe) core.register_craft = function(recipe)
old_register_craft(recipe) old_register_craft(recipe)
local output = recipe.output or (is_str(recipe.recipe) and recipe.recipe or "") local output = recipe.output or
(is_str(recipe.recipe) and recipe.recipe or "")
if output == "" then return end if output == "" then return end
output = output:match(match(output, "%S*")) output = output:match(match(output, "%S*"))
local groups local groups
if sub(output, 1,6) == "group:" then if is_group(output) then
groups = extract_groups(output) groups = extract_groups(output)
output = groups_to_items(groups, true) output = groups_to_items(groups, true)
end end
@ -806,33 +813,32 @@ core.register_craft = function(recipe)
item = current_alias[2] item = current_alias[2]
end end
local rcp = copy(recipe) recipe.items = {}
rcp.items = {}
if rcp.type == "fuel" then if recipe.type == "fuel" then
fuel_cache[item] = rcp fuel_cache[item] = recipe
elseif rcp.type == "cooking" then elseif recipe.type == "cooking" then
rcp.width = rcp.cooktime recipe.width = recipe.cooktime
rcp.cooktime = nil recipe.cooktime = nil
rcp.items[1] = rcp.recipe recipe.items[1] = recipe.recipe
elseif rcp.type == "shapeless" then elseif recipe.type == "shapeless" then
rcp.width = 0 recipe.width = 0
for j = 1, #rcp.recipe do for j = 1, #recipe.recipe do
rcp.items[#rcp.items + 1] = rcp.recipe[j] recipe.items[#recipe.items + 1] = recipe.recipe[j]
end end
else else
rcp.width = get_width(rcp.recipe) recipe.width = get_width(recipe.recipe)
local c = 1 local c = 1
for j = 1, #rcp.recipe do for j = 1, #recipe.recipe do
if rcp.recipe[j] then if recipe.recipe[j] then
for h = 1, rcp.width do for h = 1, recipe.width do
local it = rcp.recipe[j][h] local it = recipe.recipe[j][h]
if it and it ~= "" then if it and it ~= "" then
rcp.items[c] = it recipe.items[c] = it
end end
c = c + 1 c = c + 1
@ -841,10 +847,10 @@ core.register_craft = function(recipe)
end end
end end
if rcp.type ~= "fuel" then if recipe.type ~= "fuel" then
rcp.recipe = nil recipe.recipe = nil
recipes_cache[item] = recipes_cache[item] or {} recipes_cache[item] = recipes_cache[item] or {}
insert(recipes_cache[item], 1, rcp) insert(recipes_cache[item], 1, recipe)
end end
end end
end end
@ -1103,12 +1109,13 @@ if progressive_mode then
local function item_in_inv(item, inv_items) local function item_in_inv(item, inv_items)
local inv_items_size = #inv_items local inv_items_size = #inv_items
if sub(item, 1,6) == "group:" then if is_group(item) then
local groups = extract_groups(item) local groups = extract_groups(item)
for i = 1, inv_items_size do for i = 1, inv_items_size do
local inv_item = reg_items[inv_items[i]] local def = reg_items[inv_items[i]]
if inv_item then
local item_groups = inv_item.groups if def then
local item_groups = def.groups
if item_has_groups(item_groups, groups) then if item_has_groups(item_groups, groups) then
return true return true
end end