Minecraft Wiki

The Minecraft Wiki is no longer considered as official by Microsoft and therefore several changes are required to be made, including to the wiki's logo. Please read this announcement for more information.

READ MORE

Minecraft Wiki
Advertisement
[view | edit | history | purge]DocumentationJump to code ↴

This module implements {{Experience}}. It accepts no directly passed arguments, only parent arguments and is therefore recommended to not be invoked directly from templates.

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

local i18n = {
	error_exp_arg_not_a_number = "Module:Experience: %s is not a number", -- %1 = the description of the parameter
	error_exp_arg_too_small = "Module:Experience: %s is less than -32,768 (int16 lower limit)", -- %1 = the description of the parameter
	error_exp_arg_too_large = "Module:Experience: %s is greater than 32,767 (int16 upper limit)", -- %1 = the description of the parameter
	arg_desc_min_exp = "minimum experience", -- %1 = the description of the parameter
	arg_desc_max_exp = "maximum experience", -- %1 = the description of the parameter
	error_exp_min_gt_exp_max = "Module:Experience: %s (%d) > %s (%d)" -- %1 = min exp description, %2 = min exp, %3 = max exp description, %4 = max exp 
}

local EXP_VALUES = {
	{"Experience Orb Value -32768-2.png", -32768, 2},
	{"Experience Orb Value 3-6.png", 3, 6},
	{"Experience Orb Value 7-16.png", 7, 16},
	{"Experience Orb Value 17-36.png", 17, 36},
	{"Experience Orb Value 37-72.png", 37, 72},
	{"Experience Orb Value 73-148.png", 73, 148},
	{"Experience Orb Value 149-306.png", 149, 306},
	{"Experience Orb Value 307-616.png", 307, 616},
	{"Experience Orb Value 617-1236.png", 617, 1236},
	{"Experience Orb Value 1237-2476.png", 1237, 2476},
	{"Experience Orb Value 2477-32767.png", 2477, 32767}
}

local function validate_xp_arg(arg, desc)
	local arg_n = assert(tonumber(arg), i18n.error_exp_arg_not_a_number:format(desc))
	assert(arg_n >= -32768, i18n.error_exp_arg_too_small:format(desc))
	assert(arg_n <= 32767, i18n.error_exp_arg_too_large:format(desc))
	return arg_n
end

local function produce_exp_sprite(image, is_first)
	local sprite = mw.html.create('span'):addClass("pixel-image")
	if is_first then
		sprite:addClass("animated-active")
	end
	sprite:css({
		["padding-left"] = "19px",
		["background-size"] = "16px 16px",
		["background-position"] = "left center",
		["background-repeat"] = "no-repeat",
		["background-image"] = mw.getCurrentFrame():expandTemplate({title="Template:FileUrl", args = {image}})
	})
	return sprite
end

local function produce_exp_anim(exp_min, exp_max)
	local wrap = mw.html.create("span"):addClass("animated")
	
	local lower_bound = 1
	local upper_bound = #EXP_VALUES
	for i, v in ipairs(EXP_VALUES) do
		if exp_min >= v[2] and exp_min <= v[3] then
			lower_bound = i
		end
		if exp_max >= v[2] and exp_max <= v[3] then
			upper_bound = i
		end	
	end
	--mw.log(lower_bound)
	--mw.log(upper_bound)
	
	local first = true
	for i = lower_bound, upper_bound do
		if first then
			wrap:node(produce_exp_sprite(EXP_VALUES[i][1], true))
			first = false
		else
			wrap:node(produce_exp_sprite(EXP_VALUES[i][1]))
		end
	end
	
	return tostring(wrap)
end

function p.main(exp_min, exp_max)
	local anim = produce_exp_anim(exp_min, exp_max)
	if exp_min == exp_max then
		return anim .. exp_min
	end
	return anim .. exp_min .. "–" .. exp_max
end

function p.exp_range(f)
	local args = require("Module:ProcessArgs").merge(true)
	local exp_min = validate_xp_arg(args[1], i18n.arg_desc_min_exp)
	local exp_max = validate_xp_arg(args[2], i18n.arg_desc_max_exp)
	
	assert(exp_max >= exp_min, i18n.error_exp_min_gt_exp_max:format(
		i18n.arg_desc_min_exp,
		exp_min,
		i18n.arg_desc_max_exp,
		exp_max
	))

	return p.main(exp_min, exp_max)
end

return p
Advertisement