3 screwdriver = screwdriver or {}
4 local min, ceil = math.min, math.ceil
6 -- Nodes allowed to be cut
7 -- Only the regular, solid blocks without metas or explosivity can be cut
9 for node, def in pairs(minetest.registered_nodes) do
10 if (def.drawtype == "normal" or def.drawtype:sub(1,5) == "glass") and
11 (def.groups.cracky or def.groups.choppy) and
12 not def.on_construct and
13 not def.after_place_node and
14 not def.on_rightclick and
16 not def.allow_metadata_inventory_take and
17 not (def.groups.not_in_creative_inventory == 1) and
18 not (def.groups.not_cuttable == 1) and
19 not def.groups.wool and
20 (def.tiles and type(def.tiles[1]) == "string" and not
21 def.tiles[1]:find("default_mineral")) and
24 def.description ~= "" and
27 nodes[#nodes+1] = node
31 -- Optionally, you can register custom cuttable nodes in the workbench
32 WB.custom_nodes_register = {
37 __concat = function(t1, t2)
45 nodes = nodes..WB.custom_nodes_register
47 -- Nodeboxes definitions
49 -- Name Yield X Y Z W H L
50 {"nanoslab", 16, { 0, 0, 0, 8, 1, 8 }},
51 {"micropanel", 16, { 0, 0, 0, 16, 1, 8 }},
52 {"microslab", 8, { 0, 0, 0, 16, 1, 16 }},
53 {"thinstair", 8, { 0, 7, 0, 16, 1, 8 },
54 { 0, 15, 8, 16, 1, 8 }},
55 {"cube", 4, { 0, 0, 0, 8, 8, 8 }},
56 {"panel", 4, { 0, 0, 0, 16, 8, 8 }},
58 {"doublepanel", 2, { 0, 0, 0, 16, 8, 8 },
59 { 0, 8, 8, 16, 8, 8 }},
60 {"halfstair", 2, { 0, 0, 0, 8, 8, 16 },
61 { 0, 8, 8, 8, 8, 8 }},
62 {"outerstair", 1, { 0, 0, 0, 16, 8, 16 },
63 { 0, 8, 8, 8, 8, 8 }},
65 {"innerstair", 1, { 0, 0, 0, 16, 8, 16 },
66 { 0, 8, 8, 16, 8, 8 },
70 -- Tools allowed to be repaired
71 function workbench:repairable(stack)
72 local tools = {"pick", "axe", "shovel", "sword", "hoe", "armor", "shield"}
74 return stack:find(tools[i])
78 function workbench:get_output(inv, input, name)
80 for _, n in pairs(self.defs) do
81 local count = min(n[2] * input:get_count(), input:get_stack_max())
82 local item = name.."_"..n[1]
83 if not n[3] then item = "stairs:"..n[1].."_"..name:match(":(.*)") end
84 output[#output+1] = item.." "..count
86 inv:set_list("forms", output)
91 [[ label[0.9,1.23;Cut]
92 label[0.9,2.23;Repair]
93 box[-0.05,1;2.05,0.9;#555555]
94 box[-0.05,2;2.05,0.9;#555555]
95 button[0,0;2,1;craft;Crafting]
96 button[2,0;2,1;storage;Storage]
97 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
98 image[0,1;1,1;worktable_saw.png]
99 image[0,2;1,1;worktable_anvil.png]
100 image[3,2;1,1;hammer_layout.png]
101 list[context;input;2,1;1,1;]
102 list[context;tool;2,2;1,1;]
103 list[context;hammer;3,2;1,1;]
104 list[context;forms;4,0;4,3;] ]],
106 [[ image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
107 button[0,0;1.5,1;back;< Back]
108 list[current_player;craft;2,0;3,3;]
109 list[current_player;craftpreview;6,1;1,1;]
110 listring[current_player;main]
111 listring[current_player;craft] ]],
113 [[ list[context;storage;0,1;8,2;]
114 button[0,0;1.5,1;back;< Back]
115 listring[context;storage]
116 listring[current_player;main] ]]
119 function workbench:set_formspec(meta, id)
120 meta:set_string("formspec", "size[8,7;]list[current_player;main;0,3.25;8,4;]"..
121 formspecs[id]..xbg..default.get_hotbar_bg(0,3.25))
124 function workbench.construct(pos)
125 local meta = minetest.get_meta(pos)
126 local inv = meta:get_inventory()
128 inv:set_size("tool", 1)
129 inv:set_size("input", 1)
130 inv:set_size("hammer", 1)
131 inv:set_size("forms", 4*3)
132 inv:set_size("storage", 8*2)
134 meta:set_string("infotext", "Work Bench")
135 workbench:set_formspec(meta, 1)
138 function workbench.fields(pos, _, fields)
139 local meta = minetest.get_meta(pos)
140 if fields.back then workbench:set_formspec(meta, 1)
141 elseif fields.craft then workbench:set_formspec(meta, 2)
142 elseif fields.storage then workbench:set_formspec(meta, 3) end
145 function workbench.dig(pos)
146 local inv = minetest.get_meta(pos):get_inventory()
147 return inv:is_empty("input") and inv:is_empty("hammer") and
148 inv:is_empty("tool") and inv:is_empty("storage")
151 function workbench.timer(pos)
152 local timer = minetest.get_node_timer(pos)
153 local inv = minetest.get_meta(pos):get_inventory()
154 local tool = inv:get_stack("tool", 1)
155 local hammer = inv:get_stack("hammer", 1)
157 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
162 -- Tool's wearing range: 0-65535 | 0 = new condition
166 inv:set_stack("tool", 1, tool)
167 inv:set_stack("hammer", 1, hammer)
171 function workbench.put(_, listname, _, stack)
172 local stackname = stack:get_name()
173 if (listname == "tool" and stack:get_wear() > 0 and
174 workbench:repairable(stackname)) or
175 (listname == "input" and minetest.registered_nodes[stackname.."_cube"]) or
176 (listname == "hammer" and stackname == "xdecor:hammer") or
177 listname == "storage" then
178 return stack:get_count()
183 function workbench.move(_, from_list, _, to_list, _, count)
184 return (to_list == "storage" and from_list ~= "forms") and count or 0
187 function workbench.on_put(pos, listname, _, stack)
188 local inv = minetest.get_meta(pos):get_inventory()
189 if listname == "input" then
190 local input = inv:get_stack("input", 1)
191 workbench:get_output(inv, input, stack:get_name())
192 elseif listname == "tool" or listname == "hammer" then
193 local timer = minetest.get_node_timer(pos)
198 function workbench.on_take(pos, listname, index, stack, player)
199 local inv = minetest.get_meta(pos):get_inventory()
200 local input = inv:get_stack("input", 1)
201 local inputname = input:get_name()
202 local stackname = stack:get_name()
204 if listname == "input" then
205 if stackname == inputname then
206 workbench:get_output(inv, input, stackname)
208 inv:set_list("forms", {})
210 elseif listname == "forms" then
211 local fromstack = inv:get_stack(listname, index)
212 if not fromstack:is_empty() and fromstack:get_name() ~= stackname then
213 local player_inv = player:get_inventory()
214 if player_inv:room_for_item("main", fromstack) then
215 player_inv:add_item("main", fromstack)
219 input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
220 inv:set_stack("input", 1, input)
221 workbench:get_output(inv, input, inputname)
225 xdecor.register("workbench", {
226 description = "Work Bench",
227 groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
228 sounds = default.node_sound_wood_defaults(),
229 tiles = {"xdecor_workbench_top.png", "xdecor_workbench_top.png",
230 "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
231 "xdecor_workbench_front.png", "xdecor_workbench_front.png"},
232 on_rotate = screwdriver.rotate_simple,
233 can_dig = workbench.dig,
234 on_timer = workbench.timer,
235 on_construct = workbench.construct,
236 on_receive_fields = workbench.fields,
237 on_metadata_inventory_put = workbench.on_put,
238 on_metadata_inventory_take = workbench.on_take,
239 allow_metadata_inventory_put = workbench.put,
240 allow_metadata_inventory_move = workbench.move
243 for _, d in pairs(workbench.defs) do
245 local node = nodes[i]
246 local def = minetest.registered_nodes[node]
251 groups.not_in_creative_inventory = 1
253 for k, v in pairs(def.groups) do
254 if k ~= "wood" and k ~= "stone" and k ~= "level" then
260 if #def.tiles > 1 and not (def.drawtype:sub(1,5) == "glass") then
263 tiles = {def.tiles[1]}
266 tiles = {def.tile_images[1]}
269 if not minetest.registered_nodes["stairs:slab_"..node:match(":(.*)")] then
270 stairs.register_stair_and_slab(node:match(":(.*)"), node,
271 groups, tiles, def.description.." Stair",
272 def.description.." Slab", def.sounds)
275 minetest.register_node(":"..node.."_"..d[1], {
276 description = def.description.." "..d[1]:gsub("^%l", string.upper),
278 paramtype2 = "facedir",
279 drawtype = "nodebox",
283 -- `unpack` has been changed to `table.unpack` in newest Lua versions
284 node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
285 sunlight_propagates = true,
286 on_place = minetest.rotate_node