Module:Item

local getArgs = require('Module:Arguments').getArgs local string = require('Module:String') local setColor = require('Module:Color')._colorTemplates local addThousandSep = string._formatNum local yesno = require('Module:Yesno') local makeRarity = require('Module:RarityTier')._link local makeCurrency = require('Module:Currency')._currency local makeEnchant = require('Module:Link')._enchantmentsLink local lang = mw.getContentLanguage

local p = {} --- -- Template: Resource Display -- -- Displays item and its amount, alongside its image, drop chance, rarity, enchant, etc --- function p.resourceDisplay(frame) local args = getArgs(frame) local str = args[1] local img = yesno(args["image"] or args["img"] or args["i"], true) local nolink = yesno(args["nolink"] or args["nl"], false) local noErr = yesno(args["noerror"] or args["ne"], false) return string.pcall(p._item, str, img, nolink, noErr, false) end --- -- Template: Item Display -- -- Displays only item, alongside its image, drop chance, rarity, enchant, etc --- function p.itemDisplay(frame) local args = getArgs(frame) local str = args[1] local img = yesno(args["image"] or args["img"] or args["i"], true) local nolink = yesno(args["nolink"] or args["nl"], false) local noErr = yesno(args["noerror"] or args["ne"], false) return string.pcall(p._item, str, img, nolink, noErr, true) end --- -- Functions for use in other modules --- function p._resourceDisplay(str, img, nolink, noErr) return p._item(str, img ~= nil and img or true, nolink, noErr, false) end function p._itemDisplay(str, img, nolink, noErr) return p._item(str, img ~= nil and img or true, nolink, noErr, true) end

--Module Access Point function p._item(str, img, nolink, noErr, ignoreAmount) local num = nil local num2 = nil --remove commas, they break stuff str = str:gsub(",", "") --determine whether or not the hover text should be displayed local no_hover = str:match('%d+%-+%d+') and true or false if str:match('%s*[^\\];%s*(.-)$') then str, after = str:match('^(.+)%s*;%s*(.-)$') end

--separate item and quantity + handle invalid syntax if not ignoreAmount then if str:match('([%d,%-%?]+%.?[%d,%-]*x? ?or ?[%d,%-%?]+%.?[%d,%-]*x?) (.*)') then num, item = str:match('([%d,%-%?]+%.?[%d,%-]*x? ?or ?[%d,%-%?]+%.?[%d,%-]*x?) (.*)') elseif str:match('([%d,%-%?]+%.?[%d,%-]*x?) (.*)') then num, item = str:match('([%d,%-%?]+%.?[%d,%-]*x?) (.*)') elseif noErr then return str else return string.error('Invalid Resource list syntax "%s"', str) end if num:match('or') then local tempStr = num num, num2 = tempStr:match('(.+) ?or ?(.+)') end --handle colors (green = standard, red = only when number is "?") text_color = "Green" if num:match("%?") or (num2 and num2:match('%?')) then no_hover = true text_color = "Red" end --remove unnecessary characters from number num = num:gsub('[x,]', '') if num2 then num2 = num2:gsub('[x,]', '')end else if str:match('^[%d,%-%?]+%.?[%d,%-]*x? ?or ?[%d,%-%?]+%.?[%d,%-]*x? (.*)') then item = str:match('^[%d,%-%?]+%.?[%d,%-]*x? ?or ?[%d,%-%?]+%.?[%d,%-]*x? (.*)') elseif str:match('^[%d,%-%?]+%.?[%d,%-]*x? (.*)') then item = str:match('^[%d,%-%?]+%.?[%d,%-]*x? (.*)') else item = str end end --remove unnecessary characters from item item = item:gsub('[%[%]]', '') --unpack and process aliases local aliases = require('Module:Item/Aliases') item = ' '..item..' ' for pattern, replace in pairs(aliases) do		if item:match(pattern) then item = item:gsub(pattern, replace) end end item = mw.text.trim(item)

local alt = item if item:match('s$') or item:match("[Bb]locks") then alt = item item = item:gsub('s$', ''):gsub("[Bb]locks", "Block") end --generate alternate text if item:match('%|') then item, alt = item:match('(.*)%|(.*)') elseif item:match('[^%%]%!') then item, alt = item:match('(.*)%!(.*)') end --determine whether or not odds should be displayed local odds = nil if item:match(" %%.+%%") then item, odds = item:match("(.*)%s%%(.+)%%") end if alt:match(" %%.+%%") then alt, odds = alt:match("(.*)%s%%(.+)%%") end --determine whether or not enchant should be displayed local enchant = nil if item:match(" &.+&") then item, enchant = item:match("(.*)%s&(.+)&") end if alt:match(" &.+&") then alt, enchant = alt:match("(.*)%s&(.+)&") end --change the $rarity$ to actual rarity template alt = alt:gsub(		"(.*)$(.+)$(.*)", 		function(a, b, c) 			return table.concat{				a, 				makeRarity(b, nil, nil, true), 				c,			} 		end	) item = item:gsub("(.*)%s?$.+$%s?(.*)", "%1%2")

