Module:Inventory slot: Difference between revisions

From Modded Wiki
Jump to navigation Jump to search
Allow skipping aliases
Created page with "local p = {} function p.slot( f ) local args = f.args or f if f == mw.getCurrentFrame() and args[1] == nil then args = f:getParent().args end args[1] = mw.text.trim(..."
Line 1: Line 1:
local p = {}
local p = {}
-- Individual cell
 
function p.cell( f )
function p.slot( f )
local args = f.args or f
local args = f.args or f
if f == mw.getCurrentFrame() and args[1] == nil then
if f == mw.getCurrentFrame() and args[1] == nil then
Line 9: Line 9:
args[1] = mw.text.trim( args[1] or '' )
args[1] = mw.text.trim( args[1] or '' )
if not args.noalias then
-- Comment this next line out if you're not using aliases
-- Comment this next line out if you're not using aliases
local aliases = mw.loadData( 'Module:Inventory slot/Aliases' )
local aliases = mw.loadData( 'Module:Grid/Aliases' )
local modAliases = args.modaliases or ''
local modAliases = args.modaliases or ''
if modAliases ~= '' then
if modAliases ~= '' then
modAliases = mw.loadData( 'Module:' .. modAliases )
modAliases = mw.loadData( 'Module:' .. modAliases )
else
else
modAliases = nil
modAliases = nil
end
end
if aliases or modAliases then
if aliases or modAliases then
local frames = {}
local frames = {}
for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
local frameParts = p.getParts( frame, args.mod )
local frameParts = p.getParts( frame, args.mod )
local id = frameParts.name
local id = frameParts.name
if frameParts.mod then
if frameParts.mod then
id = frameParts.mod .. ':' .. id
id = frameParts.mod .. ':' .. id
end
end
local alias
local alias
if modAliases and modAliases[id] then
if modAliases and modAliases[id] then
alias = modAliases[id]
alias = modAliases[id]
elseif aliases and aliases[id] then
elseif aliases and aliases[id] then
alias = aliases[id]
alias = aliases[id]
end
if alias then
table.insert( frames, p.expandAlias( frameParts, alias ) )
else
table.insert( frames, frame )
end
end
end
args[1] = table.concat( frames, ';' )
if alias then
table.insert( frames, p.expandAlias( frameParts, alias ) )
else
table.insert( frames, frame )
end
end
end
args[1] = table.concat( frames, ';' )
end
end
local sprite
local ids = mw.loadData( [[Module:InvSprite/IDs]] ).ids
local modIds = {}
local animated = args[1]:find( ';' )
local animated = args[1]:find( ';' )
local pageName = mw.title.getCurrentTitle().subpageText
local pageName = mw.title.getCurrentTitle().subpageText
local class = args.class or ''
local imgClass = args.imgclass
local imgClass = args.imgclass or ''
local numStyle = args.numstyle
local style = args.style or ''
local body = mw.html.create( 'span' ):addClass( 'invslot' ):css{ ['vertical-align'] = args.align }
local align = args.align or ''
if animated then
local numStyle = args.numstyle or ''
body:addClass( 'animated' )
local cell = {}
end
if args.class then
body:addClass( args.class )
end
if args.style then
body:cssText( args.style )
end
if ( args.default or '' ) ~= '' then
body:css( 'background-image', '{{FileUrl|' .. args.default .. '.png}}' )
end
local first = true
for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
local item
if frame ~= '' or frame == '' and animated then
item = body:tag( 'span' ):addClass( 'invslot-item' )
if imgClass then
item:addClass( imgClass )
end
end
if frame == '' then
if frame == '' then
if animated then
( item or body ):tag( 'br' )
table.insert( cell, '<span class="item"><br></span>' )
else
table.insert( cell, '<br>' )
end
else
else
local parts = p.getParts( frame, args.mod )
local parts = p.getParts( frame, args.mod )
Line 72: Line 88:
local tooltipDesc = parts.text
local tooltipDesc = parts.text
local img
local img, spriteImg
if mod then
if mod then
img = name .. ' (' .. mod .. ')'
if not modIds[mod] and mw.title.new( 'Module:InvSprite/Mods/' .. mod .. '/IDs' ).exists then
modIds[mod] = mw.loadData( 'Module:InvSprite/Mods/' .. mod .. '/IDs' )
end
if modIds[mod] and modIds[mod][name] then
spriteImg = modIds[mod][name].pos
else
img = name .. ' (' .. mod .. ')'
end
elseif ids[name] then
spriteImg = ids[name].pos
else
else
img = name
img = name
Line 95: Line 120:
title = tooltipTitle:gsub( '&[0-9a-fk-or]', '' )
title = tooltipTitle:gsub( '&[0-9a-fk-or]', '' )
end
end
if mw.text.trim( title ) == '' or link:lower() == 'none' or link ~= name:gsub( '%s%(.*', '' ) then
if mw.text.trim( title ) == '' and ( link:lower() == 'none' or link ~= name ) then
title = name
title = name
end
end
end
if not tooltipTitle and title:lower() == 'none' then
tooltipTitle = 0
end
local alt = img
if title:lower() ~= 'none' then
alt = title
end
end
if link:lower() == 'none' then
if link:lower() == 'none' then
link = ''
link = nil
end
end
if title:lower() == 'none' then
if title == '' then
title = ''
title = nil
elseif title:lower() == 'none' then
if link then
title = ''
else
title = nil
end
end
end
local tooltip = ''
if img and ( title == '' or not link ) then
if tooltipTitle then
item:attr{ title = title }
tooltip = ' data-minetip-title="' .. tooltipTitle .. '"'
end
end
item:attr{
['data-minetip-title'] = tooltipTitle,
['data-minetip-text'] = tooltipDesc
}
if tooltipDesc then
if img then
tooltip = tooltip .. ' data-minetip-text="' .. tooltipDesc .. '"'
item:addClass( 'invslot-item-image' )
:wikitext( '[[File:Grid ', img, '.png|32x32px|link=', link or '', '|', title or '', ']]' )
else
if not sprite then
sprite = require( [[Module:Sprite]] ).base
end
local image
if mod then
image = args.spritesheet or mod .. 'Sprite.png'
end
if link then
item:wikitext( '[[', link, '|' )
end
item:wikitext( sprite{
pos = spriteImg, title = title,
image = image, settings = 'InvSprite'
} )
end
end
local image = {
'<span class="item ' .. imgClass .. '" title="' .. title .. '"' .. tooltip .. '>',
'[[File:Grid ' .. img .. '.png|32x32px|link=' .. link .. '|alt=' .. alt .. ']]',
'</span>',
}
image[1] = image[1]:gsub( ' title=""', '' )
image[2] = image[2]:gsub( '||', '|' )
if num and num > 1 and num < 1000 then
if num and num > 1 and num < 1000 then
if link ~= '' then
if img and link then
num = '[[' .. link .. '|' .. num .. ']]'
item:wikitext( '[[', link, '|' )
end
end
if numStyle ~= '' then
local number = item
numStyle = ' style="' .. numStyle .. '"'
:tag( 'span' )
:addClass( 'invslot-stacksize' )
:attr{ title = title }
:wikitext( num )
if numStyle then
number:cssText( numStyle )
end
if img and link then
item:wikitext( ']]' )
end
end
table.insert( image, 3, '<span class="number"' .. numStyle .. '>' .. num .. '</span>' )
end
end
table.insert( cell, table.concat( image, '' ) )
if spriteImg and link then
item:wikitext( ']]' )
end
end
end
end
if first then
if animated then
if animated and item then
cell[1] = cell[1]:gsub( 'class="item', 'class="item active' )
item:addClass( 'active' )
class = 'animated ' .. class
end
end
first = false
local styles = {}
if align ~= '' then
table.insert( styles, 'vertical-align:' .. align )
end
if style ~= '' then
table.insert( styles, style )
end
if #styles > 0 then
styles = ' style="' .. table.concat( styles, ';' ) .. '"'
else
styles = ''
end
local html = {
'<span class="grid ' .. class .. '"' .. styles .. '>',
table.concat( cell, '' ),
'</span>'
}
if ( args.default or '' ) ~= '' then
local defaultClass = ''
if animated then
defaultClass = ' skip'
end
end
table.insert( html, 2, '<span class="default-item' .. defaultClass .. '">[[File:Grid ' .. args.default .. '.png|32x32px|alt=|link=]]</span>' )
end
end
html = table.concat( html, '' ):gsub( ' "', '"' )
return body
return html
end
end


