Module:IDInfo:修订间差异

来自Minecraft基岩版开发Wiki
添加的内容 删除的内容
无编辑摘要
无编辑摘要
第171行: 第171行:
local name = p.getName( objectType, infoboxArgs.title, data - 1, 'en_US' ) or 'none'
local name = p.getName( objectType, infoboxArgs.title, data - 1, 'en_US' ) or 'none'
table.insert( images, name .. '.png' )
table.insert( images, name .. '.png' )
args[ 'invimage' .. data ] = args[ 'invimage' .. data ] or name
args[ 'invimage' .. data - 1 ] = args[ 'invimage' .. data - 1 ] or name
end
end
for key, v in pairs( value ) do
for key, v in pairs( value ) do

2021年9月24日 (五) 07:53的版本

[创建 | 历史 | 清除缓存]文档页面
此模块没有文档页面。如果你知道如何使用模块,请创建它。
local p = {}
function p.getObjectData( ObjectType, ID )
	return mw.text.jsonDecode(
		mw.getCurrentFrame():expandTemplate { title = mw.ustring.format( 'Data:%s/%s', ObjectType, ID ) }
	)
end

function p.getValue( objectData, data, key )
	if data ~= nil and objectData.Data then
		return ( objectData.Data[ ( tonumber( data ) or 0 ) + 1 ] and objectData.Data[ ( tonumber( data ) or 0 ) + 1 ][ key ] ) or
		( objectData.Data[ 1 ] and objectData.Data[ 1 ][ key ] )
	end
	return objectData[ key ]
end

function p.toObjectType( str )
	str = mw.ustring.lower(str or 'other')
	if str == 'biome' or str == 'biomes' then
		return 'Biome'
	elseif str == 'block' or str == 'blocks' then
		return 'Block'
	elseif str == 'effect' or str == 'effects' then
		return 'Effect'
	elseif str == 'enchantment' or str == 'enchantments' then
		return 'Enchantment'
	elseif str == 'entity' or str == 'entities' then
		return 'Entity'
	elseif str == 'env' or str == 'environment' or str == 'environments' then
		return 'Env'
	elseif str == 'item' or str == 'items' then
		return 'Item'
	elseif str == 'mod' or mw.title.getCurrentTitle().nsText:find( '^Mod' ) then
		return 'Mod'
	end
	return 'Other'
end

function p.parseID( str, namespace )
	if str == '-' then
		return '-', '-'
	end
	local config = mw.text.jsonDecode(mw.getCurrentFrame():expandTemplate { title = 'Data:IDInfo' } )
	local function parse( _str )
		if string.match( _str, '^minecraft:[^:]+:%d+$' ) then
			return string.match( _str, '^(minecraft:[^:]+):(%d+)$' )
		elseif namespace and not string.match( _str, '^[^:]+:[^:]+:?.*$' ) then
			return 'minecraft:' .. _str, nil
		else
			return _str, nil
		end
	end
	local ID, Data = parse( str )
	if config.IDTranslation and config.IDTranslation[ ID ] then
		local data
		ID, data = parse( config.IDTranslation[ ID ] )
		Data = Data or data
	end
	return ID, Data or 0
end

function p.getKey( ObjectType, ID, data )
	local Key = p.getValue( p.getObjectData( ObjectType, ID ), data, 'Key' )
	if Key then
		return Key
	elseif ObjectType == 'Biome' then
		return '-'
	elseif ObjectType == 'Block' then
		return string.gsub( 'tile.' .. ID .. '.name', 'minecraft:', '', 1 )
	elseif ObjectType == 'Effect' then
		return string.gsub( 'effect.' .. ID, ':', '.' )
	elseif ObjectType == 'Enchantment' then
		return string.gsub( 'enchantment.' .. ID, ':', '.' )
	elseif ObjectType == 'Entity' then
		return string.gsub( 'entity.' .. ID .. '.name', 'minecraft:', '', 1 )
	elseif ObjectType == 'Env' then
		return '-'
	elseif ObjectType == 'Item' then
		return string.gsub( 'item.' .. ID .. '.name', 'minecraft:', '', 1 )
	else
		return '-'
	end
end