item = mw.text.trim(item) --convert armor piece string to functional string, pick the proper image (helmet) local item_img = item if item:match('(.*) [Pp][Ii][Ee][Cc][Ee]') then item = item:match('(.*) [Pp][Ii][Ee][Cc][Ee]') if item:match('(.*) [Aa][Rr][Mm][Oo][Rr]') then item_img = item:match('(.*) [Aa][Rr][Mm][Oo][Rr]')..' Helmet' else item_img = item..' Helmet' end elseif item:match('(.+) [Aa][Rr][Mm][Oo][Rr]') then item = item:match('(.+) [Aa][Rr][Mm][Oo][Rr]') item_img = item..' Helmet' end --convert minion string to functional string, pick the proper image (tier I minion head) if item:match('Minion [IVXivx]+') then item = item:gsub('(Minion )[IVXivx]+', '%1') elseif item:match('Minion') then item_img = item..' I'	end

local title, title2 --handle the calculations performed on the number, then format it	if not ignoreAmount then if not no_hover then --get amount of stacks and items stacks = math.floor(tonumber(num) / 64) items = tonumber(num) % 64 --add thousand separator to the quantity num = addThousandSep(num) end title = nil if not no_hover then --add hover info about amount in stacks and items if stacks + items * 0.01 > 1 then local str_stacks = lang:plural( stacks, 'stack', 'stacks' ) local str_items = lang:plural( items, 'item', 'items' ) if items > 0 then title = table.concat{ addThousandSep(stacks), ' ',						str_stacks, ' and ', items, ' ',						str_items, ' (',						num,						' items total).', }				else title = table.concat{ addThousandSep(stacks), ' ', 						str_stacks, ' (',						num,						' items total).', }				end end end --same thing as above, but for second number if not no_hover and num2 then --get amount of stacks and items stacks2 = math.floor(tonumber(num2) / 64) items2 = tonumber(num2) % 64 --add thousand separator to the quantity num2 = addThousandSep(num2) end title2 = nil if not no_hover and num2 then --add hover info about amount in stacks and items if stacks2 + items2 * 0.01 > 1 then local str_stacks = lang:plural(stacks2, 'stack', 'stacks') local str_items = lang:plural(items2, 'item', 'items') if items2 > 0 then title2 = table.concat{ addThousandSep(stacks2), ' ',						str_stacks, ' and ', items2, ' ',						str_items, ' (',						num2,						' items total).', }				else title2 = table.concat{ addThousandSep(stacks2), ' ',						str_stacks, ' (',						num2,						' items total).', }				end end end end

--apply formatting local ret = {} if ignoreAmount then ret = { (img and string.makeImage(item_img, { size=21, link=item })..' ' or ''), string.makeLink(item, alt), (enchant and table.concat{				" (", makeEnchant(enchant), ")"			} or ''), (odds and table.concat{				"(", odds, ")"			} or ''), after and ' '..after or '', }	elseif item:lower:find('^%s*coins?%s*$') then ret = { tostring(makeCurrency(ignoreAmount and nil or num, nil, false, false, img, nolink, true, nil, 'coins')) }	elseif item:lower:find('^%s*bits?%s*$') then ret = { tostring(makeCurrency(ignoreAmount and nil or num, nil, false, false, img, nolink, true, nil, 'bits')) }	elseif item:lower:find('^%s*gems?%s*$') then ret = { tostring(makeCurrency(ignoreAmount and nil or num, nil, false, false, img, nolink, true, nil, 'gems')) }	elseif num2 then ret = { img and string.makeImage(					item_img, 					{ size=21, link=item }				)..' ' or '', string.makeTitle(				setColor(text_color, num), 				title, 				{ ignoreTitleNil = true }			), --returns first param if title is nil ' ', setColor(text_color, 'or'), ' ', string.makeTitle(setColor(text_color, num2..'x'), title2, {ignoreTitleNil = true}), --returns first param if title2 is nil ' ',			string.makeLink(item, alt), enchant and table.concat{ " (", makeEnchant(enchant), ")" } or '', odds and table.concat{ "(", odds, ")" } 			or '', after and ' '..after or '', }	else ret = { (img and string.makeImage(item_img, { size=21, link=item }) or ''), ' ',			string.makeTitle(setColor(text_color, num..'x'), title, {ignoreTitleNil = true}), --returns first param if title is nil ' ',			string.makeLink(item, alt), (enchant and table.concat{				" (", makeEnchant(enchant), ")"			} or ''), (odds and table.concat{				"(", odds, ")"			} or ''), after and ' '..after or '', }	end return (table.concat(ret):gsub('%%(.)', '%1')) end

--Finish Module return p