Terraria Wiki

Miss the old Hydra Skin? Try out our Hydralize gadget! Visit the preferences page while logged in and turn on the gadget.

READ MORE

Terraria Wiki
Advertisement
Lua logo.svg Documentation The documentation below is transcluded from Module:Test/doc. (edit | history)

A test module to avoid disturbing "real" pages.

See Terraria Wiki:Lua for more information about Lua and links to other help resources.

Feel free to test this module at Terraria Wiki:Sandbox.


local data = mw.loadData( 'Module:Iteminfo/data' ) -- loading data table.

local trim = mw.text.trim

-- cache
local currentFrame

-- helper function
local getArg = function(key)
	local value = currentFrame.args[key]
	if not value then
		return nil
	end
	value = trim(value)
	if value == '' then
		return nil
	else
		return value
	end
end

-- credit: http://richard.warburton.it
-- this version is with trim.
local function explode(div,str) 
	if (div=='') then return false end
	local pos,arr = 0,{}
	-- for each divider found
	for st,sp in function() return string.find(str,div,pos,true) end do
		arr[#arr + 1] = trim(string.sub(str,pos,st-1)) -- Attach chars left of current divider
		pos = sp + 1 -- Jump past current divider
	end
	arr[#arr + 1] = trim(string.sub(str,pos)) -- Attach chars right of last divider
	return arr
end

local getStat = function(itemid, stat)
	return data[itemid][stat] or data[0][stat] or nil -- "or nil" will convert "false" to "nil"
end

local statname = {
	["acc"] = "accessory",
	["autoreuse"] = "autoReuse",
	["autoswing"] = "autoReuse",
	["auto"] = "autoReuse",
	["axe power"] = "axe",
	["axepower"] = "axe",
	["bait power"] = "bait",
	["baitpower"] = "bait",
	["body slot"] = "bodySlot",
	["bodyslot"] = "bodySlot",
	["buff duration"] = "buffTime",
	["buff id"] = "buffType",
	["buffduration"] = "buffTime",
	["buffid"] = "buffType",
	["bufftime"] = "buffTime",
	["bufftype"] = "buffType",
	["carttrack"] = "cartTrack",
	["createtile"] = "createTile",
	["createwall"] = "createWall",
	["critical"] = "crit",
	["dd2summon"] = "DD2Summon",
	["dmg"] = "damage",
	["fishing power"] = "fishingPole",
	["fishingpole"] = "fishingPole",
	["fishingpower"] = "fishingPole",
	["fishing"] = "fishingPole",
	["hair dye"] = "hairDye",
	["hairdye"] = "hairDye",
	["hammer power"] = "hammer",
	["hammerpower"] = "hammer",
	["head slot"] = "headSlot",
	["headslot"] = "headSlot",
	["heallife"] = "healLife",
	["healmana"] = "healMana",
	["holdstyle"] = "holdStyle",
	["id"] = "type",
	["internal name"] = "internalName",
	["internalname"] = "internalName",
	["item id"] = "type",
	["itemid"] = "type",
	["kb"] = "knockBack",
	["knockback"] = "knockBack",
	["leg slot"] = "legSlot",
	["legslot"] = "legSlot",
	["liferegen"] = "lifeRegen",
	["makenpc"] = "makeNPC",
	["maxstack"] = "maxStack",
	["mount id"] = "mountType",
	["mountid"] = "mountType",
	["mounttype"] = "mountType",
	["mount"] = "mountType",
	["nomelee"] = "noMelee",
	["notammo"] = "notAmmo",
	["nousegraphic"] = "noUseGraphic",
	["nowet"] = "noWet",
	["pick power"] = "pick",
	["pickpower"] = "pick",
	["pickaxe power"] = "pick",
	["pickaxepower"] = "pick",
	["pickaxe"] = "pick",
	["placestyle"] = "placeStyle",
	["projectileid"] = "shoot",
	["projectile id"] = "shoot",
	["questitem"] = "questItem",
	["range bonus"] = "tileBoost",
	["rangebonus"] = "tileBoost",
	["rarity"] = "rare",
	["reusedelay"] = "reuseDelay",
	["shootspeed"] = "shootSpeed",
	["sound"] = "UseSound",
	["sounduse"] = "UseSound",
	["stack"] = "maxStack",
	["stringcolor"] = "stringColor",
	["tile id"] = "createTile",
	["tile sub id"] = "placeStyle",
	["tile subid"] = "placeStyle",
	["tilesubid"] = "placeStyle",
	["tile wand"] = "tileWand",
	["tileboost"] = "tileBoost",
	["tileid"] = "createTile",
	["tilewand"] = "tileWand",
	["tile"] = "createTile",
	["tool speed"] = "useTime",
	["toolspeed"] = "useTime",
	["tooltip"] = "ToolTip",
	["uniquestack"] = "uniqueStack",
	["use delay"] = "reuseDelay",
	["use sound"] = "UseSound",
	["use style"] = "useStyle",
	["use time"] = "useAnimation",
	["useammo"] = "useAmmo",
	["useanimation"] = "useAnimation",
	["usedelay"] = "reuseDelay",
	["usesound"] = "UseSound",
	["usestyle"] = "useStyle",
	["usetime"] = "useAnimation",
	["useturn"] = "useTurn",
	["use"] = "useAnimation",
	["velocity"] = "shootSpeed",
	["wall id"] = "createWall",
	["wallid"] = "createWall",
	["wall"] = "createWall",
	["sell"] = "value",
	--non-native stats:
	["buy"] = "buyValue",
	["price"] = "buyValue",
	["buffname"] = "buffName",
	["buff name"] = "buffName",
	["buff"] = "buffName",
	["research"] = "sacrifices"
}

local proc = {}
proc.axe = function(value)
	return currentFrame:expandTemplate{ title = 'percent', args = { tostring((value or 0) * 5) .. '%' } }
end
proc.bodySlot = function(value)
	return (value == -1) and "" or value
end
proc.createTile = proc.bodySlot
proc.createWall = proc.bodySlot
proc.damage = proc.bodySlot
proc.hairDye = proc.bodySlot
proc.headSlot = proc.bodySlot
proc.legSlot = proc.bodySlot
proc.mountType = proc.bodySlot
proc.tileWand = proc.bodySlot
proc.bait = function(value)
	return currentFrame:expandTemplate{ title = 'percent', args = { tostring(value) .. '%' } }
end
proc.fishingPole = proc.bait
proc.hammer = proc.bait
proc.pick = proc.bait
proc.buffTime = function(value)
	return currentFrame:expandTemplate{ title = 'duration', args = { (value or 0)/60 } }
end
proc.buffName = function(value, id)
	return currentFrame:expandTemplate{ title = 'GetBuffInfo', args = { getStat(id, 'buffType'), 'ename' } }
end
proc.crit = function(value, id)
	if getStat(id, 'damage') == -1 then
		return currentFrame:expandTemplate{ title = 'percent', args = { tostring(value) .. '%' } } -- no damage value
	else
		return currentFrame:expandTemplate{ title = 'percent', args = { tostring(value+4) .. '%' } }  -- has damage value
	end
end
proc.damagetype = function(value, id)
	if getStat(id, 'melee') then
		return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'damagetype_melee' } }
	end
	if getStat(id, 'ranged') then
		return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'damagetype_ranged' } }
	end
	if getStat(id, 'magic') then
		return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'damagetype_magic' } }
	end
	if getStat(id, 'summon') then
		return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'damagetype_summon' } }
	end
	if getStat(id, 'thrown') then
		return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'damagetype_thrown' } }
	end
