Module:Inventory slot: Difference between revisions

From BTA-Mirror
imported>Ttv pedro270707
m (Made it so that parseString() returns a nil name now, allowing "Any Wool" or "Any Planks" to be an item name)
imported>Ttv pedro270707
mNo edit summary
Line 74: Line 74:
local name = value.name
local name = value.name
local item = value.item
local item = value.item
local count = value.count
if p.itemSets[item] then
if p.itemSets[item] then
for j, subvalue in ipairs(p.itemSets[item]) do
for j, subvalue in ipairs(p.itemSets[item]) do
Line 85: Line 86:
if item ~= pageName and subvalue then
if item ~= pageName and subvalue then
link = subvalue
link = subvalue
end
if count then
local itemCount = mw.html.create( "span" )
itemCount:cssText( "position: absolute; right: -2px; bottom: -5px; pointer-events: none;" )
:addClass( "c-f" )
:addClass( "shadow" )
:wikitext( count )
end
end
Line 125: Line 134:


for _, str in ipairs(strings) do
for _, str in ipairs(strings) do
    local name, item = str:match("^%[(.-)%](.*)$")
local name, item, count = str:match("^%[(.-)%](.-),(%d+)$")
    if name then
if not name then
    table.insert(result, { name = name:gsub("^%s*(.-)%s*$", "%1"), item = item:gsub("^%s*(.-)%s*$", "%1") })
name, item = str:match("^%[(.-)%](.-)$")
    else
end
    table.insert(result, { name = nil, item = str:gsub("^%s*(.-)%s*$", "%1") })
if name then
    end
table.insert(result, { name = name:gsub("^%s*(.-)%s*$", "%1"), item = item:gsub("^%s*(.-)%s*$", "%1"), count = tonumber(count) })
else
table.insert(result, { name = nil, item = str:gsub("^%s*(.-)%s*$", "%1"), count = nil })
end
end
end



Revision as of 02:35, 11 March 2023

This module creates an inventory slot. {{Inventory slot}} should be preferred, rather than directly invoking this module on the article.


local p = {}

p.itemSets = {
	["Any Wool"] = {
		"White Wool",
		"Orange Wool",
		"Magenta Wool",
		"Light Blue Wool",
		"Yellow Wool",
		"Lime Wool",
		"Pink Wool",
		"Gray Wool",
		"Light Gray Wool",
		"Cyan Wool",
		"Purple Wool",
		"Blue Wool",
		"Brown Wool",
		"Green Wool",
		"Red Wool",
		"Black Wool"
	},
	["Any Planks"] = {
		"Wooden Planks",
		"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"
	}
}

function p.createSlot( f )
	local args = f
	if f == mw.getCurrentFrame() then 
		args = require( 'Module:ProcessArgs' ).merge( true )
	else
		f = mw.getCurrentFrame()
	end
	
	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
	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 p.itemSets[item] then
			for j, subvalue in ipairs(p.itemSets[item]) do
				local display = i == j == 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 subvalue) )
				slot:wikitext( sprite( { data = "InvSprite", id = subvalue } ) )
				if item ~= pageName and subvalue then
					link = subvalue
				end
				
				if count then
					local itemCount = mw.html.create( "span" )
					itemCount:cssText( "position: absolute; right: -2px; bottom: -5px; pointer-events: none;" )
						:addClass( "c-f" )
						:addClass( "shadow" )
						:wikitext( count )
				end
				
				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
		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) )
			slot:wikitext( sprite( { data = "InvSprite", id = item } ) )
			if item ~= pageName and item then
				link = item
			end
			
			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
	
	return slotContainer
end

function p.parseString(input)
	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 not name then
			name, item = str:match("^%[(.-)%](.-)$")
		end
		if name then
			table.insert(result, { name = name:gsub("^%s*(.-)%s*$", "%1"), item = item:gsub("^%s*(.-)%s*$", "%1"), count = tonumber(count) })
		else
			table.insert(result, { name = nil, item = str:gsub("^%s*(.-)%s*$", "%1"), count = nil })
		end
	end

	return result
end

return p