Module:Inventory slot: Difference between revisions
imported>Ttv pedro270707 m (Temporary test function to reduce expensive parser function calls) |
imported>Ttv pedro270707 m (Replaced very expensive function with not very expensive function) |
||
Line 94: | Line 94: | ||
end | end | ||
elseif (subname ~= pageName and subname) or args.link == "y" then | elseif (subname ~= pageName and subname) or args.link == "y" then | ||
link = subname | link = require("Module:Inventory slot/links").links[subname] | ||
end | end | ||
Line 140: | Line 140: | ||
end | end | ||
elseif (item ~= pageName and item) or args.link == "y" then | elseif (item ~= pageName and item) or args.link == "y" then | ||
link = item | link = require("Module:Inventory slot/links").links[item] | ||
end | end | ||
Line 153: | Line 153: | ||
end | end | ||
if link and | if link or (args.link and args.link ~= "y" and args.link ~= "n") then | ||
slotContainer:wikitext( "<span style='" .. display .. "'>" .. "[ | slotContainer:wikitext( "<span style='" .. display .. "' class='no-external-icon'>" .. "[https://bta.miraheze.org/wiki/" .. link:gsub(" ", "_") .. " " .. tostring( slot ) .. "]" .. "</span>" ) | ||
else | else | ||
slotContainer:wikitext( "<span style='" .. display .. "'>" .. tostring( slot ) .. "</span>" ) | slotContainer:wikitext( "<span style='" .. display .. "'>" .. tostring( slot ) .. "</span>" ) |
Revision as of 00:40, 30 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.formatDescription(description, preferredLineLength) local lineLength = 0 local result = "" for i = 1, #description do local c = description:sub(i, i) if c == " " then if lineLength > preferredLineLength then lineLength = 0 result = result .. "\\n§8" else result = result .. c end else lineLength = lineLength + 1 result = result .. c end end return result end function p.createSlot( f ) local args = f local currentFrame = mw.getCurrentFrame() if f == currentFrame then args = require( 'Module:ProcessArgs' ).merge( true ) else f = currentFrame end local aliases = require( "Module:Inventory slot/aliases" ).aliases local descriptions = require( "Module:Inventory slot/descriptions" ).descriptions 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 descriptions[subitem] or descriptions[subname] then slot:attr( "data-mcdesc", p.formatDescription((descriptions[subitem] or descriptions[subname]), 16) ) end if not subitem:match( '\.gif$' ) and not subitem:match( '\.png$' ) then slot:wikitext( sprite( { data = "InvSprite", id = subvalue } ) ) end if args.link and args.link ~= "y" then if args.link ~= "n" then link = args.link end elseif (subname ~= pageName and subname) or args.link == "y" then link = require("Module:Inventory slot/links").links[subname] end if count then local itemCount = mw.html.create( "span" ) itemCount:cssText( "position: absolute; right: -2px; bottom: -5px; pointer-events: none;" ) :addClass( "c-0" ) :addClass( "shadow" ) :wikitext( count ) slot:node( itemCount ) end if subitem:match( '\.gif$' ) or subitem:match( '\.png$' ) then if link and mw.title.new( link ).exists then slot:wikitext( "<span class=\"sprite pixelated\">[[File:" .. subitem .. "|160px|link=" .. link .. "]]</span>" ) else slot:wikitext( "<span class=\"sprite pixelated\">[[File:" .. subitem .. "|160px|link=]]</span>" ) end slotContainer:wikitext( tostring( slot ) ) else 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 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) ) if descriptions[item] then slot:attr( "data-mcdesc", p.formatDescription(descriptions[item], 16) ) end slot:wikitext( sprite( { data = "InvSprite", id = item } ) ) if args.link and args.link ~= "y" then if args.link ~= "n" then link = args.link end elseif (item ~= pageName and item) or args.link == "y" then link = require("Module:Inventory slot/links").links[item] end if count then local itemCount = mw.html.create( "span" ) itemCount:cssText( "position: absolute; right: -2px; bottom: -5px; pointer-events: none;" ) :addClass( "c-0" ) :addClass( "shadow" ) :wikitext( count ) slot:node( itemCount ) end if link or (args.link and args.link ~= "y" and args.link ~= "n") then slotContainer:wikitext( "<span style='" .. display .. "' class='no-external-icon'>" .. "[https://bta.miraheze.org/wiki/" .. link:gsub(" ", "_") .. " " .. tostring( slot ) .. "]" .. "</span>" ) else slotContainer:wikitext( "<span style='" .. display .. "'>" .. tostring( slot ) .. "</span>" ) end end end return slotContainer end p.value = { ["Steel Ingot"] = "Hello", ["Steel Ingot5"] = "Hello", ["Steel Ingot10"] = "Hello" } function p.test(value) if value == 1 then for i = 1, 1000 do if require("Module:Inventory slot/descriptions").descriptions["Steel Ingot"] then mw.log("Exists") else mw.log("Does not exist") end end else for i = 1, 101 do mw.log(mw.title.new("Steel Ingot" .. i).exists) end end 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