2 screwdriver = screwdriver or {}
3 local min, ceil = math.min, math.ceil
5 -- Nodes allowed to be cut.
6 -- Only the regular, solid blocks without metas or explosivity can be cut.
8 for node, def in pairs(minetest.registered_nodes) do
9 if (def.drawtype == "normal" or def.drawtype:sub(1,5) == "glass") and
10 (def.groups.cracky or def.groups.choppy) and
11 not def.on_construct and
12 not def.after_place_node and
13 not def.on_rightclick and
15 not def.allow_metadata_inventory_take and
16 not (def.groups.not_in_creative_inventory == 1) and
17 not (def.groups.not_cuttable == 1) and
18 not def.groups.wool and
19 (def.tiles and type(def.tiles[1]) == "string" and not
20 def.tiles[1]:find("default_mineral")) and
23 def.description ~= "" and
26 nodes[#nodes+1] = node
30 -- Optionally, you can register custom cuttable nodes in the workbench.
31 workbench.custom_nodes_register = {
36 __concat = function(t1, t2)
44 nodes = nodes..workbench.custom_nodes_register
46 -- Nodeboxes definitions.
48 -- Name Yield X Y Z W H L
49 {"nanoslab", 16, { 0, 0, 0, 8, 1, 8 }},
50 {"micropanel", 16, { 0, 0, 0, 16, 1, 8 }},
51 {"microslab", 8, { 0, 0, 0, 16, 1, 16 }},
52 {"thinstair", 8, { 0, 7, 0, 16, 1, 8 },
53 { 0, 15, 8, 16, 1, 8 }},
54 {"cube", 4, { 0, 0, 0, 8, 8, 8 }},
55 {"panel", 4, { 0, 0, 0, 16, 8, 8 }},
57 {"doublepanel", 2, { 0, 0, 0, 16, 8, 8 },
58 { 0, 8, 8, 16, 8, 8 }},
59 {"halfstair", 2, { 0, 0, 0, 8, 8, 16 },
60 { 0, 8, 8, 8, 8, 8 }},
61 {"outerstair", 1, { 0, 0, 0, 16, 8, 16 },
62 { 0, 8, 8, 8, 8, 8 }},
64 {"innerstair", 1, { 0, 0, 0, 16, 8, 16 },
65 { 0, 8, 8, 16, 8, 8 },
69 -- Tools allowed to be repaired.
70 function workbench:repairable(stack)
71 local tools = {"pick", "axe", "shovel", "sword", "hoe", "armor", "shield"}
72 for _, t in pairs(tools) do
73 if stack:find(t) then return true end
78 function workbench:get_output(inv, input, name)
79 if inv:is_empty("input") then
80 inv:set_list("forms", {}) return
84 for _, n in pairs(self.defs) do
85 local count = min(n[2] * input:get_count(), input:get_stack_max())
86 local item = name.."_"..n[1]
87 if not n[3] then item = "stairs:"..n[1].."_"..name:match(":(.*)") end
88 output[#output+1] = item.." "..count
90 inv:set_list("forms", output)
95 [[ label[0.9,1.23;Cut]
96 label[0.9,2.23;Repair]
97 box[-0.05,1;2.05,0.9;#555555]
98 box[-0.05,2;2.05,0.9;#555555]
99 button[0,0;2,1;craft;Crafting]
100 button[2,0;2,1;storage;Storage]
101 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
102 image[0,1;1,1;worktable_saw.png]
103 image[0,2;1,1;worktable_anvil.png]
104 image[3,2;1,1;hammer_layout.png]
105 list[context;input;2,1;1,1;]
106 list[context;tool;2,2;1,1;]
107 list[context;hammer;3,2;1,1;]
108 list[context;forms;4,0;4,3;] ]],
109 -- Crafting formspec.
110 [[ image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
111 button[0,0;1.5,1;back;< Back]
112 list[current_player;craft;2,0;3,3;]
113 list[current_player;craftpreview;6,1;1,1;]
114 listring[current_player;main]
115 listring[current_player;craft] ]],
117 [[ list[context;storage;0,1;8,2;]
118 button[0,0;1.5,1;back;< Back]
119 listring[context;storage]
120 listring[current_player;main] ]]
123 function workbench:set_formspec(meta, id)
124 meta:set_string("formspec", "size[8,7;]list[current_player;main;0,3.25;8,4;]"..
125 formspecs[id]..xbg..default.get_hotbar_bg(0,3.25))
128 function workbench.construct(pos)
129 local meta = minetest.get_meta(pos)
130 local inv = meta:get_inventory()
132 inv:set_size("tool", 1)
133 inv:set_size("input", 1)
134 inv:set_size("hammer", 1)
135 inv:set_size("forms", 4*3)
136 inv:set_size("storage", 8*2)
138 meta:set_string("infotext", "Work Bench")
139 workbench:set_formspec(meta, 1)
142 function workbench.fields(pos, _, fields)
143 local meta = minetest.get_meta(pos)
144 if fields.back then workbench:set_formspec(meta, 1)
145 elseif fields.craft then workbench:set_formspec(meta, 2)
146 elseif fields.storage then workbench:set_formspec(meta, 3) end
149 function workbench.dig(pos)
150 local inv = minetest.get_meta(pos):get_inventory()
151 return inv:is_empty("input") and inv:is_empty("hammer") and
152 inv:is_empty("tool") and inv:is_empty("storage")
155 function workbench.timer(pos)
156 local timer = minetest.get_node_timer(pos)
157 local inv = minetest.get_meta(pos):get_inventory()
158 local tool = inv:get_stack("tool", 1)
159 local hammer = inv:get_stack("hammer", 1)
161 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
165 -- Tool's wearing range: 0-65535 | 0 = new condition.
169 inv:set_stack("tool", 1, tool)
170 inv:set_stack("hammer", 1, hammer)
174 function workbench.put(_, listname, _, stack)
175 local stackname = stack:get_name()
176 if (listname == "tool" and stack:get_wear() > 0 and
177 workbench:repairable(stackname)) or
178 (listname == "input" and minetest.registered_nodes[stackname.."_cube"]) or
179 (listname == "hammer" and stackname == "xdecor:hammer") or
180 listname == "storage" then
181 return stack:get_count()
186 function workbench.take(_, listname, _, stack, player)
187 if listname == "forms" then
188 local inv = player:get_inventory()
189 if inv:room_for_item("main", stack:get_name()) then return -1 end
192 return stack:get_count()
195 function workbench.move(_, from_list, _, to_list, _, count)
196 if to_list == "storage" and from_list ~= "forms" then return count end
200 function workbench.on_put(pos, listname, _, stack)
201 local inv = minetest.get_meta(pos):get_inventory()
202 if listname == "input" then
203 local input = inv:get_stack("input", 1)
204 workbench:get_output(inv, input, stack:get_name())
205 elseif listname == "tool" or listname == "hammer" then
206 local timer = minetest.get_node_timer(pos)
211 function workbench.on_take(pos, listname, index, stack)
212 local inv = minetest.get_meta(pos):get_inventory()
213 local input = inv:get_stack("input", 1)
215 if listname == "input" then
216 if stack:get_name() == input:get_name() then
217 workbench:get_output(inv, input, stack:get_name())
219 inv:set_list("forms", {})
221 elseif listname == "forms" then
222 input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
223 inv:set_stack("input", 1, input)
224 workbench:get_output(inv, input, input:get_name())
228 xdecor.register("workbench", {
229 description = "Work Bench",
230 groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
231 sounds = default.node_sound_wood_defaults(),
232 tiles = {"xdecor_workbench_top.png", "xdecor_workbench_top.png",
233 "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
234 "xdecor_workbench_front.png", "xdecor_workbench_front.png"},
235 on_rotate = screwdriver.rotate_simple,
236 can_dig = workbench.dig,
237 on_timer = workbench.timer,
238 on_construct = workbench.construct,
239 on_receive_fields = workbench.fields,
240 on_metadata_inventory_put = workbench.on_put,
241 on_metadata_inventory_take = workbench.on_take,
242 allow_metadata_inventory_put = workbench.put,
243 allow_metadata_inventory_take = workbench.take,
244 allow_metadata_inventory_move = workbench.move
247 for _, d in pairs(workbench.defs) do
249 local node = nodes[i]
250 local def = minetest.registered_nodes[node]
255 groups.not_in_creative_inventory = 1
257 for k, v in pairs(def.groups) do
258 if k ~= "wood" and k ~= "stone" and k ~= "level" then
264 if #def.tiles > 1 and not (def.drawtype:sub(1,5) == "glass") then
267 tiles = {def.tiles[1]}
270 tiles = {def.tile_images[1]}
273 if not minetest.registered_nodes["stairs:slab_"..node:match(":(.*)")] then
274 stairs.register_stair_and_slab(node:match(":(.*)"), node,
275 groups, tiles, def.description.." Stair",
276 def.description.." Slab", def.sounds)
279 minetest.register_node(":"..node.."_"..d[1], {
280 description = def.description.." "..d[1]:gsub("^%l", string.upper),
282 paramtype2 = "facedir",
283 drawtype = "nodebox",
287 -- `unpack` has been changed to `table.unpack` in newest Lua versions.
288 node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
289 sunlight_propagates = true,
290 on_place = minetest.rotate_node