From 87f502a2592bbd41e99df9009a966111156f0181 Mon Sep 17 00:00:00 2001 From: Zefram Date: Fri, 13 Jun 2014 15:04:20 +0100 Subject: Show item usages in craft guide When the craft guide is showing a craft, the output slot is now a button, which causes the craft guide to show ways in which that output can be used. This mirrors the way input slots are buttons that show recipes for the selected ingredient. Usages of an item can be iterated through in the same way as recipes for the item. This incidentally offers some ability to retrace one's steps through a crafting chain, without storing actual history. --- api.lua | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 882971e..23dd6d4 100644 --- a/api.lua +++ b/api.lua @@ -42,6 +42,25 @@ minetest.after(0.01, function() end end end + for _, recipes in pairs(unified_inventory.crafts_for.recipe) do + for _, recipe in ipairs(recipes) do + local ingredient_items = {} + for _, spec in ipairs(recipe.items) do + local matches_spec = unified_inventory.canonical_item_spec_matcher(spec) + for _, name in ipairs(unified_inventory.items_list) do + if matches_spec(name) then + ingredient_items[name] = true + end + end + end + for name, _ in pairs(ingredient_items) do + if unified_inventory.crafts_for.usage[name] == nil then + unified_inventory.crafts_for.usage[name] = {} + end + table.insert(unified_inventory.crafts_for.usage[name], recipe) + end + end + end end) @@ -101,10 +120,10 @@ function unified_inventory.register_craft(options) if options.type == "normal" and options.width == 0 then options = { type = "shapeless", items = options.items, output = options.output, width = 0 } end - if unified_inventory.crafts_table[itemstack:get_name()] == nil then - unified_inventory.crafts_table[itemstack:get_name()] = {} + if unified_inventory.crafts_for.recipe[itemstack:get_name()] == nil then + unified_inventory.crafts_for.recipe[itemstack:get_name()] = {} end - table.insert(unified_inventory.crafts_table[itemstack:get_name()],options) + table.insert(unified_inventory.crafts_for.recipe[itemstack:get_name()],options) end -- cgit v1.2.3