Module:Block distribution
Jump to navigation
Jump to search
local p = {}
function p.main(frame)
local args = frame:getParent().args
return p._main(args)
end
function p._main(args)
local pageNames, argOrder = {}, {}
for key, val in pairs(args) do
if tonumber(key) then
pageNames[key] = val
argOrder[val] = key
end
end
if #pageNames == 0 then
local titleText = mw.title.getCurrentTitle().text
table.insert(pageNames, titleText)
argOrder[titleText] = 1
end
local query = {
'[[Display name::' .. table.concat(pageNames, '||') .. ']]',
'[[Resource location JSON::+]]',
'[[:+]]',
'?#-=',
'?Resource location JSON',
limit = 500
}
local smwdata = mw.smw.ask(query)
if not smwdata then
query = {
string.format('[[Resource location::~*%s]]',
string.lower(table.concat(pageNames, '||~*')):gsub(' ', '_')
),
'[[Resource location JSON::+]]',
'[[:+]]',
'?#-=',
'?Resource location JSON',
limit = 500
}
smwdata = mw.smw.ask(query)
end
assert(smwdata, 'No resource location data found for ' .. mw.text.listToText(pageNames))
local data = {}
if smwdata 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], '%#')
t['Sortkey'][2] = string.gsub(t['Sortkey'][2], 'deepslate', '~deepslate')
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], '%#')
t['Sortkey'][2] = string.gsub(t['Sortkey'][2], 'deepslate', '~deepslate')
table.insert(data, t)
end
end
end
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 = {}, {}
local namespace = 'minecraft'
if #data > 0 then
for _, json in ipairs(data) do
local displayName = json['Display name']
local resourceLocation = json['Resource location']
local edition = json['Edition']
local form = json['Form']
local blockForm = false
if type(form) == "string" then
if form == "block" then
blockForm = true
end
elseif type(form) == "table" then
for _, v in ipairs(form) do
if v == "block" and not json['Fluid tags'] then
blockForm = true
end
end
end
if resourceLocation ~= nil and displayName ~= nil and edition == "java" and blockForm == true then
if string.find(resourceLocation, ":") == nil then
resourceLocation = namespace .. ':' .. resourceLocation
end
if not added[resourceLocation] then
added[resourceLocation] = displayName
table.insert(blocks, { displayName, resourceLocation } )
end
end
end
end
assert(#blocks > 0, 'No valid resource location data found for ' .. mw.text.listToText(pageNames))
local nameList, idList = {}, {}
for _, v in ipairs(blocks) do
table.insert(nameList, v[1])
table.insert(idList, v[2])
end
local out = mw.html.create('div')
: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