Module:Inventory slot: Difference between revisions

From BTA-Mirror
imported>Ttv pedro270707
(Added animated items)
imported>Ttv pedro270707
m (Image now does not link to its page)
Line 58: Line 58:
if subitem:match( '\.gif$' ) or subitem:match( '\.png$' ) then
if subitem:match( '\.gif$' ) or subitem:match( '\.png$' ) then
slot:wikitext( "[[File:" .. subitem .. "|32px]]" )
slot:wikitext( "[[File:" .. subitem .. "|32px|link=]]" )
else
else
slot:wikitext( sprite( { data = "InvSprite", id = subvalue } ) )
slot:wikitext( sprite( { data = "InvSprite", id = subvalue } ) )

Revision as of 16:46, 26 March 2023

This module creates an inventory slot. {{Inventory slot}} should be preferred, rather than directly invoking this module on the article.


local p = {}

function p.createSlot( f )
	local args = f
	if f == mw.getCurrentFrame() then 
		args = require( 'Module:ProcessArgs' ).merge( true )
	else
		f = mw.getCurrentFrame()
	end
	
	local aliases = require( "Module:Inventory slot/aliases" ).aliases
	
	local sprite = require( "Module:Sprite" ).get
	local ids = mw.loadData( "Module:InvSprite" ).ids
	local width = args.width or 32
	local height = args.height or 32
	local item = args.item or ""
	local pageName = mw.title.getCurrentTitle().text
	
	local slotContainerCss = {
		"width: " .. width .. "px",
		"height: " .. height .. "px",
		"position: relative"
	}
	local slotContainer = mw.html.create( "div" )
	slotContainer:addClass( "inventory-slot" )
		:cssText( table.concat( slotContainerCss, ";" ) )
	
	local slotCss = {
		"display: inline-block",
		"width: 32px",
		"height: 32px",
		"position: relative"
	}
	for i, value in ipairs(p.parseString( args.item )) do
		local name = value.name
		local item = value.item
		local count = value.count
		if aliases[item] then
			for j, subvalue in ipairs(aliases[item]) do
				local display = i == j == 1 and "display: inline-block" or "display: none" -- sets display of the first element to `inline-block` and the rest to `none`
				local subname = nil
				local subitem = nil
				local sublink = nil
				if type(subvalue) == "string" then
					subname = subvalue
					subitem = subvalue
				else
					subname = subvalue.name or subvalue.item
					subitem = subvalue.item
				end
				local link = nil
				
				local slot = mw.html.create( "div" )
				slot:addClass( "minecraft-item" )
					:cssText( table.concat( slotCss, ";" ) )
					:attr( "data-mctitle", (name or subname or subitem) )
				
				if subitem:match( '\.gif$' ) or subitem:match( '\.png$' ) then
					slot:wikitext( "[[File:" .. subitem .. "|32px|link=]]" )
				else
					slot:wikitext( sprite( { data = "InvSprite", id = subvalue } ) )
				end
				
				if subitem ~= pageName and subname then
					link = subname
				end
				
				if count then
					local itemCount = mw.html.create( "span" )
					itemCount:cssText( "position: absolute; right: -2px; bottom: -5px; pointer-events: none;" )
						:addClass( "c-f" )
						:addClass( "shadow" )
						:wikitext( count )
					
					slot:node( itemCount )
				end
				
				if link and mw.title.new( link ).exists then
					slotContainer:wikitext( "<span style='" .. display .. "'>" .. "[[" .. link .. "|" .. tostring( slot ) .. "]]" .. "</span>" )
				else
					slotContainer:wikitext( "<span style='" .. display .. "'>" .. tostring( slot ) .. "</span>" )
				end
			end
		else
			local display = i == 1 and "display: inline-block" or "display: none"
			local link = nil
			local slot = mw.html.create( "div" )
			slot:addClass( "minecraft-item" )
				:cssText( table.concat( slotCss, ";" ) )
				:attr( "data-mctitle", (name or item) )
			slot:wikitext( sprite( { data = "InvSprite", id = item } ) )
			if item ~= pageName and item then
				link = item
			end
			
			if count then
				local itemCount = mw.html.create( "span" )
				itemCount:cssText( "position: absolute; right: -2px; bottom: -5px; pointer-events: none;" )
					:addClass( "c-f" )
					:addClass( "shadow" )
					:wikitext( count )
					
				slot:node( itemCount )
			end
			
			if link and mw.title.new( link ).exists then
				slotContainer:wikitext( "<span style='" .. display .. "'>" .. "[[" .. link .. "|" .. tostring( slot ) .. "]]" .. "</span>" )
			else
				slotContainer:wikitext( "<span style='" .. display .. "'>" .. tostring( slot ) .. "</span>" )
			end
		end
	end
	
	return slotContainer
end

function p.parseString(input)
	if input then
	    local strings = {}
	    for str in input:gmatch("[^;]+") do
	        table.insert(strings, str:match("^%s*(.-)%s*$"))
	    end
	    
	    local result = {}
	    
	    for _, str in ipairs(strings) do
	        local name, item, count = str:match("^%[(.-)%](.-),?(%d*)$")
	        if name then
	            table.insert(result, { name = name:gsub("^%s*(.-)%s*$", "%1"), item = item:gsub("^%s*(.-)%s*$", "%1"), count = tonumber(count) })
	        else
	            item, count = str:match("(.-),?(%d*)$")
	            table.insert(result, { name = nil, item = item:gsub("^%s*(.-)%s*$", "%1"), count = tonumber(count) })
	        end
	    end
	    
	    return result
	else
		return {}
	end
end

return p