An extensible inventory mod which allows searching crafting and browsing for recipes in the same dialogue. https://content.minetest.net/packages/RealBadAngel/unified_inventory/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

490 lines
17KB

  1. local S = minetest.get_translator("unified_inventory")
  2. local NS = function(s) return s end
  3. local F = minetest.formspec_escape
  4. minetest.register_privilege("creative", {
  5. description = S("Can use the creative inventory"),
  6. give_to_singleplayer = false,
  7. })
  8. minetest.register_privilege("ui_full", {
  9. description = S("Forces Unified Inventory to be displayed in Full mode if Lite mode is configured globally"),
  10. give_to_singleplayer = false,
  11. })
  12. local trash = minetest.create_detached_inventory("trash", {
  13. --allow_put = function(inv, listname, index, stack, player)
  14. -- if unified_inventory.is_creative(player:get_player_name()) then
  15. -- return stack:get_count()
  16. -- else
  17. -- return 0
  18. -- end
  19. --end,
  20. on_put = function(inv, listname, index, stack, player)
  21. inv:set_stack(listname, index, nil)
  22. local player_name = player:get_player_name()
  23. minetest.sound_play("trash", {to_player=player_name, gain = 1.0})
  24. end,
  25. })
  26. trash:set_size("main", 1)
  27. unified_inventory.register_button("craft", {
  28. type = "image",
  29. image = "ui_craft_icon.png",
  30. tooltip = S("Crafting Grid")
  31. })
  32. unified_inventory.register_button("craftguide", {
  33. type = "image",
  34. image = "ui_craftguide_icon.png",
  35. tooltip = S("Crafting Guide")
  36. })
  37. unified_inventory.register_button("home_gui_set", {
  38. type = "image",
  39. image = "ui_sethome_icon.png",
  40. tooltip = S("Set home position"),
  41. hide_lite=true,
  42. action = function(player)
  43. local player_name = player:get_player_name()
  44. if minetest.check_player_privs(player_name, {home=true}) then
  45. unified_inventory.set_home(player, player:get_pos())
  46. local home = unified_inventory.home_pos[player_name]
  47. if home ~= nil then
  48. minetest.sound_play("dingdong",
  49. {to_player=player_name, gain = 1.0})
  50. minetest.chat_send_player(player_name,
  51. S("Home position set to: @1", minetest.pos_to_string(home)))
  52. end
  53. else
  54. minetest.chat_send_player(player_name,
  55. S("You don't have the \"home\" privilege!"))
  56. unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
  57. end
  58. end,
  59. condition = function(player)
  60. return minetest.check_player_privs(player:get_player_name(), {home=true})
  61. end,
  62. })
  63. unified_inventory.register_button("home_gui_go", {
  64. type = "image",
  65. image = "ui_gohome_icon.png",
  66. tooltip = S("Go home"),
  67. hide_lite=true,
  68. action = function(player)
  69. local player_name = player:get_player_name()
  70. if minetest.check_player_privs(player_name, {home=true}) then
  71. minetest.sound_play("teleport",
  72. {to_player=player:get_player_name(), gain = 1.0})
  73. unified_inventory.go_home(player)
  74. else
  75. minetest.chat_send_player(player_name,
  76. S("You don't have the \"home\" privilege!"))
  77. unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
  78. end
  79. end,
  80. condition = function(player)
  81. return minetest.check_player_privs(player:get_player_name(), {home=true})
  82. end,
  83. })
  84. unified_inventory.register_button("misc_set_day", {
  85. type = "image",
  86. image = "ui_sun_icon.png",
  87. tooltip = S("Set time to day"),
  88. hide_lite=true,
  89. action = function(player)
  90. local player_name = player:get_player_name()
  91. if minetest.check_player_privs(player_name, {settime=true}) then
  92. minetest.sound_play("birds",
  93. {to_player=player_name, gain = 1.0})
  94. minetest.set_timeofday((6000 % 24000) / 24000)
  95. minetest.chat_send_player(player_name,
  96. S("Time of day set to 6am"))
  97. else
  98. minetest.chat_send_player(player_name,
  99. S("You don't have the settime privilege!"))
  100. unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
  101. end
  102. end,
  103. condition = function(player)
  104. return minetest.check_player_privs(player:get_player_name(), {settime=true})
  105. end,
  106. })
  107. unified_inventory.register_button("misc_set_night", {
  108. type = "image",
  109. image = "ui_moon_icon.png",
  110. tooltip = S("Set time to night"),
  111. hide_lite=true,
  112. action = function(player)
  113. local player_name = player:get_player_name()
  114. if minetest.check_player_privs(player_name, {settime=true}) then
  115. minetest.sound_play("owl",
  116. {to_player=player_name, gain = 1.0})
  117. minetest.set_timeofday((21000 % 24000) / 24000)
  118. minetest.chat_send_player(player_name,
  119. S("Time of day set to 9pm"))
  120. else
  121. minetest.chat_send_player(player_name,
  122. S("You don't have the settime privilege!"))
  123. unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
  124. end
  125. end,
  126. condition = function(player)
  127. return minetest.check_player_privs(player:get_player_name(), {settime=true})
  128. end,
  129. })
  130. unified_inventory.register_button("clear_inv", {
  131. type = "image",
  132. image = "ui_trash_icon.png",
  133. tooltip = S("Clear inventory"),
  134. action = function(player)
  135. local player_name = player:get_player_name()
  136. if not unified_inventory.is_creative(player_name) then
  137. minetest.chat_send_player(player_name,
  138. S("This button has been disabled outside"
  139. .." of creative mode to prevent"
  140. .." accidental inventory trashing."
  141. .."\nUse the trash slot instead."))
  142. unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
  143. return
  144. end
  145. player:get_inventory():set_list("main", {})
  146. minetest.chat_send_player(player_name, S('Inventory cleared!'))
  147. minetest.sound_play("trash_all",
  148. {to_player=player_name, gain = 1.0})
  149. end,
  150. condition = function(player)
  151. return unified_inventory.is_creative(player:get_player_name())
  152. end,
  153. })
  154. unified_inventory.register_page("craft", {
  155. get_formspec = function(player, perplayer_formspec)
  156. local formspecy = perplayer_formspec.formspec_y
  157. local formheadery = perplayer_formspec.form_header_y
  158. local player_name = player:get_player_name()
  159. local formspec = "background[2,"..formspecy..";6,3;ui_crafting_form.png]"
  160. formspec = formspec.."background[0,"..(formspecy + 3.5)..";8,4;ui_main_inventory.png]"
  161. formspec = formspec.."label[0,"..formheadery..";" ..F(S("Crafting")).."]"
  162. formspec = formspec.."listcolors[#00000000;#00000000]"
  163. formspec = formspec.."list[current_player;craftpreview;6,"..formspecy..";1,1;]"
  164. formspec = formspec.."list[current_player;craft;2,"..formspecy..";3,3;]"
  165. if unified_inventory.trash_enabled or unified_inventory.is_creative(player_name) or minetest.get_player_privs(player_name).give then
  166. formspec = formspec.."label[7,"..(formspecy + 1.5)..";" .. F(S("Trash:")) .. "]"
  167. formspec = formspec.."background[7,"..(formspecy + 2)..";1,1;ui_single_slot.png]"
  168. formspec = formspec.."list[detached:trash;main;7,"..(formspecy + 2)..";1,1;]"
  169. end
  170. formspec = formspec.."listring[current_name;craft]"
  171. formspec = formspec.."listring[current_player;main]"
  172. if unified_inventory.is_creative(player_name) then
  173. formspec = formspec.."label[0,"..(formspecy + 1.5)..";" .. F(S("Refill:")) .. "]"
  174. formspec = formspec.."list[detached:"..F(player_name).."refill;main;0,"..(formspecy +2)..";1,1;]"
  175. end
  176. return {formspec=formspec}
  177. end,
  178. })
  179. -- stack_image_button(): generate a form button displaying a stack of items
  180. --
  181. -- The specified item may be a group. In that case, the group will be
  182. -- represented by some item in the group, along with a flag indicating
  183. -- that it's a group. If the group contains only one item, it will be
  184. -- treated as if that item had been specified directly.
  185. local function stack_image_button(x, y, w, h, buttonname_prefix, item)
  186. local name = item:get_name()
  187. local count = item:get_count()
  188. local show_is_group = false
  189. local displayitem = name.." "..count
  190. local selectitem = name
  191. if name:sub(1, 6) == "group:" then
  192. local group_name = name:sub(7)
  193. local group_item = unified_inventory.get_group_item(group_name)
  194. show_is_group = not group_item.sole
  195. displayitem = group_item.item or "unknown"
  196. selectitem = group_item.sole and displayitem or name
  197. end
  198. local label = show_is_group and "G" or ""
  199. local buttonname = F(buttonname_prefix..unified_inventory.mangle_for_formspec(selectitem))
  200. local button = string.format("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
  201. x, y, w, h,
  202. F(displayitem), buttonname, label)
  203. if show_is_group then
  204. local groupstring, andcount = unified_inventory.extract_groupnames(name)
  205. local grouptip
  206. if andcount == 1 then
  207. grouptip = S("Any item belonging to the @1 group", groupstring)
  208. elseif andcount > 1 then
  209. grouptip = S("Any item belonging to the groups @1", groupstring)
  210. end
  211. grouptip = F(grouptip)
  212. if andcount >= 1 then
  213. button = button .. string.format("tooltip[%s;%s]", buttonname, grouptip)
  214. end
  215. end
  216. return button
  217. end
  218. local recipe_text = {
  219. recipe = NS("Recipe @1 of @2"),
  220. usage = NS("Usage @1 of @2"),
  221. }
  222. local no_recipe_text = {
  223. recipe = S("No recipes"),
  224. usage = S("No usages"),
  225. }
  226. local role_text = {
  227. recipe = S("Result"),
  228. usage = S("Ingredient"),
  229. }
  230. local next_alt_text = {
  231. recipe = S("Show next recipe"),
  232. usage = S("Show next usage"),
  233. }
  234. local prev_alt_text = {
  235. recipe = S("Show previous recipe"),
  236. usage = S("Show previous usage"),
  237. }
  238. local other_dir = {
  239. recipe = "usage",
  240. usage = "recipe",
  241. }
  242. unified_inventory.register_page("craftguide", {
  243. get_formspec = function(player, perplayer_formspec)
  244. local formspecy = perplayer_formspec.formspec_y
  245. local formheadery = perplayer_formspec.form_header_y
  246. local craftresultx = perplayer_formspec.craft_result_x
  247. local craftresulty = perplayer_formspec.craft_result_y
  248. local player_name = player:get_player_name()
  249. local player_privs = minetest.get_player_privs(player_name)
  250. local fs = {
  251. "background[0,"..(formspecy + 3.5)..";8,4;ui_main_inventory.png]",
  252. "label[0,"..formheadery..";" .. F(S("Crafting Guide")) .. "]",
  253. "listcolors[#00000000;#00000000]"
  254. }
  255. local item_name = unified_inventory.current_item[player_name]
  256. if not item_name then
  257. return { formspec = table.concat(fs) }
  258. end
  259. local item_name_shown
  260. if minetest.registered_items[item_name]
  261. and minetest.registered_items[item_name].description then
  262. item_name_shown = S("@1 (@2)",
  263. minetest.registered_items[item_name].description, item_name)
  264. else
  265. item_name_shown = item_name
  266. end
  267. local dir = unified_inventory.current_craft_direction[player_name]
  268. local rdir = dir == "recipe" and "usage" or "recipe"
  269. local crafts = unified_inventory.crafts_for[dir][item_name]
  270. local alternate = unified_inventory.alternate[player_name]
  271. local alternates, craft
  272. if crafts and #crafts > 0 then
  273. alternates = #crafts
  274. craft = crafts[alternate]
  275. end
  276. local has_give = player_privs.give or unified_inventory.is_creative(player_name)
  277. fs[#fs + 1] = "background[0.5,"..(formspecy + 0.2)..";8,3;ui_craftguide_form.png]"
  278. fs[#fs + 1] = string.format("textarea[%f,%f;10,1;;%s: %s;]",
  279. craftresultx, craftresulty, F(role_text[dir]), item_name_shown)
  280. fs[#fs + 1] = stack_image_button(0, formspecy, 1.1, 1.1,
  281. "item_button_" .. rdir .. "_", ItemStack(item_name))
  282. if not craft then
  283. -- No craft recipes available for this item.
  284. fs[#fs + 1] = "label[5.5,"..(formspecy + 2.35)..";"
  285. .. F(no_recipe_text[dir]) .. "]"
  286. local no_pos = dir == "recipe" and 4.5 or 6.5
  287. local item_pos = dir == "recipe" and 6.5 or 4.5
  288. fs[#fs + 1] = "image["..no_pos..","..formspecy..";1.1,1.1;ui_no.png]"
  289. fs[#fs + 1] = stack_image_button(item_pos, formspecy, 1.1, 1.1,
  290. "item_button_" .. other_dir[dir] .. "_", ItemStack(item_name))
  291. if has_give then
  292. fs[#fs + 1] = "label[0," .. (formspecy + 2.10) .. ";" .. F(S("Give me:")) .. "]"
  293. .. "button[0, " .. (formspecy + 2.7) .. ";0.6,0.5;craftguide_giveme_1;1]"
  294. .. "button[0.6," .. (formspecy + 2.7) .. ";0.7,0.5;craftguide_giveme_10;10]"
  295. .. "button[1.3," .. (formspecy + 2.7) .. ";0.8,0.5;craftguide_giveme_99;99]"
  296. end
  297. return { formspec = table.concat(fs) }
  298. end
  299. local craft_type = unified_inventory.registered_craft_types[craft.type] or
  300. unified_inventory.craft_type_defaults(craft.type, {})
  301. if craft_type.icon then
  302. fs[#fs + 1] = string.format("image[%f,%f;%f,%f;%s]",
  303. 5.7, (formspecy + 0.05), 0.5, 0.5, craft_type.icon)
  304. end
  305. fs[#fs + 1] = "label[5.5,"..(formspecy + 1)..";" .. F(craft_type.description).."]"
  306. fs[#fs + 1] = stack_image_button(6.5, formspecy, 1.1, 1.1,
  307. "item_button_usage_", ItemStack(craft.output))
  308. local display_size = craft_type.dynamic_display_size
  309. and craft_type.dynamic_display_size(craft)
  310. or { width = craft_type.width, height = craft_type.height }
  311. local craft_width = craft_type.get_shaped_craft_width
  312. and craft_type.get_shaped_craft_width(craft)
  313. or display_size.width
  314. -- This keeps recipes aligned to the right,
  315. -- so that they're close to the arrow.
  316. local xoffset = 5.5
  317. -- Offset factor for crafting grids with side length > 4
  318. local of = (3/math.max(3, math.max(display_size.width, display_size.height)))
  319. local od = 0
  320. -- Minimum grid size at which size optimazation measures kick in
  321. local mini_craft_size = 6
  322. if display_size.width >= mini_craft_size then
  323. od = math.max(1, display_size.width - 2)
  324. xoffset = xoffset - 0.1
  325. end
  326. -- Size modifier factor
  327. local sf = math.min(1, of * (1.05 + 0.05*od))
  328. -- Button size
  329. local bsize_h = 1.1 * sf
  330. local bsize_w = bsize_h
  331. if display_size.width >= mini_craft_size then
  332. bsize_w = 1.175 * sf
  333. end
  334. if (bsize_h > 0.35 and display_size.width) then
  335. for y = 1, display_size.height do
  336. for x = 1, display_size.width do
  337. local item
  338. if craft and x <= craft_width then
  339. item = craft.items[(y-1) * craft_width + x]
  340. end
  341. -- Flipped x, used to build formspec buttons from right to left
  342. local fx = display_size.width - (x-1)
  343. -- x offset, y offset
  344. local xof = (fx-1) * of + of
  345. local yof = (y-1) * of + 1
  346. if item then
  347. fs[#fs + 1] = stack_image_button(
  348. xoffset - xof, formspecy - 1 + yof, bsize_w, bsize_h,
  349. "item_button_recipe_",
  350. ItemStack(item))
  351. else
  352. -- Fake buttons just to make grid
  353. fs[#fs + 1] = string.format("image_button[%f,%f;%f,%f;ui_blank_image.png;;]",
  354. xoffset - xof, formspecy - 1 + yof, bsize_w, bsize_h)
  355. end
  356. end
  357. end
  358. else
  359. -- Error
  360. fs[#fs + 1] = string.format("label[2,%f;%s]",
  361. formspecy, F(S("This recipe is too\nlarge to be displayed.")))
  362. end
  363. if craft_type.uses_crafting_grid and display_size.width <= 3 then
  364. fs[#fs + 1] = "label[0," .. (formspecy + 0.9) .. ";" .. F(S("To craft grid:")) .. "]"
  365. .. "button[0, " .. (formspecy + 1.5) .. ";0.6,0.5;craftguide_craft_1;1]"
  366. .. "button[0.6," .. (formspecy + 1.5) .. ";0.7,0.5;craftguide_craft_10;10]"
  367. .. "button[1.3," .. (formspecy + 1.5) .. ";0.8,0.5;craftguide_craft_max;" .. F(S("All")) .. "]"
  368. end
  369. if has_give then
  370. fs[#fs + 1] = "label[0," .. (formspecy + 2.1) .. ";" .. F(S("Give me:")) .. "]"
  371. .. "button[0, " .. (formspecy + 2.7) .. ";0.6,0.5;craftguide_giveme_1;1]"
  372. .. "button[0.6," .. (formspecy + 2.7) .. ";0.7,0.5;craftguide_giveme_10;10]"
  373. .. "button[1.3," .. (formspecy + 2.7) .. ";0.8,0.5;craftguide_giveme_99;99]"
  374. end
  375. if alternates and alternates > 1 then
  376. fs[#fs + 1] = "label[5.5," .. (formspecy + 1.6) .. ";"
  377. .. F(S(recipe_text[dir], alternate, alternates)) .. "]"
  378. .. "image_button[5.5," .. (formspecy + 2) .. ";1,1;ui_left_icon.png;alternate_prev;]"
  379. .. "image_button[6.5," .. (formspecy + 2) .. ";1,1;ui_right_icon.png;alternate;]"
  380. .. "tooltip[alternate_prev;" .. F(prev_alt_text[dir]) .. "]"
  381. .. "tooltip[alternate;" .. F(next_alt_text[dir]) .. "]"
  382. end
  383. return { formspec = table.concat(fs) }
  384. end,
  385. })
  386. local function craftguide_giveme(player, formname, fields)
  387. local player_name = player:get_player_name()
  388. local player_privs = minetest.get_player_privs(player_name)
  389. if not player_privs.give and
  390. not unified_inventory.is_creative(player_name) then
  391. minetest.log("action", "[unified_inventory] Denied give action to player " ..
  392. player_name)
  393. return
  394. end
  395. local amount
  396. for k, v in pairs(fields) do
  397. amount = k:match("craftguide_giveme_(.*)")
  398. if amount then break end
  399. end
  400. amount = tonumber(amount) or 0
  401. if amount == 0 then return end
  402. local output = unified_inventory.current_item[player_name]
  403. if (not output) or (output == "") then return end
  404. local player_inv = player:get_inventory()
  405. player_inv:add_item("main", {name = output, count = amount})
  406. end
  407. local function craftguide_craft(player, formname, fields)
  408. local amount
  409. for k, v in pairs(fields) do
  410. amount = k:match("craftguide_craft_(.*)")
  411. if amount then break end
  412. end
  413. if not amount then return end
  414. amount = tonumber(amount) or -1 -- fallback for "all"
  415. if amount == 0 or amount < -1 or amount > 99 then return end
  416. local player_name = player:get_player_name()
  417. local output = unified_inventory.current_item[player_name] or ""
  418. if output == "" then return end
  419. local crafts = unified_inventory.crafts_for[
  420. unified_inventory.current_craft_direction[player_name]][output] or {}
  421. if #crafts == 0 then return end
  422. local alternate = unified_inventory.alternate[player_name]
  423. local craft = crafts[alternate]
  424. if craft.width > 3 then return end
  425. unified_inventory.craftguide_match_craft(player, "main", "craft", craft, amount)
  426. unified_inventory.set_inventory_formspec(player, "craft")
  427. end
  428. minetest.register_on_player_receive_fields(function(player, formname, fields)
  429. if formname ~= "" then
  430. return
  431. end
  432. for k, v in pairs(fields) do
  433. if k:match("craftguide_craft_") then
  434. craftguide_craft(player, formname, fields)
  435. return
  436. end
  437. if k:match("craftguide_giveme_") then
  438. craftguide_giveme(player, formname, fields)
  439. return
  440. end
  441. end
  442. end)