Difference between revisions of "Module:InventorySlot"

From Gamemode 4
Jump to navigation Jump to search
m (Shouldn't help so frick)
(Should now add `invslot` to html item slots)
Line 60: Line 60:
 
else
 
else
 
slot = mw.html.create("div")
 
slot = mw.html.create("div")
 +
end
 +
slot:addClass( "invslot" )
 +
 +
if self.__large then
 +
slot:addClass( "invslot-large")
 
end
 
end
  
 
if self.__html then
 
if self.__html then
return slot:wikitext(self.__html)
+
slot:wikitext(self.__html)
 
end
 
end
 
slot:addClass( "invslot" )
 
  
 
if self.__sprite then
 
if self.__sprite then
local spriteDiv = self.__sprite:GenerateHTML(slot)
+
self.__sprite:GenerateHTML(slot)
end
 
if self.__large then
 
slot:addClass( "invslot-large")
 
 
end
 
end
 
return slot
 
return slot

Revision as of 18:55, 4 April 2019

-- Based on work by the MC wiki: https://minecraft.gamepedia.com/

local UISlot = {}

local Sprite = require([[Module:Sprite]]):new("inventory")

function UISlot:new(item, large)
	o = {}
	setmetatable(o, {__index = self})
	o:SetItem(item)
	o.__large = large
	return o
end

function UISlot:SetItem(item)
	if string.sub(item or "",1,1) == '<' then
		-- If the item is already html just save that
		self.__html = item
		return self
	end

	if item == "" then item = nil end
	self.__sprite = item and Sprite:Item(item) --nil if item = nil else get sprite
	return self
end

function UISlot.Item(item, large, html)
	return UISlot:new(item, large):GenerateHTML(html)
end

function UISlot.Call(items, width, height, size)
	local w = width or 1
	local h = height or 1
	local grid = {}
	local large = size
	for i = 1,h do
		grid[i] = {}
		for j = 1,w do
			grid[i][j] = mw.text.trim(items[j+(i-1)*w] or "")
		end
	end
	return UISlot.Grid(grid, large)
end

function UISlot.Grid(items, large)
	local root = mw.html.create( "table" ):addClass("invgrid")
	for i = 1, #items do
		local row = root:tag("tr"):addClass("invrow")
		for j = 1, #items[i] do
			UISlot.Item(items[i][j], large, row)
		end
	end
	return root
end

function UISlot:GenerateHTML (html)
	local slot
	if html then
		slot = html:tag("td")
	else
		slot = mw.html.create("div")
	end
	slot:addClass( "invslot" )

	if self.__large then
		slot:addClass( "invslot-large")
	end

	if self.__html then
		slot:wikitext(self.__html)
	end

	if self.__sprite then
		self.__sprite:GenerateHTML(slot)
	end
	return slot
end

return UISlot