]> git.lizzy.rs Git - xdecor.git/blob - handlers/registration.lua
More cleanup
[xdecor.git] / handlers / registration.lua
1 --[[ local default_can_dig = function(pos, _)
2         local meta = minetest.get_meta(pos)
3         local inv = meta:get_inventory()
4
5         return inv:is_empty("main")
6 end --]]
7
8 xbg = default.gui_bg..default.gui_bg_img..default.gui_slots
9 local default_inventory_size = 32
10
11 local default_inventory_formspecs = {
12         ["8"] = [[ size[8,6]
13                 list[context;main;0,0;8,1;]
14                 list[current_player;main;0,2;8,4;]
15                 listring[current_player;main]
16                 listring[context;main] ]]
17                 ..default.get_hotbar_bg(0,2),
18
19         ["16"] = [[ size[8,7]
20                 list[context;main;0,0;8,2;]
21                 list[current_player;main;0,3;8,4;]
22                 listring[current_player;main]
23                 listring[context;main] ]]
24                 ..default.get_hotbar_bg(0,3),
25
26         ["24"] = [[ size[8,8]
27                 list[context;main;0,0;8,3;]
28                 list[current_player;main;0,4;8,4;]
29                 listring[current_player;main]
30                 listring[context;main]" ]]
31                 ..default.get_hotbar_bg(0,4),
32
33         ["32"] = [[ size[8,9]
34                 list[context;main;0,0.3;8,4;]
35                 list[current_player;main;0,4.85;8,1;]
36                 list[current_player;main;0,6.08;8,3;8]
37                 listring[current_player;main]
38                 listring[context;main] ]]
39                 ..default.get_hotbar_bg(0,4.85)
40 }
41
42 local function get_formspec_by_size(size)
43         local formspec = default_inventory_formspecs[tostring(size)]
44         return formspec or default_inventory_formspecs
45 end
46
47 local function drop_stuff()
48         return function(pos, oldnode, oldmetadata, digger)
49                 local meta = minetest.get_meta(pos)
50                 meta:from_table(oldmetadata)
51                 local inv = meta:get_inventory()
52
53                 for i=1, inv:get_size("main") do
54                         local stack = inv:get_stack("main", i)
55                         if not stack:is_empty() then
56                                 local p = {
57                                         x = pos.x + math.random(0,5) / 5 - 0.5,
58                                         y = pos.y,
59                                         z = pos.z + math.random(0,5) / 5 - 0.5
60                                 }
61                                 minetest.add_item(p, stack)
62                         end
63                 end
64         end
65 end
66
67 function xdecor.register(name, def)
68         def.drawtype = def.drawtype or (def.node_box and "nodebox")
69         def.paramtype = def.paramtype or "light"
70         def.sounds = def.sounds or default.node_sound_defaults()
71
72         if not (def.drawtype == "normal" or def.drawtype == "signlike" or
73                         def.drawtype == "plantlike" or def.drawtype == "glasslike_framed" or
74                         def.drawtype == "glasslike_framed_optional") then
75                 def.paramtype2 = def.paramtype2 or "facedir"
76         end
77
78         if def.drawtype == "plantlike" or def.drawtype == "torchlike" or
79                         def.drawtype == "signlike" or def.drawtype == "fencelike" then
80                 def.sunlight_propagates = true
81         end
82
83         local infotext = def.infotext
84         local inventory = def.inventory
85         def.inventory = nil
86
87         if inventory then
88                 def.on_construct = def.on_construct or function(pos)
89                         local meta = minetest.get_meta(pos)
90                         if infotext then
91                                 meta:set_string("infotext", infotext)
92                         end
93
94                         local size = inventory.size or default_inventory_size
95                         local inv = meta:get_inventory()
96                         inv:set_size("main", size)
97                         meta:set_string("formspec", (inventory.formspec or get_formspec_by_size(size))..xbg)
98                 end
99                 def.after_dig_node = def.after_dig_node or drop_stuff()
100                 --def.can_dig = def.can_dig or default_can_dig
101         elseif infotext and not def.on_construct then
102                 def.on_construct = function(pos)
103                         local meta = minetest.get_meta(pos)
104                         meta:set_string("infotext", infotext)
105                 end
106         end
107
108         minetest.register_node("xdecor:"..name, def)
109 end