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)
30 nodes = nodes..WB.custom_nodes_register
32 -- Nodeboxes definitions
34 -- Name Yield X Y Z W H L
35 {"nanoslab", 16, { 0, 0, 0, 8, 1, 8 }},
36 {"micropanel", 16, { 0, 0, 0, 16, 1, 8 }},
37 {"microslab", 8, { 0, 0, 0, 16, 1, 16 }},
38 {"thinstair", 8, { 0, 7, 0, 16, 1, 8 },
39 { 0, 15, 8, 16, 1, 8 }},
40 {"cube", 4, { 0, 0, 0, 8, 8, 8 }},
41 {"panel", 4, { 0, 0, 0, 16, 8, 8 }},
43 {"doublepanel", 2, { 0, 0, 0, 16, 8, 8 },
44 { 0, 8, 8, 16, 8, 8 }},
45 {"halfstair", 2, { 0, 0, 0, 8, 8, 16 },
46 { 0, 8, 8, 8, 8, 8 }},
47 {"stair_outer", 1, nil },
49 {"stair_inner", 1, nil }
52 -- Tools allowed to be repaired
53 function workbench:repairable(stack)
54 local tools = {"pick", "axe", "shovel", "sword", "hoe", "armor", "shield"}
55 for _, t in pairs(tools) do
56 if stack:find(t) then return true end
61 function workbench:get_output(inv, input, name)
63 for i=1, #self.defs do
64 local nbox = self.defs[i]
65 local count = min(nbox[2] * input:get_count(), input:get_stack_max())
66 local item = name.."_"..nbox[1]
67 item = nbox[3] and item or "stairs:"..nbox[1].."_"..name:match(":(.*)")
68 output[#output+1] = item.." "..count
71 inv:set_list("forms", output)
76 [[ label[0.9,1.23;Cut]
77 label[0.9,2.23;Repair]
78 box[-0.05,1;2.05,0.9;#555555]
79 box[-0.05,2;2.05,0.9;#555555]
80 button[0,0;2,1;craft;Crafting]
81 button[2,0;2,1;storage;Storage]
82 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
83 image[0,1;1,1;worktable_saw.png]
84 image[0,2;1,1;worktable_anvil.png]
85 image[3,2;1,1;hammer_layout.png]
86 list[context;input;2,1;1,1;]
87 list[context;tool;2,2;1,1;]
88 list[context;hammer;3,2;1,1;]
89 list[context;forms;4,0;4,3;]
90 listring[current_player;main]
91 listring[context;tool]
92 listring[current_player;main]
93 listring[context;hammer]
94 listring[current_player;main]
95 listring[context;forms]
96 listring[current_player;main]
97 listring[context;input] ]],
99 [[ image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
100 button[0,0;1.5,1;back;< Back]
101 list[current_player;craft;2,0;3,3;]
102 list[current_player;craftpreview;6,1;1,1;]
103 listring[current_player;main]
104 listring[current_player;craft] ]],
106 [[ list[context;storage;0,1;8,2;]
107 button[0,0;1.5,1;back;< Back]
108 listring[context;storage]
109 listring[current_player;main] ]]
112 function workbench:set_formspec(meta, id)
113 meta:set_string("formspec",
114 "size[8,7;]list[current_player;main;0,3.25;8,4;]"..
115 formspecs[id]..xbg..default.get_hotbar_bg(0,3.25))
118 function workbench.construct(pos)
119 local meta = minetest.get_meta(pos)
120 local inv = meta:get_inventory()
122 inv:set_size("tool", 1)
123 inv:set_size("input", 1)
124 inv:set_size("hammer", 1)
125 inv:set_size("forms", 4*3)
126 inv:set_size("storage", 8*2)
128 meta:set_string("infotext", "Work Bench")
129 workbench:set_formspec(meta, 1)
132 function workbench.fields(pos, _, fields)
133 if fields.quit then return end
134 local meta = minetest.get_meta(pos)
135 local id = fields.back and 1 or
136 fields.craft and 2 or
138 if not id then return end
139 workbench:set_formspec(meta, id)
142 function workbench.dig(pos)
143 local inv = minetest.get_meta(pos):get_inventory()
144 return inv:is_empty("input") and inv:is_empty("hammer") and
145 inv:is_empty("tool") and inv:is_empty("storage")
148 function workbench.timer(pos)
149 local timer = minetest.get_node_timer(pos)
150 local inv = minetest.get_meta(pos):get_inventory()
151 local tool = inv:get_stack("tool", 1)
152 local hammer = inv:get_stack("hammer", 1)
154 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
159 -- Tool's wearing range: 0-65535 | 0 = new condition
163 inv:set_stack("tool", 1, tool)
164 inv:set_stack("hammer", 1, hammer)
168 function workbench.put(_, listname, _, stack)
169 local stackname = stack:get_name()
170 if (listname == "tool" and stack:get_wear() > 0 and
171 workbench:repairable(stackname)) or
172 (listname == "input" and registered_nodes[stackname.."_cube"]) or
173 (listname == "hammer" and stackname == "xdecor:hammer") or
174 listname == "storage" then
175 return stack:get_count()
180 function workbench.move(_, from_list, _, to_list, _, count)
181 return (to_list == "storage" and from_list ~= "forms") and count or 0
184 function workbench.on_put(pos, listname, _, stack)
185 local inv = minetest.get_meta(pos):get_inventory()
186 if listname == "input" then
187 local input = inv:get_stack("input", 1)
188 workbench:get_output(inv, input, stack:get_name())
189 elseif listname == "tool" or listname == "hammer" then
190 local timer = minetest.get_node_timer(pos)
195 function workbench.on_take(pos, listname, index, stack, player)
196 local inv = minetest.get_meta(pos):get_inventory()
197 local input = inv:get_stack("input", 1)
198 local inputname = input:get_name()
199 local stackname = stack:get_name()
201 if listname == "input" then
202 if stackname == inputname and registered_nodes[inputname.."_cube"] then
203 workbench:get_output(inv, input, stackname)
205 inv:set_list("forms", {})
207 elseif listname == "forms" then
208 local fromstack = inv:get_stack(listname, index)
209 if not fromstack:is_empty() and fromstack:get_name() ~= stackname then
210 local player_inv = player:get_inventory()
211 if player_inv:room_for_item("main", fromstack) then
212 player_inv:add_item("main", fromstack)
216 input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
217 inv:set_stack("input", 1, input)
218 workbench:get_output(inv, input, inputname)
222 xdecor.register("workbench", {
223 description = "Work Bench",
224 groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
225 sounds = default.node_sound_wood_defaults(),
226 tiles = {"xdecor_workbench_top.png", "xdecor_workbench_top.png",
227 "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
228 "xdecor_workbench_front.png", "xdecor_workbench_front.png"},
229 on_rotate = screwdriver.rotate_simple,
230 can_dig = workbench.dig,
231 on_timer = workbench.timer,
232 on_construct = workbench.construct,
233 on_receive_fields = workbench.fields,
234 on_metadata_inventory_put = workbench.on_put,
235 on_metadata_inventory_take = workbench.on_take,
236 allow_metadata_inventory_put = workbench.put,
237 allow_metadata_inventory_move = workbench.move
240 for _, d in pairs(workbench.defs) do
242 local node = nodes[i]
243 local mod_name, item_name = node:match("^(.-):(.*)")
244 local def = registered_nodes[node]
246 if item_name and d[3] then
249 groups.not_in_creative_inventory = 1
251 for k, v in pairs(def.groups) do
252 if k ~= "wood" and k ~= "stone" and k ~= "level" then
258 if #def.tiles > 1 and (def.drawtype:sub(1,5) ~= "glass") then
261 tiles = {def.tiles[1]}
264 tiles = {def.tile_images[1]}
267 if not registered_nodes["stairs:slab_"..item_name] then
268 stairs.register_stair_and_slab(item_name, node,
269 groups, tiles, def.description.." Stair",
270 def.description.." Slab", def.sounds)
273 minetest.register_node(":"..node.."_"..d[1], {
274 description = def.description.." "..d[1]:gsub("^%l", string.upper),
276 paramtype2 = "facedir",
277 drawtype = "nodebox",
281 -- `unpack` has been changed to `table.unpack` in newest Lua versions
282 node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
283 sunlight_propagates = true,
284 on_place = minetest.rotate_node
286 elseif item_name and mod_name then
287 minetest.register_alias_force(
288 ('%s:%s_innerstair'):format(mod_name, item_name),
289 ('stairs:stair_inner_%s'):format(item_name)
291 minetest.register_alias_force(
292 ('%s:%s_outerstair'):format(mod_name, item_name),
293 ('stairs:stair_outer_%s'):format(item_name)
301 minetest.register_tool("xdecor:hammer", {
302 description = "Hammer",
303 inventory_image = "xdecor_hammer.png",
304 wield_image = "xdecor_hammer.png",
305 on_use = function() do return end end
310 minetest.register_craft({
311 output = "xdecor:hammer",
313 {"default:steel_ingot", "group:stick", "default:steel_ingot"},
314 {"", "group:stick", ""}
318 minetest.register_craft({
319 output = "xdecor:workbench",
321 {"group:wood", "group:wood"},
322 {"group:wood", "group:wood"}