Module:Inventory slot: Difference between revisions

From BTA-Mirror
imported>SunSeeker02
No edit summary
imported>Ttv pedro270707
mNo edit summary
 
(92 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local p = {}


p.itemSets = {
function p.formatDescription(description, preferredLineLength)
["Any Wool"] = {
local lineLength = 0
"White Wool",
local result = ""
"Orange Wool",
 
"Magenta Wool",
for i = 1, #description do
"Light Blue Wool",
local c = description:sub(i, i)
"Yellow Wool",
    if c == " " then
"Lime Wool",
    if lineLength > preferredLineLength then
"Pink Wool",
        lineLength = 0
"Gray Wool",
        result = result .. "\\n§8"
"Light Gray Wool",
    else
"Cyan Wool",
        result = result .. c
"Purple Wool",
    end
"Blue Wool",
    else
"Brown Wool",
    lineLength = lineLength + 1
"Green Wool",
    result = result .. c
"Red Wool",
    end
"Black Wool"
end
},
 
["Any Planks"] = {
return result
"Wooden Planks",
end
"White Wooden Planks",
"Orange Wooden Planks",
"Magenta Wooden Planks",
"Light Blue Wooden Planks",
"Yellow Wooden Planks",
"Lime Wooden Planks",
"Pink Wooden Planks",
"Gray Wooden Planks",
"Light Gray Wooden Planks",
"Cyan Wooden Planks",
"Purple Wooden Planks",
"Blue Wooden Planks",
"Brown Wooden Planks",
"Green Wooden Planks",
"Red Wooden Planks",
"Black Wooden Planks"
},
["Any Wooden Stairs"] = {
"Wooden Stairs",
"White Wooden Stairs",
"Orange Wooden Stairs",
"Magenta Wooden Stairs",
"Light Blue Wooden Stairs",
"Yellow Wooden Stairs",
"Lime Wooden Stairs",
"Pink Wooden Stairs",
"Gray Wooden Stairs",
"Light Gray Wooden Stairs",
"Cyan Wooden Stairs",
"Purple Wooden Stairs",
"Blue Wooden Stairs",
"Brown Wooden Stairs",
"Green Wooden Stairs",
"Red Wooden Stairs",
"Black Wooden Stairs"
},
["Any Fence"] = {
"Oak Fence",
"White Wooden Fence",
"Orange Wooden Fence",
"Magenta Wooden Fence",
"Light Blue Wooden Fence",
"Yellow Wooden Fence",
"Lime Wooden Fence",
"Pink Wooden Fence",
"Gray Wooden Fence",
"Light Gray Wooden Fence",
"Cyan Wooden Fence",
"Purple Wooden Fence",
"Blue Wooden Fence",
"Brown Wooden Fence",
"Green Wooden Fence",
"Red Wooden Fence",
"Black Wooden Fence"
},
["Any Fence Gate"] = {
"Wooden Fence Gate",
"White Wooden Fence Gate",
"Orange Wooden Fence Gate",
"Magenta Wooden Fence Gate",
"Light Blue Wooden Fence Gate",
"Yellow Wooden Fence Gate",
"Lime Wooden Fence Gate",
"Pink Wooden Fence Gate",
"Gray Wooden Fence Gate",
"Light Gray Wooden Fence Gate",
"Cyan Wooden Fence Gate",
"Purple Wooden Fence Gate",
"Blue Wooden Fence Gate",
"Brown Wooden Fence Gate",
"Green Wooden Fence Gate",
"Red Wooden Fence Gate",
"Black Wooden Fence Gate"
},
["Any Wooden Slab"] = {
"Wooden Slab",
"White Wooden Slab",
"Orange Wooden Slab",
"Magenta Wooden Slab",
"Light Blue Wooden Slab",
"Yellow Wooden Slab",
"Lime Wooden Slab",
"Pink Wooden Slab",
"Gray Wooden Slab",
"Light Gray Wooden Slab",
"Cyan Wooden Slab",
"Purple Wooden Slab",
"Blue Wooden Slab",
"Brown Wooden Slab",
"Green Wooden Slab",
"Red Wooden Slab",
"Black Wooden Slab"
},
["Any Wooden Chest"] = {
"Wooden Chest",
"White Wooden Chest",
"Orange Wooden Chest",
"Magenta Wooden Chest",
"Light Blue Wooden Chest",
"Yellow Wooden Chest",
"Lime Wooden Chest",
"Pink Wooden Chest",
"Gray Wooden Chest",
"Light Gray Wooden Chest",
"Cyan Wooden Chest",
"Purple Wooden Chest",
"Blue Wooden Chest",
"Brown Wooden Chest",
"Green Wooden Chest",
"Red Wooden Chest",
"Black Wooden Chest"
},
["Any Cobblestone"] = {
"Cobblestone",
"Cobbled Basalt",
"Cobbled Limestone",
"Cobbled Granite"
},
["Any Log"] = {
"Oak Log",
"Pine Log",
"Birch Log",
"Cherry Log",
"Eucalyptus Log",
"Mossy Oak Log"
},
["Any Redstone Ore"] = {
"Stone Redstone Ore",
"Granite Redstone Ore",
"Limestone Redstone Ore",
"Basalt Redstone Ore"
},
["Any Iron Ore"] = {
"Raw Iron Ore",
"Stone Iron Ore",
"Granite Iron Ore",
"Limestone Iron Ore",
"Basalt Iron Ore"
},
["Any Gold Ore"] = {
"Raw Gold Ore",
"Stone Iron Ore",
"Granite Iron Ore",
"Limestone Iron Ore",
"Basalt Iron Ore"
},
["Any Coal Ore"] = {
"Stone Coal Ore",
"Granite Coal Ore",
"Limestone Coal Ore",
"Basalt Coal Ore"
},
["Any Lapis Lazuli Ore"] = {
"Stone Lapis Lazuli Ore",
"Granite Lapis Lazuli Ore",
"Limestone Lapis Lazuli Ore",
"Basalt Lapis Lazuli Ore"
},
["Any Diamond Ore"] = {
"Stone Diamond Ore",
"Granite Diamond Ore",
"Limestone Diamond Ore",
"Basalt Diamond Ore"
},
["Blast Furnace Fuel"] = {
"Nether Coal",
"Olivine"
},
}


function p.createSlot( f )
function p.createSlot( f )
local args = f
local args = f
if f == mw.getCurrentFrame() then  
local currentFrame = mw.getCurrentFrame()
if f == currentFrame then  
args = require( 'Module:ProcessArgs' ).merge( true )
args = require( 'Module:ProcessArgs' ).merge( true )
else
else
f = mw.getCurrentFrame()
f = currentFrame
end
end
local aliases = require( "Module:Inventory slot/aliases" ).aliases
local descriptions = require( "Module:Inventory slot/descriptions" ).descriptions
local sprite = require( "Module:Sprite" ).get
local sprite = require( "Module:Sprite" ).get
Line 214: Line 49:
local slotContainer = mw.html.create( "div" )
local slotContainer = mw.html.create( "div" )
slotContainer:addClass( "inventory-slot" )
slotContainer:addClass( "inventory-slot" )
:addClass( "animated" )
:cssText( table.concat( slotContainerCss, ";" ) )
:cssText( table.concat( slotContainerCss, ";" ) )
Line 220: Line 56:
"width: 32px",
"width: 32px",
"height: 32px",
"height: 32px",
"position: relative"
"position: relative",
"padding: 2px"
}
}
for i, value in ipairs(p.parseString( args.item )) do
for i, value in ipairs(p.parseString( args.item )) do
Line 226: Line 63:
local item = value.item
local item = value.item
local count = value.count
local count = value.count
if p.itemSets[item] then
if aliases[item] then
for j, subvalue in ipairs(p.itemSets[item]) do
for j, subvalue in ipairs(aliases[item]) do
local display = i == j == 1 and "display: inline-block" or "display: none"
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 link = nil
-- Set name, item and description
local subname = nil
local subitem = nil
local subdescription = nil
if type(subvalue) == "string" then
subname = subvalue
subitem = subvalue
subdescription = subvalue
else
subname = subvalue.name or subvalue.item
subitem = subvalue.item
subdescription = subvalue.description or subvalue.item
end
local slot = mw.html.create( "div" )
local slot = mw.html.create( "div" )
slot:addClass( "minecraft-item" )
slot:addClass( "minecraft-item" )
:cssText( table.concat( slotCss, ";" ) )
:cssText( table.concat( slotCss, ";" ) )
:attr( "data-mctitle", (name or subvalue) )
:attr( "data-mctitle", (name or subname or subitem) )
slot:wikitext( sprite( { data = "InvSprite", id = subvalue } ) )
if descriptions[subdescription] or descriptions[subitem] or descriptions[subname] then
if item ~= pageName and subvalue then
slot:attr( "data-mcdesc", p.formatDescription((descriptions[subdescription] or descriptions[subitem] or descriptions[subname]), 16) )
link = subvalue
end
if not subitem:match( '\.gif$' ) and not subitem:match( '\.png$' ) then
slot:wikitext( sprite( { data = "InvSprite", id = subitem } ) )
end
-- Set link
local link = nil
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
end
-- Set the item count
if count then
if count then
local itemCount = mw.html.create( "span" )
local itemCount = mw.html.create( "span" )
itemCount:cssText( "position: absolute; right: -2px; bottom: -5px; pointer-events: none;" )
itemCount:cssText( "position: absolute; right: 0; bottom: -2px; pointer-events: none;" )
:addClass( "c-f" )
:addClass( "c-0" )
:addClass( "shadow" )
:addClass( "shadow" )
:wikitext( count )
:wikitext( count )
Line 249: Line 113:
end
end
if link and mw.title.new( link ).exists then
-- Use an image if the item ends in .gif or .png
slotContainer:wikitext( "<span style='" .. display .. "'>" .. "[[" .. link .. "|" .. tostring( slot ) .. "]]" .. "</span>" )
if subitem:match( '\.gif$' ) or subitem:match( '\.png$' ) then
slot:wikitext( "<span class=\"sprite pixelated\" style=\"width: 32px; height: 32px;\">[[File:" .. subitem .. "|160px|link=" .. (link or "") .. "]]</span>" )
slotContainer:wikitext( tostring( slot ) )
else
else
slotContainer:wikitext( "<span style='" .. display .. "'>" .. tostring( slot ) .. "</span>" )
if link 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
end
end
else
else
local display = i == 1 and "display: inline-block" or "display: none"
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) )
slot:wikitext( sprite( { data = "InvSprite", id = item } ) )
if item ~= pageName and item then
link = item
end
if count then
if item ~= 'Air' then
local itemCount = mw.html.create( "span" )
local slot = mw.html.create( "div" )
itemCount:cssText( "position: absolute; right: -2px; bottom: -5px; pointer-events: none;" )
slot:addClass( "minecraft-item" )
:addClass( "c-f" )
:cssText( table.concat( slotCss, ";" ) )
:addClass( "shadow" )
:attr( "data-mctitle", (name or item) )
:wikitext( count )
if descriptions[item] then
slot:attr( "data-mcdesc", p.formatDescription(descriptions[item], 16) )
slot:node( itemCount )
end
end
slot:wikitext( sprite( { data = "InvSprite", id = item } ) )
if link and mw.title.new( link ).exists then
local link = nil
slotContainer:wikitext( "<span style='" .. display .. "'>" .. "[[" .. link .. "|" .. tostring( slot ) .. "]]" .. "</span>" )
if args.link and args.link ~= "y" then
if args.link ~= "n" then
link = args.link
end
elseif (require("Module:Inventory slot/links").links[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: 0; bottom: -2px; 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
else
else
slotContainer:wikitext( "<span style='" .. display .. "'>" .. tostring( slot ) .. "</span>" )
slotContainer:wikitext( "<span style='" .. display .. "'><!-- Air --></span>")
end
end
end
end
Line 286: Line 169:
return slotContainer
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
end



Latest revision as of 22:13, 2 April 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" )
		:addClass( "animated" )
		:cssText( table.concat( slotContainerCss, ";" ) )
	
	local slotCss = {
		"display: inline-block",
		"width: 32px",
		"height: 32px",
		"position: relative",
		"padding: 2px"
	}
	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`
				
				-- Set name, item and description
				local subname = nil
				local subitem = nil
				local subdescription = nil
				if type(subvalue) == "string" then
					subname = subvalue
					subitem = subvalue
					subdescription = subvalue
				else
					subname = subvalue.name or subvalue.item
					subitem = subvalue.item
					subdescription = subvalue.description or subvalue.item
				end
				
				local slot = mw.html.create( "div" )
				slot:addClass( "minecraft-item" )
					:cssText( table.concat( slotCss, ";" ) )
					:attr( "data-mctitle", (name or subname or subitem) )
				if descriptions[subdescription] or descriptions[subitem] or descriptions[subname] then
					slot:attr( "data-mcdesc", p.formatDescription((descriptions[subdescription] or descriptions[subitem] or descriptions[subname]), 16) )
				end
				if not subitem:match( '\.gif$' ) and not subitem:match( '\.png$' ) then
					slot:wikitext( sprite( { data = "InvSprite", id = subitem } ) )
				end
				
				-- Set link
				local link = nil
				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
				
				-- Set the item count
				if count then
					local itemCount = mw.html.create( "span" )
					itemCount:cssText( "position: absolute; right: 0; bottom: -2px; pointer-events: none;" )
						:addClass( "c-0" )
						:addClass( "shadow" )
						:wikitext( count )
					
					slot:node( itemCount )
				end
				
				-- Use an image if the item ends in .gif or .png
				if subitem:match( '\.gif$' ) or subitem:match( '\.png$' ) then
					slot:wikitext( "<span class=\"sprite pixelated\" style=\"width: 32px; height: 32px;\">[[File:" .. subitem .. "|160px|link=" .. (link or "") .. "]]</span>" )
					slotContainer:wikitext( tostring( slot ) )
				else
					if link 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
		else
			local display = i == 1 and "display: inline-block" or "display: none"
			
			if item ~= 'Air' then
				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 } ) )
				
				local link = nil
				if args.link and args.link ~= "y" then
					if args.link ~= "n" then
						link = args.link
					end
				elseif (require("Module:Inventory slot/links").links[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: 0; bottom: -2px; 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
			else
				slotContainer:wikitext( "<span style='" .. display .. "'><!-- Air --></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