Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:InfoboxItem

From Vault Hunters Official Wiki
Revision as of 07:56, 13 July 2025 by Mnooseman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision β†’ (diff)

Documentation for this module may be created at Module:InfoboxItem/doc

local p = {}
local yesno = require('Module:Yesno')

local function getImage(frame, baseName)
	local png = 'File:' .. baseName .. '.png'
	local gif = 'File:' .. baseName .. '.gif'

	local existsPng = frame:preprocess('{{#ifexist:' .. png .. '|yes|no}}')
	if existsPng == 'yes' then return png end

	local existsGif = frame:preprocess('{{#ifexist:' .. gif .. '|yes|no}}')
	if existsGif == 'yes' then return gif end

	return nil
end

function p.main(frame)
    
	local args = frame:getParent().args
	local title = mw.title.getCurrentTitle().text

	local name = args.name or title
	local baseImageName = args.image or ('Invicon_' .. title)
	local imageFile = getImage(frame, baseImageName)
	local craftable = yesno(args.craftable, false)
	local lootable = yesno(args.lootable, false)
    local lootContainer = args.lootcontainer or 'Unknown'
    local rarity = args.rarity or 'Unknown'
    local rarityOutput = frame:expandTemplate{ title = 'Rarity', args = {rarity} }

	-- Extract crafting table arguments
	local craftArgs = {}
	local hasInput = false
	local hasOutput = args.Output and args.Output ~= ''

	for k, v in pairs(args) do
		if type(k) == "string" and mw.ustring.match(k, '^[ABC][123]$') and v and v ~= '' then
			craftArgs[k] = v
			hasInput = true
		end
	end

	if hasOutput then
		craftArgs["Output"] = args.Output
	end

	local html = mw.html.create('table')
	html
		:addClass('infobox')
		:addClass('infobox-item')
        :addClass('floatright')
        :css('width', '300px')
		:tag('tr')
			:tag('th')
				:attr('colspan', '2')
				:addClass('infobox-header')
				:wikitext(name)
			:done()
		:done()

	-- Main block image
	html
		:tag('tr')
			:tag('td')
				:attr('colspan', '2')
				:css('text-align', 'center')
                :css('image-rendering', 'pixelated')
				:wikitext(imageFile and ('[[' .. imageFile .. '|center|frameless|128px]]') or 'No image available')
			:done()
		:done()

	-- Inventory slot under image
	html
		:tag('tr')
			:tag('td')
				:attr('colspan', '2')
				:css('text-align', 'center')
				:wikitext(frame:expandTemplate{title = 'Inventory slot', args = {name}})
			:done()
		:done()

	-- Crafting recipe header and grid
	if hasInput and hasOutput and craftable then
		html
            :tag('tr')
			    :tag('td'):wikitext('Can be crafted'):done()
			    :tag('td'):wikitext(craftable and 'Yes' or 'No'):done()
		    :done()
			:tag('tr')
				:tag('td')
					:attr('colspan', '2')
                    :addClass('crafting-caption')
					:wikitext('Crafting Recipe')
				:done()
			:done()
			:tag('tr')
				:tag('td')
					:attr('colspan', '2')
					:css('text-align', 'center')
					:wikitext(frame:expandTemplate{title = 'Crafting table', args = craftArgs})
				:done()
			:done()
	end

	-- Craftable, Lootable, Loot Container and Rarity
	html
		:tag('tr')
			:tag('td'):wikitext('Can be looted'):done()
			:tag('td'):wikitext(lootable and 'Yes' or 'No'):done()
		:done()
        if lootable then
          html
            :tag('tr')
                :tag('td'):wikitext('Found in'):done()
                :tag('td'):wikitext('[[' .. lootContainer .. ']]'):done()
            :done()
        end
    html
        :tag('tr')
            :tag('td'):wikitext('Rarity'):done()
            :tag('td'):wikitext(rarityOutput):done()
        :done()

	return tostring(html)
end

return p