end
proc.rare = function(value)
	return currentFrame:expandTemplate{ title = 'rare', args = { (value or 0) } }
end
proc.tileBoost= function(value)
	if value ~= 0 then
		if value > 0 then
			value = '+' .. tostring(value)
		else
			value = tostring(value)
		end
		return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'tileBoost', ['$tiles$'] = value } }
	end
end
proc.useAmmo = function(value)
	if value == 40 or value == 71 or value == 97 or value == 283 or value == 771 or value == 931 then
		return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'ammo'..tostring(value) } }
	end
	return currentFrame:expandTemplate{ title = 'itemNameFromId', args = { value } }
end
proc.UseSound = function(value)
	local str = ''
	for _,v in pairs(explode(',', value)) do
		str = str .. currentFrame:expandTemplate{ title = 'sound', args = { '', v..'.wav' } }
	end
	return str
end
proc.useStyle = function(value)
	if value == 0 then
		return
	end
	if value >= 1 and value <= 5 then
		return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'usestyle'..tostring(value) } }
	end
	return value
end
proc.buyValue = function(value, id)
	local t=getStat(id, 'shopCustomPrice')
	if t then
		return currentFrame:expandTemplate{ title = 'dm', args = { t } }
	else
		return currentFrame:expandTemplate{ title = 'coin', args = { tostring(getStat(id, 'value'))..'cc' } }
	end
