]> git.lizzy.rs Git - xdecor.git/blob - itemframe.lua
Mailbox : add a box to see a list of last 7 donators
[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                 elseif 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                 if self.texture ~= nil then
25                         self.object:set_properties({textures={self.texture}})
26                 end
27         end,
28         get_staticdata = function(self)
29                 if self.nodename ~= nil and self.texture ~= nil then
30                         return self.nodename..";"..self.texture
31                 end
32                 return ""
33         end
34 })
35
36 local remove_item = function(pos, node)
37         local objs = nil
38         objs = minetest.get_objects_inside_radius(pos, .5)
39         if not objs then return end
40
41         for _, obj in pairs(objs) do
42                 if obj and obj:get_luaentity() and
43                         obj:get_luaentity().name == "xdecor:f_item" then
44                         obj:remove()
45                 end
46         end
47 end
48
49 local facedir = {}
50 facedir[0] = {x=0, y=0, z=1}
51 facedir[1] = {x=1, y=0, z=0}
52 facedir[2] = {x=0, y=0, z=-1}
53 facedir[3] = {x=-1, y=0, z=0}
54
55 local update_item = function(pos, node)
56         remove_item(pos, node)
57         local meta = minetest.get_meta(pos)
58         local str_item = meta:get_string("item")
59         if str_item == "" then return end
60         
61         local posad = facedir[node.param2]
62         if not posad then return end
63         pos.x = pos.x + posad.x * 6.5/16
64         pos.y = pos.y + posad.y * 6.5/16
65         pos.z = pos.z + posad.z * 6.5/16
66         tmp.nodename = node.name
67         tmp.texture = ItemStack(str_item):get_name()
68
69         local e = minetest.add_entity(pos, "xdecor:f_item")
70         local yaw = math.pi*2 - node.param2 * math.pi/2
71         e:setyaw(yaw)
72 end
73
74 local drop_item = function(pos, node)
75         local meta = minetest.get_meta(pos)
76         if meta:get_string("item") == "" then return end
77         minetest.add_item(pos, meta:get_string("item"))
78         meta:set_string("item", "")
79         remove_item(pos, node)
80 end
81
82 xdecor.register("frame", {
83         description = "Item Frame",
84         groups = {choppy=3, oddly_breakable_by_hand=2},
85         sounds = default.node_sound_wood_defaults(),
86         on_rotate = screwdriver.disallow,
87         sunlight_propagates = true,
88         node_box = {
89                 type = "fixed",
90                 fixed = {-0.5, -0.5, 7/16, 0.5, 0.5, 0.5}
91         },
92         tiles = {
93                 "xdecor_wood.png", "xdecor_wood.png", "xdecor_wood.png",
94                 "xdecor_wood.png", "xdecor_wood.png", "xdecor_frame.png"
95         }, 
96         inventory_image = "xdecor_frame.png",
97         after_place_node = function(pos, placer, itemstack)
98                 local meta = minetest.get_meta(pos)
99                 local name = placer:get_player_name()
100                 meta:set_string("owner", name)
101                 meta:set_string("infotext", "Item frame (owned by "..name..")")
102         end,
103         on_rightclick = function(pos, node, clicker, itemstack)
104                 if not itemstack then return end
105                 local meta = minetest.get_meta(pos)
106                 local player = clicker:get_player_name()
107                 if player ~= meta:get_string("owner") then return end
108
109                 drop_item(pos, node)
110                 local s = itemstack:take_item()
111                 meta:set_string("item", s:to_string())
112                 update_item(pos, node)
113                 return itemstack
114         end,
115         on_punch = function(pos, node, puncher)
116                 local meta = minetest.get_meta(pos)
117                 local player = puncher:get_player_name()
118                 if player ~= meta:get_string("owner") then return end
119                 drop_item(pos, node)
120         end,
121         can_dig = function(pos, player)
122                 local meta = minetest.get_meta(pos)
123                 local owner = meta:get_string("owner")
124
125                 if not player or player:get_player_name() ~= owner then return false end
126                 return true
127         end,
128         on_destruct = function(pos)
129                 local meta = minetest.get_meta(pos)
130                 local node = minetest.get_node(pos)
131                 if meta:get_string("item") == "" then return end
132                 drop_item(pos, node)
133         end
134 })
135
136 minetest.register_abm({
137         nodenames = {"xdecor:frame"},
138         interval = 15, chance = 1,
139         action = function(pos, node, _, _)
140                 local num
141                 if node.name ~= "xdecor:frame" then return end
142                 num = #minetest.get_objects_inside_radius(pos, 0.5)
143                 if num > 0 then return end
144                 update_item(pos, node)
145         end
146 })