Minecraft Wiki
Advertisement
[view | edit | history | purge]DocumentationJump to code ↴

This module implements {{sprite}}. It should generally be invoked directly on template pages, rather than using the sprite template.

Parent arguments are automatically merged with directly passed arguments (the latter overwriting the former) and all arguments are normalised to trim whitespace and set empty arguments to nil.

Dependencies

See also

Minecraft
Minecraft (legacy)
Minecraft Dungeons
Other
[view | edit | history | purge]The above documentation is transcluded from Module:Sprite/doc.
local p = {}
function p.base( f )
	local args = f
	if f == mw.getCurrentFrame() then 
		args = require( 'Module:ProcessArgs' ).merge()
	end
	local name = args.name or 'Block'
	local scale = args.scale or 1
	local sheetWidth = ( args.sheetsize or 256 ) * scale
	local size = ( args.size or 16 ) * scale
	local pos = math.abs( args.pos or sheetWidth ) - 1
	local link = args.link or ''
	local align = args.align or 'text-top'
	local tiles = sheetWidth / size
	local class = args.class or ''
	local left = pos % tiles * size
	local top = math.floor( pos / tiles ) * size
	local text = args.text or ''
	local css = args.css or ''
	local styles = {}
	local imgClasses = {
		Block = 1
	}
	
	if not imgClasses[name] then
		table.insert( styles, 'background-image:{{FileUrl|' .. name .. 'Sprite.png}}' )
	end
	
	table.insert( styles, 'background-position:-' .. left .. 'px -' .. top .. 'px' )
	
	if scale ~= 1 then
		table.insert( styles, 'background-size:' .. sheetWidth .. 'px auto' )
	end
	
	if size ~= 16 then
		table.insert( styles, 'height:' .. size .. 'px;width:' .. size .. 'px' )
	end
	
	if align ~= 'text-top' then
		table.insert( styles, 'vertical-align:' .. align )
	end
	
	if css ~= '' then
		table.insert( styles, css )
	end
	
	local sprite = table.concat( {
		'<span',
			'class="sprite2 ' .. name:lower() .. '-sprite ' .. class .. '"',
			'style="' .. table.concat( styles, ';' ) .. '"',
		'><br></span>'
	}, ' ' )
	sprite = sprite:gsub( '%s([">])', '%1' )
	
	if text ~= '' then
		text = '<span class="nowrap" style="padding-left:0.3em">' .. text .. '</span>'
	end
	
	if link ~= '' then
		if link:find( '//' ) then
			-- External link
			return '[' .. link .. ' ' .. sprite .. text .. ']'
		else
			-- Internal link
			return '[[' .. link .. '|' .. sprite .. text .. ']]'
		end
	else
		return sprite .. text
	end
end

function p.sprite( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge()
	end
	
	if tonumber( args[1] ) then
		args.pos = args[1]
	else
		local ids = mw.loadData( 'Module:Sprite/' .. args.name )
		local name = mw.text.trim( args[1] or '' )
		args.pos = ids[name] or ids[name:lower():gsub( '%s', '-' )]
	end
	
	return p.base( args )
end

function p.link( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge()
	end
	
	local link = mw.text.split( args[1] or '', '%-' )[1]
	local text = args[2] or link
	
	args[1] = args.id or args[1] or ''
	args.link = link
	args.text = text
	
	return p.sprite( args )
end

function p.doc( f )
	local args = f.args
	args.name = args.name or 'Block'
	local idTable = mw.title.new( 'Module:Sprite/' .. args.name ):getContent()
	idTable = idTable:gsub( '(\n%s*%-%-%s*.-%s*%-%-%s*\n)', '%1,' ):gsub( '^return {', '' ):gsub( '}$', '' )
	
	local html = {}
	local names = {}
	local idKeys = {}
	local section = ''
	for line in mw.text.gsplit( idTable, ',' ) do
		line = mw.text.trim( line )
		name = line:match( '^%[[\'"](.+)[\'"]%]' ) or line:match( '^%w+' ) or ''
		id = line:match( '=%s*(%d+)%s*,?$' ) or ''
		section = line:match( '^%-%-%s*(.+)%s*%-%-$' ) or section
		
		if name ~= '' and id ~= '' then
			if names[id] then
				if type( names[id].names ) == 'table' then
					table.insert( names[id].names, name )
				else
					names[id].names = { names[id].names, name }
				end
			else
				names[id] = { names = name, section = section }
				table.insert( idKeys, id )
			end
		end
	end
		
	local list = {}
	local listHead = '<ul style="border:1px solid #AAA;border-width:1px 0 0 1px;line-height:19px;list-style:none;margin:0;-moz-column-width:250px;-moz-column-gap:0">'
	local listFoot = '</ul>'
	local lastSection = ''
	for i, id in ipairs( idKeys ) do
		local name = names[id].names
		local newSection = mw.text.trim( names[id].section )
		
		if newSection ~= lastSection or i == 1 then
			if newSection ~= lastSection then
				if lastSection ~= '' then
					table.insert( list, listFoot )
				end
				
				table.insert( list, '\n===' .. newSection .. '===\n' )
				lastSection = newSection
			end
			table.insert( list, listHead )
		end
		table.insert( list, '<li style="border:1px solid #AAA;background-color:#F9F9F9;padding:0 0.5em;border-top-width:0;margin:0 0 0 -1px"><table><tr><td>' )
		if type( name ) == 'table' then
			for i, name2 in ipairs( name ) do
				if i == 1 then
					args[1] = name2
					table.insert( list, p.sprite( args ) .. '</td><td><code>' .. name2 .. '</code>' )
				else
					table.insert( list, '<br><code style="display:inline-block;margin-top:8px">' .. name2 .. '</code>' )
				end
			end
		else
			args[1] = name
			table.insert( list, p.sprite( args ) .. '</td><td><code>' .. name .. '</code>' )
		end
		table.insert( list, '</td></tr></table></li>' )
		
		if i == #idKeys then
			table.insert( list, listFoot )
		end
	end
	
	return table.concat( list )
end
return p
Advertisement