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