]> git.lizzy.rs Git - xdecor.git/blob - itemframe.lua
update craft recipes
[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
75                 return itemstack
76         end
77
78         drop_item(pos, node)
79         local itemstring = itemstack:take_item():to_string()
80         meta:set_string("item", itemstring)
81         update_item(pos, node)
82
83         return itemstack
84 end
85
86 function itemframe.punch(pos, node, puncher)
87         local meta = minetest.get_meta(pos)
88         local player = puncher:get_player_name()
89         local owner = meta:get_string("owner")
90
91         if player ~= owner then return end
92         drop_item(pos, node)
93 end
94
95 function itemframe.dig(pos, player)
96         local meta = minetest.get_meta(pos)
97         local pname = player and player:get_player_name()
98         local owner = meta:get_string("owner")
99
100         return pname == owner
101 end
102
103 xdecor.register("itemframe", {
104         description = "Item Frame",
105         groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
106         sounds = default.node_sound_wood_defaults(),
107         on_rotate = screwdriver.disallow,
108         sunlight_propagates = true,
109         inventory_image = "xdecor_itemframe.png",
110         node_box = xdecor.nodebox.slab_z(0.9375),
111         tiles = {"xdecor_wood.png", "xdecor_wood.png", "xdecor_wood.png",
112                  "xdecor_wood.png", "xdecor_wood.png", "xdecor_itemframe.png"},
113         after_place_node = itemframe.after_place,
114         on_timer = itemframe.timer,
115         on_rightclick = itemframe.rightclick,
116         on_punch = itemframe.punch,
117         can_dig = itemframe.dig,
118         after_destruct = remove_item
119 })
120
121 minetest.register_entity("xdecor:f_item", {
122         visual = "wielditem",
123         visual_size = {x=0.33, y=0.33},
124         collisionbox = {0},
125         physical = false,
126         textures = {"air"},
127         on_activate = function(self, staticdata)
128                 local pos = self.object:getpos()
129                 if minetest.get_node(pos).name ~= "xdecor:itemframe" then
130                         self.object:remove()
131                 end
132
133                 if tmp.nodename and tmp.texture then
134                         self.nodename = tmp.nodename
135                         tmp.nodename = nil
136                         self.texture = tmp.texture
137                         tmp.texture = nil
138                 elseif staticdata and staticdata ~= "" then
139                         local data = staticdata:split(";")
140                         if data and data[1] and data[2] then
141                                 self.nodename = data[1]
142                                 self.texture = data[2]
143                         end
144                 end
145                 if self.texture then
146                         self.object:set_properties({textures={self.texture}})
147                 end
148         end,
149         get_staticdata = function(self)
150                 if self.nodename and self.texture then
151                         return self.nodename..";"..self.texture
152                 end
153                 return ""
154         end
155 })
156