Module:Recipe: Difference between revisions

From Subnautica Wiki
(adding temporary default image handling)
m (forgot to apply the default image to the output)
Line 53: Line 53:
image = dataSN[v[1]].name .. " Icon.png"
image = dataSN[v[1]].name .. " Icon.png"
end
end
output = output .. "[[File:" .. dataSN[v[1]].image .. "|50px]] " .. dataSN[v[1]].name .. " -- x" .. v[2] .. "<br>"
output = output .. "[[File:" .. image .. "|50px]] " .. dataSN[v[1]].name .. " -- x" .. v[2] .. "<br>"
end
end
return output
return output

Revision as of 00:14, 10 June 2022

This module standardizes the display of crafting recipes and makes it easy to display a specified item's recipe or all recipes that use a specified ingredient. See Template:RecipeNew for more information. Thanks to BryghtShadow for helping out with the loops!


--[[
	This module standardizes the display of crafting recipes
	and makes it easy to display a specified item's recipe
	or all recipes that use a specified ingredient.
	Thanks to User:BryghtShadow for helping out with the loops!
--]]

-- <nowiki>
local p = {}
local dataSN = mw.loadData('Module:Recipe/SN')
-- local dataBZ = mw.loadData('Module:Recipe/BZ')
local getArgs = require('Dev:Arguments').getArgs

function p.getItemID(id, data)
	-- removing disambiguations from pagenames
	id = string.gsub(id, " %(Subnautica%)", "")
	id = string.gsub(id, " %(Below Zero%)", "")
	
	-- if item ID is provided directly...
	if data[id] then
		return id
	end
	
	-- ...otherwise try to find by item name
	for k, v in pairs(data) do
		if v.name == id then -- item found
			return k
		end
	end
	
	return nil -- Could not find item
end

function p.SN(frame)
	local args = getArgs(frame)
	local output = ""
	
	-- First, get the ID of the product item provided by template; pagename used by default
	local id = args[1] or mw.title.getCurrentTitle().text
	local itemID = p.getItemID(id, dataSN)
	if not itemID then error("Could not find recipe for item '" .. id .. "'") end
	
	-- Item is found, start processing it
	if dataSN[itemID].original then
		-- If the item is made as part of a different item's recipe
		error ("No standalone recipe for item '" .. itemID .. "' (use '" .. dataSN[itemID].original .. "' instead)")
	else
		if dataSN[itemID].recipe then
			-- Recipe is found, start creating the output
			for i,v in ipairs(dataSN[itemID].recipe) do
				local image = dataSN[v[1]].image
				if not image then
					image = dataSN[v[1]].name .. " Icon.png"
				end
				output = output .. "[[File:" .. image .. "|50px]] " .. dataSN[v[1]].name .. " -- x" .. v[2] .. "<br>"
			end
			return output
		else
			-- If there is no recipe for the item
			error ("Cannot craft item '" .. itemID .. "' (use Template:Uses instead)")
		end
	end
end

return p
-- </nowiki>