Module:IDTable

来自Minecraft基岩版开发Wiki
[创建 | 历史 | 清除缓存]文档页面
此模块没有文档页面。如果你知道如何使用模块,请创建它。
local p = {}
local IDInfo = require('Module:IDInfo')
local getArgs = require('Module:Arguments').getArgs

function p.row(f)
	local object = {}
	local args = getArgs(f)
	if args.id or args[2] then
		object.Type = IDInfo.getObjectType(tostring(args.type or args[1]))
		object.ID = args.id or args[2]
	else
		object.Type = 'Other'
		object.ID = '-'
	end
	object.ID, object.Data =
	IDInfo.getIdentifier(
		tostring(object.ID or '-'),
		object.Type == 'Block' or object.Type == 'Entity' or object.Type == 'Item'
	)
	object.Data = args.datavalue or object.Data
	if object.Type ~= 'Mod' and object.Type ~= 'Other' and object.ID ~= '-' and
			mw.title.makeTitle('Data', object.Type .. '/' .. object.ID).exists
	then
		object.RawName, object.NumericID, object.Key =
		IDInfo.getMainInfo(object.Type, object.ID, object.Data, 'en_US')
		object.Name =
		require('Module:Sprite').sprite {
			[1] = object.RawName,
			data = object.Type .. 'Sprite',
			text = args.name or object.RawName
		}

		if object.Type == 'Biome' or object.Type == 'Block' or object.Type == 'Item' then
			object[object.Type .. 'Tags'] =
			table.concat(IDInfo.getTags(object.Type, object.ID, object.Data) or {}, ';')
		end
	else
		object.Name = args.name or '-'
	end
	object.NumericID = args.numericid or object.NumericID or '-'
	object.Data = args.datavalue or object.Data or '-'
	object.BiomeTags = args.biometags or object.BiomeTags or '-'
	object.BlockTags = args.blocktags or object.BlockTags or '-'
	object.ItemTags = args.itemtags or object.ItemTags or '-'
	object.Key = args.key or object.Key or '-'
	if object.BiomeTags == '' then
		object.BiomeTags = '-'
	end
	if object.BlockTags == '' then
		object.BlockTags = '-'
	end
	if object.ItemTags == '' then
		object.ItemTags = '-'
	end
	return mw.text.jsonEncode(object)
end

