module:KnightMiner/Sandbox

From Minecraft Wiki
Jump to: navigation, search
[view | edit | history | purge]DocumentationJump to code ↴

This is a module for KnightMiner to test bits of lua code, since modules are required to be in the module namespace.

Do not use this module in the main namespaces, as its contents will change at any time without warning.


[view | edit | history | purge]The above documentation is transcluded from Module:KnightMiner/Sandbox/doc.
local p = {}
function p.grid( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = f:getParent().args
	end
	local rows = {}
	local sprite = require( 'Module:Sprite' ).link
	local scale = args.scale
	local size = 16 * ( scale or 1 )
	local categories = {}
	local keys = args.keys or args
	for k, v in ipairs( args ) do
		local row = {}
		for v2 in mw.text.gsplit( v:gsub( '^%s-\n+', '' ):gsub( '\n+%s-$', '' ), '' ) do
			if mw.text.trim( v2 ) == '' or keys[v2] == 'air' then
				table.insert( row, ' style="width:' .. size .. 'px;height:' .. size .. 'px" | ' )
			else
				local cssRot = nil
				local spriteName = nil
				if keys[v2] then
					local spritePart = mw.text.split( keys[v2], '-rot', true )
					spriteName = spritePart[1]
					if spritePart[2] then
						cssRot = 'transform:rotate(' .. spritePart[2] .. 'deg)'
					end
				end
				local image, spriteCat = sprite{ settings = 'BlockSprite', spriteName, '', scale = scale, nocat = args.nocat, css = cssRot }
				table.insert( row, image )
				table.insert( categories, spriteCat )
			end
		end
		
		table.insert( rows, table.concat( row, ' || ' ) )
	end
	
	local spacing = tonumber( args.spacing or '' ) or 0
	
	return '{| cellspacing="' .. spacing .. '" cellpadding="0" style="margin:0; line-height:0"\n| ' ..
		table.concat( rows, '\n|-\n| ' ) ..
	'\n|}' .. table.concat( categories )
end
return p