init.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. local craftguide, datas, mt = {}, {searches = {}}, minetest
  2. local progressive_mode = mt.settings:get_bool("craftguide_progressive_mode")
  3. local get_recipe, get_recipes = mt.get_craft_recipe, mt.get_all_craft_recipes
  4. local get_result, show_formspec = mt.get_craft_result, mt.show_formspec
  5. local reg_items = mt.registered_items
  6. -- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage:
  7. -- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1
  8. local remove, maxn, sort = table.remove, table.maxn, table.sort
  9. local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
  10. local DEFAULT_SIZE, MIN_LIMIT, MAX_LIMIT = 10, 9, 12
  11. local group_stereotypes = {
  12. wool = "wool:white",
  13. dye = "dye:white",
  14. water_bucket = "bucket:bucket_water",
  15. vessel = "vessels:glass_bottle",
  16. coal = "default:coal_lump",
  17. flower = "flowers:dandelion_yellow",
  18. mesecon_conductor_craftable = "mesecons:wire_00000000_off",
  19. }
  20. function craftguide:group_to_item(item)
  21. if item:sub(1,6) == "group:" then
  22. local itemsub = item:sub(7)
  23. if group_stereotypes[itemsub] then
  24. item = group_stereotypes[itemsub]
  25. elseif reg_items["default:" .. itemsub] then
  26. item = item:gsub("group:", "default:")
  27. else
  28. for name, def in pairs(reg_items) do
  29. if def.groups[item:match("[^,:]+$")] then
  30. item = name
  31. end
  32. end
  33. end
  34. end
  35. return item:sub(1,6) == "group:" and "" or item
  36. end
  37. local function extract_groups(str)
  38. if str:sub(1,6) ~= "group:" then return end
  39. return str:sub(7):split(",")
  40. end
  41. local function colorize(str)
  42. -- If client <= 0.4.14, don't colorize for compatibility.
  43. return mt.colorize and mt.colorize("#FFFF00", str) or str
  44. end
  45. local function get_fueltime(item)
  46. return get_result({method = "fuel", width = 1, items = {item}}).time
  47. end
  48. function craftguide:get_tooltip(item, recipe_type, cooktime, groups)
  49. local tooltip, item_desc = "tooltip[" .. item .. ";", ""
  50. local fueltime = get_fueltime(item)
  51. local has_extras = groups or recipe_type == "cooking" or fueltime > 0
  52. if reg_items[item] then
  53. if not groups then
  54. item_desc = reg_items[item].description
  55. end
  56. else
  57. return tooltip .. "Unknown Item (" .. item .. ")]"
  58. end
  59. if groups then
  60. local groupstr = "Any item belonging to the "
  61. for i = 1, #groups do
  62. groupstr = groupstr .. colorize(groups[i]) ..
  63. (groups[i + 1] and " and " or "")
  64. end
  65. tooltip = tooltip .. groupstr .. " group(s)"
  66. end
  67. if recipe_type == "cooking" then
  68. tooltip = tooltip .. item_desc .. "\nCooking time: " .. colorize(cooktime)
  69. end
  70. if fueltime > 0 then
  71. tooltip = tooltip .. item_desc .. "\nBurning time: " .. colorize(fueltime)
  72. end
  73. return has_extras and tooltip .. "]" or ""
  74. end
  75. function craftguide:get_recipe(iY, xoffset, tooltip, item, recipe_num, recipes)
  76. local formspec, recipes_total = "", #recipes
  77. if recipes_total > 1 then
  78. formspec = formspec ..
  79. "button[0," .. (iY + 3.3) .. ";2,1;alternate;Alternate]" ..
  80. "label[0," .. (iY + 2.8) .. ";Recipe " ..
  81. recipe_num .. " of " .. recipes_total .. "]"
  82. end
  83. local recipe_type = recipes[recipe_num].type
  84. local items = recipes[recipe_num].items
  85. local width = recipes[recipe_num].width
  86. if recipe_type == "cooking" or (recipe_type == "normal" and width == 0) then
  87. local icon = recipe_type == "cooking" and "furnace" or "shapeless"
  88. formspec = formspec ..
  89. "image[" .. (xoffset - 0.8) .. "," .. (iY + 1.7) ..
  90. ".5;0.5,0.5;craftguide_" .. icon .. ".png]"
  91. end
  92. if width == 0 then
  93. width = min(3, #items)
  94. end
  95. local rows = ceil(maxn(items) / width)
  96. local btn_size, craftgrid_limit = 1, 5
  97. if recipe_type == "normal" and
  98. width > craftgrid_limit or rows > craftgrid_limit then
  99. formspec = formspec ..
  100. "label[" .. xoffset .. "," .. (iY + 2) ..
  101. ";Recipe is too big to\nbe displayed (" ..
  102. width .. "x" .. rows .. ")]"
  103. else
  104. for i, v in pairs(items) do
  105. local X = (i - 1) % width + xoffset
  106. local Y = ceil(i / width + (iY + 2) - min(2, rows))
  107. if recipe_type == "normal" and width > 3 or rows > 3 then
  108. btn_size = width > 3 and 3 / width or 3 / rows
  109. X = btn_size * (i % width) + xoffset
  110. Y = btn_size * floor((i - 1) / width) + (iY + 3) - min(2, rows)
  111. end
  112. local groups = extract_groups(v)
  113. local label = groups and "\nG" or ""
  114. local item_r = self:group_to_item(v)
  115. local tltip = self:get_tooltip(item_r, recipe_type, width, groups)
  116. formspec = formspec ..
  117. "item_image_button[" .. X .. "," .. (Y + 0.2) .. ";" ..
  118. btn_size .. "," .. btn_size .. ";" .. item_r ..
  119. ";" .. item_r .. ";" .. label .. "]" .. tltip
  120. end
  121. end
  122. local output = recipes[recipe_num].output
  123. return formspec ..
  124. "image[" .. (xoffset - 1) .. "," .. (iY + 2.35) ..
  125. ".12;0.9,0.7;craftguide_arrow.png]" ..
  126. "item_image_button[" .. (xoffset - 2) .. "," .. (iY + 2.2) .. ";1,1;" ..
  127. output .. ";" .. item .. ";]" .. tooltip
  128. end
  129. function craftguide:get_formspec(player_name, is_fuel)
  130. local data = datas[player_name]
  131. local iY = data.iX - 5
  132. local ipp = data.iX * iY
  133. if not data.items then
  134. data.items = datas.init_items
  135. end
  136. data.pagemax = max(1, ceil(#data.items / ipp))
  137. local formspec = "size[" .. (data.iX - 0.35) .. "," .. (iY + 4) .. ";]" .. [[
  138. background[1,1;1,1;craftguide_bg.png;true]
  139. button[2.4,0.23;0.8,0.5;search;?]
  140. button[3.05,0.23;0.8,0.5;clear;X]
  141. tooltip[search;Search]
  142. tooltip[clear;Reset]
  143. tooltip[size_inc;Increase window size]
  144. tooltip[size_dec;Decrease window size]
  145. field_close_on_enter[filter;false] ]] ..
  146. "button[" .. (data.iX * 0.48) .. ",-0.02;0.7,1;size_inc;+]" ..
  147. "button[" .. ((data.iX * 0.48) + 0.5) ..
  148. ",-0.02;0.7,1;size_dec;-]" ..
  149. "button[" .. (data.iX - 3.1) .. ".4,0;0.8,0.95;prev;<]" ..
  150. "label[" .. (data.iX - 2.2) .. ".1,0.18;" ..
  151. colorize(data.pagenum) .. " / " .. data.pagemax .. "]" ..
  152. "button[" .. (data.iX - 1.2) .. ".2,0;0.8,0.95;next;>]" ..
  153. "field[0.3,0.32;2.5,1;filter;;" ..
  154. mt.formspec_escape(data.filter) .. "]"
  155. local even_num = data.iX % 2 == 0
  156. local xoffset = data.iX / 2 + (even_num and 0.5 or 0)
  157. if not next(data.items) then
  158. formspec = formspec ..
  159. "label[" .. (xoffset - (even_num and 1.5 or 1)) .. ",2;No item to show]"
  160. end
  161. local first_item = (data.pagenum - 1) * ipp
  162. for i = first_item, first_item + ipp - 1 do
  163. local name = data.items[i + 1]
  164. if not name then break end
  165. local X = i % data.iX
  166. local Y = (i % ipp - X) / data.iX + 1
  167. formspec = formspec ..
  168. "item_image_button[" .. (X - (X * 0.05)) .. "," .. Y .. ";1.1,1.1;" ..
  169. name .. ";" .. name .. "_inv;]"
  170. end
  171. if data.item and reg_items[data.item] then
  172. local tooltip = self:get_tooltip(data.item)
  173. if not data.recipes_item or (is_fuel and not get_recipe(data.item).items) then
  174. formspec = formspec ..
  175. "image[" .. (xoffset - 1) .. "," .. (iY + 2.35) ..
  176. ".12;0.9,0.7;craftguide_arrow.png]" ..
  177. "item_image_button[" .. xoffset .. "," .. (iY + 2.2) ..
  178. ";1,1;" .. data.item .. ";" .. data.item .. ";]" ..
  179. tooltip ..
  180. "image[" .. (xoffset - 2) .. "," ..
  181. (iY + 2.18) .. ";1,1;craftguide_fire.png]"
  182. else
  183. formspec = formspec ..
  184. self:get_recipe(iY, xoffset, tooltip, data.item,
  185. data.recipe_num, data.recipes_item)
  186. end
  187. end
  188. data.formspec = formspec
  189. show_formspec(player_name, "craftguide", formspec)
  190. end
  191. local function player_has_item(T)
  192. for i = 1, #T do
  193. if T[i] then
  194. return true
  195. end
  196. end
  197. end
  198. local function group_to_items(group)
  199. local items_with_group, counter = {}, 0
  200. for name, def in pairs(reg_items) do
  201. if def.groups[group:sub(7)] then
  202. counter = counter + 1
  203. items_with_group[counter] = name
  204. end
  205. end
  206. return items_with_group
  207. end
  208. local function item_in_inv(inv, item)
  209. return inv:contains_item("main", item)
  210. end
  211. function craftguide:recipe_in_inv(inv, item_name, recipes_f)
  212. local recipes = recipes_f or get_recipes(item_name) or {}
  213. local show_item_recipes = {}
  214. for i = 1, #recipes do
  215. show_item_recipes[i] = true
  216. for _, item in pairs(recipes[i].items) do
  217. local group_in_inv = false
  218. if item:sub(1,6) == "group:" then
  219. local groups = group_to_items(item)
  220. for j = 1, #groups do
  221. if item_in_inv(inv, groups[j]) then
  222. group_in_inv = true
  223. end
  224. end
  225. end
  226. if not group_in_inv and not item_in_inv(inv, item) then
  227. show_item_recipes[i] = false
  228. end
  229. end
  230. end
  231. for i = #show_item_recipes, 1, -1 do
  232. if not show_item_recipes[i] then
  233. remove(recipes, i)
  234. end
  235. end
  236. return recipes, player_has_item(show_item_recipes)
  237. end
  238. function craftguide:get_init_items()
  239. local items_list, counter = {}, 0
  240. for name, def in pairs(reg_items) do
  241. local is_fuel = get_fueltime(name) > 0
  242. if not (def.groups.not_in_creative_inventory == 1) and
  243. (get_recipe(name).items or is_fuel) and
  244. def.description and def.description ~= "" then
  245. counter = counter + 1
  246. items_list[counter] = name
  247. end
  248. end
  249. sort(items_list)
  250. datas.init_items = items_list
  251. end
  252. function craftguide:get_filter_items(data, player)
  253. local filter = data.filter
  254. if datas.searches[filter] then
  255. data.items = datas.searches[filter]
  256. return
  257. end
  258. local items_list = progressive_mode and data.init_filter_items or datas.init_items
  259. local inv = player:get_inventory()
  260. local filtered_list, counter = {}, 0
  261. for i = 1, #items_list do
  262. local item = items_list[i]
  263. local item_desc = reg_items[item].description:lower()
  264. if filter ~= "" then
  265. if item:find(filter, 1, true) or item_desc:find(filter, 1, true) then
  266. counter = counter + 1
  267. filtered_list[counter] = item
  268. end
  269. elseif progressive_mode then
  270. local _, has_item = self:recipe_in_inv(inv, item)
  271. if has_item then
  272. counter = counter + 1
  273. filtered_list[counter] = item
  274. end
  275. end
  276. end
  277. if progressive_mode then
  278. if not data.items then
  279. data.init_filter_items = filtered_list
  280. end
  281. elseif filter ~= "" and not datas.searches[filter] then
  282. datas.searches[filter] = filtered_list
  283. end
  284. data.items = filtered_list
  285. end
  286. mt.register_on_player_receive_fields(function(player, formname, fields)
  287. if formname ~= "craftguide" then return end
  288. local player_name = player:get_player_name()
  289. local data = datas[player_name]
  290. if fields.clear then
  291. data.filter, data.item, data.pagenum, data.recipe_num = "", nil, 1, 1
  292. data.items = progressive_mode and data.init_filter_items or datas.init_items
  293. craftguide:get_formspec(player_name)
  294. elseif fields.alternate then
  295. local recipe = data.recipes_item[data.recipe_num + 1]
  296. data.recipe_num = recipe and data.recipe_num + 1 or 1
  297. craftguide:get_formspec(player_name)
  298. elseif (fields.key_enter_field == "filter" or fields.search) and
  299. fields.filter ~= "" then
  300. data.filter = fields.filter:lower()
  301. data.pagenum = 1
  302. craftguide:get_filter_items(data, player)
  303. craftguide:get_formspec(player_name)
  304. elseif fields.prev or fields.next then
  305. data.pagenum = data.pagenum - (fields.prev and 1 or -1)
  306. if data.pagenum > data.pagemax then
  307. data.pagenum = 1
  308. elseif data.pagenum == 0 then
  309. data.pagenum = data.pagemax
  310. end
  311. craftguide:get_formspec(player_name)
  312. elseif (fields.size_inc and data.iX < MAX_LIMIT) or
  313. (fields.size_dec and data.iX > MIN_LIMIT) then
  314. data.pagenum = 1
  315. data.iX = data.iX - (fields.size_dec and 1 or -1)
  316. craftguide:get_formspec(player_name)
  317. else for item in pairs(fields) do
  318. if item:find(":") then
  319. if item:sub(-4) == "_inv" then
  320. item = item:sub(1,-5)
  321. end
  322. local is_fuel = get_fueltime(item) > 0
  323. local recipes = get_recipes(item)
  324. if not recipes and not is_fuel then return end
  325. if item == data.item then
  326. if data.recipes_item and #data.recipes_item >= 2 then
  327. local recipe = data.recipes_item[data.recipe_num + 1]
  328. data.recipe_num = recipe and data.recipe_num + 1 or 1
  329. craftguide:get_formspec(player_name)
  330. end
  331. else
  332. if progressive_mode then
  333. local inv = player:get_inventory()
  334. local _, has_item = craftguide:recipe_in_inv(inv, item)
  335. if not has_item then return end
  336. recipes = craftguide:recipe_in_inv(inv, item, recipes)
  337. end
  338. data.item = item
  339. data.recipe_num = 1
  340. data.recipes_item = recipes
  341. craftguide:get_formspec(player_name, is_fuel)
  342. end
  343. end
  344. end
  345. end
  346. end)
  347. function craftguide:on_use(itemstack, user)
  348. if not datas.init_items then
  349. craftguide:get_init_items()
  350. end
  351. local player_name = user:get_player_name()
  352. local data = datas[player_name]
  353. if progressive_mode or not data then
  354. datas[player_name] = {filter = "", pagenum = 1, iX = DEFAULT_SIZE}
  355. if progressive_mode then
  356. craftguide:get_filter_items(datas[player_name], user)
  357. end
  358. craftguide:get_formspec(player_name)
  359. else
  360. show_formspec(player_name, "craftguide", data.formspec)
  361. end
  362. end
  363. mt.register_craftitem("craftguide:book", {
  364. description = "Crafting Guide",
  365. inventory_image = "craftguide_book.png",
  366. wield_image = "craftguide_book.png",
  367. stack_max = 1,
  368. groups = {book = 1},
  369. on_use = function(itemstack, user)
  370. craftguide:on_use(itemstack, user)
  371. end
  372. })
  373. mt.register_node("craftguide:sign", {
  374. description = "Crafting Guide Sign",
  375. drawtype = "nodebox",
  376. tiles = {"craftguide_sign.png"},
  377. inventory_image = "craftguide_sign_inv.png",
  378. wield_image = "craftguide_sign_inv.png",
  379. paramtype = "light",
  380. paramtype2 = "wallmounted",
  381. sunlight_propagates = true,
  382. groups = {wood = 1, oddly_breakable_by_hand = 1, flammable = 3},
  383. node_box = {
  384. type = "wallmounted",
  385. wall_top = {-0.4375, 0.4375, -0.3125, 0.4375, 0.5, 0.3125},
  386. wall_bottom = {-0.4375, -0.5, -0.3125, 0.4375, -0.4375, 0.3125},
  387. wall_side = {-0.5, -0.3125, -0.4375, -0.4375, 0.3125, 0.4375}
  388. },
  389. on_construct = function(pos)
  390. local meta = minetest.get_meta(pos)
  391. meta:set_string("infotext", "Crafting Guide Sign")
  392. end,
  393. on_rightclick = function(pos, node, user, itemstack)
  394. craftguide:on_use(itemstack, user)
  395. end
  396. })
  397. mt.register_craft({
  398. output = "craftguide:book",
  399. type = "shapeless",
  400. recipe = {"default:book"}
  401. })
  402. mt.register_craft({
  403. type = "fuel",
  404. recipe = "craftguide:book",
  405. burntime = 3
  406. })
  407. mt.register_craft({
  408. output = "craftguide:sign",
  409. type = "shapeless",
  410. recipe = {"default:sign_wall_wood"}
  411. })
  412. mt.register_craft({
  413. type = "fuel",
  414. recipe = "craftguide:sign",
  415. burntime = 10
  416. })
  417. if rawget(_G, "sfinv_buttons") then
  418. sfinv_buttons.register_button("craftguide", {
  419. title = "Crafting guide",
  420. tooltip = "Shows a list of available crafting recipes, cooking recipes and fuels",
  421. action = function(player)
  422. craftguide:on_use(nil, player)
  423. end,
  424. image = "craftguide_book.png",
  425. })
  426. end
  427. mt.register_alias("xdecor:crafting_guide", "craftguide:book")