]> git.lizzy.rs Git - xdecor.git/blob - itemframe.lua
Misc. cleaning
[xdecor.git] / itemframe.lua
1 local 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()
12                 end
13         end
14 end
15
16 local facedir = {
17         [0] = {x=0, y=0, z=1}, {x=1, y=0, z=0},
18               {x=0, y=0, z=-1},{x=-1, y=0, z=0}
19 }
20
21 local function update_item(pos, node)
22         remove_item(pos, node)
23         local meta = minetest.get_meta(pos)
24         local itemstring = meta:get_string("item")
25         local posad = facedir[node.param2]
26         if not posad or itemstring == "" then return end
27
28         pos.x = pos.x + posad.x * 6.5/16
29         pos.y = pos.y + posad.y * 6.5/16
30         pos.z = pos.z + posad.z * 6.5/16
31         tmp.nodename = node.name
32         tmp.texture = ItemStack(itemstring):get_name()
33
34         local entity = minetest.add_entity(pos, "xdecor:f_item")
35         local yaw = math.pi*2 - node.param2 * math.pi/2
36         entity:setyaw(yaw)
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 end
48
49 minetest.register_entity("xdecor:f_item", {
50         hp_max = 1,
51         visual = "wielditem",
52         visual_size = {x=.33, y=.33},
53         collisionbox = {0},
54         physical = false,
55         textures = {"air"},
56         on_activate = function(self, staticdata)
57                 if tmp.nodename and tmp.texture then
58                         self.nodename = tmp.nodename
59                         tmp.nodename = nil
60                         self.texture = tmp.texture
61                         tmp.texture = nil
62                 elseif staticdata and staticdata ~= "" then
63                         local data = staticdata:split(";")
64                         if data and data[1] and data[2] then
65                                 self.nodename = data[1]
66                                 self.texture = data[2]
67                         end
68                 end
69                 if self.texture then
70                         self.object:set_properties({textures={self.texture}})
71                 end
72         end,
73         get_staticdata = function(self)
74                 if self.nodename and self.texture then
75                         return self.nodename..";"..self.texture
76                 end
77                 return ""
78         end
79 })
80
81 xdecor.register("frame", {
82         description = "Item Frame",
83         groups = {choppy=3, oddly_breakable_by_hand=2},
84         sounds = default.node_sound_wood_defaults(),
85         on_rotate = screwdriver.disallow,
86         sunlight_propagates = true,
87         inventory_image = "xdecor_frame.png",
88         node_box = xdecor.nodebox.slab_z(0.9375),
89         tiles = {"xdecor_wood.png", "xdecor_wood.png", "xdecor_wood.png",
90                  "xdecor_wood.png", "xdecor_wood.png", "xdecor_frame.png"},
91         after_place_node = function(pos, placer, itemstack)
92                 local meta = minetest.get_meta(pos)
93                 local name = placer:get_player_name()
94                 meta:set_string("owner", name)
95                 meta:set_string("infotext", "Item Frame (owned by "..name..")")
96         end,
97         on_rightclick = function(pos, node, clicker, itemstack)
98                 local meta = minetest.get_meta(pos)
99                 local player = clicker:get_player_name()
100                 local owner = meta:get_string("owner")
101                 if player ~= owner or not itemstack then return end
102
103                 drop_item(pos, node)
104                 local itemstring = itemstack:take_item():to_string()
105                 meta:set_string("item", itemstring)
106                 update_item(pos, node)
107
108                 return itemstack
109         end,
110         on_punch = function(pos, node, puncher)
111                 local meta = minetest.get_meta(pos)
112                 local player = puncher:get_player_name()
113                 local owner = meta:get_string("owner")
114
115                 if player ~= owner then return end
116                 drop_item(pos, node)
117         end,
118         can_dig = function(pos, player)
119                 local meta = minetest.get_meta(pos)
120                 local pname = player:get_player_name()
121                 local owner = meta:get_string("owner")
122
123                 return player and pname == owner
124         end,
125         after_destruct = remove_item
126 })
127
128 minetest.register_abm({
129         nodenames = {"xdecor:frame"},
130         interval = 15, chance = 1,
131         action = function(pos, node)
132                 local num = #minetest.get_objects_inside_radius(pos, 0.5)
133                 if num > 0 then return end
134                 update_item(pos, node)
135         end
136 })