Itemframes: Show item name as info text
[MineClone/MineClone2.git] / mods / ITEMS / mcl_itemframes / init.lua
blobd04922e4fb9b4f38d7763203acdb9038cc2ff8fe
1 minetest.register_entity("mcl_itemframes:item",{
2 hp_max = 1,
3 visual = "wielditem",
4 visual_size = {x=0.3,y=0.3},
5 collisionbox = {0,0,0,0,0,0},
6 physical = false,
7 textures = { "empty.png" },
8 on_activate = function(self, staticdata)
9 if staticdata ~= nil and staticdata ~= "" then
10 local data = staticdata:split(';')
11 if data and data[1] and data[2] then
12 self._nodename = data[1]
13 self._texture = data[2]
14 end
15 end
16 if self._texture ~= nil then
17 self.object:set_properties({textures={self._texture}})
18 end
19 end,
20 get_staticdata = function(self)
21 if self._nodename ~= nil and self._texture ~= nil then
22 return self._nodename .. ';' .. self._texture
23 end
24 return ""
25 end,
27 _update_texture = function(self)
28 if self._texture ~= nil then
29 self.object:set_properties({textures={self._texture}})
30 end
31 end,
35 local facedir = {}
36 facedir[0] = {x=0,y=0,z=1}
37 facedir[1] = {x=1,y=0,z=0}
38 facedir[2] = {x=0,y=0,z=-1}
39 facedir[3] = {x=-1,y=0,z=0}
41 local remove_item_entity = function(pos, node)
42 local objs = nil
43 if node.name == "mcl_itemframes:item_frame" then
44 objs = minetest.get_objects_inside_radius(pos, .5)
45 end
46 if objs then
47 for _, obj in ipairs(objs) do
48 if obj and obj:get_luaentity() and obj:get_luaentity().name == "mcl_itemframes:item" then
49 obj:remove()
50 end
51 end
52 end
53 end
55 local update_item_entity = function(pos, node)
56 remove_item_entity(pos, node)
57 local meta = minetest.get_meta(pos)
58 if meta:get_string("item") ~= "" then
59 if node.name == "mcl_itemframes:item_frame" then
60 local posad = facedir[node.param2]
61 pos.x = pos.x + posad.x*6.5/16
62 pos.y = pos.y + posad.y*6.5/16
63 pos.z = pos.z + posad.z*6.5/16
64 end
65 local e = minetest.add_entity(pos,"mcl_itemframes:item")
66 local lua = e:get_luaentity()
67 lua._nodename = node.name
68 lua._texture = ItemStack(meta:get_string("item")):get_name()
69 lua:_update_texture()
70 if node.name == "mcl_itemframes:item_frame" then
71 local yaw = math.pi*2 - node.param2 * math.pi/2
72 e:setyaw(yaw)
73 end
74 end
75 end
77 local drop_item = function(pos, node, meta)
78 if meta:get_string("item") ~= "" then
79 if node.name == "mcl_itemframes:item_frame" and not minetest.settings:get_bool("creative_mode") then
80 local item = ItemStack(minetest.deserialize(meta:get_string("itemdata")))
81 minetest.add_item(pos, item)
82 end
83 meta:set_string("item", "")
84 meta:set_string("itemdata", "")
85 meta:set_string("infotext", "")
86 end
87 remove_item_entity(pos, node)
88 end
90 local on_rotate
91 if minetest.get_modpath("screwdriver") then
92 on_rotate = screwdriver.disallow
93 end
95 minetest.register_node("mcl_itemframes:item_frame",{
96 description = "Item Frame",
97 _doc_items_longdesc = "Item frames are decorative blocks in which items can be placed.",
98 _doc_items_usagehelp = "Hold any item in your hand and right-click the item frame to place the item into the frame. Rightclick the item frame again to retrieve the item.",
99 drawtype = "mesh",
100 is_ground_content = false,
101 mesh = "mcl_itemframes_itemframe1facedir.obj",
102 selection_box = { type = "fixed", fixed = {-6/16, -6/16, 7/16, 6/16, 6/16, 0.5} },
103 collision_box = { type = "fixed", fixed = {-6/16, -6/16, 7/16, 6/16, 6/16, 0.5} },
104 tiles = {"mcl_itemframes_itemframe_background.png", "mcl_itemframes_itemframe_background.png", "mcl_itemframes_itemframe_background.png", "mcl_itemframes_itemframe_background.png", "default_wood.png", "mcl_itemframes_itemframe_background.png"},
105 inventory_image = "mcl_itemframes_item_frame.png",
106 wield_image = "mcl_itemframes_item_frame.png",
107 paramtype = "light",
108 paramtype2 = "facedir",
109 sunlight_propagates = true,
110 groups = { dig_immediate=3,deco_block=1,dig_by_piston=1},
111 sounds = mcl_sounds.node_sound_defaults(),
112 on_rightclick = function(pos, node, clicker, itemstack)
113 if not itemstack then return end
114 local meta = minetest.get_meta(pos)
115 drop_item(pos, node, meta)
116 -- item holds the itemstring
117 meta:set_string("item", itemstack:get_name())
118 local put_itemstack = ItemStack(itemstack)
119 put_itemstack:set_count(1)
120 local itemdata = minetest.serialize(put_itemstack:to_table())
121 -- itemdata holds the serialized itemstack in table form
122 update_item_entity(pos,node)
123 -- Add node infotext when item has been named
124 meta:set_string("itemdata", itemdata)
125 local imeta = itemstack:get_meta()
126 local iname = imeta:get_string("name")
127 if iname then
128 meta:set_string("infotext", iname)
131 if not minetest.settings:get_bool("creative_mode") then
132 itemstack:take_item()
134 return itemstack
135 end,
136 on_destruct = function(pos)
137 local meta = minetest.get_meta(pos)
138 local node = minetest.get_node(pos)
139 drop_item(pos, node, meta)
140 end,
141 on_rotate = on_rotate,
144 minetest.register_craft({
145 output = 'mcl_itemframes:item_frame',
146 recipe = {
147 {'mcl_core:stick', 'mcl_core:stick', 'mcl_core:stick'},
148 {'mcl_core:stick', 'mcl_mobitems:leather', 'mcl_core:stick'},
149 {'mcl_core:stick', 'mcl_core:stick', 'mcl_core:stick'},
153 minetest.register_lbm({
154 label = "Update legacy item frames",
155 name = "mcl_itemframes:update_legacy_item_frames",
156 nodenames = {"itemframes:frame"},
157 action = function(pos, node)
158 -- Swap legacy node, then respawn entity
159 node.name = "mcl_itemframes:item_frame"
160 minetest.swap_node(pos, node)
161 update_item_entity(pos, node)
162 end,