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[#output + 1] = 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.put(_, listname, _, stack)
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.move(_, from_list, _, to_list, _, count)
201 return (to_list == "storage" and from_list ~= "forms") and count or 0
204 function workbench.on_put(pos, listname, _, stack)
205 local inv = minetest.get_meta(pos):get_inventory()
206 if listname == "input" then
207 local input = inv:get_stack("input", 1)
208 workbench:get_output(inv, input, stack:get_name())
209 elseif listname == "tool" or listname == "hammer" then
210 local timer = minetest.get_node_timer(pos)
215 function workbench.on_take(pos, listname, index, stack, player)
216 local inv = minetest.get_meta(pos):get_inventory()
217 local input = inv:get_stack("input", 1)
218 local inputname = input:get_name()
219 local stackname = stack:get_name()
221 if listname == "input" then
222 if stackname == inputname and registered_nodes[inputname .. "_cube"] then
223 workbench:get_output(inv, input, stackname)
225 inv:set_list("forms", {})
227 elseif listname == "forms" then
228 local fromstack = inv:get_stack(listname, index)
229 if not fromstack:is_empty() and fromstack:get_name() ~= stackname then
230 local player_inv = player:get_inventory()
231 if player_inv:room_for_item("main", fromstack) then
232 player_inv:add_item("main", fromstack)
236 input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
237 inv:set_stack("input", 1, input)
238 workbench:get_output(inv, input, inputname)
242 xdecor.register("workbench", {
243 description = "Work Bench",
244 groups = {cracky = 2, choppy = 2, oddly_breakable_by_hand = 1},
245 sounds = default.node_sound_wood_defaults(),
247 "xdecor_workbench_top.png","xdecor_workbench_top.png",
248 "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
249 "xdecor_workbench_front.png", "xdecor_workbench_front.png"
251 on_rotate = screwdriver.rotate_simple,
252 can_dig = workbench.dig,
253 on_timer = workbench.timer,
254 on_construct = workbench.construct,
255 on_receive_fields = workbench.fields,
256 on_metadata_inventory_put = workbench.on_put,
257 on_metadata_inventory_take = workbench.on_take,
258 allow_metadata_inventory_put = workbench.put,
259 allow_metadata_inventory_move = workbench.move
262 for _, d in ipairs(workbench.defs) do
264 local node = nodes[i]
265 local mod_name, item_name = node:match("^(.-):(.*)")
266 local def = registered_nodes[node]
268 if item_name and d[3] then
271 groups.not_in_creative_inventory = 1
273 for k, v in pairs(def.groups) do
274 if k ~= "wood" and k ~= "stone" and k ~= "level" then
280 if #def.tiles > 1 and (def.drawtype:sub(1,5) ~= "glass") then
283 tiles = {def.tiles[1]}
286 tiles = {def.tile_images[1]}
289 if not registered_nodes["stairs:slab_" .. item_name] then
290 stairs.register_stair_and_slab(item_name, node,
291 groups, tiles, def.description .. " Stair",
292 def.description .. " Slab", def.sounds)
295 minetest.register_node(":" .. node .. "_" .. d[1], {
296 description = def.description .. " " .. d[1]:gsub("^%l", string.upper),
298 paramtype2 = "facedir",
299 drawtype = "nodebox",
303 -- `unpack` has been changed to `table.unpack` in newest Lua versions
304 node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
305 sunlight_propagates = true,
306 on_place = minetest.rotate_node
309 elseif item_name and mod_name then
310 minetest.register_alias_force(
311 ("%s:%s_innerstair"):format(mod_name, item_name),
312 ("stairs:stair_inner_%s"):format(item_name)
314 minetest.register_alias_force(
315 ("%s:%s_outerstair"):format(mod_name, item_name),
316 ("stairs:stair_outer_%s"):format(item_name)
324 minetest.register_tool("xdecor:hammer", {
325 description = "Hammer",
326 inventory_image = "xdecor_hammer.png",
327 wield_image = "xdecor_hammer.png",
328 on_use = function() do
335 minetest.register_craft({
336 output = "xdecor:hammer",
338 {"default:steel_ingot", "group:stick", "default:steel_ingot"},
339 {"", "group:stick", ""}
343 minetest.register_craft({
344 output = "xdecor:workbench",
346 {"group:wood", "group:wood"},
347 {"group:wood", "group:wood"}