function p.getName( ObjectType, ID, data, language )
	local name = mw.text.jsonDecode( mw.getCurrentFrame():expandTemplate { title = 'Data:Lang/en_US' } )[ p.getKey( ObjectType, ID, data ) ]
	if language == 'en_US' or language == 'en' or language == 'English' or language == nil then
		return name
	elseif language == 'zh_CN' or language == 'zh' or language == 'Chinese' then
		return require( 'Module:Autolink' ).invlink( name, 'nolink', ObjectType .. 'Sprite' )
	end
	return nil
end

function p.getNumericID( ObjectType, ID)
	return p.getObjectData( ObjectType, ID ).NumericID
end

function p.getTags( ObjectType, ID, data )
	return p.getValue( p.getObjectData( ObjectType, ID ), data, 'Tags' )
end

function p.getMainInfo( ObjectType, ID, data, language )
	return
		p.getName( ObjectType, ID, data, language ),
		p.getNumericID( ObjectType, ID ),
		p.getKey( ObjectType, ID ,data )
end

function p.detect( ObjectType, ID )
	if not mw.title.makeTitle( 'Data', mw.ustring.format( '%s/%s', ObjectType, ID) ).exists then
		return mw.html.create( 'strong' ):addClass( 'error' ):wikitext(
			mw.ustring.format( '[[Category:缺失标识符信息的页面]]Error: Page [[Data:%s/%s]] does not exist.', ObjectType, ID )
		)
	end
	return nil
end

function p.main( f )
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local language, mode, objectType = args.language or 'zh_CN', mw.ustring.lower( args.mode ), p.toObjectType( args.type )
	local ID, data =
		p.parseID( args.id or '-', objectType == 'Block' or objectType == 'Entity' or objectType == 'Item' )
	local detect = p.detect( objectType, ID )
	if detect then
		return detect
	end
	data = args.data or data
	if mode == 'name' then
		return p.getName( objectType, ID, data, language )
	elseif mode == 'numericid' then
		return p.getNumericID( objectType, ID )
	elseif mode == 'key' then
		return p.getKey( objectType, ID, data )
	elseif mode == 'tags' then
		return table.concat( p.getTags( objectType, ID, data ), ',' )
	end
	return nil
end

