From 93ee3be6966728a18e51784e0dc989377d4847cf Mon Sep 17 00:00:00 2001 From: Alexander Weber Date: Tue, 28 Feb 2017 16:07:47 +0100 Subject: [PATCH] moved doc integration to doc_addon.lua. Added doc button on survival page --- cache.lua | 3 ++- crafting.lua | 20 +++++++++++++++-- doc_addon.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ filter.lua | 32 --------------------------- init.lua | 1 + 5 files changed, 81 insertions(+), 35 deletions(-) create mode 100644 doc_addon.lua diff --git a/cache.lua b/cache.lua index 5554115..e7bd4f0 100644 --- a/cache.lua +++ b/cache.lua @@ -1,4 +1,5 @@ local filter = smart_inventory.filter +local doc_addon = smart_inventory.doc_addon local cache = {} cache.cgroups = {} @@ -249,7 +250,7 @@ function cache.get_recipes_craftable_atnext(player, item) for recipe_item, itemtab in pairs(cache.crecipes[recipe].recipe_items) do recipe_ok = false for _, itemname in ipairs(itemtab) do - if filter.is_revealed_item(itemname, player) == true then + if doc_addon.is_revealed_item(itemname, player) == true then recipe_ok = true break end diff --git a/crafting.lua b/crafting.lua index 68985be..6255f6b 100644 --- a/crafting.lua +++ b/crafting.lua @@ -1,5 +1,5 @@ local cache = smart_inventory.cache -local filter = smart_inventory.filter +local doc_addon = smart_inventory.doc_addon local ui_tools = smart_inventory.ui_tools ----------------------------------------------------- @@ -39,7 +39,7 @@ local function update_preview(state) if state.param.crafting_items_in_inventory[item_in_list.name] then item = item_in_list.name break - elseif filter.is_revealed_item(item_in_list.name, player) then + elseif doc_addon.is_revealed_item(item_in_list.name, player) then item = item_in_list.name elseif item == nil then item = item_in_list.name @@ -304,6 +304,22 @@ local function crafting_callback(state) smart_inventory.smartfs_elements.craft_preview(state, 6, 0, "craft_preview") inf_state:label(6.7,3,"cr_type", "") inf_state:item_image(10.2,0.3, 1, 1, "craft_result",nil):setVisible(false) + + if smart_inventory.doc_items_mod then + local doc_btn = inf_state:item_image_button(10.2,2.3, 1, 1, "doc_btn","", "doc_identifier:identifier_solid") + doc_btn:setVisible(true) + doc_btn:onClick(function(self, state, player) + local outitem = state:get("craft_result"):getImage() + if outitem then + outitem:gsub("[^%s]+", function(z) + if minetest.registered_items[z] then + doc_addon.show(z, player) + end + end) + end + end) + end + inf_area:setVisible(false) local pr_prev_btn = state:button(6, 3, 1, 0.5, "preview_prev", "<<") diff --git a/doc_addon.lua b/doc_addon.lua new file mode 100644 index 0000000..749ffaf --- /dev/null +++ b/doc_addon.lua @@ -0,0 +1,60 @@ +local filter = smart_inventory.filter +local doc_addon = {} + +function doc_addon.is_revealed_item(itemname, playername) + local cache = smart_inventory.cache + if minetest.registered_items[itemname] == nil then + return false + end + + if smart_inventory.doc_items_mod then + local category_id + if not cache.citems[itemname] then + -- not in creative or something like + return false + else + for _, group in pairs(cache.citems[itemname].cgroups) do + if group.name == "type:node" then + category_id = "nodes" + elseif group.name == "type:tool" then + category_id = "tools" + elseif group.name == "type:craft" then + category_id = "craftitems" + end + end + if category_id then + return doc.entry_revealed(playername, category_id, itemname) + else + -- unknown item + return false + end + end + end + return true +end + + +function doc_addon.show(itemname, playername) + local cache = smart_inventory.cache + if smart_inventory.doc_items_mod then + local category_id + if cache.citems[itemname] then + for _, group in pairs(cache.citems[itemname].cgroups) do + if group.name == "type:node" then + category_id = "nodes" + elseif group.name == "type:tool" then + category_id = "tools" + elseif group.name == "type:craft" then + category_id = "craftitems" + end + end + if category_id then + doc.show_entry(playername, category_id, itemname, true) + end + end + end + return true +end + +------------------------- +return doc_addon diff --git a/filter.lua b/filter.lua index 09b8984..4526bee 100644 --- a/filter.lua +++ b/filter.lua @@ -23,38 +23,6 @@ function filter.register_filter(def) filter.registered_filter[self.name] = self end -function filter.is_revealed_item(itemname, playername) - local cache = smart_inventory.cache - if minetest.registered_items[itemname] == nil then - return false - end - - if smart_inventory.doc_items_mod then - local category_id - if not cache.citems[itemname] then - -- not in creative or something like - return false - else - for _, group in pairs(cache.citems[itemname].cgroups) do - if group.name == "type:node" then - category_id = "nodes" - elseif group.name == "type:tool" then - category_id = "tools" - elseif group.name == "type:craft" then - category_id = "craftitems" - end - end - if category_id then - return doc.entry_revealed(playername, category_id, itemname) - else - -- unknown item - return false - end - end - end - return true -end - filter.register_filter({ name = "transluc", shortdesc = "Translucent blocks", diff --git a/init.lua b/init.lua index 9d46bf1..1f633b7 100644 --- a/init.lua +++ b/init.lua @@ -127,6 +127,7 @@ end -- build up caches smart_inventory.filter = dofile(modpath.."/filter.lua") +smart_inventory.doc_addon = dofile(modpath.."/doc_addon.lua") smart_inventory.cache = dofile(modpath.."/cache.lua") smart_inventory.ui_tools = dofile(modpath.."/ui_tools.lua")