Module:Breaking time

From Create: Minecraft Mod Wiki
Revision as of 20:07, 22 November 2022 by Create: Minecraft Mod Wiki>NonaWax (Wrong wiki, it looks like you managed to get this up on the right one already, so I'll go ahead and revert this)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Breaking time/doc

local p = {}

local displayItem = require( [[Module:Item]] ).item
local mcWikiLink = require( [[Module:Link]] ).mcWikiLink

local function getHeader(f)
	return mcWikiLink{'Breaking'}..' time' .. f:preprocess( '<ref group="Note" name="breakingtimenote">Times are for unenchanted tools as wielded by players with no status effects, measured in seconds. For more information, see '..mcWikiLink{'Breaking#Speed', 'Breaking § Speed'}..'.</ref>' )
end

function ucfirst(s)
	if s == nil then s = "" end
	return s:gsub("(%w)([%w']*)", function(a,b) return string.upper(a)..b end)
end
function p.breaking( f )
	local args = require( [[Module:ProcessArgs]] ).norm()
	
	local materials = {
		any = {grade=0,speed=1,display={nolink=true,item='Default',img='Blank'}},
		wooden = {grade=1,speed=2,display={nolink=true,item='Wooden',img='Oak Planks'}},
		stone = {grade=2,speed=4,display={nolink=true,item='Stone',img='Cobblestone'}},
		iron = {grade=3,speed=6,display={nolink=true,item='Iron',img='Iron Ingot'}},
		diamond = {grade=4,speed=8,display={nolink=true,item='Diamond'}},
        netherite = {grade=5,speed=9,display={nolink=true,item='Netherite',img='Netherite Ingot'}},
		golden = {grade=1,speed=12,display={nolink=true,item='Golden',img='Gold Ingot'}},
	}
	local tools = {
		pickaxe=2,
		axe=2,
		sword=2,
		shovel=2,
		shears=1,
		bucket=1,
	}
	
	local hardness = args[1] or args.hardness or args.h
	local requiredGrade = args[2] or args.material or args.mat or args.grade
	local tool1 = args[3] or args.tool or args.tool1 or args.t or args.t1
	local tool2 = args[4] or args.tool2 or args.t2
	local block = args.block or args.bl or args.b
	
	if not hardness then return '<strong><font color="red">Missing argument #1 (hardness)</font></strong>' end
	if (not requiredGrade or not materials[requiredGrade]) and tools[tool1] == 2 then return '<strong><font color="red">Missing argument #2 (required material)</font></strong>' end
	if not tool1 then return '<strong><font color="red">Missing argument #3 (tool)</font></strong>' end
	
	hardness = tonumber(hardness)
	requiredGrade = requiredGrade:lower()
	tool1 = tool1:lower()
	if tool2 then tool2 = tool2:lower() end
	
	local wikitable = mw.html.create('table'):addClass('wikitable'):css{['text-align']='center'}
		--:tag('tr')
			--:tag('th'):wikitext('Block'):done()
			--:tag('th'):wikitext(displayItem{nolink=true,item=block}):attr{colspan=tool2 and 2 or 1}:done()
		:done()
		:tag('tr')
			:tag('th'):wikitext('Hardness'):done()
			:tag('td'):wikitext(hardness):attr{colspan=tool2 and 2 or 1}:done()
		:done()
	if tool2 then
		wikitable:tag('tr')
			:tag('th'):wikitext('Tool'):done()
			:tag('td'):wikitext(materials[requiredGrade].grade == 0 and 
				table.concat{'[[File:', ucfirst(tool1), ' Slot.png|21px|link=w:c:minecraft:', ucfirst(tool1), ']]'} or
				table.concat{'[[File:', tools[tool1] == 2 and ucfirst(requiredGrade) or '', ' ', ucfirst(tool1), '.png|21px|link=w:c:minecraft:', ucfirst(tool1), ']]'}
			):css{['min-width']='50px'}:done()
			:tag('td'):wikitext(materials[requiredGrade].grade == 0 and 
				table.concat{'[[File:', ucfirst(tool2), ' Slot.png|21px|link=w:c:minecraft:', ucfirst(tool2), ']]'} or
				table.concat{'[[File:', tools[tool2] == 2 and ucfirst(requiredGrade) or '', ' ', ucfirst(tool2), '.png|21px|link=w:c:minecraft:', ucfirst(tool2), ']]'}
			):css{['min-width']='50px'}:done()
		:done()
	else
		wikitable:tag('tr')
			:tag('th'):wikitext('Tool'):done()
			:tag('td'):wikitext(table.concat{
				'[[File:', tools[tool1] == 2 and ucfirst(requiredGrade) or '', ' ', ucfirst(tool1), '.png|21px|link=w:c:minecraft:', ucfirst(tool1), ']]'
			}):css{['min-width']='50px'}:done()
		:done()
	end
	wikitable:tag('tr')
		:tag('th')
			:wikitext(getHeader(f))
			:attr{colspan=tool2 and 3 or 2}
			:done()
	:done()
	local materialOrder = {'any', 'wooden', 'stone', 'iron', 'diamond', 'netherite', 'golden'}
	for _,mat in ipairs(materialOrder) do
		local v = materials[mat]
		local reqGradeNum = materials[requiredGrade].grade
		local calc = v.speed
		calc = calc / hardness
		local shouldBeRecalculated = false
		if v.grade >= reqGradeNum then 
			calc = calc / 30
			shouldBeRecalculated = true
		else 
			calc = calc / 100
		end
		if reqGradeNum == 0 then shouldBeRecalculated = false end
		calc = 1 / calc
		calc = math.ceil(calc)
		calc = calc / 20
		
		if tool2 then
			local calc2 = v.speed
			calc2 = calc2 / hardness
			calc2 = calc2 / 100
			calc2 = 1 / calc2
			calc2 = math.ceil(calc2)
			calc2 = calc2 / 20
			wikitable:tag('tr')
				:tag('th'):wikitext(displayItem(v.display)):css{['text-align']='left'}:done()
				:tag('td'):wikitext(calc):addClass((v.grade >= reqGradeNum) and 'green' or 'red'):done()
				:tag('td'):wikitext(shouldBeRecalculated and calc2 or calc):addClass((v.grade >= reqGradeNum) and 'green' or 'red'):done()
			:done()
		else
			wikitable:tag('tr')
				:tag('th'):wikitext(displayItem(v.display)):css{['text-align']='left'}:done()
				:tag('td'):wikitext(calc):addClass((v.grade >= reqGradeNum) and 'green' or 'red'):done()
			:done()
		end
	end
	return tostring(wikitable)..f:preprocess('<references group="Note"/>')
end
return p