Module:Inventory slot: Difference between revisions
Remove old class |
Update to allow aliases to be pre-split, for better performance; also avoid splitting and stringifying input twice |
||
Line 8: | Line 8: | ||
'Damaged' | 'Damaged' | ||
} | } | ||
local function mergeList( parentTable, content ) | |||
if content[1] then | |||
-- Merge list into table | |||
for _, v in ipairs( content ) do | |||
table.insert( parentTable, v ) | |||
end | |||
else | |||
-- Add strings or tables to table | |||
table.insert( parentTable, content ) | |||
end | |||
end | |||
local function cloneTable( origTable ) | |||
local newTable = {} | |||
for k, v in pairs( origTable ) do | |||
if type( v ) == 'table' then | |||
v = cloneTable( v ) | |||
end | |||
newTable[k] = v | |||
end | |||
return newTable | |||
end | |||
function p.slot( f ) | function p.slot( f ) | ||
Line 31: | Line 54: | ||
randomise = false | randomise = false | ||
end | end | ||
local frames = {} | |||
for frameText in mw.text.gsplit( args[1], '%s*;%s*' ) do | |||
local frame = p.makeFrame( frameText, args.mod ) | |||
local id = frame.name | |||
if frame.mod then | |||
id = frame.mod .. ':' .. id | |||
end | |||
local alias | |||
if modAliases and modAliases[id] then | |||
alias = modAliases[id] | |||
elseif aliases and aliases[id] then | |||
alias = aliases[id] | |||
end | end | ||
if alias then | |||
mergeList( frames, p.expandAlias( alias, frame ) ) | |||
else | |||
table.insert( frames, frame ) | |||
end | |||
-- Randomise starting frame for "Any *" aliases, as long as the alias is the only frame | |||
-- and this isn't an output slot | |||
if randomise ~= false and frame.name:match( '^Any ' ) then | |||
randomise = true | |||
else | |||
randomise = false | |||
end | |||
end | end | ||
Line 68: | Line 87: | ||
local ids = mw.loadData( [[Module:InvSprite/IDs]] ).ids | local ids = mw.loadData( [[Module:InvSprite/IDs]] ).ids | ||
local modIds = {} | local modIds = {} | ||
local animated = | local animated = #frames > 1 | ||
local pageName = mw.title.getCurrentTitle().text | local pageName = mw.title.getCurrentTitle().text | ||
local imgClass = args.imgclass | local imgClass = args.imgclass | ||
Line 87: | Line 106: | ||
end | end | ||
local activeFrame = randomise and math.random( #frames ) or 1 | local activeFrame = randomise and math.random( #frames ) or 1 | ||
for i, frame in ipairs( frames ) do | for i, frame in ipairs( frames ) do | ||
local item | local item | ||
if frame ~= '' or frame == '' and animated then | if frame ~= '' or frame.name == '' and animated then | ||
item = body:tag( 'span' ):addClass( 'invslot-item' ) | item = body:tag( 'span' ):addClass( 'invslot-item' ) | ||
if imgClass then | if imgClass then | ||
Line 98: | Line 116: | ||
end | end | ||
if frame == '' then | if frame == '' or frame.name == '' then | ||
( item or body ):tag( 'br' ) | ( item or body ):tag( 'br' ) | ||
else | else | ||
local category | local category | ||
local | local title = frame.title or mw.text.trim( args.title or '' ) | ||
local mod = frame.mod | |||
local mod = | local name = frame.name | ||
local name = | local num = frame.num | ||
local num = | local description = frame.text | ||
local description = | |||
local img, idData | local img, idData | ||
Line 232: | Line 249: | ||
end | end | ||
function p. | function p.getAlias( alias, parentFrame ) | ||
-- If the frame | local aliasFrames | ||
if | if type( alias ) == 'string' then | ||
aliasFrames = {} | |||
for frameText in mw.text.gsplit( alias, '%s*;%s*' ) do | |||
table.insert( aliasFrames, p.makeFrame( frameText ) ) | |||
end | |||
else | |||
aliasFrames = alias | |||
end | |||
-- If alias is just a name, return the original frame with the new name | |||
if type( aliasFrames ) == 'string' then | |||
local aliasParts = mw.clone( parentFrame ) | |||
aliasParts.name = aliasFrames | |||
return aliasParts | |||
end | |||
-- Single frame alias, put in list | |||
if aliasFrames.name then | |||
aliasFrames = { aliasFrames } | |||
end | end | ||
local expandedFrames = {} | local expandedFrames = {} | ||
for aliasFrame in | for i, aliasFrame in ipairs( aliasFrames ) do | ||
local aliasParts = | local aliasParts | ||
aliasParts.title = | if type( aliasFrame ) == 'string' then | ||
aliasParts.mod = | aliasParts = { name = aliasFrame } | ||
aliasParts.num = | else | ||
aliasParts.text = | aliasParts = cloneTable( aliasFrame ) | ||
end | |||
aliasParts.title = parentFrame.title or aliasParts.title | |||
aliasParts.mod = parentFrame.mod or aliasParts.mod | |||
aliasParts.num = parentFrame.num or aliasParts.num | |||
aliasParts.text = parentFrame.text or aliasParts.text | |||
expandedFrames[i] = aliasParts | |||
end | end | ||
return | return expandedFrames | ||
end | |||
function p.expandAlias( parentFrame, alias ) | |||
return p.getAlias( alias, parentFrame ) | |||
end | |||
function p.stringifyFrame( frame ) | |||
if not frame.name then | |||
return '' | |||
end | |||
return string.format( | |||
'[%s]%s:%s,%s[%s]', | |||
frame.title or '', | |||
frame.mod or 'Minecraft', | |||
frame.name, | |||
frame.num or '', | |||
frame.text or '' | |||
) | |||
end | end | ||
function p. | function p.stringifyFrames( frames ) | ||
for i, frame in ipairs( frames ) do | |||
frames[i] = p.stringifyFrame( frame ) | |||
end | |||
return table.concat( frames, ';' ) | |||
end | |||
function p.makeFrame( frameText, mod ) | |||
-- Simple frame with no parts | |||
if not frameText:match( '[%[:,]' ) then | |||
return { | |||
mod = mod, | |||
name = mw.text.trim( frameText ), | |||
} | |||
end | |||
frameText = frameText:gsub( '%s*([%[%]:,;])%s*', '%1' ) | |||
local frame = {} | |||
frame.title = frameText:match( '^%[([^%]]+)%]' ) | |||
frame.mod = frameText:match( '([^:%]]+):' ) or mod | |||
local vanilla = { v = 1, vanilla = 1, mc = 1, minecraft = 1 } | local vanilla = { v = 1, vanilla = 1, mc = 1, minecraft = 1 } | ||
if | if frame.mod == '' or vanilla[mw.ustring.lower( frame.mod )] then | ||
frame.mod = nil | |||
end | end | ||
local nameStart = ( | local nameStart = ( frameText:find( ':' ) or frameText:find( '%]' ) or 0 ) + 1 | ||
if nameStart - 1 == # | if nameStart - 1 == #frameText then | ||
nameStart = 1 | nameStart = 1 | ||
end | end | ||
frame.name = frameText:sub( nameStart, ( frameText:find( '[,%[]', nameStart ) or 0 ) - 1 ) | |||
frame.num = math.floor( frameText:match( ',(%d+)' ) or 0 ) | |||
if | if frame.num == 0 then | ||
frame.num = nil | |||
end | end | ||
frame.text = frameText:match( '%[([^%]]+)%]$' ) | |||
return | return frame | ||
end | |||
function p.getParts( frameText, mod ) | |||
return p.makeFrame( frameText, mod ) | |||
end | end | ||
return p | return p |