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:match("[^,:]+$")..":white"
38 elseif minetest.registered_items["default:"..item:match("[^,:]+$")] then
39 item = item:gsub("group:", "default:")
41 for node, definition in pairs(minetest.registered_items) do
42 if definition.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 }
133 return id_category[tab_id] or id_category[1]
136 function worktable.craftguide_main_list(meta, filter, tab_id)
137 local items_list = {}
138 for name, def in pairs(tab_category(tab_id)) do
139 if not (def.groups.not_in_creative_inventory == 1) and
140 minetest.get_craft_recipe(name).items and
141 def.description and def.description ~= "" and
142 (not filter or def.name:find(filter, 1, true)) then
143 items_list[#items_list+1] = name
147 meta:set_int("inv_size", #items_list)
148 table.sort(items_list)
152 worktable.formspecs = {
153 crafting = function(meta)
154 meta:set_string("formspec", [[ size[8,7;]
155 list[current_player;main;0,3.3;8,4;]
156 image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
157 button[0,0;1.5,1;back;< Back]
158 button[0,1;1.5,1;craftguide;Guide]
159 list[current_player;craft;2,0;3,3;]
160 list[current_player;craftpreview;6,1;1,1;]
161 listring[current_player;main]
162 listring[current_player;craft] ]]
163 ..xbg..default.get_hotbar_bg(0,3.3))
165 storage = function(meta)
166 meta:set_string("formspec", [[ size[8,7]
167 list[context;storage;0,1;8,2;]
168 list[current_player;main;0,3.25;8,4;]
169 listring[context;storage]
170 listring[current_player;main]
171 button[0,0;1.5,1;back;< Back] ]]
172 ..xbg..default.get_hotbar_bg(0,3.25))
174 main = function(meta)
175 meta:set_string("formspec", [[ size[8,7;]
177 label[0.9,2.23;Repair]
178 box[-0.05,1;2.05,0.9;#555555]
179 box[-0.05,2;2.05,0.9;#555555]
180 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
181 image[0,1;1,1;worktable_saw.png]
182 image[0,2;1,1;worktable_anvil.png]
183 image[3,2;1,1;hammer_layout.png]
184 list[context;input;2,1;1,1;]
185 list[context;tool;2,2;1,1;]
186 list[context;hammer;3,2;1,1;]
187 list[context;forms;4,0;4,3;]
188 list[current_player;main;0,3.25;8,4;]
189 button[0,0;2,1;craft;Crafting]
190 button[2,0;2,1;storage;Storage] ]]
191 ..xbg..default.get_hotbar_bg(0,3.25))
195 function worktable.construct(pos)
196 local meta = minetest.get_meta(pos)
197 local inv = meta:get_inventory()
199 inv:set_size("tool", 1)
200 inv:set_size("input", 1)
201 inv:set_size("hammer", 1)
202 inv:set_size("forms", 4*3)
203 inv:set_size("storage", 8*2)
204 meta:set_string("infotext", "Work Table")
206 worktable.formspecs.main(meta)
209 function worktable.fields(pos, _, fields)
210 if fields.quit then return end
211 local meta = minetest.get_meta(pos)
212 local formspec = meta:to_table().fields.formspec
213 local filter = formspec:match("filter;;([%w_:]+)") or ""
214 local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1
215 local tab_id = tonumber(formspec:match("tabheader%[.*;(%d+)%;.*]")) or 1
218 worktable.formspecs.main(meta)
219 elseif fields.craft or fields.backcraft then
220 worktable.formspecs.crafting(meta)
221 elseif fields.storage then
222 worktable.formspecs.storage(meta)
223 elseif fields.craftguide or fields.clearfilter then
224 worktable.craftguide_main_list(meta, nil, 1)
225 worktable.craftguide_formspec(meta, 1, nil, 1, "", 1)
226 elseif fields.alternate then
227 local item = formspec:match("item_image%[.*;([%w_:]+)%]") or ""
228 local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
229 recipe_num = recipe_num + 1
230 worktable.craftguide_formspec(meta, pagenum, item, recipe_num, filter, tab_id)
231 elseif fields.search then
232 worktable.craftguide_main_list(meta, fields.filter:lower(), tab_id)
233 worktable.craftguide_formspec(meta, 1, nil, 1, fields.filter:lower(), tab_id)
234 elseif fields.tabs then
235 worktable.craftguide_main_list(meta, filter, tonumber(fields.tabs))
236 worktable.craftguide_formspec(meta, 1, nil, 1, filter, tonumber(fields.tabs))
237 elseif fields.prev or fields.next then
239 pagenum = pagenum - 1
241 pagenum = pagenum + 1
243 worktable.craftguide_formspec(meta, pagenum, nil, 1, filter, tab_id)
245 for item in pairs(fields) do
246 if item:match("[%w_]+:[%w_]+") and
247 minetest.get_craft_recipe(item).items then
248 worktable.craftguide_formspec(meta, pagenum, item, 1, filter, tab_id)
254 function worktable.dig(pos)
255 local inv = minetest.get_meta(pos):get_inventory()
256 return inv:is_empty("input") and inv:is_empty("hammer") and
257 inv:is_empty("tool") and inv:is_empty("storage")
260 function worktable.contains(table, element)
262 for _, value in pairs(table) do
263 if value == element then
271 function worktable.put(_, listname, _, stack)
272 local stackname = stack:get_name()
273 local mod, node = stackname:match("([%w_]+):([%w_]+)")
275 if (listname == "tool" and stack:get_wear() > 0 and stackname ~= "xdecor:hammer") or
276 (listname == "input" and worktable.contains(nodes[mod], node)) or
277 (listname == "hammer" and stackname == "xdecor:hammer") or
278 listname == "storage" then
279 return stack:get_count()
284 function worktable.take(pos, listname, _, stack, player)
285 if listname == "forms" then
286 local user_inv = player:get_inventory()
287 if user_inv:room_for_item("main", stack:get_name()) then
292 return stack:get_count()
295 function worktable.move(_, from_list, _, to_list, _, count)
296 if from_list == "storage" and to_list == "storage" then
302 function worktable.get_output(inv, stack)
303 if inv:is_empty("input") then
304 inv:set_list("forms", {})
308 local input, output = inv:get_stack("input", 1), {}
309 for _, n in pairs(def) do
310 local count = math.min(n[2] * input:get_count(), input:get_stack_max())
311 output[#output+1] = stack:get_name().."_"..n[1].." "..count
314 inv:set_list("forms", output)
317 function worktable.on_put(pos, listname, _, stack)
318 if listname == "input" then
319 local inv = minetest.get_meta(pos):get_inventory()
320 worktable.get_output(inv, stack)
324 function worktable.on_take(pos, listname, index, stack)
325 local inv = minetest.get_meta(pos):get_inventory()
326 local inputstack = inv:get_stack("input", 1)
328 if listname == "input" then
329 if stack:get_name() == inputstack:get_name() then
330 worktable.get_output(inv, stack)
332 inv:set_list("forms", {})
334 elseif listname == "forms" then
335 inputstack:take_item(math.ceil(stack:get_count() / def[index][2]))
336 inv:set_stack("input", 1, inputstack)
337 worktable.get_output(inv, inputstack)
341 xdecor.register("worktable", {
342 description = "Work Table",
343 groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
344 sounds = default.node_sound_wood_defaults(),
346 "xdecor_worktable_top.png", "xdecor_worktable_top.png",
347 "xdecor_worktable_sides.png", "xdecor_worktable_sides.png",
348 "xdecor_worktable_front.png", "xdecor_worktable_front.png"
350 on_rotate = screwdriver.rotate_simple,
351 can_dig = worktable.dig,
352 on_construct = worktable.construct,
353 on_receive_fields = worktable.fields,
354 on_metadata_inventory_put = worktable.on_put,
355 on_metadata_inventory_take = worktable.on_take,
356 allow_metadata_inventory_put = worktable.put,
357 allow_metadata_inventory_take = worktable.take,
358 allow_metadata_inventory_move = worktable.move
361 for _, d in pairs(def) do
362 for mod, n in pairs(nodes) do
363 for _, name in pairs(n) do
364 local ndef = minetest.registered_nodes[mod..":"..name]
366 local groups, tiles, light = {}, {}
367 groups.not_in_creative_inventory = 1
369 for k, v in pairs(ndef.groups) do
370 if k ~= "wood" and k ~= "stone" and k ~= "level" then
375 if #ndef.tiles > 1 and not ndef.drawtype:find("glass") then
378 tiles = {ndef.tiles[1]}
381 if ndef.light_source > 3 then
382 light = ndef.light_source - 1
385 minetest.register_node(":"..mod..":"..name.."_"..d[1], {
386 description = ndef.description.." "..d[1]:gsub("^%l", string.upper),
388 paramtype2 = "facedir",
389 drawtype = "nodebox",
390 light_source = light,
391 sounds = ndef.sounds,
394 node_box = {type = "fixed", fixed = d[3]},
395 sunlight_propagates = true,
396 on_place = minetest.rotate_node
403 minetest.register_abm({
404 nodenames = {"xdecor:worktable"},
405 interval = 3, chance = 1,
406 action = function(pos)
407 local inv = minetest.get_meta(pos):get_inventory()
408 local tool = inv:get_stack("tool", 1)
409 local hammer = inv:get_stack("hammer", 1)
411 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
415 -- Wear : 0-65535 | 0 = new condition.
419 inv:set_stack("tool", 1, tool)
420 inv:set_stack("hammer", 1, hammer)