function p.infobox ( f )
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local objectType = p.toObjectType ( mw.getCurrentFrame().args.ObjectType )
	local object = require( 'Module:IDInfo/' .. objectType )
	local infoboxArgs = {
		image = 'No_image.svg',
		invimage = 'none',
		subheader = object.subheader,
		footer = args.notes or args.footer,
		title = args.namespaceid or args.title or mw.ustring.gsub( mw.ustring.gsub( mw.ustring.lower( mw.title.getCurrentTitle().text ), '_%(.*%)$', '' ), ' ', '_' )
	}
	local dictionary = {}
	local attributes = {
		common = {}
	}
	local isEqual = {}
	local maxData = 0
	if ( args.autocomplete == 'true' or ( ( mw.title.getCurrentTitle().nsText == '' or mw.title.getCurrentTitle().nsText:find( '^Manual' ) ) and args.autocomplete ~= 'false' ) ) and mw.title.makeTitle( 'Data', objectType .. '/' .. infoboxArgs.title ).exists then
		local objectData = mw.text.jsonDecode( mw.getCurrentFrame():expandTemplate{ title = mw.ustring.format( 'Data:%s/%s', objectType, infoboxArgs.title ) } )
		for attribute, value in pairs( objectData ) do
			attributes.common[ attribute ] = value
		end
		if type( attributes.common.Data ) == 'table' then
			local images = {}
			for data, value in ipairs( objectData.Data ) do
				if type( value ) == 'table' then
					if attributes[ data ] == nil then
						attributes[ data ] = {}
						maxData = ( ( tonumber( data ) or maxData ) > maxData ) and tonumber( data ) or maxData
					end
					if value.Key then
						local name = p.getName( objectType, infoboxArgs.title, data - 1, 'en_US' ) or 'none'
						table.insert( images, name .. '.png' )
						args[ 'invimage' .. data - 1 ] = args[ 'invimage' .. data - 1 ] or name
					end
					for key, v in pairs( value ) do
						attributes[ data ][ mw.ustring.lower( key ) ] = v
					end
				end
			end
			args.image = args.image or table.concat( images, ';' )
		end
		attributes.common.Data = nil
	end
	for _, v in ipairs( object.attributeList ) do
		dictionary[ mw.ustring.lower( v ) ] = v
	end
	for i, v in pairs( args ) do
		if type( i ) == 'string' then
			local name, data = mw.ustring.match( i, '^(.-)(%d*)$' )
			data = tonumber( data ) and tonumber( data ) + 1 or 'common'
			if name == 'image' or name == 'invimage' then
				infoboxArgs[ i ] = v
			else
				if dictionary[ name ] then
					if attributes[ data ] == nil then
						attributes[ data ] = {}
						maxData = ( ( tonumber( data ) or maxData ) > maxData ) and tonumber( data ) or maxData
					end
					attributes[ data ][ dictionary[ name ] ] = v
				end
			end
		end
	end
	attributes = object.format( attributes )
	for _, attribute in ipairs( object.attributeList ) do
		local values = {}
		local count = {}
		for __, _attributes in pairs( attributes ) do
			values[ tostring( _attributes[ attribute ] ) ] = true
		end
		values[ 'nil' ] = nil
		for value, __ in pairs( values ) do
			table.insert( count, value )
		end
		if #count > 1 then
			isEqual[ attribute ] = false
		else
			isEqual[ attribute ] = true
			attributes.common[ attribute ] = count[ 1 ]
		end
	end
	for i, row in ipairs( object.infoboxRows ) do
		if type( row.field ) == 'table' then
			local count = 0
			for _, subrow in ipairs( row.field ) do
				count = isEqual[ object.attributeList[ subrow.field ] ] and ( count + 1 ) or count
			end
			object.infoboxRows[ i ][ 'equal' ] = #row.field == count
		else
			object.infoboxRows[ i ][ 'equal' ] = isEqual[ object.attributeList[ row.field ] ]
		end
	end
	local function createRows( data )
		local rows = mw.html.create()
		local changed = false
		for _, row in ipairs( object.infoboxRows ) do
			if data == 'common' == row.equal then
				if type( row.field ) == 'table' then
					local field = mw.html.create( 'div' ):addClass( 'infobox-rows subinfobox' )
					local count = 0
					for __, subrow in ipairs( row.field ) do
						if tostring( attributes[ data ][ object.attributeList[ subrow.field ] ] ) ~= 'nil' then
							field:tag( 'div' ):addClass( 'infobox-row' )
								:tag( 'div' ):addClass( 'infobox-cell-header' ):wikitext( '\'\'\'' .. subrow.laber .. '\'\'\'' ):done()
								:tag( 'div' ):addClass( 'infobox-cell-data' ):wikitext( attributes[ data ][ object.attributeList[ subrow.field ] ] ):done()
							:done()
							count = count + 1
						end
					end
					if count >= 1 then
						rows:tag( 'div' ):addClass( 'infobox-row' )
							:tag( 'div' ):addClass( 'infobox-cell-header' ):wikitext( '\'\'\'' .. row.laber .. '\'\'\'' ):done()
							:tag( 'div' ):addClass( 'infobox-cell-data' ):wikitext( tostring( field:done() ) ):done()
						:done()
						changed = true
					end
				elseif tostring( attributes[ data ][ object.attributeList[ row.field ] ] ) ~= 'nil' then
					rows:tag( 'div' ):addClass( 'infobox-row' )
						:tag( 'div' ):addClass( 'infobox-cell-header' ):wikitext( '\'\'\'' .. row.laber .. '\'\'\'' ):done()
						:tag( 'div' ):addClass( 'infobox-cell-data' ):wikitext( attributes[ data ][ object.attributeList[ row.field ] ] ):done()
					:done()
					changed = true
				end
			end
		end
		return ( ( data ~= 'common' and changed ) and mw.ustring.format( '</div><div class="infobox-subheader">数据值:%d</div><div class="infobox-rows">', tonumber( data ) - 1 ) or '' ) .. tostring( rows )
	end
	local infoboxRows = {}
	for data, _ in pairs( attributes ) do
		infoboxRows[ data ] = createRows( data )
	end
	for data = 0, maxData do
		infoboxRows[ data ] = infoboxRows[ data ] or ''
	end
	infoboxArgs.rows = infoboxRows.common .. table.concat( infoboxRows, '' )
	return require( 'Module:Infobox' ).infobox( infoboxArgs )
end

return p