]> git.lizzy.rs Git - xdecor.git/blob - itemframe.lua
Move crafting guide to its own book
[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},
9         physical = false,
10         textures = {"air"},
11         on_activate = function(self, staticdata)
12                 if tmp.nodename and tmp.texture then
13                         self.nodename = tmp.nodename
14                         tmp.nodename = nil
15                         self.texture = tmp.texture
16                         tmp.texture = nil
17                 elseif staticdata 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 then
25                         self.object:set_properties({textures={self.texture}})
26                 end
27         end,
28         get_staticdata = function(self)
29                 if self.nodename and self.texture 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 = minetest.get_objects_inside_radius(pos, 0.5)
38         if not objs then return end
39
40         for _, obj in pairs(objs) do
41                 if obj and obj:get_luaentity() and
42                         obj:get_luaentity().name == "xdecor:f_item" then
43                         obj:remove()
44                 end
45         end
46 end
47
48 local facedir = {}
49 facedir[0] = {x=0, y=0, z=1}
50 facedir[1] = {x=1, y=0, z=0}
51 facedir[2] = {x=0, y=0, z=-1}
52 facedir[3] = {x=-1, y=0, z=0}
53
54 local update_item = function(pos, node)
55         remove_item(pos, node)
56         local meta = minetest.get_meta(pos)
57         local itemstring = meta:get_string("item")
58         local posad = facedir[node.param2]
59         if not posad or itemstring == "" then return end
60
61         pos.x = pos.x + posad.x * 6.5/16
62         pos.y = pos.y + posad.y * 6.5/16
63         pos.z = pos.z + posad.z * 6.5/16
64         tmp.nodename = node.name
65         tmp.texture = ItemStack(itemstring):get_name()
66
67         local entity = minetest.add_entity(pos, "xdecor:f_item")
68         local yaw = math.pi*2 - node.param2 * math.pi/2
69         entity:setyaw(yaw)
70 end
71
72 local drop_item = function(pos, node)
73         local meta = minetest.get_meta(pos)
74         if meta:get_string("item") == "" then return end
75
76         minetest.add_item(pos, meta:get_string("item"))
77         meta:set_string("item", "")
78         remove_item(pos, node)
79 end
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 = {
90                 "xdecor_wood.png", "xdecor_wood.png", "xdecor_wood.png",
91                 "xdecor_wood.png", "xdecor_wood.png", "xdecor_frame.png"
92         },
93         after_place_node = function(pos, placer, itemstack)
94                 local meta = minetest.get_meta(pos)
95                 local name = placer:get_player_name()
96
97                 meta:set_string("owner", name)
98                 meta:set_string("infotext", "Item Frame (owned by "..name..")")
99         end,
100         on_rightclick = function(pos, node, clicker, itemstack)
101                 local meta = minetest.get_meta(pos)
102                 local player = clicker:get_player_name()
103
104                 if player ~= meta:get_string("owner") or not itemstack then
105                         return
106                 end
107
108                 drop_item(pos, node)
109                 local itemstring = itemstack:take_item():to_string()
110                 meta:set_string("item", itemstring)
111                 update_item(pos, node)
112
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
119                 if player ~= meta:get_string("owner") then return end
120                 drop_item(pos, node)
121         end,
122         can_dig = function(pos, player)
123                 local meta = minetest.get_meta(pos)
124                 local owner = meta:get_string("owner")
125
126                 if not player or player:get_player_name() ~= owner then
127                         return false
128                 end
129
130                 return true
131         end,
132         after_destruct = remove_item
133 })
134
135 minetest.register_abm({
136         nodenames = {"xdecor:frame"},
137         interval = 15, chance = 1,
138         action = function(pos, node)
139                 local num = #minetest.get_objects_inside_radius(pos, 0.5)
140                 if num > 0 then return end
141                 update_item(pos, node)
142         end
143 })