Line 197: Line 213:
aliasParts.text = frameParts.text or aliasParts.text or ''
aliasParts.text = frameParts.text or aliasParts.text or ''
table.insert( expandedFrames, string.format( '[%s]%s:%s,%s[%s]', aliasParts.title, aliasParts.mod, aliasParts.name, aliasParts.num, aliasParts.text ) )
table.insert( expandedFrames, string.format(
'[%s]%s:%s,%s[%s]',
aliasParts.title, aliasParts.mod, aliasParts.name, aliasParts.num, aliasParts.text
) )
end
end
Line 228: Line 247:
return parts
return parts
end
--- GUI variants; called directly to avoid the overhead of a bunch of #invoke calls per GUI
-- Crafting table
function p.craftingTable( f )
local args = f
if f == mw.getCurrentFrame() then
args = f:getParent().args
end
local arrow = 'Arrow (small)'
local shapeless = ''
if args.arrow or '' ~= '' then
arrow = args.arrow .. ' (' .. args.Mod .. ')'
end
if args.shapeless or '' ~= '' then
shapeless = '<span title="This recipe is shapeless; the inputs may be placed in any arrangement in the crafting grid.">[[File:Grid layout Shapeless.png|link=]]</span>'
elseif args.fixed or '' ~= '' then
local notFixed = ''
if args.notfixed or '' ~= '' then
notFixed = '; except for ' .. args.notfixed .. ', which can go anywhere'
end
shapeless = '<span title="This recipe is fixed, the input arrangement may not be moved or mirrored' .. notFixed .. '.">[[File:Grid layout Fixed.png|link=]]</span>'
end
local html = {
'{| class="grid-Crafting_Table" cellpadding="0" cellspacing="0"',
'| ' .. p.cell{ args.A1, mod = args.Mod, link = args.A1link, title = args.A1title },
'| ' .. p.cell{ args.B1, mod = args.Mod, link = args.B1link, title = args.B1title },
'| ' .. p.cell{ args.C1, mod = args.Mod, link = args.C1link, title = args.C1title },
'| rowspan="2" class="arrow" | [[File:Grid layout ' .. arrow .. '.png|link=]]',
'| rowspan="3" | ' .. p.cell{ args.Output, mod = args.Mod, link = args.Olink, title = args.Otitle, class = 'output' },
'|-',
'| ' .. p.cell{ args.A2, mod = args.Mod, link = args.A2link, title = args.A2title },
'| ' .. p.cell{ args.B2, mod = args.Mod, link = args.B2link, title = args.B2title },
'| ' .. p.cell{ args.C2, mod = args.Mod, link = args.C2link, title = args.C2title },
'|-',
'| ' .. p.cell{ args.A3, mod = args.Mod, link = args.A3link, title = args.A3title },
'| ' .. p.cell{ args.B3, mod = args.Mod, link = args.B3link, title = args.B3title },
'| ' .. p.cell{ args.C3, mod = args.Mod, link = args.C3link, title = args.C3title },
'| class="shapeless" | ' .. shapeless,
'|}'
}
return table.concat( html, '\n' );
end
-- Furnace
function p.furnace( f )
local args = f
if f == mw.getCurrentFrame() then
args = f:getParent().args
end
args = require( 'Module:ProcessArgs' ).norm( args )
local progress = 'Furnace Progress'
local burning = ' (in-active)'
local smelting = burning
local fuelUsage = 'Fire'
if args.Progress then
progress = args.Progress .. ' Progress'
if args.Mod then
progress = progress .. ' (' .. args.Mod .. ')'
end
end
if args.Input and args.Fuel then
burning = ''
if args.Output then
smelting = ''
end
end
if args.FuelUsage then
fuelUsage = args.FuelUsage
if args.Mod then
fuelUsage = fuelUsage .. ' (' .. args.Mod .. ')'
end
end
local html = {
'{| class="grid-Furnace" cellpadding="0" cellspacing="0"',
'| ' .. p.cell{ args.Input, mod = args.Mod, link = args.Ilink, title = args.Ititle },
'| rowspan="3" class="arrow" | [[File:Grid layout ' .. progress .. smelting .. '.png|link=]]',
'| rowspan="3" class="output" | ' .. p.cell{ args.Output, mod = args.Mod, link = args.Olink, title = args.Otitle, class = 'output' },
'|-',
'| [[File:Grid layout ' .. fuelUsage .. burning .. '.png|link=]]',
'|-',
'| ' .. p.cell{ args.Fuel, mod = args.Mod, link = args.Flink, title = args.Ftitle },
'|}'
}
return table.concat( html, '\n' );
end
-- Brewing Stand
function p.brewingStand( f )
local args = f
if f == mw.getCurrentFrame() then
args = f:getParent().args
end
args = require( 'Module:ProcessArgs' ).norm( args )
local inactive = ' (In-active)'
if args.Input and ( args.Output1 or args.Output2 or args.Output3 ) then
inactive = ''
end
local html = {
'<div class="grid-Brewing_Stand">',
'{| cellpadding="0" cellspacing="0"',
'| class="bubbles" | [[File:Grid layout Brewing Bubbles.gif|link=]]',
'| class="input" | ' .. p.cell{ args.Input, mod = args.Mod, link = args.Ilink, title = args.Ititle },
'| [[File:Grid layout Brewing Arrow' .. inactive .. '.png|link=]]',
'|-',
'| class="output1" | ' .. p.cell{ args.Output1, mod = args.Mod, link = args.O1link, title = args.O1title, default = 'layout Brewing Empty' },
'| class="output2" | ' .. p.cell{ args.Output2, mod = args.Mod, link = args.O2link, title = args.O2title, default = 'layout Brewing Empty' },
'| class="output3" | ' .. p.cell{ args.Output3, mod = args.Mod, link = args.O3link, title = args.O3title, default = 'layout Brewing Empty' },
'|-',
'| class="paths" colspan="3" | [[File:Grid layout Brewing Paths.png|link=]]',
'|}',
'</div>'
}
return table.concat( html, '\n' );
end
end
   
   
return p
return p

