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.get_recipe(item)
35 if item:find("^group:") then
36 if item:find("wool$") or item:find("dye$") then
37 item = item:sub(7)..":white"
38 elseif minetest.registered_items["default:"..item:sub(7)] then
39 item = item:gsub("group:", "default:")
41 for node, def in pairs(minetest.registered_items) do
42 if def.groups[item:match("[^,:]+$")] then
51 function worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
52 local inv_size = meta:get_int("inv_size")
53 local npp, i, s = 8*3, 0, 0
54 local pagemax = math.floor((inv_size - 1) / npp + 1)
56 if pagenum > pagemax then
58 elseif pagenum == 0 then
62 local formspec = [[ size[8,6.6;]
63 tablecolumns[color;text;color;text]
64 tableoptions[background=#00000000;highlight=#00000000;border=false]
65 button[5.5,0;0.7,1;prev;<]
66 button[7.3,0;0.7,1;next;>]
67 button[4,0.2;0.7,0.5;search;?]
68 button[4.6,0.2;0.7,0.5;clearfilter;X]
69 button[0,0;1.5,1;backcraft;< Back]
70 tooltip[search;Search]
71 tooltip[clearfilter;Reset] ]]
72 .."tabheader[0,0;tabs;All,Nodes,Tools,Items;"..tostring(tab_id)..";true;false]"..
73 "table[6.1,0.2;1.1,0.5;pagenum;#FFFF00,"..tostring(pagenum)..
74 ",#FFFFFF,/ "..tostring(pagemax).."]"..
75 "field[1.8,0.32;2.6,1;filter;;"..filter.."]"..xbg
77 for _, name in pairs(worktable.craftguide_main_list(meta, filter, tab_id)) do
78 if s < (pagenum - 1) * npp then
81 if i >= npp then break end
82 formspec = formspec.."item_image_button["..(i%8)..","..
83 (math.floor(i/8)+1)..";1,1;"..name..";"..name..";]"
88 if item and minetest.registered_items[item] then
89 --print(dump(minetest.get_all_craft_recipes(item)))
90 local items_num = #minetest.get_all_craft_recipes(item)
91 if recipe_num > items_num then recipe_num = 1 end
94 formspec = formspec.."button[0,6;1.6,1;alternate;Alternate]"..
95 "label[0,5.5;Recipe "..recipe_num.." of "..items_num.."]"
98 local type = minetest.get_all_craft_recipes(item)[recipe_num].type
99 if type == "cooking" then
100 formspec = formspec.."image[3.75,4.6;0.5,0.5;default_furnace_fire_fg.png]"
103 local items = minetest.get_all_craft_recipes(item)[recipe_num].items
104 local width = minetest.get_all_craft_recipes(item)[recipe_num].width
105 if width == 0 then width = math.min(3, #items) end
106 local rows = math.ceil(table.maxn(items) / width)
108 local function is_group(item)
109 if item:find("^group:") then return "G" end
113 for i, v in pairs(items) do
114 formspec = formspec.."item_image_button["..((i-1) % width + 4.5)..","..
115 (math.floor((i-1) / width + (6 - math.min(2, rows))))..";1,1;"..
116 worktable.get_recipe(v)..";"..worktable.get_recipe(v)..";"..is_group(v).."]"
119 formspec = formspec.."item_image[2.5,5;1,1;"..item.."]"..
120 "image[3.5,5;1,1;gui_furnace_arrow_bg.png^[transformR90]"
123 meta:set_string("formspec", formspec)
126 local function tab_category(tab_id)
127 local id_category = {
128 minetest.registered_items,
129 minetest.registered_nodes,
130 minetest.registered_tools,
131 minetest.registered_craftitems
134 return id_category[tab_id] or id_category[1]
137 function worktable.craftguide_main_list(meta, filter, tab_id)
138 local items_list = {}
139 for name, def in pairs(tab_category(tab_id)) do
140 if not (def.groups.not_in_creative_inventory == 1) and
141 minetest.get_craft_recipe(name).items and
142 def.description and def.description ~= "" and
143 (not filter or def.name:find(filter, 1, true)) then
144 items_list[#items_list+1] = name
148 meta:set_int("inv_size", #items_list)
149 table.sort(items_list)
153 worktable.formspecs = {
154 crafting = function(meta)
155 meta:set_string("formspec", [[ size[8,7;]
156 image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
157 image[0.06,2.12;0.8,0.8;trash_icon.png]
158 button[0,0;1.5,1;back;< Back]
159 button[0,0.85;1.5,1;craftguide;Guide]
160 list[context;trash;0,2;1,1;]
161 list[current_player;main;0,3.3;8,4;]
162 list[current_player;craft;2,0;3,3;]
163 list[current_player;craftpreview;6,1;1,1;]
164 listring[current_player;main]
165 listring[current_player;craft] ]]
166 ..xbg..default.get_hotbar_bg(0,3.3))
168 storage = function(meta)
169 meta:set_string("formspec", [[ size[8,7]
170 image[7.06,0.12;0.8,0.8;trash_icon.png]
171 list[context;trash;7,0;1,1;]
172 list[context;storage;0,1;8,2;]
173 list[current_player;main;0,3.25;8,4;]
174 listring[context;storage]
175 listring[current_player;main]
176 button[0,0;1.5,1;back;< Back] ]]
177 ..xbg..default.get_hotbar_bg(0,3.25))
179 main = function(meta)
180 meta:set_string("formspec", [[ size[8,7;]
182 label[0.9,2.23;Repair]
183 box[-0.05,1;2.05,0.9;#555555]
184 box[-0.05,2;2.05,0.9;#555555]
185 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
186 image[0,1;1,1;worktable_saw.png]
187 image[0,2;1,1;worktable_anvil.png]
188 image[3,2;1,1;hammer_layout.png]
189 list[context;input;2,1;1,1;]
190 list[context;tool;2,2;1,1;]
191 list[context;hammer;3,2;1,1;]
192 list[context;forms;4,0;4,3;]
193 list[current_player;main;0,3.25;8,4;]
194 button[0,0;2,1;craft;Crafting]
195 button[2,0;2,1;storage;Storage] ]]
196 ..xbg..default.get_hotbar_bg(0,3.25))
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("trash", 1)
206 inv:set_size("input", 1)
207 inv:set_size("hammer", 1)
208 inv:set_size("forms", 4*3)
209 inv:set_size("storage", 8*2)
210 meta:set_string("infotext", "Work Table")
212 worktable.formspecs.main(meta)
215 function worktable.fields(pos, _, fields)
216 if fields.quit then return end
217 local meta = minetest.get_meta(pos)
218 local formspec = meta:to_table().fields.formspec
219 local filter = formspec:match("filter;;([%w_:]+)") or ""
220 local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1
221 local tab_id = tonumber(formspec:match("tabheader%[.*;(%d+)%;.*%]")) or 1
224 worktable.formspecs.main(meta)
225 elseif fields.craft or fields.backcraft then
226 worktable.formspecs.crafting(meta)
227 elseif fields.storage then
228 worktable.formspecs.storage(meta)
229 elseif fields.craftguide or fields.clearfilter then
230 worktable.craftguide_main_list(meta, nil, 1)
231 worktable.craftguide_formspec(meta, 1, nil, 1, "", 1)
232 elseif fields.alternate then
233 local item = formspec:match("item_image%[.*;([%w_:]+)%]") or ""
234 local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
235 recipe_num = recipe_num + 1
236 worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
237 elseif fields.search then
238 worktable.craftguide_main_list(meta, fields.filter:lower(), tab_id)
239 worktable.craftguide_formspec(meta, 1, nil, 1, fields.filter:lower(), tab_id)
240 elseif fields.tabs then
241 worktable.craftguide_main_list(meta, filter, tonumber(fields.tabs))
242 worktable.craftguide_formspec(meta, 1, nil, 1, filter, tonumber(fields.tabs))
243 elseif fields.prev or fields.next then
245 pagenum = pagenum - 1
247 pagenum = pagenum + 1
249 worktable.craftguide_formspec(meta, pagenum, nil, 1, filter, tab_id)
251 for item in pairs(fields) do
252 if item:match(".-:") and minetest.get_craft_recipe(item).items then
253 worktable.craftguide_formspec(meta, pagenum, item, 1, filter, tab_id)
259 function worktable.dig(pos)
260 local inv = minetest.get_meta(pos):get_inventory()
261 return inv:is_empty("input") and inv:is_empty("hammer") and
262 inv:is_empty("tool") and inv:is_empty("storage")
265 function worktable.allowed(table, element)
267 for _, value in pairs(table) do
268 if value == element then
276 local function trash_delete(pos)
277 local inv = minetest.get_meta(pos):get_inventory()
278 minetest.after(0, function()
279 inv:set_stack("trash", 1, "")
283 function worktable.put(pos, listname, _, stack)
284 local stackname = stack:get_name()
285 local mod, node = stackname:match("(.*):(.*)")
287 if (listname == "tool" and stack:get_wear() > 0 and stackname ~= "xdecor:hammer") or
288 (listname == "input" and worktable.allowed(nodes[mod], node)) or
289 (listname == "hammer" and stackname == "xdecor:hammer") or
290 listname == "storage" or listname == "trash" then
291 if listname == "trash" then trash_delete(pos) end
292 return stack:get_count()
298 function worktable.take(_, listname, _, stack, player)
299 if listname == "forms" then
300 local inv = player:get_inventory()
301 if inv:room_for_item("main", stack:get_name()) then
306 return stack:get_count()
309 function worktable.move(pos, _, _, to_list, _, count)
310 if to_list == "storage" then
312 elseif to_list == "trash" then
319 function worktable.get_output(inv, stack)
320 if inv:is_empty("input") then
321 inv:set_list("forms", {})
325 local input, output = inv:get_stack("input", 1), {}
326 for _, n in pairs(def) do
327 local count = math.min(n[2] * input:get_count(), input:get_stack_max())
328 output[#output+1] = stack:get_name().."_"..n[1].." "..count
331 inv:set_list("forms", output)
334 function worktable.on_put(pos, listname, _, stack)
335 if listname == "input" then
336 local inv = minetest.get_meta(pos):get_inventory()
337 worktable.get_output(inv, stack)
341 function worktable.on_take(pos, listname, index, stack)
342 local inv = minetest.get_meta(pos):get_inventory()
343 local inputstack = inv:get_stack("input", 1)
345 if listname == "input" then
346 if stack:get_name() == inputstack:get_name() then
347 worktable.get_output(inv, stack)
349 inv:set_list("forms", {})
351 elseif listname == "forms" then
352 inputstack:take_item(math.ceil(stack:get_count() / def[index][2]))
353 inv:set_stack("input", 1, inputstack)
354 worktable.get_output(inv, inputstack)
358 xdecor.register("worktable", {
359 description = "Work Table",
360 groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
361 sounds = default.node_sound_wood_defaults(),
363 "xdecor_worktable_top.png", "xdecor_worktable_top.png",
364 "xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
365 "xdecor_worktable_front.png", "xdecor_worktable_front.png"
367 on_rotate = screwdriver.rotate_simple,
368 can_dig = worktable.dig,
369 on_construct = worktable.construct,
370 on_receive_fields = worktable.fields,
371 on_metadata_inventory_put = worktable.on_put,
372 on_metadata_inventory_take = worktable.on_take,
373 allow_metadata_inventory_put = worktable.put,
374 allow_metadata_inventory_take = worktable.take,
375 allow_metadata_inventory_move = worktable.move
378 for _, d in pairs(def) do
379 for mod, n in pairs(nodes) do
380 for _, name in pairs(n) do
381 local ndef = minetest.registered_nodes[mod..":"..name]
383 local groups, tiles, light = {}, {}
384 groups.not_in_creative_inventory = 1
386 for k, v in pairs(ndef.groups) do
387 if k ~= "wood" and k ~= "stone" and k ~= "level" then
392 if #ndef.tiles > 1 and not ndef.drawtype:find("glass") then
395 tiles = {ndef.tiles[1]}
398 if ndef.light_source > 3 then
399 light = ndef.light_source - 1
402 minetest.register_node(":"..mod..":"..name.."_"..d[1], {
403 description = ndef.description.." "..d[1]:gsub("^%l", string.upper),
405 paramtype2 = "facedir",
406 drawtype = "nodebox",
407 light_source = light,
408 sounds = ndef.sounds,
411 node_box = {type = "fixed", fixed = d[3]},
412 sunlight_propagates = true,
413 on_place = minetest.rotate_node
420 minetest.register_abm({
421 nodenames = {"xdecor:worktable"},
422 interval = 3, chance = 1,
423 action = function(pos)
424 local inv = minetest.get_meta(pos):get_inventory()
425 local tool = inv:get_stack("tool", 1)
426 local hammer = inv:get_stack("hammer", 1)
428 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
432 -- Wear : 0-65535 | 0 = new condition.
436 inv:set_stack("tool", 1, tool)
437 inv:set_stack("hammer", 1, hammer)