]> git.lizzy.rs Git - xdecor.git/blob - itemframe.lua
Add Cobweb and allow picking water from cauldron
[xdecor.git] / itemframe.lua
1 local tmp = {}
2 screwdriver = screwdriver or {}
3
4 minetest.register_entity("xdecor:f_item", {
5         hp_max = 1,
6         visual = "wielditem",
7         visual_size = {x=.33,y=.33},
8         collisionbox = {0, 0, 0, 0, 0, 0},
9         physical = false,
10         textures = {"air"},
11         on_activate = function(self, staticdata)
12                 if tmp.nodename ~= nil and tmp.texture ~= nil then
13                         self.nodename = tmp.nodename
14                         tmp.nodename = nil
15                         self.texture = tmp.texture
16                         tmp.texture = nil
17                 else
18                         if staticdata ~= nil and staticdata ~= "" then
19                                 local data = staticdata:split(';')
20                                 if data and data[1] and data[2] then
21                                         self.nodename = data[1]
22                                         self.texture = data[2]
23                                 end
24                         end
25                 end
26                 if self.texture ~= nil then
27                         self.object:set_properties({textures={self.texture}})
28                 end
29         end,
30         get_staticdata = function(self)
31                 if self.nodename ~= nil and self.texture ~= nil then
32                         return self.nodename .. ';' .. self.texture
33                 end
34                 return ""
35         end
36 })
37
38 local remove_item = function(pos, node)
39         local objs = nil
40         objs = minetest.get_objects_inside_radius(pos, .5)
41
42         if objs then
43                 for _, obj in ipairs(objs) do
44                         if obj and obj:get_luaentity() and
45                                 obj:get_luaentity().name == "xdecor:f_item" then
46                                 obj:remove()
47                         end
48                 end
49         end
50 end
51
52 local facedir = {}
53 facedir[0] = {x=0, y=0, z=1}
54 facedir[1] = {x=1, y=0, z=0}
55 facedir[2] = {x=0, y=0, z=-1}
56 facedir[3] = {x=-1, y=0, z=0}
57
58 local update_item = function(pos, node)
59         remove_item(pos, node)
60         local meta = minetest.get_meta(pos)
61
62         if meta:get_string("item") ~= "" then
63                 local posad = facedir[node.param2]
64
65                 if not posad then
66                         return
67                 end
68                 pos.x = pos.x + posad.x*6.5/16
69                 pos.y = pos.y + posad.y*6.5/16
70                 pos.z = pos.z + posad.z*6.5/16
71                 tmp.nodename = node.name
72                 tmp.texture = ItemStack(meta:get_string("item")):get_name()
73
74                 local e = minetest.add_entity(pos, "xdecor:f_item")
75                 local yaw = math.pi*2 - node.param2 * math.pi/2
76                 e:setyaw(yaw)
77         end
78 end
79
80 local drop_item = function(pos, node)
81         local meta = minetest.get_meta(pos)
82         if meta:get_string("item") ~= "" then
83                 minetest.add_item(pos, meta:get_string("item"))
84                 meta:set_string("item", "")
85         end
86         remove_item(pos, node)
87 end
88
89 xdecor.register("frame", {
90         description = "Item frame",
91         groups = {snappy=3},
92         on_rotate = screwdriver.disallow,
93         node_box = {
94                 type = "fixed",
95                 fixed = {-0.5, -0.5, 7/16, 0.5, 0.5, 0.5}
96         },
97         tiles = {
98                 "xdecor_wood.png", "xdecor_wood.png", "xdecor_wood.png",
99                 "xdecor_wood.png", "xdecor_wood.png", "xdecor_frame.png"
100         }, 
101         inventory_image = "xdecor_frame.png",
102         after_place_node = function(pos, placer, itemstack)
103                 local meta = minetest.get_meta(pos)
104                 meta:set_string("owner", placer:get_player_name())
105                 meta:set_string("infotext", "Item frame (owned by "..placer:get_player_name()..")")
106         end,
107         on_rightclick = function(pos, node, clicker, itemstack)
108                 if not itemstack then
109                         return
110                 end
111
112                 local meta = minetest.get_meta(pos)
113                 if clicker:get_player_name() == meta:get_string("owner") then
114                         drop_item(pos, node)
115                         local s = itemstack:take_item()
116                         meta:set_string("item", s:to_string())
117                         update_item(pos, node)
118                 end
119
120                 return itemstack
121         end,
122         on_punch = function(pos, node, puncher)
123                 local meta = minetest.get_meta(pos)
124                 if puncher:get_player_name() == meta:get_string("owner") then
125                         drop_item(pos, node)
126                 end
127         end,
128         can_dig = function(pos, player)
129                 local meta = minetest.get_meta(pos)
130                 return player:get_player_name() == meta:get_string("owner")
131         end,
132         after_destruct = remove_item
133 })
134
135 minetest.register_abm({
136         nodenames = {"xdecor:frame"},
137         interval = 15,
138         chance = 1,
139         action = function(pos, node, active_object_count, active_object_count_wider)
140                 if #minetest.get_objects_inside_radius(pos, 0.5) > 0 then
141                         return
142                 end
143                 update_item(pos, node)
144         end
145 })