Module:Crafts/ingredient

From Terraria Mods Wiki
Jump to navigation Jump to search
Lua.svg Documentation The documentation below is transcluded from Module:Crafts/ingredient/doc. (edit | history)

Internal sub-module pertaining to the set of {{crafts}}.

Normalizes an ingredients input and outputs it in a properly formatted way.


local item = require('Module:Item').go

local metals = {
	['Copper/Tin'] = 1,
	['Silver/Tungsten'] = 1,
	['Gold/Platinum'] = 1,
	['Iron/Lead'] = 1,
	['Cobalt/Palladium'] = 1,
	['Mythril/Orichalcum'] = 1,
	['Adamantite/Titanium'] = 1,
	['Tin/Copper'] = 2,
	['Tungsten/Silver'] = 2,
	['Platinum/Gold'] = 2,
	['Lead/Iron'] = 2,
	['Palladium/Cobalt'] = 2,
	['Orichalcum/Mythril'] = 2,
	['Titanium/Adamantite'] = 2,
}

local function split(name)
	local item1a, item1b, item2a, item2b = name:match("^(%S+)%s*(.-)/(%S+)%s*(.-)$")
	if item1a then
		local x = metals[item1a..'/'..item2a]
		if tostring(item1b) == '' and x then
			item1b = item2b
		end
		if x == 2 then
			return item2a..' '..item2b, item1a..' '..item1b
		else
			return item1a..' '..item1b, item2a..' '..item2b
		end
	else
		return name
	end
end

-- main return object 
return {
go = function(frame)
	
	local _input = mw.text.trim(frame.args[1])
	local _basepage = mw.text.trim(frame.args[2])
	local modname = frame:expandTemplate{ title = 'modname', args = { 'get' } }

	local icons

	local result = ''
	for name, amount in _input:gmatch("(.-)¦(.-)¶") do
		local li = ''
		item1, item2 = split(name)
		if item2 then
			li = li .. item(frame, {item1, '%%' .. item1, mod = modname})
			li = li .. " ''or'' " 
			li = li .. item(frame, {item2, '%%' .. item2, mod = modname})
		else 
			li = li .. item(frame, {name, '%%' .. name, mod = modname})
		end
		result = result .. mw.text.tag('li', nil, li .. amount)
	end
	return mw.text.tag('ul', nil, result)

end
}