Add minimal itemframe LBM for compatability
[MineClone/MineClone2.git] / mods / ITEMS / mcl_itemframes / init.lua
bloba4807f0edd04339eccc90bdd9c2f03e01f924bea
1 local tmp = {}
3 minetest.register_entity("mcl_itemframes:item",{
4 hp_max = 1,
5 visual="wielditem",
6 visual_size={x=0.3,y=0.3},
7 collisionbox = {0,0,0,0,0,0},
8 physical=false,
9 textures={"air"},
10 on_activate = function(self, staticdata)
11 if tmp.nodename ~= nil and tmp.texture ~= nil then
12 self.nodename = tmp.nodename
13 tmp.nodename = nil
14 self.texture = tmp.texture
15 tmp.texture = nil
16 else
17 if staticdata ~= nil and staticdata ~= "" then
18 local data = staticdata:split(';')
19 if data and data[1] and data[2] then
20 self.nodename = data[1]
21 self.texture = data[2]
22 end
23 end
24 end
25 if self.texture ~= nil then
26 self.object:set_properties({textures={self.texture}})
27 end
28 end,
29 get_staticdata = function(self)
30 if self.nodename ~= nil and self.texture ~= nil then
31 return self.nodename .. ';' .. self.texture
32 end
33 return ""
34 end,
38 local facedir = {}
39 facedir[0] = {x=0,y=0,z=1}
40 facedir[1] = {x=1,y=0,z=0}
41 facedir[2] = {x=0,y=0,z=-1}
42 facedir[3] = {x=-1,y=0,z=0}
44 local remove_item = function(pos, node)
45 local objs = nil
46 if node.name == "mcl_itemframes:item_frame" then
47 objs = minetest.get_objects_inside_radius(pos, .5)
48 end
49 if objs then
50 for _, obj in ipairs(objs) do
51 if obj and obj:get_luaentity() and obj:get_luaentity().name == "mcl_itemframes:item" then
52 obj:remove()
53 end
54 end
55 end
56 end
58 local update_item = function(pos, node)
59 remove_item(pos, node)
60 local meta = minetest.get_meta(pos)
61 if meta:get_string("item") ~= "" then
62 if node.name == "mcl_itemframes:item_frame" then
63 local posad = facedir[node.param2]
64 pos.x = pos.x + posad.x*6.5/16
65 pos.y = pos.y + posad.y*6.5/16
66 pos.z = pos.z + posad.z*6.5/16
67 end
68 tmp.nodename = node.name
69 tmp.texture = ItemStack(meta:get_string("item")):get_name()
70 local e = minetest.add_entity(pos,"mcl_itemframes:item")
71 if node.name == "mcl_itemframes:item_frame" then
72 local yaw = math.pi*2 - node.param2 * math.pi/2
73 e:setyaw(yaw)
74 end
75 end
76 end
78 local drop_item = function(pos, node, meta)
79 if meta:get_string("item") ~= "" then
80 if node.name == "mcl_itemframes:item_frame" and not minetest.settings:get_bool("creative_mode") then
81 local item = ItemStack(minetest.deserialize(meta:get_string("itemdata")))
82 minetest.add_item(pos, item)
83 end
84 meta:set_string("item","")
85 meta:set_string("itemdata","")
86 end
87 remove_item(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 meta:set_string("itemdata", itemdata)
123 update_item(pos,node)
124 if not minetest.settings:get_bool("creative_mode") then
125 itemstack:take_item()
127 return itemstack
128 end,
129 on_destruct = function(pos)
130 local meta = minetest.get_meta(pos)
131 local node = minetest.get_node(pos)
132 drop_item(pos, node, meta)
133 end,
134 on_rotate = on_rotate,
137 minetest.register_craft({
138 output = 'mcl_itemframes:item_frame',
139 recipe = {
140 {'mcl_core:stick', 'mcl_core:stick', 'mcl_core:stick'},
141 {'mcl_core:stick', 'mcl_mobitems:leather', 'mcl_core:stick'},
142 {'mcl_core:stick', 'mcl_core:stick', 'mcl_core:stick'},
146 minetest.register_lbm({
147 label = "Update legacy item frames",
148 name = "mcl_itemframes:update_legacy_item_frames",
149 nodenames = {"itemframes:frame"},
150 action = function(pos, node)
151 -- Swap legacy node, then respawn entity
152 node.name = "mcl_itemframes:item_frame"
153 minetest.swap_node(pos, node)
154 update_item(pos, node)
155 end,