function p.main(f)
	local objects = {}
	local visible = {
		NumericID = true,
		Data = true,
		Biome = false,
		Block = false,
		Item = false
	}
	local header = '名称'
	local args = getArgs(f)
	if args.shownumericid == 'false' or
			(mw.title.getCurrentTitle():inNamespaces(3004, 3005) and args.shownumericid ~= 'true')
	then
		visible.NumericID = false
	end
	if args.showbiometags and args.showbiometags ~= 'false' then
		visible.Biome = true
	end
	if args.showblocktags and args.showblocktags ~= 'false' then
		visible.Block = true
	end
	if args.showitemtags and args.showitemtags ~= 'false' then
		visible.Item = true
	end
	if args.showdatavalue == 'false' then
		visible.Data = false
	end
	if args.header and args.header ~= '*' then
		header = args.header
	end
	local function getTags(str)
		return mw.ustring.gsub(
			mw.ustring.gsub('<code>' .. (str or '-') .. '</code>', '%s*;%s*', '</code><br><code>'),
			'<code>%-?</code>',
			'-'
		)
	end

	local function getIDRowSpan(key, ID, NumericID)
		local count = 0
		repeat
			count = count + 1
			key = key + 1
		until objects[key] == nil or objects[key]['ID'] ~= ID or objects[key]['NumericID'] ~= NumericID
		return count
	end

	local function getTagRowSpan(key, value, Type, ID, NumericID)
		local count = 0
		local _count = getIDRowSpan(key, ID, NumericID)
		repeat
			count = count + 1
			key = key + 1
		until objects[key] == nil or objects[key][Type] ~= value
		if count > _count then
			return _count
		end
		return count
	end

	for _, value in ipairs(args) do
		local object = mw.text.jsonDecode(value, mw.text.JSON_TRY_FIXING)
		local function code(key)
			object[key] = object[key] ~= '-' and '<code>' .. object[key] .. '</code>' or object[key]
		end

		code('ID')
		code('NumericID')
		code('Data')
		code('Key')
		object.BiomeTags = visible.Biome and getTags(object.BiomeTags) or nil
		object.BlockTags = visible.Block and getTags(object.BlockTags) or nil
		object.ItemTags = visible.Item and getTags(object.ItemTags) or nil
		table.insert(objects, object)
	end
	local result =
	mw.html.create('table'):addClass('wikitable')
			:tag('tr')
			:tag('th'):wikitext(header):done()
			:tag('th'):wikitext('[[标识符|赋命名空间标识符]]'):done()
	if visible.NumericID then
		result = result:tag('th'):wikitext('[[标识符|数字标识符]]'):done()
	end
	if visible.Data then
		result = result:tag('th'):wikitext('[[数据值]]'):done()
	end
	if visible.Biome then
		result = result:tag('th'):wikitext('[[生物群系/标签|生物群系标签]]'):done()
	end
	if visible.Block then
		result = result:tag('th'):wikitext('[[方块/标签|方块标签]]'):done()
	end
	if visible.Item then
		result = result:tag('th'):wikitext('[[物品/标签|物品标签]]'):done()
	end
	result = result:tag('th'):wikitext('本地化键名'):done():done()
	local _ID, _NumericID, isIDChanged
	local _Tags, isTagsChanged = {}, {}
	for k, object in ipairs(objects) do
		if object.ID ~= _ID or object.NumericID ~= _NumericID then
			_ID = object.ID
			_NumericID = object.NumericID
			isIDChanged = true
			isTagsChanged = { Biome = true, Block = true, Item = true }
		end
		if object.BiomeTags ~= _Tags.Biome then
			_Tags.Biome = object.BiomeTags
			isTagsChanged.Biome = true
		end
		if object.BlockTags ~= _Tags.Block then
			_Tags.Block = object.BlockTags
			isTagsChanged.Block = true
		end
		if object.ItemTags ~= _Tags.Item then
			_Tags.Item = object.ItemTags
			isTagsChanged.Item = true
		end
		result = result:tag('tr'):css('text-align', 'center'):tag('td'):wikitext(object.Name):done()
		if isIDChanged then
			isIDChanged = false
			local rowSpan = getIDRowSpan(k, object.ID, object.NumericID)
			result = result:tag('td'):attr({ rowspan = rowSpan }):wikitext(object.ID):done()
			if visible.NumericID then
				result = result:tag('td'):attr({ rowspan = rowSpan }):wikitext(object.NumericID):done()
			end
		end
		if visible.Data then
			result = result:tag('td'):wikitext(object.Data):done()
		end
		if visible.Biome and isTagsChanged.Biome then
			isTagsChanged.Biome = false
			result =
			result:tag('td'):attr(
				{ rowspan = getTagRowSpan(k, object.BiomeTags, 'BiomeTags', object.ID, object.NumericID) }
			):wikitext(object.BiomeTags):done()
		end
		if visible.Block and isTagsChanged.Block then
			isTagsChanged.Block = false
			result =
			result:tag('td'):attr(
				{ rowspan = getTagRowSpan(k, object.BlockTags, 'BlockTags', object.ID, object.NumericID) }
			):wikitext(object.BlockTags):done()
		end
		if visible.Item and isTagsChanged.Item then
			isTagsChanged.Item = false
			result =
			result:tag('td'):attr(
				{ rowspan = getTagRowSpan(k, object.ItemTags, 'ItemTags', object.ID, object.NumericID) }
			):wikitext(object.ItemTags):done()
		end
		result = result:tag('td'):wikitext(object.Key):done():done()
	end
	return tostring(result)
end

return p