]> git.lizzy.rs Git - xdecor.git/blob - handlers/registration.lua
cbeabdb03822c105f22d5c9e3d17abe0343877a0
[xdecor.git] / handlers / registration.lua
1 xbg = default.gui_bg..default.gui_bg_img..default.gui_slots
2 local default_inventory_size = 32
3
4 local default_inventory_formspecs = {
5         ["8"] = [[ size[8,6]
6                 list[context;main;0,0;8,1;]
7                 list[current_player;main;0,2;8,4;]
8                 listring[current_player;main]
9                 listring[context;main] ]]
10                 ..default.get_hotbar_bg(0,2),
11
12         ["16"] = [[ size[8,7]
13                 list[context;main;0,0;8,2;]
14                 list[current_player;main;0,3;8,4;]
15                 listring[current_player;main]
16                 listring[context;main] ]]
17                 ..default.get_hotbar_bg(0,3),
18
19         ["24"] = [[ size[8,8]
20                 list[context;main;0,0;8,3;]
21                 list[current_player;main;0,4;8,4;]
22                 listring[current_player;main]
23                 listring[context;main]" ]]
24                 ..default.get_hotbar_bg(0,4),
25
26         ["32"] = [[ size[8,9]
27                 list[context;main;0,0.3;8,4;]
28                 list[current_player;main;0,4.85;8,1;]
29                 list[current_player;main;0,6.08;8,3;8]
30                 listring[current_player;main]
31                 listring[context;main] ]]
32                 ..default.get_hotbar_bg(0,4.85)
33 }
34
35 local function get_formspec_by_size(size)
36         local formspec = default_inventory_formspecs[tostring(size)]
37         return formspec or default_inventory_formspecs
38 end
39
40 local default_can_dig = function(pos)
41         local inv = minetest.get_meta(pos):get_inventory()
42         return inv:is_empty("main")
43 end
44
45 function xdecor.register(name, def)
46         local function xdecor_stairs_alternative(nodename, def)
47                 local mod, name = nodename:match("(.*):(.*)")
48                 for groupname, value in pairs(def.groups) do
49                         if groupname ~= "cracky"    and groupname ~= "choppy"  and
50                            groupname ~= "flammable" and groupname ~= "crumbly" and
51                            groupname ~= "snappy"    then
52                                 def.groups.groupname = nil
53                         end
54                 end
55
56                 if minetest.get_modpath("moreblocks") then
57                         stairsplus:register_all(
58                                 mod,
59                                 name,
60                                 nodename,
61                                 {
62                                         description = def.description,
63                                         tiles = def.tiles,
64                                         groups = def.groups,
65                                         sounds = def.sounds,
66                                 }
67                         )
68                 elseif minetest.get_modpath("stairs") then      
69                         stairs.register_stair_and_slab(name,nodename,
70                                 def.groups,
71                                 def.tiles,
72                                 ("%s Stair"):format(def.description),
73                                 ("%s Slab"):format(def.description),
74                                 def.sounds
75                         )       
76                 end     
77         end
78
79         def.drawtype = def.drawtype or (def.mesh and "mesh") or (def.node_box and "nodebox")
80         def.sounds = def.sounds or default.node_sound_defaults()
81
82         if not (def.drawtype == "normal" or def.drawtype == "signlike" or
83                         def.drawtype == "plantlike" or def.drawtype == "glasslike_framed" or
84                         def.drawtype == "glasslike_framed_optional") then
85                 def.paramtype2 = def.paramtype2 or "facedir"
86         end
87
88         if def.sunlight_propagates ~= false and
89                         (def.drawtype == "plantlike" or def.drawtype == "torchlike" or
90                         def.drawtype == "signlike" or def.drawtype == "fencelike") then
91                 def.sunlight_propagates = true
92         end
93
94         if not def.paramtype and
95                 (def.light_source or def.sunlight_propagates or
96                 def.drawtype == "nodebox" or def.drawtype == "mesh") then
97                 def.paramtype = "light"
98         end
99
100         local infotext = def.infotext
101         local inventory = def.inventory
102         def.inventory = nil
103
104         if inventory then
105                 def.on_construct = def.on_construct or function(pos)
106                         local meta = minetest.get_meta(pos)
107                         if infotext then meta:set_string("infotext", infotext) end
108
109                         local size = inventory.size or default_inventory_size
110                         local inv = meta:get_inventory()
111                         inv:set_size("main", size)
112                         meta:set_string("formspec", (inventory.formspec or
113                                         get_formspec_by_size(size))..xbg)
114                 end
115                 def.can_dig = def.can_dig or default_can_dig
116         elseif infotext and not def.on_construct then
117                 def.on_construct = function(pos)
118                         local meta = minetest.get_meta(pos)
119                         meta:set_string("infotext", infotext)
120                 end
121         end
122
123         minetest.register_node("xdecor:"..name, def)
124
125         local workbench = minetest.settings:get_bool("enable_xdecor_workbench")
126
127         if workbench == false and
128            (minetest.get_modpath("moreblocks") or minetest.get_modpath("stairs")) then
129                 if xdecor.stairs_valid_def(def) then
130                         xdecor_stairs_alternative("xdecor:"..name, def)
131                 end
132         end
133 end