Module:Sandbox/Block distribution

Jump to navigation Jump to search
Documentation[create] [refresh]
This module has no documentation. If you know how to use this module, please create it.
local p = {}

function getLocation(resourceLocation) 
	-- returns location if it's nil
	local namespace = 'minecraft'
	if string.find(resourceLocation, ':') == nil then
		resourceLocation = namespace .. ':' .. resourceLocation
	end
	return resourceLocation
end

function addResourceToTable(added, displayName, resourceLocation, blocks)
	-- return added and blocks table
	-- if the name is 'Block', set the name in the chart to the resourceLocation instead
	if not added[resourceLocation] and displayName ~= 'Block' then
		added[resourceLocation] = displayName
		table.insert(blocks, { displayName, resourceLocation } )
	elseif not added[resourceLocation] and displayName == 'Block' then
		added[resourceLocation] = resourceLocation
		table.insert(blocks, { resourceLocation, resourceLocation } )
	end
	return added, blocks
end

function p.main( f )
	local args = f
	local argOrder = {}
	local out = mw.html.create('div')
		
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge( true )
	else
		f = mw.getCurrentFrame()
	end

	local additionalParams = ''
	if args.additionalParams then
		additionalParams = '[[' ..args.additionalParams.. ']]'
	end
	local pageNames = {}
	if args[1] then
		pageNames[1] = args[1]
		argOrder[args[1]] = 1
	else
		local titleText = mw.title.getCurrentTitle().text
		table.insert(pageNames, titleText)
		argOrder[titleText] = 1
	end
	local pageNamesStr = table.concat(pageNames, '||')
	
	local query = {
			'[[Display name::' ..pageNamesStr.. ']]',
			'[[Resource location JSON::+]]',
			additionalParams,
			'?#-=',
			'?Resource location JSON',
			limit = 500
		}
	
	local smwdata = mw.smw.ask(query)
	local data = {}


	if smwdata and (smwdata[1]['Resource location JSON'] ~= '+') then
		for _, v in ipairs(smwdata) do

			if(type(v['Resource location JSON']) == 'string') then
				local t = mw.text.jsonDecode(v['Resource location JSON'])
				t['Sortkey'] = mw.text.split(v[1], '%#')
				deepSlate = string.gsub(t['Sortkey'][2], 'deepslate', '~deepslate')
				
				if not deepSlate == '' then
					t['Sortkey'][2] = #deepSlate
				end
				table.insert(data, t)
			elseif type(v['Resource location JSON']) == 'table' then
				for _, w in ipairs(v['Resource location JSON']) do
					local t = mw.text.jsonDecode(w)
					t['Sortkey'] = mw.text.split(v[1], '%#')
					if not deepSlate == '' then
						t['Sortkey'][2] = #deepSlate
					end
					table.insert(data, t)
				end
			end
		end
	else
		local queryStr = mw.text.nowiki(table.concat(query, '  '))
		output = mw.html.create('div')
		output
			:addClass('error')
			:wikitext('Query \" ' .. queryStr .. ' \" could not find any objects.<br/>')
		if smwdata then
			output
				:wikitext('data returned: <br/>' ..mw.dumpObject(smwdata).. '<br/>')
		end
		return output
	end
	-- Sort blocks by arg input order
	-- Blocks that share a page are subsorted by resource id
	table.sort(data, function(a, b)
		local aPos = argOrder[a['Sortkey'][1]] or 99999
		local bPos = argOrder[b['Sortkey'][1]] or 99999
		
		if aPos < bPos then
			return true
		elseif aPos > bPos then
			return false
		elseif aPos == bPos then
			return a['Sortkey'][2] < b['Sortkey'][2]
		end
	end)
	
	local blocks, added = {}, {}
	
	if #data > 0 then
		for _, json in ipairs(data) do
			jsondump = mw.dumpObject(json)
			
			local displayName = json['Display name']
			local resourceLocation = json['Resource location']
			local edition = json['Edition']
			local isBlock = (json['Type'] == 'block')
			
			
			local isJavaBlock = resourceLocation ~= nil and displayName ~= nil and edition == 'java' and isBlock
			
			if isJavaBlock then
				resourceLocation = getLocation(resourceLocation)
				added, blocks = addResourceToTable(added, displayName, resourceLocation, blocks)
			end
		end
	end
	
	local nameList, idList = {}, {}
	for _, v in ipairs(blocks) do
		table.insert(nameList, v[1])
		table.insert(idList, v[2])
	end
	nameListStr = mw.dumpObject(nameList)
	idListStr = mw.dumpObject(idList)

	 out
	 	:addClass('mcw-calc')
	 	:attr({
	 		['data-type'] = 'blockDistribution',
	 		['data-block-names'] = table.concat(nameList, ','),
	 		['data-blocks'] = table.concat(idList, ','),
	 		['data-page-name'] = mw.title.getCurrentTitle().rootText
	 	})
	
	return out
end

return p