3 screwdriver = screwdriver or {}
4 local min, ceil = math.min, math.ceil
5 local registered_nodes = minetest.registered_nodes
7 -- Nodes allowed to be cut
8 -- Only the regular, solid blocks without metas or explosivity can be cut
10 for node, def in pairs(registered_nodes) do
11 if xdecor.stairs_valid_def(def) then
12 nodes[#nodes + 1] = node
16 -- Optionally, you can register custom cuttable nodes in the workbench
17 WB.custom_nodes_register = {
22 __concat = function(t1, t2)
31 nodes = nodes .. WB.custom_nodes_register
33 -- Nodeboxes definitions
35 -- Name YieldX YZ WH L
36 {"nanoslab", 16, { 0, 0, 0, 8, 1, 8 }},
37 {"micropanel", 16, { 0, 0, 0, 16, 1, 8 }},
38 {"microslab", 8, { 0, 0, 0, 16, 1, 16 }},
39 {"thinstair", 8, { 0, 7, 0, 16, 1, 8 },
40 { 0, 15, 8, 16, 1, 8 }},
41 {"cube", 4, { 0, 0, 0, 8, 8, 8 }},
42 {"panel", 4, { 0, 0, 0, 16, 8, 8 }},
44 {"doublepanel", 2, { 0, 0, 0, 16, 8, 8 },
45 { 0, 8, 8, 16, 8, 8 }},
46 {"halfstair", 2, { 0, 0, 0, 8, 8, 16 },
47 { 0, 8, 8, 8, 8, 8 }},
48 {"stair_outer", 1, nil },
50 {"stair_inner", 1, nil }
53 local repairable_tools = {"pick", "axe", "shovel", "sword", "hoe", "armor", "shield"}
55 -- Tools allowed to be repaired
56 function workbench:repairable(stack)
57 for _, t in ipairs(repairable_tools) do
64 function workbench:get_output(inv, input, name)
66 for i = 1, #self.defs do
67 local nbox = self.defs[i]
68 local count = min(nbox[2] * input:get_count(), input:get_stack_max())
69 local item = name .. "_" .. nbox[1]
71 item = nbox[3] and item or "stairs:" .. nbox[1] .. "_" .. name:match(":(.*)")
72 output[i] = item .. " " .. count
75 inv:set_list("forms", output)
80 label[0.9,2.23;Repair]
81 box[-0.05,1;2.05,0.9;#555555]
82 box[-0.05,2;2.05,0.9;#555555]
83 button[0,0;2,1;craft;Crafting]
84 button[2,0;2,1;storage;Storage]
85 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
86 image[0,1;1,1;worktable_saw.png]
87 image[0,2;1,1;worktable_anvil.png]
88 image[3,2;1,1;hammer_layout.png]
89 list[context;input;2,1;1,1;]
90 list[context;tool;2,2;1,1;]
91 list[context;hammer;3,2;1,1;]
92 list[context;forms;4,0;4,3;]
93 listring[current_player;main]
94 listring[context;tool]
95 listring[current_player;main]
96 listring[context;hammer]
97 listring[current_player;main]
98 listring[context;forms]
99 listring[current_player;main]
100 listring[context;input]
103 local crafting_fs = [[
104 image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
105 button[0,0;1.5,1;back;< Back]
106 list[current_player;craft;2,0;3,3;]
107 list[current_player;craftpreview;6,1;1,1;]
108 listring[current_player;main]
109 listring[current_player;craft]
112 local storage_fs = [[
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]
130 function workbench:set_formspec(meta, id)
131 meta:set_string("formspec",
132 "size[8,7;]list[current_player;main;0,3.25;8,4;]" ..
133 formspecs[id] .. xbg .. default.get_hotbar_bg(0,3.25))
136 function workbench.construct(pos)
137 local meta = minetest.get_meta(pos)
138 local inv = meta:get_inventory()
140 inv:set_size("tool", 1)
141 inv:set_size("input", 1)
142 inv:set_size("hammer", 1)
143 inv:set_size("forms", 4*3)
144 inv:set_size("storage", 8*2)
146 meta:set_string("infotext", "Work Bench")
147 workbench:set_formspec(meta, 1)
150 function workbench.fields(pos, _, fields)
151 if fields.quit then return end
153 local meta = minetest.get_meta(pos)
154 local id = fields.back and 1 or fields.craft and 2 or fields.storage and 3
155 if not id then return end
157 workbench:set_formspec(meta, id)
160 function workbench.dig(pos)
161 local inv = minetest.get_meta(pos):get_inventory()
162 return inv:is_empty("input") and inv:is_empty("hammer") and
163 inv:is_empty("tool") and inv:is_empty("storage")
166 function workbench.timer(pos)
167 local timer = minetest.get_node_timer(pos)
168 local inv = minetest.get_meta(pos):get_inventory()
169 local tool = inv:get_stack("tool", 1)
170 local hammer = inv:get_stack("hammer", 1)
172 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
177 -- Tool's wearing range: 0-65535; 0 = new condition
181 inv:set_stack("tool", 1, tool)
182 inv:set_stack("hammer", 1, hammer)
187 function workbench.allow_put(pos, listname, index, stack, player)
188 local stackname = stack:get_name()
189 if (listname == "tool" and stack:get_wear() > 0 and
190 workbench:repairable(stackname)) or
191 (listname == "input" and registered_nodes[stackname .. "_cube"]) or
192 (listname == "hammer" and stackname == "xdecor:hammer") or
193 listname == "storage" then
194 return stack:get_count()
200 function workbench.on_put(pos, listname, index, stack, player)
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.allow_move(pos, from_list, from_index, to_list, to_index, count, player)
212 return (to_list == "storage" and from_list ~= "forms") and count or 0
215 function workbench.on_move(pos, from_list, from_index, to_list, to_index, count, player)
216 local meta = minetest.get_meta(pos)
217 local inv = meta:get_inventory()
218 local from_stack = inv:get_stack(from_list, from_index)
219 local to_stack = inv:get_stack(to_list, to_index)
221 workbench.on_take(pos, from_list, from_index, from_stack, player)
222 workbench.on_put(pos, to_list, to_index, to_stack, player)
225 function workbench.allow_take(pos, listname, index, stack, player)
226 return stack:get_count()
229 function workbench.on_take(pos, listname, index, stack, player)
230 local inv = minetest.get_meta(pos):get_inventory()
231 local input = inv:get_stack("input", 1)
232 local inputname = input:get_name()
233 local stackname = stack:get_name()
235 if listname == "input" then
236 if stackname == inputname and registered_nodes[inputname .. "_cube"] then
237 workbench:get_output(inv, input, stackname)
239 inv:set_list("forms", {})
241 elseif listname == "forms" then
242 local fromstack = inv:get_stack(listname, index)
243 if not fromstack:is_empty() and fromstack:get_name() ~= stackname then
244 local player_inv = player:get_inventory()
245 if player_inv:room_for_item("main", fromstack) then
246 player_inv:add_item("main", fromstack)
250 input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
251 inv:set_stack("input", 1, input)
252 workbench:get_output(inv, input, inputname)
256 xdecor.register("workbench", {
257 description = "Work Bench",
258 groups = {cracky = 2, choppy = 2, oddly_breakable_by_hand = 1},
259 sounds = default.node_sound_wood_defaults(),
261 "xdecor_workbench_top.png","xdecor_workbench_top.png",
262 "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
263 "xdecor_workbench_front.png", "xdecor_workbench_front.png"
265 on_rotate = screwdriver.rotate_simple,
266 can_dig = workbench.dig,
267 on_timer = workbench.timer,
268 on_construct = workbench.construct,
269 on_receive_fields = workbench.fields,
270 on_metadata_inventory_put = workbench.on_put,
271 on_metadata_inventory_take = workbench.on_take,
272 on_metadata_inventory_move = workbench.on_move,
273 allow_metadata_inventory_put = workbench.allow_put,
274 allow_metadata_inventory_take = workbench.allow_take,
275 allow_metadata_inventory_move = workbench.allow_move
278 for _, d in ipairs(workbench.defs) do
280 local node = nodes[i]
281 local mod_name, item_name = node:match("^(.-):(.*)")
282 local def = registered_nodes[node]
284 if item_name and d[3] then
287 groups.not_in_creative_inventory = 1
289 for k, v in pairs(def.groups) do
290 if k ~= "wood" and k ~= "stone" and k ~= "level" then
296 if #def.tiles > 1 and (def.drawtype:sub(1,5) ~= "glass") then
299 tiles = {def.tiles[1]}
302 tiles = {def.tile_images[1]}
305 if not registered_nodes["stairs:slab_" .. item_name] then
306 stairs.register_stair_and_slab(item_name, node,
307 groups, tiles, def.description .. " Stair",
308 def.description .. " Slab", def.sounds)
311 minetest.register_node(":" .. node .. "_" .. d[1], {
312 description = def.description .. " " .. d[1]:gsub("^%l", string.upper),
314 paramtype2 = "facedir",
315 drawtype = "nodebox",
319 -- `unpack` has been changed to `table.unpack` in newest Lua versions
320 node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
321 sunlight_propagates = true,
322 on_place = minetest.rotate_node
325 elseif item_name and mod_name then
326 minetest.register_alias_force(
327 ("%s:%s_innerstair"):format(mod_name, item_name),
328 ("stairs:stair_inner_%s"):format(item_name)
330 minetest.register_alias_force(
331 ("%s:%s_outerstair"):format(mod_name, item_name),
332 ("stairs:stair_outer_%s"):format(item_name)
340 minetest.register_tool("xdecor:hammer", {
341 description = "Hammer",
342 inventory_image = "xdecor_hammer.png",
343 wield_image = "xdecor_hammer.png",
344 on_use = function() do
351 minetest.register_craft({
352 output = "xdecor:hammer",
354 {"default:steel_ingot", "group:stick", "default:steel_ingot"},
355 {"", "group:stick", ""}
359 minetest.register_craft({
360 output = "xdecor:workbench",
362 {"group:wood", "group:wood"},
363 {"group:wood", "group:wood"}