]> git.lizzy.rs Git - xdecor.git/blob - itemframe.lua
Add footstep sound for Trampoline
[xdecor.git] / itemframe.lua
1 local itemframe, tmp = {}, {}
2 screwdriver = screwdriver or {}
3
4 local function remove_item(pos, node)
5         local objs = minetest.get_objects_inside_radius(pos, 0.5)
6         if not objs then return end
7
8         for _, obj in pairs(objs) do
9                 if obj and obj:get_luaentity() and
10                                 obj:get_luaentity().name == "xdecor:f_item" then
11                         obj:remove() break
12                 end
13         end
14 end
15
16 local facedir = {
17         [0] = {x=0, y=0, z=1}, {x=1, y=0, z=0}, {x=0, y=0, z=-1}, {x=-1, y=0, z=0}
18 }
19
20 local function update_item(pos, node)
21         remove_item(pos, node)
22         local meta = minetest.get_meta(pos)
23         local itemstring = meta:get_string("item")
24         local posad = facedir[node.param2]
25         if not posad or itemstring == "" then return end
26
27         pos = vector.add(pos, vector.multiply(posad, 6.5/16))
28         tmp.nodename = node.name
29         tmp.texture = ItemStack(itemstring):get_name()
30
31         local entity = minetest.add_entity(pos, "xdecor:f_item")
32         local yaw = math.pi*2 - node.param2 * math.pi/2
33         entity:setyaw(yaw)
34
35         local timer = minetest.get_node_timer(pos)
36         timer:start(15.0)
37 end
38
39 local function drop_item(pos, node)
40         local meta = minetest.get_meta(pos)
41         local item = meta:get_string("item")
42         if item == "" then return end
43
44         minetest.add_item(pos, item)
45         meta:set_string("item", "")
46         remove_item(pos, node)
47
48         local timer = minetest.get_node_timer(pos)
49         timer:stop()
50 end
51
52 function itemframe.after_place(pos, placer, itemstack)
53         local meta = minetest.get_meta(pos)
54         local name = placer:get_player_name()
55         meta:set_string("owner", name)
56         meta:set_string("infotext", "Item Frame (owned by "..name..")")
57 end
58
59 function itemframe.timer(pos)
60         local node = minetest.get_node(pos)
61         local meta = minetest.get_meta(pos)
62         local num = #minetest.get_objects_inside_radius(pos, 0.5)
63
64         if num == 0 and meta:get_string("item") ~= "" then
65                 update_item(pos, node)
66         end
67         return true
68 end
69
70 function itemframe.rightclick(pos, node, clicker, itemstack)
71         local meta = minetest.get_meta(pos)
72         local player = clicker:get_player_name()
73         local owner = meta:get_string("owner")
74         if player ~= owner or not itemstack then return end
75
76         drop_item(pos, node)
77         local itemstring = itemstack:take_item():to_string()
78         meta:set_string("item", itemstring)
79         update_item(pos, node)
80
81         return itemstack
82 end
83
84 function itemframe.punch(pos, node, puncher)
85         local meta = minetest.get_meta(pos)
86         local player = puncher:get_player_name()
87         local owner = meta:get_string("owner")
88
89         if player ~= owner then return end
90         drop_item(pos, node)
91 end
92
93 function itemframe.dig(pos, player)
94         local meta = minetest.get_meta(pos)
95         local pname = player:get_player_name()
96         local owner = meta:get_string("owner")
97
98         return player and pname == owner
99 end
100
101 xdecor.register("itemframe", {
102         description = "Item Frame",
103         groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
104         sounds = default.node_sound_wood_defaults(),
105         on_rotate = screwdriver.disallow,
106         sunlight_propagates = true,
107         inventory_image = "xdecor_itemframe.png",
108         node_box = xdecor.nodebox.slab_z(0.9375),
109         tiles = {"xdecor_wood.png", "xdecor_wood.png", "xdecor_wood.png",
110                  "xdecor_wood.png", "xdecor_wood.png", "xdecor_itemframe.png"},
111         after_place_node = itemframe.after_place,
112         on_timer = itemframe.timer,
113         on_rightclick = itemframe.rightclick,
114         on_punch = itemframe.punch,
115         can_dig = itemframe.dig,
116         after_destruct = remove_item
117 })
118
119 minetest.register_entity("xdecor:f_item", {
120         visual = "wielditem",
121         visual_size = {x=0.33, y=0.33},
122         collisionbox = {0},
123         physical = false,
124         textures = {"air"},
125         on_activate = function(self, staticdata)
126                 local pos = self.object:getpos()
127                 if minetest.get_node(pos).name ~= "xdecor:itemframe" then
128                         self.object:remove()
129                 end
130
131                 if tmp.nodename and tmp.texture then
132                         self.nodename = tmp.nodename
133                         tmp.nodename = nil
134                         self.texture = tmp.texture
135                         tmp.texture = nil
136                 elseif staticdata and staticdata ~= "" then
137                         local data = staticdata:split(";")
138                         if data and data[1] and data[2] then
139                                 self.nodename = data[1]
140                                 self.texture = data[2]
141                         end
142                 end
143                 if self.texture then
144                         self.object:set_properties({textures={self.texture}})
145                 end
146         end,
147         get_staticdata = function(self)
148                 if self.nodename and self.texture then
149                         return self.nodename..";"..self.texture
150                 end
151                 return ""
152         end
153 })
154