Module:Grid/Item

From Feed The Beast Wiki
Jump to: navigation, search
Lua Logo.svg This is the Lua module for Template:Grid/Item. Below is the documentation for that template.

This template is used inside of {{Grid}}'s content parameter to insert an item grid image with link and optional stack size. It can also be used to display any other image with a size of 32 × 32 that may be found in a GUI, e. g. the Metal Former's mode icons. However it cannot be used correctly outside of a Grid template, to display item images in article text (which should rarely be necessary) use either {{itemref}} or {{Grid2}} instead.

Parameters[edit]

  • name: The item's name, always used for hover text, and is used to determine image name and link target if not overwritten.
  • top: Distance of the top-left image corner from the top edge of the grid.
  • left: Distance of the top-left image corner from the left edge of the grid.
  • image: Replacement image to be displayed in place of the default grid image, without File: prefix. Default: File:Grid {{{name}}}.png
  • link: Replacement link target. Default: {{{name}}}
  • dict: Ore dictionary name in an ore dictionary enabled recipe.
  • amount: Stack size number, must be between 2 and 64 to be displayed. Default: Hidden
  • liquid: Liquid amount, must be a decimal number with 1 being 1000mB (or 1 Bucket). Default: Hidden; Only available if no value is given for amount
  • chance: Chance of getting a certain item after processing. Shown when hovering the cursor over the image of the item. Automatically puts "%" after a number. Default: Hidden
  • fill: A background color with which to fill the item slot. "Default: No background"
  • tooltext: Custom tooltip that will be displayed when hovering over the item.
  • noname: Removes item name from tooltip when using tooltext.

local p = {}

local g = require("Module:Common")
local s = require("Module:Sprite")
local cn = require("Module:Grid/Crafting_Square_Numbers")

function p.makeItem(name, amount, img, link, tooltip, top, left, fill, isDict)
	local output = '<div data-tooltip="' .. tooltip .. '" class="tooltip griditem" '
	output = output .. 'style="top:' .. g.px(top) .. ';left:' .. g.px(left)
	if g.isGiven(fill) then
		output = output .. ';background-color:' .. fill .. ';'
	end
	output = output .. '">'
	if isDict then
		output = output .. '<div style="position:absolute;">' .. g.img('Grid_Ore_Dictionary.png',32,link,name) .. '</div>'
	end
	output = output .. g.img(img, 32, link, name) .. '</div>'
	
	if amount > 1 then
		output = output .. '<div data-tooltip="' .. tooltip .. '" class="tooltip gridamount" '
		output = output .. 'style="top:' .. g.px(top + 16) .. ';left:' .. g.px(left - 4)
		output = output .. '">' .. cn.makeCraftingNumbers(amount, link) .. '</div>'
	end
	
	return output
end

function p.main(frame)
	local frame, args = g.getFrameAndArgs(frame)
	
	if not g.isGiven(args.name) then
		return ''
	end
	
	local tooltip = ''
	if g.isGiven(args.chance) and tonumber(args.chance) then
		local chance = tonumber(args.chance)
		if chance < 0 then
			chance = 0
		end
		if chance > 100 then
			chance = 100
		end
		tooltip = tooltip .. chance .. '% chance of&nbsp;'
	end
	
	local amount = 1
	if g.isGiven(args.amount) and tonumber(args.amount) then
		amount = tonumber(args.amount)
	end
	
	if amount > 1 then
		tooltip = tooltip .. amount .. '&nbsp;'
	elseif g.isGiven(args.liquid) and tonumber(args.liquid) then 
		local liquid = tonumber(args.liquid) * 1000
		tooltip = tooltip .. liquid .. 'mB of&nbsp;'
	end
	
	tooltip = tooltip .. args.name
	
	local tooltext = args.tooltext
	if g.isGiven(tooltext) then
		if g.isGiven(args.noname) then
			tooltip = tooltext
		else
			tooltip = tooltext .. ' - ' .. tooltip
		end
	end
	
	local image = args.image
	if not g.isGiven(image) then
		image = 'Grid ' .. args.name .. '.png'
	end
	
	local dict = args.dict
	local isDict = false
	if g.isGiven(dict) then
		tooltip = dict .. ' - ' .. tooltip
		isDict = true
	end
	
	local link = args.link
	if not g.isGiven(link) then
		if g.isGiven(dict) and 
			((not g.isGiven(args.dictamount)) or 
				tonumber(args.dictamount) > 1) then
			link = dict
		else
			link = args.name
		end
	end
	
	local top = 0
	if g.isGiven(args.top) and tonumber(args.top) then
		top = tonumber(args.top)
	end
	
	local left = 0
	if g.isGiven(args.left) and tonumber(args.left) then
		left = tonumber(args.left)
	end
	
	return p.makeItem(args.name, amount, image, link, tooltip, top, left, args.fill, isDict)
end

return p