Module:Downloadlinks

local p = {}

local i18n = { process_args = 'Module:ProcessArgs', hashes = 'Module:Downloadlinks/hashes', category = 'Categorie:Invalid download link',

version_error = 'No or invalid version given', variant_error = 'No or invalid variant given', hash_error = 'No hash found',

client = 'Client', server = 'Server', windows_server = '.exe', json = '.json', json_package = '.json', nowiki_template = 'Downloadlink/nw', }

function p.downloadlink_constructor(f)

local args = f   if f == mw.getCurrentFrame then args = require(i18n['process_args']).merge(true) end -- Construct category local title = mw.title.getCurrentTitle local category = '' if not args.nocat and title.namespace == 0 and not title.isSubpage then category =  .. title.text ..  end

local version local variant -- Checks before constructing if args[1] then version = mw.text.trim(tostring(args[1])):lower elseif args.v then -- args.v is deprecated, yet still here for backwards compatibility version = mw.text.trim(tostring(args.v)):lower end if args[2] then variant = mw.text.trim(tostring(args[2])):lower elseif args.s then -- args.s is deprecated, yet still here for backwards compatibility variant = mw.text.trim(tostring(args.s)):lower end -- change 'windows' to 'windows_server' if variant == 'windows' then variant = 'windows_server' end local nw = f:expandTemplate{title = i18n['nowiki_template']}

local version_data = mw.loadData(i18n['hashes']) -- Check if version or variant outputs nil if version_data[version] == nil then text = i18n['version_error'] return text .. category elseif version_data[version][variant] == nil then text = i18n['variant_error'] return text .. category end local hash = version_data[version][variant] local link local download_link local text

-- Start constructing -- Check if variant also outputs a hash as 'json_package' (only works when force_download is NOT set) if variant == 'json' and not args.force_download then hash = version_data[version]['json_package'] if hash ~= nil then variant = 'json_package' else variant = 'json' hash = version_data[version]['json'] end end -- Check if text parameter exists, if not, use default if args[3] then text = args[3] elseif args.description then -- args.description is deprecated, yet still here for backwards compatibility text = args.description else text = i18n[variant] end -- And now finally actually start contructing links -- Client if variant == 'client' then -- Check if hash exists if hash then download_link = 'https://launcher.mojang.com/v1/objects/' .. hash .. '/client.jar' link = '[' .. download_link .. ' ' .. text .. ']' .. nw   		return link else download_link = i18n['hash_error'] return download_link .. category end -- Server elseif variant == 'server' then -- Check if hash exists if hash then download_link = 'https://launcher.mojang.com/v1/objects/' .. hash .. '/server.jar' link = '[' .. download_link .. ' ' .. text .. ']' .. nw   		return link else download_link = i18n['hash_error'] return download_link .. category end

-- Windows server elseif variant == 'windows_server' then -- Check if hash exists if hash then download_link = 'https://launcher.mojang.com/v1/objects/' .. hash .. '/windows_server.exe' link = '[' .. download_link .. ' ' .. text .. ']' .. nw   		return link else download_link = i18n['hash_error'] return download_link .. category .. hash end -- JSON elseif variant == 'json' then -- Check if hash exists if hash then download_link = 'https://launchermeta.mojang.com/mc/game/' .. hash .. '/' .. version .. '.json' link = '[' .. download_link .. ' ' .. text .. ']' .. nw   		return link else download_link = i18n['hash_error'] return download_link .. category end

-- JSON in browser elseif variant == 'json_package' then -- Check if hash exists if hash ~= nil then download_link = 'https://launchermeta.mojang.com/v1/packages/' .. hash .. '/' .. version .. '.json' link = '[' .. download_link .. ' ' .. text .. ']' .. nw   		return link else download_link = i18n['hash_error'] return download_link .. category end end end

return p