2 screwdriver = screwdriver or {}
4 -- Nodes allowed to be cut.
5 -- Registration format: [mod name] = [[ node names ]].
8 wood tree cobble desert_stone
9 junglewood jungletree mossycobble stonebrick
10 pine_wood pine_tree desert_cobble sandstonebrick
11 acacia_wood acacia_tree stone desert_stonebrick
12 aspen_wood aspen_tree sandstone obsidianbrick
14 coalblock mese obsidian
15 copperblock brick obsidian_glass
23 coalstone_tile hard_clay
24 desertstone_tile packed_ice
26 stone_tile woodframed_glass
31 -- Nodeboxes definitions.
33 -- Name Yield X Y Z W H L
34 {"nanoslab", 16, { 0, 0, 0, 8, 1, 8 }},
35 {"micropanel", 16, { 0, 0, 0, 16, 1, 8 }},
36 {"microslab", 8, { 0, 0, 0, 16, 1, 16 }},
37 {"thinstair", 8, { 0, 7, 0, 16, 1, 8 },
38 { 0, 15, 8, 16, 1, 8 }},
39 {"cube", 4, { 0, 0, 0, 8, 8, 8 }},
40 {"panel", 4, { 0, 0, 0, 16, 8, 8 }},
42 {"doublepanel", 2, { 0, 0, 0, 16, 8, 8 },
43 { 0, 8, 8, 16, 8, 8 }},
44 {"halfstair", 2, { 0, 0, 0, 8, 8, 16 },
45 { 0, 8, 8, 8, 8, 8 }},
46 {"outerstair", 1, { 0, 0, 0, 16, 8, 16 },
47 { 0, 8, 8, 8, 8, 8 }},
49 {"innerstair", 1, { 0, 0, 0, 16, 8, 16 },
50 { 0, 8, 8, 16, 8, 8 },
54 function worktable.get_recipe(item)
55 if item:find("^group:") then
56 if item:find("wool$") or item:find("dye$") then
57 item = item:sub(7)..":white"
58 elseif minetest.registered_items["default:"..item:sub(7)] then
59 item = item:gsub("group:", "default:")
61 for node, def in pairs(minetest.registered_items) do
62 if def.groups[item:match("[^,:]+$")] then
71 function worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
72 local inv_size = meta:get_int("inv_size")
73 local npp, i, s = 8*3, 0, 0
74 local pagemax = math.floor((inv_size - 1) / npp + 1)
76 if pagenum > pagemax then
78 elseif pagenum == 0 then
82 local formspec = [[ size[8,6.6;]
83 tablecolumns[color;text;color;text]
84 tableoptions[background=#00000000;highlight=#00000000;border=false]
85 button[5.5,0;0.7,1;prev;<]
86 button[7.3,0;0.7,1;next;>]
87 button[4,0.2;0.7,0.5;search;?]
88 button[4.6,0.2;0.7,0.5;clearfilter;X]
89 button[0,0;1.5,1;backcraft;< Back]
90 tooltip[search;Search]
91 tooltip[clearfilter;Reset] ]]
92 .."tabheader[0,0;tabs;All,Nodes,Tools,Items;"..tostring(tab_id)..";true;false]"..
93 "table[6.1,0.2;1.1,0.5;pagenum;#FFFF00,"..tostring(pagenum)..
94 ",#FFFFFF,/ "..tostring(pagemax).."]"..
95 "field[1.8,0.32;2.6,1;filter;;"..filter.."]"..xbg
97 for _, name in pairs(worktable.craftguide_main_list(meta, filter, tab_id)) do
98 if s < (pagenum - 1) * npp then
101 if i >= npp then break end
102 formspec = formspec.."item_image_button["..(i%8)..","..
103 (math.floor(i/8)+1)..";1,1;"..name..";"..name..";]"
108 if item and minetest.registered_items[item] then
109 --print(dump(minetest.get_all_craft_recipes(item)))
110 local items_num = #minetest.get_all_craft_recipes(item)
111 if recipe_num > items_num then recipe_num = 1 end
113 if items_num > 1 then
114 formspec = formspec.."button[0,6;1.6,1;alternate;Alternate]"..
115 "label[0,5.5;Recipe "..recipe_num.." of "..items_num.."]"
118 local type = minetest.get_all_craft_recipes(item)[recipe_num].type
119 if type == "cooking" then
120 formspec = formspec.."image[3.75,4.6;0.5,0.5;default_furnace_fire_fg.png]"
123 local items = minetest.get_all_craft_recipes(item)[recipe_num].items
124 local width = minetest.get_all_craft_recipes(item)[recipe_num].width
125 local yield = minetest.get_all_craft_recipes(item)[recipe_num].output:match("%s(%d+)") or ""
126 if width == 0 then width = math.min(3, #items) end
127 local rows = math.ceil(table.maxn(items) / width)
129 local function is_group(item)
130 if item:find("^group:") then return "G" end
134 for i, v in pairs(items) do
135 formspec = formspec.."item_image_button["..((i-1) % width + 4.5)..","..
136 (math.floor((i-1) / width + (6 - math.min(2, rows))))..";1,1;"..
137 worktable.get_recipe(v)..";"..worktable.get_recipe(v)..";"..is_group(v).."]"
140 formspec = formspec.."item_image_button[2.5,5;1,1;"..item..";"..item..";"..yield.."]"..
141 "image[3.5,5;1,1;gui_furnace_arrow_bg.png^[transformR90]"
144 meta:set_string("formspec", formspec)
147 local function tab_category(tab_id)
148 local id_category = {
149 minetest.registered_items,
150 minetest.registered_nodes,
151 minetest.registered_tools,
152 minetest.registered_craftitems
155 return id_category[tab_id] or id_category[1]
158 function worktable.craftguide_main_list(meta, filter, tab_id)
159 local items_list = {}
160 for name, def in pairs(tab_category(tab_id)) do
161 if not (def.groups.not_in_creative_inventory == 1) and
162 minetest.get_craft_recipe(name).items and
163 def.description and def.description ~= "" and
164 (not filter or def.name:find(filter, 1, true)) then
165 items_list[#items_list+1] = name
169 meta:set_int("inv_size", #items_list)
170 table.sort(items_list)
174 worktable.formspecs = {
175 crafting = function(meta)
176 meta:set_string("formspec", [[ size[8,7;]
177 image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
178 image[0.06,2.12;0.8,0.8;trash_icon.png]
179 button[0,0;1.5,1;back;< Back]
180 button[0,0.85;1.5,1;craftguide;Guide]
181 list[context;trash;0,2;1,1;]
182 list[current_player;main;0,3.3;8,4;]
183 list[current_player;craft;2,0;3,3;]
184 list[current_player;craftpreview;6,1;1,1;]
185 listring[current_player;main]
186 listring[current_player;craft] ]]
187 ..xbg..default.get_hotbar_bg(0,3.3))
189 storage = function(meta)
190 meta:set_string("formspec", [[ size[8,7]
191 image[7.06,0.12;0.8,0.8;trash_icon.png]
192 list[context;trash;7,0;1,1;]
193 list[context;storage;0,1;8,2;]
194 list[current_player;main;0,3.25;8,4;]
195 listring[context;storage]
196 listring[current_player;main]
197 button[0,0;1.5,1;back;< Back] ]]
198 ..xbg..default.get_hotbar_bg(0,3.25))
200 main = function(meta)
201 meta:set_string("formspec", [[ size[8,7;]
203 label[0.9,2.23;Repair]
204 box[-0.05,1;2.05,0.9;#555555]
205 box[-0.05,2;2.05,0.9;#555555]
206 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
207 image[0,1;1,1;worktable_saw.png]
208 image[0,2;1,1;worktable_anvil.png]
209 image[3,2;1,1;hammer_layout.png]
210 list[context;input;2,1;1,1;]
211 list[context;tool;2,2;1,1;]
212 list[context;hammer;3,2;1,1;]
213 list[context;forms;4,0;4,3;]
214 list[current_player;main;0,3.25;8,4;]
215 button[0,0;2,1;craft;Crafting]
216 button[2,0;2,1;storage;Storage] ]]
217 ..xbg..default.get_hotbar_bg(0,3.25))
221 function worktable.construct(pos)
222 local meta = minetest.get_meta(pos)
223 local inv = meta:get_inventory()
225 inv:set_size("tool", 1)
226 inv:set_size("trash", 1)
227 inv:set_size("input", 1)
228 inv:set_size("hammer", 1)
229 inv:set_size("forms", 4*3)
230 inv:set_size("storage", 8*2)
231 meta:set_string("infotext", "Work Table")
233 worktable.formspecs.main(meta)
236 function worktable.fields(pos, _, fields)
237 if fields.quit then return end
238 local meta = minetest.get_meta(pos)
239 local formspec = meta:to_table().fields.formspec
240 local filter = formspec:match("filter;;([%w_:]+)") or ""
241 local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1
242 local tab_id = tonumber(formspec:match("tabheader%[.*;(%d+)%;.*%]")) or 1
245 worktable.formspecs.main(meta)
246 elseif fields.craft or fields.backcraft then
247 worktable.formspecs.crafting(meta)
248 elseif fields.storage then
249 worktable.formspecs.storage(meta)
250 elseif fields.craftguide or fields.clearfilter then
251 worktable.craftguide_main_list(meta, nil, tab_id)
252 worktable.craftguide_formspec(meta, 1, nil, 1, "", tab_id)
253 elseif fields.alternate then
254 local item = formspec:match("item_image_button%[.*;([%w_:]+);.*%]") or ""
255 local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
256 recipe_num = recipe_num + 1
257 worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
258 elseif fields.search then
259 worktable.craftguide_main_list(meta, fields.filter:lower(), tab_id)
260 worktable.craftguide_formspec(meta, 1, nil, 1, fields.filter:lower(), tab_id)
261 elseif fields.tabs then
262 worktable.craftguide_main_list(meta, filter, tonumber(fields.tabs))
263 worktable.craftguide_formspec(meta, 1, nil, 1, filter, tonumber(fields.tabs))
264 elseif fields.prev or fields.next then
266 pagenum = pagenum - 1
268 pagenum = pagenum + 1
270 worktable.craftguide_formspec(meta, pagenum, nil, 1, filter, tab_id)
272 for item in pairs(fields) do
273 if item:match(".-:") and minetest.get_craft_recipe(item).items then
274 worktable.craftguide_formspec(meta, pagenum, item, 1, filter, tab_id)
280 function worktable.dig(pos)
281 local inv = minetest.get_meta(pos):get_inventory()
282 return inv:is_empty("input") and inv:is_empty("hammer") and
283 inv:is_empty("tool") and inv:is_empty("storage")
286 function worktable.allowed(mod, node)
287 if not mod then return end
288 for it in mod:gmatch("[%w_]+") do
289 if it == node then return true end
294 local function trash_delete(pos)
295 local inv = minetest.get_meta(pos):get_inventory()
296 minetest.after(0, function()
297 inv:set_stack("trash", 1, "")
301 function worktable.put(pos, listname, _, stack)
302 local stackname = stack:get_name()
303 local mod, node = stackname:match("(.*):(.*)")
304 local allowed_tools = "pick, axe, shovel, sword, hoe, armor"
306 for v in allowed_tools:gmatch("[%w_]+") do
307 if listname == "tool" and stack:get_wear() > 0 and stackname:find(v) then
308 return stack:get_count()
311 if (listname == "input" and worktable.allowed(worktable.nodes[mod], node)) or
312 (listname == "hammer" and stackname == "xdecor:hammer") or
313 listname == "storage" or listname == "trash" then
314 if listname == "trash" then trash_delete(pos) end
315 return stack:get_count()
321 function worktable.take(_, listname, _, stack, player)
322 if listname == "forms" then
323 local inv = player:get_inventory()
324 if inv:room_for_item("main", stack:get_name()) then
329 return stack:get_count()
332 function worktable.move(pos, _, _, to_list, _, count)
333 if to_list == "storage" then
335 elseif to_list == "trash" then
342 function worktable.get_output(inv, input, name)
343 if inv:is_empty("input") then
344 inv:set_list("forms", {})
349 for _, n in pairs(worktable.defs) do
350 local count = math.min(n[2] * input:get_count(), input:get_stack_max())
351 local item = name.."_"..n[1]
352 if not n[3] then item = "stairs:"..n[1].."_"..name:match(":(.*)") end
354 output[#output+1] = item.." "..count
357 inv:set_list("forms", output)
360 function worktable.on_put(pos, listname, _, stack)
361 if listname == "input" then
362 local inv = minetest.get_meta(pos):get_inventory()
363 local input = inv:get_stack("input", 1)
364 worktable.get_output(inv, input, stack:get_name())
368 function worktable.on_take(pos, listname, index, stack)
369 local inv = minetest.get_meta(pos):get_inventory()
370 local input = inv:get_stack("input", 1)
372 if listname == "input" then
373 if stack:get_name() == input:get_name() then
374 worktable.get_output(inv, input, stack:get_name())
376 inv:set_list("forms", {})
378 elseif listname == "forms" then
379 input:take_item(math.ceil(stack:get_count() / worktable.defs[index][2]))
380 inv:set_stack("input", 1, input)
381 worktable.get_output(inv, input, input:get_name())
385 xdecor.register("worktable", {
386 description = "Work Table",
387 groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
388 sounds = default.node_sound_wood_defaults(),
390 "xdecor_worktable_top.png", "xdecor_worktable_top.png",
391 "xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
392 "xdecor_worktable_front.png", "xdecor_worktable_front.png"
394 on_rotate = screwdriver.rotate_simple,
395 can_dig = worktable.dig,
396 on_construct = worktable.construct,
397 on_receive_fields = worktable.fields,
398 on_metadata_inventory_put = worktable.on_put,
399 on_metadata_inventory_take = worktable.on_take,
400 allow_metadata_inventory_put = worktable.put,
401 allow_metadata_inventory_take = worktable.take,
402 allow_metadata_inventory_move = worktable.move
405 for _, d in pairs(worktable.defs) do
406 for mod, n in pairs(worktable.nodes) do
407 for name in n:gmatch("[%w_]+") do
408 local ndef = minetest.registered_nodes[mod..":"..name]
409 if ndef and d[3] then
410 local groups, tiles, light = {}, {}, 0
411 groups.not_in_creative_inventory = 1
413 for k, v in pairs(ndef.groups) do
414 if k ~= "wood" and k ~= "stone" and k ~= "level" then
419 if #ndef.tiles > 1 and not ndef.drawtype:find("glass") then
422 tiles = {ndef.tiles[1]}
425 stairs.register_stair_and_slab(name, mod..":"..name, groups, tiles,
426 ndef.description.." Stair", ndef.description.." Slab", ndef.sounds)
428 if ndef.light_source > 3 then
429 light = ndef.light_source - 1
430 minetest.override_item("stairs:slab_"..name, {light_source=light})
431 minetest.override_item("stairs:stair_"..name, {light_source=light})
434 minetest.register_node(":"..mod..":"..name.."_"..d[1], {
435 description = ndef.description.." "..d[1]:gsub("^%l", string.upper),
437 paramtype2 = "facedir",
438 drawtype = "nodebox",
439 light_source = light,
440 sounds = ndef.sounds,
443 node_box = xdecor.pixelnodebox(16, {d[3], d[4], d[5]}),
444 sunlight_propagates = true,
445 on_place = minetest.rotate_node,
446 on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
447 local pointed_nodebox = minetest.get_node(pos).name:match("(%w+)$")
448 local wield_item = clicker:get_wielded_item():get_name()
449 local player_name = clicker:get_player_name()
452 if minetest.is_protected(pos, player_name) then
453 minetest.record_protection_violation(pos, player_name)
458 {"nanoslab", nil, 2},
459 {"micropanel", nil, 3},
461 {"cube", "panel", 9},
462 {"cube", "outerstair", 11},
463 {"cube", "halfstair", 7},
464 {"cube", "innerstair", nil},
466 {"panel", "outerstair", 12},
467 {"halfstair", nil, 11},
468 {"halfstair", "outerstair", nil}
471 for _, x in pairs(T) do
472 if wield_item == mod..":"..name.."_"..x[1] then
473 if not x[2] then x[2] = x[1] end
474 if x[2] == pointed_nodebox then
476 newnode = mod..":"..name
478 newnode = mod..":"..name.."_"..worktable.defs[x[3]][1]
484 if clicker:get_player_control().sneak then
485 if not minetest.registered_nodes[newnode] then return end
486 minetest.set_node(pos, {name=newnode, param2=node.param2})
488 minetest.item_place_node(ItemStack(wield_item), clicker, pointed_thing)
491 if not minetest.setting_getbool("creative_mode") then
492 itemstack:take_item()
500 minetest.register_alias(mod..":"..name.."_"..d[1], "stairs:"..d[1].."_"..name)
506 minetest.register_abm({
507 nodenames = {"xdecor:worktable"},
508 interval = 3, chance = 1,
509 action = function(pos)
510 local inv = minetest.get_meta(pos):get_inventory()
511 local tool = inv:get_stack("tool", 1)
512 local hammer = inv:get_stack("hammer", 1)
514 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
518 -- Wear : 0-65535 | 0 = new condition.
522 inv:set_stack("tool", 1, tool)
523 inv:set_stack("hammer", 1, hammer)