end
proc.value = function(value)
	value = tonumber(value or 0)
	if value > 0 then
		if value < 5 then
			-- items with value<5 have a sell value of 1, not 0 (contrary to regular rounding)
			return currentFrame:expandTemplate{ title = 'coin', args = { '1cc' } }  
		else
			return currentFrame:expandTemplate{ title = 'coin', args = { tostring(math.floor(value/5))..'cc' } }  
		end
	else
		return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'noValue' } }
	end
end


---------------------------

local p = {
	getItemInfo = function(itemid)
		if itemid >= 5088 or itemid <= 0 then
			itemid = 0
		end
		local result = {['type'] = itemid}
		for k,v in pairs(data[0]) do
			result[k] = v
		end
		for k,v in pairs(data[itemid]) do
			result[k] = v
		end
		return result
	end,

	-- for template, get all stats, setting in dplvars.
	getInfo = function(frame)
		currentFrame = frame -- cache

		local itemid = tonumber(getArg('id')) or tonumber(getArg(1)) or 0
		local prefix = getArg('prefix') or getArg(2) or '_iteminfo_'

		if itemid >= 5088 or itemid <= 0 then
			itemid = 0
		end
		
		local result = {['type'] = itemid}
		for k,v in pairs(data[0]) do
			result[k] = v
		end
		for k,v in pairs(data[itemid]) do
			result[k] = v
		end

		local args = {}
		for k,v in pairs(result) do
			args[#args + 1] = prefix .. k
			args[#args + 1] = v
		end

		frame:callParserFunction{ name = '#dplvar:set', args = args }
	end,

	-- for other module, get single stat, return this stat directly
	getItemStat = function(itemid, stat)
		if stat == 'count' then
			return 5087
		end
		if itemid >= 5088 or itemid <= 0 then
			itemid = 0
		end
		if stat == 'type' then
			return itemid
		end
		return data[itemid][stat] or data[0][stat]
	end,

	-- for template, get single stat, return directly
	getStat = function(frame)
		currentFrame = frame -- cache

		local itemid = tonumber(getArg('id')) or tonumber(getArg(1)) or 0
		local stat = getArg('stat') or getArg(2)
		stat = statname[stat] or stat

		if not stat then
			return
		end

		if itemid >= 5088 or itemid <= 0 then
			itemid = 0
		end

		local result
		if stat == 'type' then
			return itemid
		end
		if stat == 'count' then
			return 5087
		end
		result = getStat(itemid, stat) 
		if not getArg('raw') and proc[stat] then
			result = proc[stat](result, itemid)
		end

		if getArg('format') then
			result = frame:callParserFunction('formatnum', result)
		end

		return result
	end,
}

p.go = function()
	local table_output = mw.html.create('table'):addClass('terraria sortable cellborder')
	local header = table_output:tag('tr')
	header:tag('th'):wikitext('Item ID')
	header:tag('th'):wikitext('Item')
	header:tag('th'):wikitext('Tile ID')
	header:tag('th'):wikitext('Tile sub ID')
	for i=1, 5087 do
		local tile_id = p.getItemStat(i, 'createTile')
		if tile_id ~= -1 then 
			local row = table_output:tag('tr')
			row:tag('td'):wikitext(i)
			row:tag('td'):wikitext('[['..p.getItemStat(i, 'name') ..']]')
			row:tag('td'):wikitext( tile_id )
			row:tag('td'):wikitext( p.getItemStat(i, 'placeStyle') )
		end
	end
	return tostring(table_output)
end


return p
Advertisement