Module:ItemNames

From Terraria Wiki
Jump to navigation Jump to search
Important.svg
CAUTION: Terraria Wiki code is complex!!!
If you want to use this code on another wiki, wiki.gg staff are not able to assist you.
Please consider picking a different wiki to adapt code from, or making your own templates!
Remember that content on a wiki is more important than fancy formatting.
Lua.svg Documentation The documentation below is transcluded from Module:ItemNames/doc. (edit | history)

This module makes item name and ID information available. It should not be invoked normally in article text.

Usage

Wikitext

The module can be called from wikitext with the functions listed below.

loadData

{{#invoke:ItemNames| loadData | <table name> }}

Sets variables for all items for the given table, as demonstrated below. The variable names are prefixed with the table's name. For example, {{#invoke:ItemNames|loadData|itemNameFromId}} sets the variables {{#var:itemNameFromId:1}} (Iron Pickaxe), {{#var:itemNameFromId:2}} (Dirt Block), and so on, up to {{#var:itemNameFromId:5455}} (Guide to Peaceful Coexistence (Inactive)).

This function is only intended to be called from a template (one for each table), namely:

Other modules

The module can be called from another module with the functions listed below.

getData

require('Module:ItemNames').getData('<table name>', <value>)

Returns the data for the given value from the given table. Available tables and expected values:

Code Variable result
local itemNames = require('Module:ItemNames')
local result = itemNames.getData('itemNameFromId', 3279)
'Malaise'
local itemNames = require('Module:ItemNames')
local result = itemNames.getData('itemIdFromName', 'Malaise')
'3279'
local itemNames = require('Module:ItemNames')
local result = itemNames.getData('itemInternalNameFromId', 3279)
'CorruptYoyo'
local itemNames = require('Module:ItemNames')
local result = itemNames.getData('itemIdFromInternalName', 'CorruptYoyo')
'3279'

Beware that item IDs are returned as strings, not integers.


local itemdata = mw.loadData('Module:ItemNames/loaddata')
local VariablesLua = mw.ext.VariablesLua

return {
	loadData = function(frame)
		local tableName = frame.args[1]
		
		local data = itemdata[tableName]
		local prefix = tableName .. ':'
		
		for k, v in pairs(data) do
			VariablesLua.vardefine( prefix .. k, v)
		end
		
		VariablesLua.vardefine( prefix .. '__OK__', '__OK__')
	end,
	
	getData = function(tableName, value)
		return itemdata[tableName][value]
	end,
}