Revision as of 17:18, 8 August 2015

This module implements {{inventory slot}}.

Dependencies

de:Modul:Slot es:Módulo:Inventory slot fr:Module:Case inventaire ja:モジュール:Inventory slot ko:모듈:Inventory slot pl:Moduł:Inventory slot ru:Модуль:Инвентарный слот pt:Módulo:Inventory slot uk:Модуль:Інвентарний слот zh:模块:Inventory slot



local p = {}

function p.slot( f )
	local args = f.args or f
	if f == mw.getCurrentFrame() and args[1] == nil then
		args = f:getParent().args
	end
	
	args[1] = mw.text.trim( args[1] or '' )
	
	-- Comment this next line out if you're not using aliases
	local aliases = mw.loadData( 'Module:Inventory slot/Aliases' )
	
	local modAliases = args.modaliases or ''
	if modAliases ~= '' then
		modAliases = mw.loadData( 'Module:' .. modAliases )
	else
		modAliases = nil
	end
	
	if aliases or modAliases then
		local frames = {}
		for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
			local frameParts = p.getParts( frame, args.mod )
			
			local id = frameParts.name
			if frameParts.mod then
				id = frameParts.mod .. ':' .. id
			end
			
			local alias
			if modAliases and modAliases[id] then
				alias = modAliases[id]
			elseif aliases and aliases[id] then
				alias = aliases[id]
			end
			
			if alias then
				table.insert( frames, p.expandAlias( frameParts, alias ) )
			else
				table.insert( frames, frame )
			end
		end
		
		args[1] = table.concat( frames, ';' )
	end
	
	local sprite
	local ids = mw.loadData( [[Module:InvSprite/IDs]] ).ids
	local modIds = {}
	local animated = args[1]:find( ';' )
	local pageName = mw.title.getCurrentTitle().subpageText
	local imgClass = args.imgclass
	local numStyle = args.numstyle
	local body = mw.html.create( 'span' ):addClass( 'invslot' ):css{ ['vertical-align'] = args.align }
	if animated then
		body:addClass( 'animated' )
	end
	if args.class then
		body:addClass( args.class )
	end
	if args.style then
		body:cssText( args.style )
	end
	
	if ( args.default or '' ) ~= '' then
		body:css( 'background-image', '{{FileUrl|' .. args.default .. '.png}}' )
	end
	
	local first = true
	for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
		local item
		if frame ~= '' or frame == '' and animated then
			item = body:tag( 'span' ):addClass( 'invslot-item' )
			if imgClass then
				item:addClass( imgClass )
			end
		end
		
		if frame == '' then
			( item or body ):tag( 'br' )
		else
			local parts = p.getParts( frame, args.mod )
			local tooltipTitle = parts.title
			local mod = parts.mod
			local name = parts.name
			local num = parts.num
			local tooltipDesc = parts.text
			
			local img, spriteImg
			if mod then
				if not modIds[mod] and mw.title.new( 'Module:InvSprite/Mods/' .. mod .. '/IDs' ).exists then
					modIds[mod] = mw.loadData( 'Module:InvSprite/Mods/' .. mod .. '/IDs' )
				end
				if modIds[mod] and modIds[mod][name] then
					spriteImg = modIds[mod][name].pos
				else
					img = name .. ' (' .. mod .. ')'
				end
			elseif ids[name] then
				spriteImg = ids[name].pos
			else
				img = name
			end
			
			local link = args.link or ''
			if link == '' then
				if name == pageName then
					link = 'none'
				elseif mod then
					link = 'Mods/' .. mod .. '/' .. name
				else
					link = name
				end
			end
			
			local title = args.title or ''
			if title == '' then
				if tooltipTitle then
					title = tooltipTitle:gsub( '&[0-9a-fk-or]', '' )
				end
				if mw.text.trim( title ) == '' and ( link:lower() == 'none' or link ~= name ) then
					title = name
				end
			end
			
			if link:lower() == 'none' then
				link = nil
			end
			if title == '' then
				title = nil
			elseif title:lower() == 'none' then
				if link then
					title = ''
				else
					title = nil
				end
			end
			
			if img and ( title == '' or not link ) then
				item:attr{ title = title }
			end
			item:attr{
				['data-minetip-title'] = tooltipTitle,
				['data-minetip-text'] = tooltipDesc
			}
			
			if img then
				item:addClass( 'invslot-item-image' )
					:wikitext( '[[File:Grid ', img, '.png|32x32px|link=', link or '', '|', title or '', ']]' )
			else
				if not sprite then
					sprite = require( [[Module:Sprite]] ).base
				end
				local image
				if mod then
					image = args.spritesheet or mod .. 'Sprite.png'
				end
				if link then
					item:wikitext( '[[', link, '|' )
				end
				item:wikitext( sprite{
					pos = spriteImg, title = title,
					image = image, settings = 'InvSprite'
				} )
			end
			
			if num and num > 1 and num < 1000 then
				if img and link then
					item:wikitext( '[[', link, '|' )
				end
				local number = item
					:tag( 'span' )
						:addClass( 'invslot-stacksize' )
						:attr{ title = title }
						:wikitext( num )
				if numStyle then
					number:cssText( numStyle )
				end
				if img and link then
					item:wikitext( ']]' )
				end
			end
			
			if spriteImg and link then
				item:wikitext( ']]' )
			end
		end
		
		if first then
			if animated and item then
				item:addClass( 'active' )
			end
			first = false
		end
	end
	
	return body
