Module:Inventory slot: Difference between revisions

From BTA-Mirror
imported>Ttv pedro270707
mNo edit summary
imported>Ttv pedro270707
mNo edit summary
Line 26: Line 26:
local slotCss = {
local slotCss = {
"display: inline-block",
"width: 32px",
"width: 32px",
"height: 32px",
"height: 32px",
Line 38: Line 39:
local slot = mw.html.create( "div" )
local slot = mw.html.create( "div" )
slot:addClass( "minecraft-item" )
slot:addClass( "minecraft-item" )
:cssText( table.concat( slotCss, ";" ) .. ";" .. display )
:cssText( table.concat( slotCss, ";" ) )
:attr( "data-mctitle", name )
:attr( "data-mctitle", name )
slot:wikitext( sprite( { data = "InvSprite", id = item } ) )
slot:wikitext( sprite( { data = "InvSprite", id = item } ) )
Line 48: Line 49:
slotContainer:wikitext( "<span style='" .. display .. "'>" .. "[[" .. link .. "|" .. tostring( slot ) .. "]]" .. "</span>" )
slotContainer:wikitext( "<span style='" .. display .. "'>" .. "[[" .. link .. "|" .. tostring( slot ) .. "]]" .. "</span>" )
else
else
slotContainer:wikitext( tostring( slot ) )
slotContainer:wikitext( "<span style='" .. display .. "'>" .. tostring( slot ) .. "</span>" )
end
end
end
end

Revision as of 01: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 = {}

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 display = i == 1 and "display: inline-block" or "display: none"
		
		local name = value.name
		local item = value.item
		local link = nil
		local slot = mw.html.create( "div" )
		slot:addClass( "minecraft-item" )
			:cssText( table.concat( slotCss, ";" ) )
			:attr( "data-mctitle", name )
		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
	
	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 = str:match("^%[(.-)%](.*)$")
    if name then
      table.insert(result, { name = name:gsub("^%s*(.-)%s*$", "%1"), item = item:gsub("^%s*(.-)%s*$", "%1") })
    else
      table.insert(result, { name = str:gsub("^%s*(.-)%s*$", "%1"), item = str:gsub("^%s*(.-)%s*$", "%1") })
    end
  end

  return result
end

return p