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.wool and
18 not def.tiles[1]:find("default_mineral") and
21 def.description ~= "" and
24 nodes[#nodes+1] = node
28 -- Optionally, you can register custom cuttable nodes in the workbench
29 workbench.custom_nodes_register = {
34 __concat = function(t1, t2)
42 nodes = nodes..workbench.custom_nodes_register
44 -- Nodeboxes definitions.
46 -- Name Yield X Y Z W H L
47 {"nanoslab", 16, { 0, 0, 0, 8, 1, 8 }},
48 {"micropanel", 16, { 0, 0, 0, 16, 1, 8 }},
49 {"microslab", 8, { 0, 0, 0, 16, 1, 16 }},
50 {"thinstair", 8, { 0, 7, 0, 16, 1, 8 },
51 { 0, 15, 8, 16, 1, 8 }},
52 {"cube", 4, { 0, 0, 0, 8, 8, 8 }},
53 {"panel", 4, { 0, 0, 0, 16, 8, 8 }},
55 {"doublepanel", 2, { 0, 0, 0, 16, 8, 8 },
56 { 0, 8, 8, 16, 8, 8 }},
57 {"halfstair", 2, { 0, 0, 0, 8, 8, 16 },
58 { 0, 8, 8, 8, 8, 8 }},
59 {"outerstair", 1, { 0, 0, 0, 16, 8, 16 },
60 { 0, 8, 8, 8, 8, 8 }},
62 {"innerstair", 1, { 0, 0, 0, 16, 8, 16 },
63 { 0, 8, 8, 16, 8, 8 },
67 -- Tools allowed to be repaired.
68 function workbench:repairable(stack)
69 local tools = {"pick", "axe", "shovel", "sword", "hoe", "armor", "shield"}
70 for _, t in pairs(tools) do
71 if stack:find(t) then return true end
76 function workbench:get_output(inv, input, name)
77 if inv:is_empty("input") then
78 inv:set_list("forms", {}) return
82 for _, n in pairs(self.defs) do
83 local count = min(n[2] * input:get_count(), input:get_stack_max())
84 local item = name.."_"..n[1]
85 if not n[3] then item = "stairs:"..n[1].."_"..name:match(":(.*)") end
86 output[#output+1] = item.." "..count
88 inv:set_list("forms", output)
93 [[ label[0.9,1.23;Cut]
94 label[0.9,2.23;Repair]
95 box[-0.05,1;2.05,0.9;#555555]
96 box[-0.05,2;2.05,0.9;#555555]
97 button[0,0;2,1;craft;Crafting]
98 button[2,0;2,1;storage;Storage]
99 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
100 image[0,1;1,1;worktable_saw.png]
101 image[0,2;1,1;worktable_anvil.png]
102 image[3,2;1,1;hammer_layout.png]
103 list[context;input;2,1;1,1;]
104 list[context;tool;2,2;1,1;]
105 list[context;hammer;3,2;1,1;]
106 list[context;forms;4,0;4,3;] ]],
107 -- Crafting formspec.
108 [[ image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
109 button[0,0;1.5,1;back;< Back]
110 list[current_player;craft;2,0;3,3;]
111 list[current_player;craftpreview;6,1;1,1;]
112 listring[current_player;main]
113 listring[current_player;craft] ]],
115 [[ list[context;storage;0,1;8,2;]
116 button[0,0;1.5,1;back;< Back]
117 listring[context;storage]
118 listring[current_player;main] ]]
121 function workbench:set_formspec(meta, id)
122 meta:set_string("formspec", "size[8,7;]list[current_player;main;0,3.25;8,4;]"..
123 formspecs[id]..xbg..default.get_hotbar_bg(0,3.25))
126 function workbench.construct(pos)
127 local meta = minetest.get_meta(pos)
128 local inv = meta:get_inventory()
130 inv:set_size("tool", 1)
131 inv:set_size("input", 1)
132 inv:set_size("hammer", 1)
133 inv:set_size("forms", 4*3)
134 inv:set_size("storage", 8*2)
136 meta:set_string("infotext", "Work Bench")
137 workbench:set_formspec(meta, 1)
140 function workbench.fields(pos, _, fields)
141 local meta = minetest.get_meta(pos)
142 if fields.back then workbench:set_formspec(meta, 1)
143 elseif fields.craft then workbench:set_formspec(meta, 2)
144 elseif fields.storage then workbench:set_formspec(meta, 3) end
147 function workbench.dig(pos)
148 local inv = minetest.get_meta(pos):get_inventory()
149 return inv:is_empty("input") and inv:is_empty("hammer") and
150 inv:is_empty("tool") and inv:is_empty("storage")
153 function workbench.timer(pos)
154 local timer = minetest.get_node_timer(pos)
155 local inv = minetest.get_meta(pos):get_inventory()
156 local tool = inv:get_stack("tool", 1)
157 local hammer = inv:get_stack("hammer", 1)
159 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
163 -- Tool's wearing range: 0-65535 | 0 = new condition.
167 inv:set_stack("tool", 1, tool)
168 inv:set_stack("hammer", 1, hammer)
172 function workbench.put(_, listname, _, stack)
173 local stackname = stack:get_name()
174 if (listname == "tool" and stack:get_wear() > 0 and 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.take(_, listname, _, stack, player)
184 if listname == "forms" then
185 local inv = player:get_inventory()
186 if inv:room_for_item("main", stack:get_name()) then return -1 end
189 return stack:get_count()
192 function workbench.move(_, from_list, _, to_list, _, count)
193 if to_list == "storage" and from_list ~= "forms" then return count end
197 function workbench.on_put(pos, listname, _, stack)
198 local inv = minetest.get_meta(pos):get_inventory()
199 if listname == "input" then
200 local input = inv:get_stack("input", 1)
201 workbench:get_output(inv, input, stack:get_name())
202 elseif listname == "tool" or listname == "hammer" then
203 local timer = minetest.get_node_timer(pos)
208 function workbench.on_take(pos, listname, index, stack)
209 local inv = minetest.get_meta(pos):get_inventory()
210 local input = inv:get_stack("input", 1)
212 if listname == "input" then
213 if stack:get_name() == input:get_name() then
214 workbench:get_output(inv, input, stack:get_name())
216 inv:set_list("forms", {})
218 elseif listname == "forms" then
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, input:get_name())
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_take = workbench.take,
241 allow_metadata_inventory_move = workbench.move
244 for _, d in pairs(workbench.defs) do
246 local node = nodes[i]
247 local def = minetest.registered_nodes[node]
252 groups.not_in_creative_inventory = 1
254 for k, v in pairs(def.groups) do
255 if k ~= "wood" and k ~= "stone" and k ~= "level" then
261 if #def.tiles > 1 and not (def.drawtype:sub(1,5) == "glass") then
264 tiles = {def.tiles[1]}
267 tiles = {def.tile_images[1]}
270 if not minetest.registered_nodes["stairs:slab_"..node:match(":(.*)")] then
271 stairs.register_stair_and_slab(node:match(":(.*)"), node, groups, tiles,
272 def.description.." Stair", 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