3 local craftguide
, datas
, mt
= {}, {}, minetest
5 -- true: Only show recipes which include at least one of the items the player posesses
6 -- false: Show all crafting recipes
7 local progressive_mode
= false
8 if mt
.settings
:get_bool("craftguide_progressive_mode") == true then
9 progressive_mode
= true
11 local get_recipe
= mt
.get_craft_recipe
12 local get_result
, show_formspec
= mt
.get_craft_result
, mt
.show_formspec
13 local reg_items
= mt
.registered_items
15 local get_recipes
= function(query_item
)
16 local recipes
= mt
.get_all_craft_recipes(query_item
)
18 -- Manually add repairing recipes (workaround, because get_all_craft_recipes
19 -- doesn't return repairing recipes)
20 if minetest
.get_modpath("mcl_core") then
21 local def
= minetest
.registered_items
[query_item
]
25 if def
.type == "tool" then
26 if recipes
== nil then
29 table.insert(recipes
, {
32 items
= { [1] = query_item
, [2] = query_item
},
34 -- Special marker for repairing recipes
35 _is_toolrepair
= true,
42 -- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage:
43 -- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1
44 local remove, maxn
, sort = table.remove, table.maxn
, table.sort
45 local min, max, floor, ceil = math
.min, math
.max, math
.floor, math
.ceil
47 local group_stereotypes
= {
48 wool
= "mcl_wool:white",
49 carpet
= "mcl_wool:white_carpet",
51 water_bucket
= "mcl_buckets:bucket_water",
52 flower
= "mcl_flowers:dandelion",
53 mushroom
= "mcl_mushrooms:mushroom_brown",
54 wood_slab
= "mcl_stairs:slab_wood",
55 wood_stairs
= "mcl_stairs:stairs_wood",
56 coal
= "mcl_core:coal_lump",
57 shulker_box
= "mcl_chests:violet_shulker_box",
58 quartz_block
= "mcl_nether:quartz_block",
59 banner
= "mcl_banners:banner_item_white",
60 mesecon_conductor_craftable
= "mesecons:wire_00000000_off",
61 compass
= mcl_compass
.stereotype
,
62 clock = mcl_clock
.sterotype
,
66 shulker_box
= "Any shulker box",
68 wood
= "Any wood planks",
71 sandstone
= "Any sandstone (yellow)",
72 redsandstone
= "Any red sandstone",
73 carpet
= "Any carpet",
75 water_bucket
= "Any water bucket",
76 flower
= "Any flower",
77 mushroom
= "Any mushroom",
78 wood_slab
= "Any wooden slab",
79 wood_stairs
= "Any wooden stairs",
81 quartz_block
= "Any kind of quartz block",
82 stonebrick
= "Any stone bricks",
85 function craftguide
:group_to_item(item
)
86 if item
:sub(1,6) == "group:" then
87 local itemsub
= item
:sub(7)
88 if group_stereotypes
[itemsub
] then
89 item
= group_stereotypes
[itemsub
]
90 elseif reg_items
["mcl_core:"..itemsub
] then
91 item
= item
:gsub("group:", "mcl_core:")
93 for name
, def
in pairs(reg_items
) do
94 if def
.groups
[item
:match("[^,:]+$")] then
100 return item
:sub(1,6) == "group:" and "" or item
103 local function extract_groups(str
)
104 if str
:sub(1,6) ~= "group:" then return end
105 return str
:sub(7):split(",")
108 local function colorize(str
)
109 -- If client <= 0.4.14, don't colorize for compatibility.
110 return mt
.colorize
and mt
.colorize("#FFFF00", str
) or str
113 local function get_fueltime(item
)
114 return get_result({method
="fuel", width
=1, items
={item
}}).time
117 function craftguide
:get_tooltip(item
, recipe_type
, cooktime
, groups
)
118 local raw
= self
:get_tooltip_raw(item
, recipe_type
, cooktime
, groups
)
122 local tooltip
= "tooltip["..item
..";"
123 tooltip
= tooltip
.. raw
124 tooltip
= tooltip
.. "]"
129 function craftguide
:get_tooltip_raw(item
, recipe_type
, cooktime
, groups
)
130 local tooltip
, item_desc
= "", ""
131 local fueltime
= get_fueltime(item
)
132 local has_extras
= groups
or recipe_type
== "cooking" or fueltime
> 0
134 if reg_items
[item
] then
136 item_desc
= reg_items
[item
].description
139 return tooltip
.."Unknown Item ("..item
..")]"
142 local gcol
= "#FFAAFF"
145 local g
= group_names
[groups
[1]]
146 -- Treat the groups “compass” and “clock” as fake groups
147 -- and just print the normal group name without special formatting
148 if groups
[1] == "compass" or groups
[1] == "clock" then
150 groupstr
= reg_items
[item
].description
151 elseif group_names
[groups
[1]]
then
152 -- Use the special group name string
153 groupstr
= group_names
[groups
[1]]
155 --[[ Fallback: Generic group explanation: This always
156 works, but the internally used group name (which
157 looks ugly) is exposed to the user. ]]
158 groupstr
= "Any item belonging to the " .. groups
[1] .. " group"
161 groupstr
= "Any item belonging to the following groups: "
163 groupstr
= groupstr
.. groups
[i
]..
164 (groups
[i
+1] and " and " or "")
167 tooltip
= tooltip
..core
.colorize(gcol
, groupstr
)
169 tooltip
= tooltip
.. item_desc
170 if recipe_type
== "cooking" then
171 tooltip
= tooltip
.."\nCooking time: "..
174 if fueltime
> 0 and not groups
then
175 tooltip
= tooltip
.."\nBurning time: "..
182 function craftguide
:get_recipe(iY
, xoffset
, tooltip_raw
, item
, recipe_num
, recipes
)
183 local formspec
, recipes_total
= "", #recipes
184 if recipes_total
> 1 then
185 formspec
= formspec
..
186 "button[0,"..(iY
+3)..";2,1;alternate;Alternate]"..
187 "label[0,"..(iY
+2)..".5;Recipe "..
188 recipe_num
.." of "..recipes_total
.."]"
190 local recipe_type
= recipes
[recipe_num
].type
192 local items
= recipes
[recipe_num
].items
193 local width
= recipes
[recipe_num
].width
194 local output
= recipes
[recipe_num
].output
195 local cooking_time
= 10
196 local is_shapeless
= false
197 if recipe_type
== "normal" and width
== 0 then
202 width
= min(3, #items
)
206 --[[ Recipe type symbols ]]
209 if recipe_type
== "cooking" then
212 formspec
= formspec
..
213 "image["..(xoffset
-0.8)..","..(iY
+1)..
214 ".5;0.5,0.5;default_furnace_front_active.png]"
215 -- Shapeless recipes (intertwined arrows)
216 elseif is_shapeless
then
217 formspec
= formspec
..
218 "image["..(xoffset
-0.8)..","..(iY
+1)..
219 ".5;0.5,0.5;craftguide_shapeless.png]"
222 -- Recipe only available in v6 (“v6” icon)
223 -- TODO/FIXME: This only works for the unique red sand recipe.
224 -- Remove this when red sand becomes regularily available.
225 local v6_only_recipe
= false
226 if output
== "mcl_core:redsand 8" and
228 items
[1] == "mcl_core:sand" and
229 items
[2] == "mcl_core:sand" and
230 items
[3] == "mcl_core:sand" and
231 items
[4] == "mcl_core:sand" and
232 items
[5] == "mcl_dye:red" and
233 items
[6] == "mcl_core:sand" and
234 items
[7] == "mcl_core:sand" and
235 items
[8] == "mcl_core:sand" and
236 items
[9] == "mcl_core:sand" then
237 v6_only_recipe
= true
240 if v6_only_recipe
then
241 formspec
= formspec
..
242 "image["..(xoffset
-0.8)..","..(iY
+2.75)..
243 ".5;0.5,0.5;mcl_craftguide_v6.png]"
248 local rows
= ceil(maxn(items
) / width
)
249 local btn_size
, craftgrid_limit
= 1, 5
251 if recipe_type
== "normal" and
252 width
> craftgrid_limit
or rows
> craftgrid_limit
then
253 formspec
= formspec
..
254 "label["..xoffset
..","..(iY
+2)..
255 ";Recipe is too big to\nbe displayed ("..
256 width
.."x"..rows
..")]"
258 for i
, v
in pairs(items
) do
259 local X
= (i
-1) % width
+ xoffset
- 4 + (3 - max(1, width
))
260 local Y
= ceil(i
/ width
+ iY
+2 - min(2, rows
))
262 if recipe_type
== "normal" and
263 width
> 3 or rows
> 3 then
264 btn_size
= width
> 3 and 3 / width
or 3 / rows
265 X
= btn_size
* (i
% width
) + xoffset
- 4 + (3 - max(1, width
))
267 Y
= btn_size
* floor((i
-1) / width
) + iY
+3 -
271 local groups
= extract_groups(v
)
273 -- Add the “G” symbols for group item slots
275 --[[ Exception: Groups “compass” and “clock” since the items in these groups should
276 be treated as a single item from the user perspective. ]]
277 if not (#groups
== 1 and (groups
[1] == "compass" or groups
[1] == "clock")) then
281 local item_r
= self
:group_to_item(v
)
282 local tltip
= self
:get_tooltip(
283 item_r
, recipe_type
, cooking_time
, groups
)
285 formspec
= formspec
..
286 "item_image_button["..X
..","..Y
..";"..
287 btn_size
..","..btn_size
..";"..item_r
..
288 ";"..item_r
..";"..label
.."]"..tltip
292 if recipes
[recipe_num
]._is_toolrepair
then
293 tooltip_raw
= tooltip_raw
.. "\n" .. core
.colorize("#00FF00", string.format("Repaired by %.0f%%", (mcl_core
.repair
*100)))
297 "image["..(xoffset
-1)..","..(iY
+2)..
298 ".12;0.9,0.7;craftguide_arrow.png]"..
299 "item_image_button["..(xoffset
)..","..(iY
+2)..";1,1;"..
300 output
..";"..item
.."_out"..";"..label
.."]".."tooltip["..item
.."_out"..";"..minetest
.formspec_escape(tooltip_raw
).."]"
303 function craftguide
:get_formspec(player_name
, is_fuel
)
304 local data
= datas
[player_name
]
305 local iY
= data
.iX
- 5
306 local ipp
= data
.iX
* iY
308 if not data
.items
then
309 data
.items
= datas
.init_items
311 data
.pagemax
= max(1, ceil(#data
.items
/ ipp
))
313 local formspec
= "size["..data
.iX
..","..(iY
+3)..".6;]"..
314 mcl_vars
.gui_slots
..
316 [=[background[1,1;1,1;craftguide_bg.png;true]
317 button[2.4,0.21;0.8,0.5;search;?]
318 button[3.05,0.21;0.8,0.5;clear;X]
319 tooltip[search;Search]
321 tooltip[size_inc;Increase window size]
322 tooltip[size_dec;Decrease window size]
323 field_close_on_enter[filter;false]]=]..
324 "button["..(data.iX/2)..",-0.02;0.7,1;size_inc;+]"..
325 "button["..((data.iX/2) + 0.5)..
326 ",-0.02;0.7,1;size_dec;-]"..
327 "button["..(data.iX-3)..".4,0;0.8,0.95;prev;<]"..
328 "label["..(data.iX-2)..".1,0.18;"..
329 colorize(data.pagenum).." / "..data.pagemax.."]"..
330 "button["..(data.iX-1)..".2,0;0.8,0.95;next;>]"..
331 "field[0.3,0.32;2.5,1;filter;;"..
332 mt.formspec_escape(data.filter).."]"
334 local even_num = data.iX % 2 == 0
335 local xoffset = data.iX / 2 + (even_num and 0.5 or 0) + 2
337 if not next(data.items) then
339 if data.filter == "" then
340 msg = "You don't know any crafting recipes yet.\nCollect some items and open the recipe book again."
342 msg = "No crafting recipes found.\nReset the search and try again."
344 formspec = formspec.."label[0,2;"..mt.formspec_escape(msg).."]"
347 local first_item = (data.pagenum - 1) * ipp
348 for i = first_item, first_item + ipp - 1 do
349 local name = data.items[i+1]
350 if not name then break end
351 local X = i % data.iX
352 local Y = (i % ipp - X) / data.iX + 1
354 formspec = formspec..
355 "item_image_button["..X..","..Y..";1,1;"..
356 name..";"..name.."_inv;]"
359 if data.item and reg_items[data.item] then
360 local tooltip_raw = self:get_tooltip_raw(data.item)
362 if tooltip_raw ~= "" then
363 tooltip = "tooltip["..data.item..";"..minetest.formspec_escape(tooltip_raw).."]"
365 if not data.recipes_item or (is_fuel and not
366 get_recipe(data.item).items) then
367 formspec = formspec..
368 "image["..(xoffset-1)..","..(iY+2)..
369 ".12;0.9,0.7;craftguide_arrow.png]"..
370 "item_image_button["..(xoffset-2)..","..(iY+2)..
371 ";1,1;"..data.item..";"..data.item..";]"..
373 "image["..(xoffset)..","..
374 -- TODO: Remove fire icon, find better way to represent fuel
375 (iY+1.98)..";1,1;mcl_craftguide_fuel.png]"
377 formspec = formspec..self:get_recipe(
378 iY, xoffset, tooltip_raw, data.item,
379 data.recipe_num, data.recipes_item)
383 data.formspec = formspec
384 show_formspec(player_name, "craftguide", formspec)
387 local function player_has_item(T)
389 if T[i] then return true end
393 local function group_to_items(group)
394 local items_with_group, counter = {}, 0
395 for name, def in pairs(reg_items) do
396 if def.groups[group:sub(7)] then
397 counter = counter + 1
398 items_with_group[counter] = name
401 return items_with_group
404 local function item_in_inv(inv, item)
405 return inv:contains_item("main", item)
408 -- Returns true if player knows the item. Used for progressive mode (EXPERIMENTAL).
409 local function knows_item(playername, item)
410 local has_item = doc.entry_exists("nodes", item) and doc.entry_revealed(playername, "nodes", item)
412 has_item = doc.entry_exists("tools", item) and doc.entry_revealed(playername, "tools", item)
415 has_item = doc.entry_exists("craftitems", item) and doc.entry_revealed(playername, "craftitems", item)
420 function craftguide:recipe_in_inv(inv, item_name, recipes_f, playername)
421 local recipes = recipes_f or get_recipes(item_name) or {}
422 local show_item_recipes = {}
425 show_item_recipes[i] = false
426 for _, item in pairs(recipes[i].items) do
427 local group_in_inv = false
428 if item:sub(1,6) == "group:" then
429 local groups = group_to_items(item)
431 if item_in_inv(inv, groups[j]) then
436 if group_in_inv or item_in_inv(inv, item) or knows_item(playername, item) then
437 show_item_recipes[i] = true
441 for i=#show_item_recipes, 1, -1 do
442 if not show_item_recipes[i] then
447 return recipes, player_has_item(show_item_recipes)
450 function craftguide:get_init_items()
451 local items_list, counter = {}, 0
452 for name, def in pairs(reg_items) do
453 local is_fuel = get_fueltime(name) > 0
454 local is_tool = def.type == "tool"
455 if (not def.groups.not_in_craft_guide or def.groups.not_in_craft_guide == 0)
456 and (get_recipe(name).items or is_fuel or is_tool)
457 and def.description and def.description ~= "" then
458 counter = counter + 1
459 items_list[counter] = name
464 datas.init_items = items_list
467 function craftguide:get_filter_items(data, player)
468 local filter = data.filter
469 local items_list = progressive_mode and data.init_filter_items or
471 local inv = player:get_inventory()
472 local filtered_list, counter = {}, 0
474 for i=1, #items_list do
475 local item = items_list[i]
476 local item_desc = reg_items[item].description:lower()
479 if item:find(filter, 1, true) or
480 item_desc:find(filter, 1, true) then
481 counter = counter + 1
482 filtered_list[counter] = item
484 elseif progressive_mode then
485 local _, has_item = self:recipe_in_inv(inv, item, nil, player:get_player_name())
487 counter = counter + 1
488 filtered_list[counter] = item
493 if progressive_mode and not data.items then
494 data.init_filter_items = filtered_list
496 data.items = filtered_list
499 mt.register_on_player_receive_fields(function(player, formname, fields)
500 if formname ~= "craftguide" then return end
501 local player_name = player:get_player_name()
502 local data = datas[player_name]
505 data.filter, data.item, data.pagenum, data.recipe_num =
507 data.items = progressive_mode and data.init_filter_items or
509 craftguide:get_formspec(player_name)
510 elseif fields.alternate then
511 local recipe = data.recipes_item[data.recipe_num + 1]
512 data.recipe_num = recipe and data.recipe_num + 1 or 1
513 craftguide:get_formspec(player_name)
514 elseif (fields.key_enter_field == "filter" or fields.search) and
515 fields.filter ~= "" then
516 data.filter = fields.filter:lower()
518 craftguide:get_filter_items(data, player)
519 craftguide:get_formspec(player_name)
520 elseif fields.prev or fields.next then
521 data.pagenum = data.pagenum - (fields.prev and 1 or -1)
522 if data.pagenum > data.pagemax then
524 elseif data.pagenum == 0 then
525 data.pagenum = data.pagemax
527 craftguide:get_formspec(player_name)
528 elseif (fields.size_inc and data.iX < 12) or
529 (fields.size_dec and data.iX > 8) then
531 data.iX = data.iX - (fields.size_dec and 1 or -1)
532 craftguide:get_formspec(player_name)
533 elseif (fields.quit) then
534 datas[player_name] = nil
536 for item in pairs(fields) do
537 if item:find(":") then
538 if item:sub(-4) == "_inv" or item:sub(-4) == "_out" then
539 item = item:sub(1,-5)
542 local is_fuel = get_fueltime(item) > 0
543 local recipes = get_recipes(item)
544 if not recipes and not is_fuel then return end
546 if item == data.item then
547 if data.recipes_item and #data.recipes_item >= 2 then
548 local recipe = data.recipes_item[data.recipe_num + 1]
549 data.recipe_num = recipe and data.recipe_num + 1 or 1
550 craftguide:get_formspec(player_name)
554 if progressive_mode then
555 local inv = player:get_inventory()
556 local _, has_item = craftguide:recipe_in_inv(inv, item, nil, player:get_player_name())
558 if not has_item then return end
559 recipes = craftguide:recipe_in_inv(inv, item, recipes, player_name)
564 data.recipes_item = recipes
566 craftguide:get_formspec(player_name, is_fuel)
573 function craftguide:on_use(user)
574 if not datas.init_items then
575 craftguide:get_init_items()
578 local player_name = user:get_player_name()
579 local data = datas[player_name]
581 if progressive_mode or not data then
582 datas[player_name] = {filter="", pagenum=1, iX=9}
583 if progressive_mode then
584 craftguide:get_filter_items(
585 datas[player_name], user)
587 craftguide:get_formspec(player_name)
589 show_formspec(player_name, "craftguide", data.formspec)
593 mcl_craftguide.show_craftguide = function(player)
594 craftguide:on_use(player)
597 mt.register_on_player_receive_fields(function(player, formname, fields)
598 if fields.__mcl_craftguide then
599 craftguide:on_use(player)
603 mt.register_on_leaveplayer(function(player)
604 datas[player:get_player_name()] = nil