aboutsummaryrefslogtreecommitdiff
path: root/games/devtest/mods/unittests/itemdescription.lua
blob: d6ee6551affbce9a35a235e9d9577b00c5ed8699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
local full_description = "Colorful Pickaxe\nThe best pick."
minetest.register_tool("unittests:colorful_pick", {
	description = full_description,
	inventory_image = "basetools_mesepick.png",
	tool_capabilities = {
		full_punch_interval = 1.0,
		max_drop_level=3,
		groupcaps={
			cracky={times={[1]=2.0, [2]=1.0, [3]=0.5}, uses=20, maxlevel=3},
			crumbly={times={[1]=2.0, [2]=1.0, [3]=0.5}, uses=20, maxlevel=3},
			snappy={times={[1]=2.0, [2]=1.0, [3]=0.5}, uses=20, maxlevel=3}
		},
		damage_groups = {fleshy=4},
	},
})

minetest.register_chatcommand("item_description", {
	param = "",
	description = "Show the short and full description of the wielded item.",
	func = function(name)
		local player = minetest.get_player_by_name(name)
		local item = player:get_wielded_item()
		return true, string.format("short_description: %s\ndescription: %s",
				item:get_short_description(), item:get_description())
	end
})

function unittests.test_short_desc()
	local function get_short_description(item)
		return ItemStack(item):get_short_description()
	end

	local stack = ItemStack("unittests:colorful_pick")
	assert(stack:get_short_description() == "Colorful Pickaxe")
	assert(get_short_description("unittests:colorful_pick") == "Colorful Pickaxe")
	assert(minetest.registered_items["unittests:colorful_pick"].short_description == nil)
	assert(stack:get_description() == full_description)
	assert(stack:get_description() == minetest.registered_items["unittests:colorful_pick"].description)

	stack:get_meta():set_string("description", "Hello World")
	assert(stack:get_short_description() == "Hello World")
	assert(stack:get_description() == "Hello World")
	assert(get_short_description(stack) == "Hello World")
	assert(get_short_description("unittests:colorful_pick") == "Colorful Pickaxe")

	stack:get_meta():set_string("short_description", "Foo Bar")
	assert(stack:get_short_description() == "Foo Bar")
	assert(stack:get_description() == "Hello World")

	return true
end
pt">~= nil and type(tab.cbf_formspec) == "function") assert(tab.cbf_button_handler == nil or type(tab.cbf_button_handler) == "function") assert(tab.cbf_events == nil or type(tab.cbf_events) == "function") local newtab = { name = tab.name, caption = tab.caption, button_handler = tab.cbf_button_handler, event_handler = tab.cbf_events, get_formspec = tab.cbf_formspec, tabsize = tab.tabsize, on_change = tab.on_change, tabdata = {}, } self.tablist[#self.tablist + 1] = newtab if self.last_tab_index == #self.tablist then self.current_tab = tab.name if tab.on_activate ~= nil then tab.on_activate(nil,tab.name) end end end -------------------------------------------------------------------------------- local function get_formspec(self) if self.hidden or (self.parent ~= nil and self.parent.hidden) then return "" end local tab = self.tablist[self.last_tab_index] local content, prepend = tab.get_formspec(self, tab.name, tab.tabdata, tab.tabsize) if self.parent == nil and not prepend then local tsize = tab.tabsize or {width=self.width, height=self.height} prepend = string.format("size[%f,%f,%s]", tsize.width, tsize.height, dump(self.fixed_size)) end local formspec = (prepend or "") .. self:tab_header() .. content return formspec end -------------------------------------------------------------------------------- local function handle_buttons(self,fields) if self.hidden then return false end if self:handle_tab_buttons(fields) then return true end if self.glb_btn_handler ~= nil and self.glb_btn_handler(self,fields) then return true end local tab = self.tablist[self.last_tab_index] if tab.button_handler ~= nil then return tab.button_handler(self, fields, tab.name, tab.tabdata) end return false end -------------------------------------------------------------------------------- local function handle_events(self,event) if self.hidden then return false end if self.glb_evt_handler ~= nil and self.glb_evt_handler(self,event) then return true end local tab = self.tablist[self.last_tab_index] if tab.evt_handler ~= nil then return tab.evt_handler(self, event, tab.name, tab.tabdata) end return false end -------------------------------------------------------------------------------- local function tab_header(self) local toadd = "" for i=1,#self.tablist,1 do if toadd ~= "" then toadd = toadd .. "," end toadd = toadd .. self.tablist[i].caption end return string.format("tabheader[%f,%f;%s;%s;%i;true;false]", self.header_x, self.header_y, self.name, toadd, self.last_tab_index); end -------------------------------------------------------------------------------- local function switch_to_tab(self, index) --first call on_change for tab to leave if self.tablist[self.last_tab_index].on_change ~= nil then self.tablist[self.last_tab_index].on_change("LEAVE", self.current_tab, self.tablist[index].name) end --update tabview data self.last_tab_index = index local old_tab = self.current_tab self.current_tab = self.tablist[index].name if (self.autosave_tab) then core.settings:set(self.name .. "_LAST",self.current_tab) end -- call for tab to enter