2 screwdriver = screwdriver or {}
3 local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots
5 local nodes = { -- Nodes allowed to be cut. Mod name = {node name}.
6 ["default"] = {"wood", "junglewood", "pine_wood", "acacia_wood",
7 "tree", "jungletree", "pine_tree", "acacia_tree",
8 "cobble", "mossycobble", "desert_cobble",
9 "stone", "sandstone", "desert_stone", "obsidian",
10 "stonebrick", "sandstonebrick", "desert_stonebrick", "obsidianbrick",
11 "coalblock", "copperblock", "steelblock", "goldblock",
12 "bronzeblock", "mese", "diamondblock",
13 "brick", "cactus", "ice", "meselamp", "glass", "obsidian_glass"},
15 ["xdecor"] = {"coalstone_tile", "desertstone_tile", "stone_rune", "stone_tile",
16 "cactusbrick", "hard_clay", "packed_ice", "moonbrick",
17 "woodframed_glass", "wood_tile"},
19 ["oresplus"] = {"emerald_block", "glowstone"},
22 local def = { -- Nodebox name, yield, definition.
23 {"nanoslab", 16, {-.5,-.5,-.5,0,-.4375,0}},
24 {"micropanel", 16, {-.5,-.5,-.5,.5,-.4375,0}},
25 {"microslab", 8, {-.5,-.5,-.5,.5,-.4375,.5}},
26 {"thinstair", 8, {{-.5,-.0625,-.5,.5,0,0},{-.5,.4375,0,.5,.5,.5}}},
27 {"cube", 4, {-.5,-.5,0,0,0,.5}},
28 {"panel", 4, {-.5,-.5,-.5,.5,0,0}},
29 {"slab", 2, {-.5,-.5,-.5,.5,0,.5}},
30 {"doublepanel", 2, {{-.5,-.5,-.5,.5,0,0},{-.5,0,0,.5,.5,.5}}},
31 {"halfstair", 2, {{-.5,-.5,-.5,0,0,.5},{-.5,0,0,0,.5,.5}}},
32 {"outerstair", 1, {{-.5,-.5,-.5,.5,0,.5},{-.5,0,0,0,.5,.5}}},
33 {"stair", 1, {{-.5,-.5,-.5,.5,0,.5},{-.5,0,0,.5,.5,.5}}},
34 {"innerstair", 1, {{-.5,-.5,-.5,.5,0,.5},{-.5,0,0,.5,.5,.5},{-.5,0,-.5,0,.5,0}}}
37 function worktable.craft_output_recipe(pos, start_i, pagenum, stackname, recipe_num, filter)
38 local meta = minetest.get_meta(pos)
39 local inv = meta:get_inventory()
40 local inventory_size = #meta:to_table().inventory.inv_items_list
41 local pagemax = math.floor((inventory_size - 1) / (8*4) + 1)
43 local formspec = [[ size[8,8;]
44 list[context;item_craft_input;3,6.3;1,1;]
45 tablecolumns[color;text;color;text]
46 tableoptions[background=#00000000;highlight=#00000000;border=false]
47 button[5.5,0;0.7,1;prev;<]
48 button[7.3,0;0.7,1;next;>]
49 button[4,0.2;0.7,0.5;search;?]
50 button[4.6,0.2;0.7,0.5;clearfilter;X]
51 button[0,0;1.5,1;backcraft;< Back]
52 tooltip[search;Search]
53 tooltip[clearfilter;Reset]
54 label[3,5.8;Input] ]]..
55 "list[context;inv_items_list;0,1;8,4;"..tostring(start_i).."]"..
56 "table[6.1,0.2;1.1,0.5;pagenum;#FFFF00,"..tostring(math.floor(pagenum))..
57 ",#FFFFFF,/ "..tostring(pagemax).."]"..
58 "field[1.8,0.32;2.6,1;filter;;"..filter.."]"..xbg
61 local items_num = #minetest.get_all_craft_recipes(stackname)
62 if recipe_num > items_num then
66 --print(dump(minetest.get_all_craft_recipes(stackname)))
67 local stack_width = minetest.get_all_craft_recipes(stackname)[recipe_num].width
68 local stack_items = minetest.get_all_craft_recipes(stackname)[recipe_num].items
69 local stack_type = minetest.get_all_craft_recipes(stackname)[recipe_num].type
70 local stack_output = minetest.get_all_craft_recipes(stackname)[recipe_num].output
71 local stack_count = stack_output:match("%s(%d+)")
74 formspec = formspec.."button[0,5.7;1.6,1;alternate;Alternate]"..
75 "label[0,5.2;Recipe "..recipe_num.." of "..items_num.."]"
79 inv:set_stack("item_craft_input", 1, stackname.." "..stack_count)
81 inv:set_stack("item_craft_input", 1, stackname)
84 if stack_width == 0 then
85 if #stack_items <= 2 then
86 formspec = formspec.."list[context;craft_output_recipe;5,6.3;2,1;]"
87 inv:set_size("craft_output_recipe", 2)
88 elseif #stack_items > 2 and #stack_items <= 4 then
89 formspec = formspec.."list[context;craft_output_recipe;5,5.3;2,2;]"
90 inv:set_size("craft_output_recipe", 2*2)
92 formspec = formspec.."list[context;craft_output_recipe;5,5.3;3,3;]"
93 inv:set_size("craft_output_recipe", 3*3)
95 elseif stack_width == 1 then
96 if #stack_items == 1 then
97 formspec = formspec.."list[context;craft_output_recipe;5,6.3;1,1;]"
99 formspec = formspec.."list[context;craft_output_recipe;5,5.3;1,"..#stack_items..";]"
101 inv:set_size("craft_output_recipe", #stack_items)
102 elseif stack_width == 2 then
103 formspec = formspec.."list[context;craft_output_recipe;5,5.3;2,3;]"
104 inv:set_size("craft_output_recipe", 2*3)
105 elseif stack_width == 3 then
106 if stack_type == "cooking" then
107 formspec = formspec..[[ list[context;craft_output_recipe;5,6.3;1,1;]
108 image[4.25,5.9;0.5,0.5;default_furnace_fire_fg.png] ]]
109 inv:set_size("craft_output_recipe", 1)
111 formspec = formspec.."list[context;craft_output_recipe;5,5.3;3,3;]"
112 inv:set_size("craft_output_recipe", 3*3)
114 elseif stack_type == "cooking" and stack_width > 3 then
115 formspec = formspec..[[ list[context;craft_output_recipe;5,6.3;1,1;]
116 image[4.25,5.9;0.5,0.5;default_furnace_fire_fg.png] ]]
118 inv:set_size("craft_output_recipe", 1)
122 for k, def in pairs(stack_items) do
123 craft[#craft+1] = def
124 if def and def:find("^group:") then
125 if def:find("wool$") then
127 elseif def:find("dye$") then
130 if minetest.registered_items["default:"..def:match("^group:([%w_]+)$")] then
131 def = def:gsub("group", "default")
133 for node, definition in pairs(minetest.registered_items) do
134 for group in pairs(definition.groups) do
135 if def:match("^group:"..group.."$") or
136 ((def:find("dye") or def:find("flower")) and
137 group == def:match("^group:.*,("..group..")")) then
146 inv:set_stack("craft_output_recipe", k, def)
149 formspec = formspec..[[ image[4,6.3;1,1;gui_furnace_arrow_bg.png^[transformR90]
150 button[0,6.5;1.6,1;trash;Clear] ]]..
151 "label[0,7.5;"..stackname:sub(1,30).."]"
154 meta:set_string("formspec", formspec)
157 function worktable.craftguide_update(pos, filter)
158 local meta = minetest.get_meta(pos)
159 local inv = meta:get_inventory()
160 local inv_items_list = {}
162 for name, def in pairs(minetest.registered_items) do
163 if not (def.groups.not_in_creative_inventory == 1) and
164 minetest.get_craft_recipe(name).items and
165 def.description and def.description ~= "" and
166 (not filter or def.name:find(filter, 1, true)) then
167 inv_items_list[#inv_items_list+1] = name
170 table.sort(inv_items_list)
172 inv:set_size("inv_items_list", #inv_items_list)
173 inv:set_list("inv_items_list", inv_items_list)
176 function worktable.crafting(pos)
177 local meta = minetest.get_meta(pos)
178 local formspec = [[ size[8,7;]
179 list[current_player;main;0,3.3;8,4;]
180 image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
181 button[0,0;1.5,1;back;< Back]
182 button[0,1;1.5,1;craftguide;Guide]
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 meta:set_string("formspec", formspec)
192 function worktable.storage(pos)
193 local meta = minetest.get_meta(pos)
194 local formspec = [[ size[8,7]
195 list[context;storage;0,1;8,2;]
196 list[current_player;main;0,3.25;8,4;]
197 listring[context;storage]
198 listring[current_player;main]
199 button[0,0;1.5,1;back;< Back] ]]
200 ..xbg..default.get_hotbar_bg(0,3.25)
202 meta:set_string("formspec", formspec)
205 function worktable.main(pos)
206 local meta = minetest.get_meta(pos)
207 local formspec = [[ size[8,7;]
209 label[0.9,2.23;Repair]
210 box[-0.05,1;2.05,0.9;#555555]
211 box[-0.05,2;2.05,0.9;#555555]
212 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
213 image[0,1;1,1;worktable_saw.png]
214 image[0,2;1,1;worktable_anvil.png]
215 image[3,2;1,1;hammer_layout.png]
216 list[context;input;2,1;1,1;]
217 list[context;tool;2,2;1,1;]
218 list[context;hammer;3,2;1,1;]
219 list[context;forms;4,0;4,3;]
220 list[current_player;main;0,3.25;8,4;]
221 button[0,0;2,1;craft;Crafting]
222 button[2,0;2,1;storage;Storage] ]]
223 ..xbg..default.get_hotbar_bg(0,3.25)
225 meta:set_string("formspec", formspec)
228 function worktable.construct(pos)
229 local meta = minetest.get_meta(pos)
230 local inv = meta:get_inventory()
232 inv:set_size("tool", 1)
233 inv:set_size("input", 1)
234 inv:set_size("hammer", 1)
235 inv:set_size("forms", 4*3)
236 inv:set_size("storage", 8*2)
237 inv:set_size("item_craft_input", 1)
238 meta:set_string("infotext", "Work Table")
241 worktable.craftguide_update(pos, nil)
244 function worktable.fields(pos, _, fields, sender)
245 local meta = minetest.get_meta(pos)
246 local inv = meta:get_inventory()
247 local formspec = meta:to_table().fields.formspec
248 local filter = formspec:match("filter;;([%w_:]+)") or ""
249 local start_i = tonumber(formspec:match("inv_items_list;.*;(%d+)%]")) or 0
251 if fields.storage then
252 worktable.storage(pos)
253 elseif fields.back then
255 elseif fields.backcraft or fields.craft then
256 if fields.backcraft then
257 inv:set_list("item_craft_input", {})
258 inv:set_list("craft_output_recipe", {})
260 worktable.crafting(pos)
261 elseif fields.craftguide then
262 if not meta:to_table().inventory.inv_items_list then return end -- legacy code
263 worktable.craft_output_recipe(pos, 0, 1, nil, 1, "")
264 elseif fields.alternate then
265 inv:set_list("craft_output_recipe", {})
266 local inputstack = inv:get_stack("item_craft_input", 1):get_name()
267 local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
269 recipe_num = recipe_num + 1
270 worktable.craft_output_recipe(pos, start_i, start_i / (8*4) + 1, inputstack, recipe_num, filter)
271 elseif fields.trash or fields.search or fields.clearfilter or
272 fields.prev or fields.next then
274 worktable.craft_output_recipe(pos, start_i, start_i / (8*4) + 1, nil, 1, filter)
275 elseif fields.search then
276 worktable.craftguide_update(pos, fields.filter:lower())
277 worktable.craft_output_recipe(pos, 0, 1, nil, 1, fields.filter:lower())
278 elseif fields.clearfilter then
279 worktable.craftguide_update(pos, nil)
280 worktable.craft_output_recipe(pos, 0, 1, nil, 1, "")
281 elseif fields.prev or fields.next then
282 local inventory_size = #meta:to_table().inventory.inv_items_list
284 if fields.prev or start_i >= inventory_size then
285 start_i = start_i - 8*4
286 elseif fields.next or start_i < 0 then
287 start_i = start_i + 8*4
290 if start_i >= inventory_size then
292 elseif start_i < 0 then
293 start_i = inventory_size - (inventory_size % (8*4))
296 worktable.craft_output_recipe(pos, start_i, start_i / (8*4) + 1, nil, 1, filter)
299 inv:set_list("item_craft_input", {})
300 inv:set_list("craft_output_recipe", {})
304 function worktable.dig(pos, _)
305 local inv = minetest.get_meta(pos):get_inventory()
306 return inv:is_empty("input") and inv:is_empty("hammer") and
307 inv:is_empty("tool") and inv:is_empty("storage")
310 function worktable.contains(table, element)
312 for _, value in pairs(table) do
313 if value == element then
321 function worktable.put(_, listname, _, stack, _)
322 local stn = stack:get_name()
323 local mod, node = stn:match("([%w_]+):([%w_]+)")
324 local tdef = minetest.registered_tools[stn]
325 local twear = stack:get_wear()
327 if (listname == "input" and worktable.contains(nodes[mod], node)) or
328 listname == "storage" then
329 return stack:get_count()
330 elseif (listname == "hammer" and stn == "xdecor:hammer") or
331 (listname == "tool" and tdef and twear > 0) then
338 function worktable.take(pos, listname, _, stack, player)
339 if listname == "forms" then
340 local user_inv = player:get_inventory()
341 if user_inv:room_for_item("main", stack:get_name()) then
345 elseif listname == "inv_items_list" or listname == "item_craft_input" or
346 listname == "craft_output_recipe" then
350 return stack:get_count()
353 function worktable.move(pos, from_list, from_index, to_list, to_index, count, _)
354 local meta = minetest.get_meta(pos)
355 local inv = meta:get_inventory()
357 if from_list == "storage" and to_list == "storage" then
359 elseif inv:is_empty("item_craft_input") and from_list == "inv_items_list" and
360 to_list == "item_craft_input" then
361 local stackname = inv:get_stack(from_list, from_index):get_name()
362 local formspec = meta:to_table().fields.formspec
363 local filter = formspec:match("filter;;([%w_:]+)") or ""
364 local start_i = tonumber(formspec:match("inv_items_list;.*;(%d+)%]")) or 0
366 worktable.craft_output_recipe(pos, start_i, start_i / (8*4) + 1, stackname, 1, filter)
368 minetest.after(0, function()
369 inv:set_stack(from_list, from_index, stackname)
376 local function update_inventory(inv, inputstack)
377 if inv:is_empty("input") then
378 inv:set_list("forms", {})
383 local input = inv:get_stack("input", 1)
385 for _, n in pairs(def) do
386 local count = math.min(n[2] * input:get_count(), input:get_stack_max())
387 output[#output+1] = inputstack:get_name().."_"..n[1].." "..count
390 inv:set_list("forms", output)
393 function worktable.on_put(pos, listname, _, stack, _)
394 if listname == "input" then
395 local inv = minetest.get_meta(pos):get_inventory()
396 update_inventory(inv, stack)
400 function worktable.on_take(pos, listname, index, stack, _)
401 local inv = minetest.get_meta(pos):get_inventory()
402 if listname == "input" then
403 if stack:get_name() == inv:get_stack("input", 1):get_name() then
404 update_inventory(inv, stack)
406 inv:set_list("forms", {})
408 elseif listname == "forms" then
409 local inputstack = inv:get_stack("input", 1)
410 inputstack:take_item(math.ceil(stack:get_count() / def[index][2]))
411 inv:set_stack("input", 1, inputstack)
412 update_inventory(inv, inputstack)
416 xdecor.register("worktable", {
417 description = "Work Table",
418 groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
419 sounds = default.node_sound_wood_defaults(),
421 "xdecor_worktable_top.png", "xdecor_worktable_top.png",
422 "xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
423 "xdecor_worktable_front.png", "xdecor_worktable_front.png"
425 on_rotate = screwdriver.rotate_simple,
426 can_dig = worktable.dig,
427 on_construct = worktable.construct,
428 on_receive_fields = worktable.fields,
429 on_metadata_inventory_put = worktable.on_put,
430 on_metadata_inventory_take = worktable.on_take,
431 allow_metadata_inventory_put = worktable.put,
432 allow_metadata_inventory_take = worktable.take,
433 allow_metadata_inventory_move = worktable.move
436 for _, d in pairs(def) do
437 for mod, n in pairs(nodes) do
438 for _, name in pairs(n) do
439 local ndef = minetest.registered_nodes[mod..":"..name]
441 local groups, tiles, light = {}, {}
442 groups.not_in_creative_inventory = 1
444 for k, v in pairs(ndef.groups) do
445 if k ~= "wood" and k ~= "stone" and k ~= "level" then
450 if #ndef.tiles > 1 and not ndef.drawtype:find("glass") then
453 tiles = {ndef.tiles[1]}
456 if ndef.light_source > 3 then
457 light = ndef.light_source - 1
460 minetest.register_node(":"..mod..":"..name.."_"..d[1], {
461 description = ndef.description.." "..d[1]:gsub("^%l", string.upper),
463 paramtype2 = "facedir",
464 drawtype = "nodebox",
465 light_source = light,
466 sounds = ndef.sounds,
469 node_box = {type = "fixed", fixed = d[3]},
470 sunlight_propagates = true,
471 on_place = minetest.rotate_node
474 minetest.register_alias("xdecor:"..d[1].."_"..name, mod..":"..name.."_"..d[1]) -- legacy code
479 minetest.register_abm({
480 nodenames = {"xdecor:worktable"},
481 interval = 3, chance = 1,
482 action = function(pos, _, _, _)
483 local inv = minetest.get_meta(pos):get_inventory()
484 local tool = inv:get_stack("tool", 1)
485 local hammer = inv:get_stack("hammer", 1)
486 local wear = tool:get_wear()
488 if tool:is_empty() or hammer:is_empty() or wear == 0 then
492 -- Wear : 0-65535 | 0 = new condition.
496 inv:set_stack("tool", 1, tool)
497 inv:set_stack("hammer", 1, hammer)