2 screwdriver = screwdriver or {}
4 local nodes = { -- Nodes allowed to be cut. Mod name = {node name}.
5 ["default"] = {"wood", "junglewood", "pine_wood", "acacia_wood",
6 "tree", "jungletree", "pine_tree", "acacia_tree",
7 "cobble", "mossycobble", "desert_cobble",
8 "stone", "sandstone", "desert_stone", "obsidian",
9 "stonebrick", "sandstonebrick", "desert_stonebrick", "obsidianbrick",
10 "coalblock", "copperblock", "steelblock", "goldblock",
11 "bronzeblock", "mese", "diamondblock",
12 "brick", "cactus", "ice", "meselamp", "glass", "obsidian_glass"},
14 ["xdecor"] = {"coalstone_tile", "desertstone_tile", "stone_rune", "stone_tile",
15 "cactusbrick", "hard_clay", "packed_ice", "moonbrick",
16 "woodframed_glass", "wood_tile"},
19 local def = { -- Nodebox name, yield, definition.
20 {"nanoslab", 16, {-.5,-.5,-.5,0,-.4375,0}},
21 {"micropanel", 16, {-.5,-.5,-.5,.5,-.4375,0}},
22 {"microslab", 8, {-.5,-.5,-.5,.5,-.4375,.5}},
23 {"thinstair", 8, {{-.5,-.0625,-.5,.5,0,0},{-.5,.4375,0,.5,.5,.5}}},
24 {"cube", 4, {-.5,-.5,0,0,0,.5}},
25 {"panel", 4, {-.5,-.5,-.5,.5,0,0}},
26 {"slab", 2, {-.5,-.5,-.5,.5,0,.5}},
27 {"doublepanel", 2, {{-.5,-.5,-.5,.5,0,0},{-.5,0,0,.5,.5,.5}}},
28 {"halfstair", 2, {{-.5,-.5,-.5,0,0,.5},{-.5,0,0,0,.5,.5}}},
29 {"outerstair", 1, {{-.5,-.5,-.5,.5,0,.5},{-.5,0,0,0,.5,.5}}},
30 {"stair", 1, {{-.5,-.5,-.5,.5,0,.5},{-.5,0,0,.5,.5,.5}}},
31 {"innerstair", 1, {{-.5,-.5,-.5,.5,0,.5},{-.5,0,0,.5,.5,.5},{-.5,0,-.5,0,.5,0}}}
34 function worktable.craftguide_output_lists(meta, num, items, stackname)
35 local inv = meta:get_inventory()
36 local output = minetest.get_all_craft_recipes(stackname)[num].output
37 local yield = output:match("%s(%d+)") or 1
38 inv:set_stack("item_craft_input", 1, stackname.." "..yield)
40 for k, def in pairs(items) do
41 if def and def:find("^group:") then
42 if def:find("wool$") or def:find("dye$") then
43 def = def:match(":([%w_]+)")..":white"
44 elseif minetest.registered_items["default:"..def:match("^group:([%w_,]+)$")] then
45 def = def:gsub("group:", "default:")
47 for node, definition in pairs(minetest.registered_items) do
48 for group in pairs(definition.groups) do
49 if def:match("^group:"..group.."$") or
50 ((def:find("dye") or def:find("flower")) and
51 group == def:match("^group:.*,("..group..")")) then
59 inv:set_stack("craft_output_recipe", k, def)
63 function worktable.craftguide_formspec(pos, start_i, pagenum, stackname, recipe_num, filter)
64 local meta = minetest.get_meta(pos)
65 local inv_size = #meta:to_table().inventory.inv_items_list
66 local pagemax = math.floor((inv_size - 1) / (8*4) + 1)
68 local formspec = [[ size[8,8;]
69 list[context;item_craft_input;3,6.3;1,1;]
70 tablecolumns[color;text;color;text]
71 tableoptions[background=#00000000;highlight=#00000000;border=false]
72 button[5.5,0;0.7,1;prev;<]
73 button[7.3,0;0.7,1;next;>]
74 button[4,0.2;0.7,0.5;search;?]
75 button[4.6,0.2;0.7,0.5;clearfilter;X]
76 button[0,0;1.5,1;backcraft;< Back]
77 tooltip[search;Search]
78 tooltip[clearfilter;Reset]
80 .."list[context;inv_items_list;0,1;8,4;"..tostring(start_i).."]"..
81 "table[6.1,0.2;1.1,0.5;pagenum;#FFFF00,"..tostring(math.floor(pagenum))..
82 ",#FFFFFF,/ "..tostring(pagemax).."]"..
83 "field[1.8,0.32;2.6,1;filter;;"..filter.."]"..xbg
85 if stackname and minetest.registered_items[stackname] then
86 local items_num = #minetest.get_all_craft_recipes(stackname)
87 if recipe_num > items_num then
91 --print(dump(minetest.get_all_craft_recipes(stackname)))
92 local items = minetest.get_all_craft_recipes(stackname)[recipe_num].items
93 local width = minetest.get_all_craft_recipes(stackname)[recipe_num].width
94 local type = minetest.get_all_craft_recipes(stackname)[recipe_num].type
97 formspec = formspec.."button[0,5.7;1.6,1;alternate;Alternate]"..
98 "label[0,5.2;Recipe "..recipe_num.." of "..items_num.."]"
101 if type == "cooking" or table.maxn(items) == 1 then
102 if type == "cooking" then
103 formspec = formspec.."image[4.25,5.9;0.5,0.5;default_furnace_fire_fg.png]"
105 formspec = formspec.."list[context;craft_output_recipe;5,6.3;1,1;]"
108 local rows, r = math.ceil(#items / math.min(3, #items))
109 if rows == 3 then r = 2 else r = rows end
111 formspec = formspec.."list[context;craft_output_recipe;5,"..
112 (7.3-r)..";"..math.min(3, #items)..","..rows..";]"
114 local rows, r = math.ceil(table.maxn(items) / width)
115 if rows == 3 then r = 2 else r = rows end
117 formspec = formspec.."list[context;craft_output_recipe;5,"..
118 (7.3-r)..";"..width..","..rows..";]"
122 formspec = formspec..[[ image[4,6.3;1,1;gui_furnace_arrow_bg.png^[transformR90]
123 button[0,6.5;1.6,1;trash;Clear] ]]
124 .."label[0,7.5;"..stackname:sub(1, 30).."]"
126 worktable.craftguide_output_lists(meta, recipe_num, items, stackname)
129 meta:set_string("formspec", formspec)
132 function worktable.craftguide_main_list(pos, filter)
133 local meta = minetest.get_meta(pos)
134 local inv = meta:get_inventory()
135 local inv_items_list = {}
137 for name, def in pairs(minetest.registered_items) do
138 if not (def.groups.not_in_creative_inventory == 1) and
139 minetest.get_craft_recipe(name).items and
140 def.description and def.description ~= "" and
141 (not filter or def.name:find(filter, 1, true)) then
142 inv_items_list[#inv_items_list+1] = name
145 table.sort(inv_items_list)
147 inv:set_size("inv_items_list", #inv_items_list)
148 inv:set_list("inv_items_list", inv_items_list)
151 function worktable.crafting(meta)
152 local formspec = [[ size[8,7;]
153 list[current_player;main;0,3.3;8,4;]
154 image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
155 button[0,0;1.5,1;back;< Back]
156 button[0,1;1.5,1;craftguide;Guide]
157 list[current_player;craft;2,0;3,3;]
158 list[current_player;craftpreview;6,1;1,1;]
159 listring[current_player;main]
160 listring[current_player;craft] ]]
161 ..xbg..default.get_hotbar_bg(0,3.3)
163 meta:set_string("formspec", formspec)
166 function worktable.storage(meta)
167 local formspec = [[ size[8,7]
168 list[context;storage;0,1;8,2;]
169 list[current_player;main;0,3.25;8,4;]
170 listring[context;storage]
171 listring[current_player;main]
172 button[0,0;1.5,1;back;< Back] ]]
173 ..xbg..default.get_hotbar_bg(0,3.25)
175 meta:set_string("formspec", formspec)
178 function worktable.main(meta)
179 local formspec = [[ size[8,7;]
181 label[0.9,2.23;Repair]
182 box[-0.05,1;2.05,0.9;#555555]
183 box[-0.05,2;2.05,0.9;#555555]
184 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
185 image[0,1;1,1;worktable_saw.png]
186 image[0,2;1,1;worktable_anvil.png]
187 image[3,2;1,1;hammer_layout.png]
188 list[context;input;2,1;1,1;]
189 list[context;tool;2,2;1,1;]
190 list[context;hammer;3,2;1,1;]
191 list[context;forms;4,0;4,3;]
192 list[current_player;main;0,3.25;8,4;]
193 button[0,0;2,1;craft;Crafting]
194 button[2,0;2,1;storage;Storage] ]]
195 ..xbg..default.get_hotbar_bg(0,3.25)
197 meta:set_string("formspec", formspec)
200 function worktable.construct(pos)
201 local meta = minetest.get_meta(pos)
202 local inv = meta:get_inventory()
204 inv:set_size("tool", 1)
205 inv:set_size("input", 1)
206 inv:set_size("hammer", 1)
207 inv:set_size("forms", 4*3)
208 inv:set_size("storage", 8*2)
209 inv:set_size("item_craft_input", 1)
210 inv:set_size("craft_output_recipe", 3*3)
211 meta:set_string("infotext", "Work Table")
216 function worktable.fields(pos, _, fields, _)
217 if fields.quit then return end
218 local meta = minetest.get_meta(pos)
219 local inv = meta:get_inventory()
220 local formspec = meta:to_table().fields.formspec
221 local filter = formspec:match("filter;;([%w_:]+)") or ""
222 local start_i = tonumber(formspec:match("inv_items_list;.*;(%d+)%]")) or 0
224 if fields.storage then
225 worktable.storage(meta)
226 elseif fields.back then
228 elseif fields.craft then
229 worktable.crafting(meta)
230 elseif fields.alternate then
231 inv:set_list("craft_output_recipe", {})
232 local inputstack = inv:get_stack("item_craft_input", 1):get_name()
233 local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
234 recipe_num = recipe_num + 1
235 worktable.craftguide_formspec(pos, start_i, start_i / (8*4) + 1, inputstack, recipe_num, filter)
237 if fields.backcraft then
238 worktable.crafting(meta)
239 elseif fields.search then
240 worktable.craftguide_main_list(pos, fields.filter:lower())
241 worktable.craftguide_formspec(pos, 0, 1, nil, 1, fields.filter:lower())
242 elseif fields.craftguide or fields.clearfilter then
243 worktable.craftguide_main_list(pos, nil)
244 worktable.craftguide_formspec(pos, 0, 1, nil, 1, "")
246 if fields.prev or fields.next then
247 local inv_size = #meta:to_table().inventory.inv_items_list
248 if fields.prev or start_i >= inv_size then
249 start_i = start_i - 8*4
250 elseif fields.next or start_i < 0 then
251 start_i = start_i + 8*4
254 if start_i >= inv_size then
256 elseif start_i < 0 then
257 start_i = inv_size - (inv_size % (8*4))
261 worktable.craftguide_formspec(pos, start_i, start_i / (8*4) + 1, nil, 1, filter)
264 inv:set_list("item_craft_input", {})
265 inv:set_list("craft_output_recipe", {})
269 function worktable.dig(pos, _)
270 local inv = minetest.get_meta(pos):get_inventory()
271 return inv:is_empty("input") and inv:is_empty("hammer") and
272 inv:is_empty("tool") and inv:is_empty("storage")
275 function worktable.contains(table, element)
277 for _, value in pairs(table) do
278 if value == element then
286 function worktable.put(_, listname, _, stack, _)
287 local stn = stack:get_name()
288 local mod, node = stn:match("([%w_]+):([%w_]+)")
289 local tdef = minetest.registered_tools[stn]
290 local twear = stack:get_wear()
292 if (listname == "input" and worktable.contains(nodes[mod], node)) or
293 listname == "storage" then
294 return stack:get_count()
295 elseif (listname == "hammer" and stn == "xdecor:hammer") or
296 (listname == "tool" and tdef and twear > 0) then
303 function worktable.take(pos, listname, _, stack, player)
304 if listname == "forms" then
305 local user_inv = player:get_inventory()
306 if user_inv:room_for_item("main", stack:get_name()) then
310 elseif listname == "inv_items_list" or listname == "item_craft_input" or
311 listname == "craft_output_recipe" then
315 return stack:get_count()
318 function worktable.move(pos, from_list, from_index, to_list, to_index, count, _)
319 local meta = minetest.get_meta(pos)
320 local inv = meta:get_inventory()
322 if from_list == "storage" and to_list == "storage" then
324 elseif inv:is_empty("item_craft_input") and from_list == "inv_items_list" and
325 to_list == "item_craft_input" then
326 local stackname = inv:get_stack(from_list, from_index):get_name()
327 local formspec = meta:to_table().fields.formspec
328 local filter = formspec:match("filter;;([%w_:]+)") or ""
329 local start_i = tonumber(formspec:match("inv_items_list;.*;(%d+)%]")) or 0
331 worktable.craftguide_formspec(pos, start_i, start_i / (8*4) + 1, stackname, 1, filter)
337 function worktable.update_inventory(inv, inputstack)
338 if inv:is_empty("input") then
339 inv:set_list("forms", {})
344 local input = inv:get_stack("input", 1)
346 for _, n in pairs(def) do
347 local count = math.min(n[2] * input:get_count(), input:get_stack_max())
348 output[#output+1] = inputstack:get_name().."_"..n[1].." "..count
351 inv:set_list("forms", output)
354 function worktable.on_put(pos, listname, _, stack, _)
355 if listname == "input" then
356 local inv = minetest.get_meta(pos):get_inventory()
357 worktable.update_inventory(inv, stack)
361 function worktable.on_take(pos, listname, index, stack, _)
362 local inv = minetest.get_meta(pos):get_inventory()
363 if listname == "input" then
364 if stack:get_name() == inv:get_stack("input", 1):get_name() then
365 worktable.update_inventory(inv, stack)
367 inv:set_list("forms", {})
369 elseif listname == "forms" then
370 local inputstack = inv:get_stack("input", 1)
371 inputstack:take_item(math.ceil(stack:get_count() / def[index][2]))
372 inv:set_stack("input", 1, inputstack)
373 worktable.update_inventory(inv, inputstack)
377 xdecor.register("worktable", {
378 description = "Work Table",
379 groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
380 sounds = default.node_sound_wood_defaults(),
382 "xdecor_worktable_top.png", "xdecor_worktable_top.png",
383 "xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
384 "xdecor_worktable_front.png", "xdecor_worktable_front.png"
386 on_rotate = screwdriver.rotate_simple,
387 can_dig = worktable.dig,
388 on_construct = worktable.construct,
389 on_receive_fields = worktable.fields,
390 on_metadata_inventory_put = worktable.on_put,
391 on_metadata_inventory_take = worktable.on_take,
392 allow_metadata_inventory_put = worktable.put,
393 allow_metadata_inventory_take = worktable.take,
394 allow_metadata_inventory_move = worktable.move
397 for _, d in pairs(def) do
398 for mod, n in pairs(nodes) do
399 for _, name in pairs(n) do
400 local ndef = minetest.registered_nodes[mod..":"..name]
402 local groups, tiles, light = {}, {}
403 groups.not_in_creative_inventory = 1
405 for k, v in pairs(ndef.groups) do
406 if k ~= "wood" and k ~= "stone" and k ~= "level" then
411 if #ndef.tiles > 1 and not ndef.drawtype:find("glass") then
414 tiles = {ndef.tiles[1]}
417 if ndef.light_source > 3 then
418 light = ndef.light_source - 1
421 minetest.register_node(":"..mod..":"..name.."_"..d[1], {
422 description = ndef.description.." "..d[1]:gsub("^%l", string.upper),
424 paramtype2 = "facedir",
425 drawtype = "nodebox",
426 light_source = light,
427 sounds = ndef.sounds,
430 node_box = {type = "fixed", fixed = d[3]},
431 sunlight_propagates = true,
432 on_place = minetest.rotate_node
439 minetest.register_abm({
440 nodenames = {"xdecor:worktable"},
441 interval = 3, chance = 1,
442 action = function(pos, _, _, _)
443 local inv = minetest.get_meta(pos):get_inventory()
444 local tool = inv:get_stack("tool", 1)
445 local hammer = inv:get_stack("hammer", 1)
446 local wear = tool:get_wear()
448 if tool:is_empty() or hammer:is_empty() or wear == 0 then
452 -- Wear : 0-65535 | 0 = new condition.
456 inv:set_stack("tool", 1, tool)
457 inv:set_stack("hammer", 1, hammer)