end

function p.expandAlias( frameParts, alias )
	-- If the frame has no parts, we can just return the alias as-is
	if not frameParts.title and not frameParts.mod and not frameParts.num and not frameParts.text then
		return alias
	end
	
	local expandedFrames = {}
	for aliasFrame in mw.text.gsplit( alias, '%s*;%s*' ) do
		local aliasParts = p.getParts( aliasFrame )
		aliasParts.title = frameParts.title or aliasParts.title or ''
		aliasParts.mod = frameParts.mod or aliasParts.mod or 'Minecraft'
		aliasParts.num = frameParts.num or aliasParts.num or ''
		aliasParts.text = frameParts.text or aliasParts.text or ''
		
		table.insert( expandedFrames, string.format(
			'[%s]%s:%s,%s[%s]',
			aliasParts.title, aliasParts.mod, aliasParts.name, aliasParts.num, aliasParts.text
		) )
	end
	
	return table.concat( expandedFrames, ';' )
end

function p.getParts( frame, mod )
	local parts = {}
	parts.title = frame:match( '^%[%s*([^%]]+)%s*%]' )
	
	parts.mod = mw.text.trim( frame:match( '([^:%]]+):' ) or mod or '' )
	
	local vanilla = { v = 1, vanilla = 1, mc = 1, minecraft = 1 }
	if parts.mod == '' or vanilla[mw.ustring.lower( parts.mod )] then
		parts.mod = nil
	end
	
	local nameStart = ( frame:find( ':' ) or frame:find( '%]' ) or 0 ) + 1
	if nameStart - 1 == #frame then
		nameStart = 1
	end
	parts.name = mw.text.trim( frame:sub( nameStart, ( frame:find( '[,%[]', nameStart ) or 0 ) - 1 ) )
	
	parts.num = math.floor( frame:match( ',%s*(%d+)' ) or 0 )
	if parts.num == 0 then
		parts.num = nil
	end
	
	parts.text = frame:match( '%[%s*([^%]]+)%s*%]$' )
	
	return parts
end
 
return p