Module:RecipeUsage

local p = {} local cargo = mw.ext.cargo

local dpl_argList = { 'product_count', 'product_name', 'base_energy', 'base_time', 'ingredient1_name', 'ingredient1_count', 'ingredient2_name', 'ingredient2_count', 'ingredient3_name', 'ingredient3_count', 'ingredient4_name', 'ingredient4_count' }

--Creates crafting table for given recipes -- function createTable(recipes) local table = "" for i=1,#recipes do   if i == #recipes then recipes[i].foot = 1 end table = table .. mw.getCurrentFrame:expandTemplate{title='CraftingTable', args=recipes[i]} end return table end

--Extracts the anonymous pipe-delimited arguments from the   DPL query into a table with the corresponding keys, skipping    templates with `ignoreusage` set, and skipping duplicate templates    copied from Minecraft Gamepedia -- local extractArgs do local seen = {} extractArgs = function( template ) -- Check for duplicate template if seen[template] then return end

seen[template] = true

local tArgs = {} local i = 1 for arg in mw.text.gsplit( template, '\n|' ) do     if arg ~= '' then tArgs[dpl_argList[i]] = arg end i = i + 1 end return tArgs end end

--Filters recipes containig ingredient from recipeTable -- function filterCraftingRecipes(recipeTable, ingredient) local filtered = {} for i=1,#recipeTable do   local recipe = recipeTable[i] if recipe.ingredient1_name == ingredient or recipe.ingredient2_name == ingredient or       recipe.ingredient3_name == ingredient or recipe.ingredient4_name == ingredient then filtered[#filtered + 1] = recipe end end return filtered end

--Performs the DPL query which retrieves the arguments from the crafting   templates on the pages within the requested categories    copied from Minecraft Gamepedia -- function dplQuery return mw.getCurrentFrame:callParserFunction( '#dpl:', {   category = 'Items',    include = '{CraftingTable}:' .. table.concat( dpl_argList, ':' ),    ordermethod = 'title',    mode = 'userformat',    secseparators = '====',    multisecseparators = '===='  } ) end

p.usage = function(f) local data = dplQuery

local recipes = {} for templateArgs in mw.text.gsplit( data:sub( 5 ), '====' ) do   local tArgs = extractArgs( templateArgs ) if tArgs ~= nil then tArgs.structure = "hidden" recipes[#recipes + 1] = tArgs end end

local ingredient = mw.title.getCurrentTitle.text recipes = filterCraftingRecipes(recipes, ingredient)

return createTable(recipes) end

p.table = function(f) local ingredient = mw.title.getCurrentTitle.text

local table = 'crafting_recipes' local fields = 'product_name, product_count, ingredient1_name, ingredient1_count, ingredient2_name, ingredient2_count, ingredient3_name, ingredient3_count, ingredient4_name, ingredient4_count, base_energy, base_time' local args = { where = 'ingredient1_name="' .. ingredient .. '" OR ingredient2_name="' .. ingredient .. '" OR ingredient3_name="' .. ingredient .. '" OR ingredient4_name="' .. ingredient .. '"', orderBy = 'product_name ASC' }

local recipes = cargo.query(table, fields, args)

return createTable(recipes) end

return p