summaryrefslogtreecommitdiff
path: root/init.lua
blob: 4554b0e4c7701596dbdb014b2231362206a700e3 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76


minetest.register_node("engrave:table", {
	description = "Engraving Table",
	tiles = {"engrave_top.png", "engrave_side.png"},
	groups = {choppy=2,flammable=3, oddly_breakable_by_hand=2},
	sounds = default and default.node_sound_wood_defaults(),
	on_rightclick = function(pos, node, player)
		local pname=player:get_player_name()
		local stack=player:get_wielded_item()
		if stack:get_count()==0 then
			minetest.chat_send_player(pname, "Please wield the item you want to name, and then click the engraving table again.")
			return
		end
		local idef=minetest.registered_items[stack:get_name()]
		if not idef then
			minetest.chat_send_player(pname, "You can't name an unknown item!")
			return
		end
		local name=idef.description or stack:get_name()
		local what=name or "whatever"
		if stack:get_count()>1 then
			what="stack of "..what
		end
		
		local meta=stack:get_meta()
		if meta then
			local metaname=meta:get_string("description")
			if metaname~="" then
				name=metaname
			end
		end
		
		minetest.show_formspec(pname, "engrave", "field[name;Enter a new name for this "..what..";"..name.."]")
	end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
	if formname=="engrave" and fields.name then
		local pname=player:get_player_name()
		local stack=player:get_wielded_item()
		if stack:get_count()==0 then
			minetest.chat_send_player(pname, "Please wield the item you want to name, and then click the engraving table again.")
			return
		end
		local idef=minetest.registered_items[stack:get_name()]
		if not idef then
			minetest.chat_send_player(pname, "You can't name an unknown item!")
			return
		end
		local name=idef.description or stack:get_name()
		
		local meta=stack:get_meta()
		if not meta then
			minetest.chat_send_player(pname, "For some reason, the stack metadata couldn't be acquired. Try again!")
			return
		end
		
		if fields.name==name then
			meta:set_string("description", "")
		else
			meta:set_string("description", fields.name)
		end
		--write back
		player:set_wielded_item(stack)
	end
end)

minetest.register_craft({
	output = "engrave:table",
	recipe = {
		{"group:wood", "group:wood", "group:wood"},
		{"group:wood", "default:diamond", "group:wood"},
		{"group:wood", "group:wood", "group:wood"},
	},
})