Please take a moment to fill out this survey on your gaming habits!

Module:KnightBot4/SpriteSub

From Minecraft Wiki
Jump to: navigation, search
[view | edit | history | purge]DocumentationJump to code ↴

This module implements {{User:KnightBot4/Sprite}}

[view | edit | history | purge]The above documentation is transcluded from Module:KnightBot4/SpriteSub/doc.
local p = {}

function p.sub( f )
	local args = f
	if mw.getCurrentFrame() == f then
		args = f:getParent().args
	end
	local name = args.name
	
	-- Find the name of the sprite sheet
	local module, sprite
	if name:find( 'Sprite' ) then
		module = 'Module:' .. mw.text.trim( name ) .. '/IDs'
		sprite = true
	else
		module = 'Module:' .. name:gsub( '^(.-)Link.*$', '%1Sprite/IDs' )
	end
	local ids = {}
	local names = {}

	-- if the sprite template does not have a sprite module with the standard name, skip it
	if mw.title.new( module ).exists then
		ids = mw.loadData( module ).ids or {} -- just in case

		-- Inverting the table into a "number = name" format
		for id, data in pairs( ids ) do
			if not data.deprecated then
				local num = data.pos
				if not names[num] then
					names[num] = {}
				end
				table.insert( names[num], id )
			end
		end
	end
	
	-- figure out information about the sprite
	local id = args.id or args[1]
	local idData = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
	
	-- if the sprite is deprecated
	if idData and idData.deprecated then
		local id = idData.pos
		if args.id then
			-- check all available names for the number to see if any match args[1]
			local check = mw.ustring.lower( args[1] ):gsub( '[%s%+]', '-' )
			for _, v in ipairs( names[id] ) do
				if args[1] == v or check == v then
					args.id = nil
					break
				end
			end
			
			-- if none do, then pull the first name, or if none exist return the original deprecated sprite
			if args.id then
				args.id = names[id] and names[id][1] or args.id
			end
		else
			-- only set args[1] if we are using sprite mode, otherwise set the id
			if sprite then
				args[1] = names[id] and names[id][1] or args[1]
			else
				args.id = names[id] and names[id][1] or nil
			end
		end
	end
	
	-- Return the added args
	local outArgs = {}
	local argTypes = { 'id', 'link', 1, 2, 'text' }
	for _, arg in ipairs( argTypes ) do
		if args[arg] then
			if arg == 1 or arg == 2 then
				table.insert( outArgs, args[arg] )
			else
				table.insert( outArgs, arg .. '=' .. args[arg] )
			end
		end
	end

	return '{{' .. name .. '|' .. table.concat( outArgs, '|' ) .. '}}'
end

return p