init.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. --------------------------------------------------------------------------------
  2. -- Inventory Plus for Minetest
  3. --
  4. -- Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
  5. -- Source Code: https://github.com/cornernote/minetest-inventory_plus
  6. -- License: BSD-3-Clause https://raw.github.com/cornernote/minetest-inventory_plus/master/LICENSE
  7. --
  8. -- Edited by TenPlus1 (23rd March 2016)
  9. -- Edited and packaged for MTS (Must Test Survival) by GoldFireUn (5th September 2018)
  10. --------------------------------------------------------------------------------
  11. -- Expose API.
  12. inventory_plus = inventory_plus or {}
  13. inventory_plus.modpath = minetest.get_modpath("inventory_plus")
  14. -- Define buttons.
  15. inventory_plus.buttons = inventory_plus.buttons or {}
  16. -- Register button. Buttons are stored globally, NOT per-player.
  17. function inventory_plus.register_button(button, label)
  18. inventory_plus.buttons[button] = label
  19. end
  20. -- Set inventory formspec.
  21. function inventory_plus.set_inventory_formspec(player, formspec)
  22. -- Error checking.
  23. if not player or not formspec then
  24. return
  25. end
  26. player:set_inventory_formspec(formspec)
  27. end
  28. -- Get player formspec.
  29. function inventory_plus.get_formspec()
  30. -- Default inventory page.
  31. local formspec = "size[8,8.5]"
  32. .. default.gui_bg
  33. .. default.gui_bg_img
  34. .. default.gui_slots
  35. .. "list[current_player;main;0,4.25;8,1;]"
  36. .. "list[current_player;main;0,5.5;8,3;8]"
  37. .. default.get_hotbar_bg(0, 4.25)
  38. -- Obtain hooks into the trash mod's trash slot inventory.
  39. local ltrash, mtrash = trash.get_listname()
  40. local itrash = trash.get_iconname()
  41. formspec = formspec
  42. .. "list[current_player;craft;3,0.5;3,3;]"
  43. .. "list[current_player;craftpreview;7,1.5;1,1;]"
  44. .. "image[6,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"
  45. .. "listring[current_name;craft]"
  46. .. "listring[current_player;main]"
  47. -- Trash icon.
  48. .. "list[" .. ltrash .. ";" .. mtrash .. ";7,0.5;1,1;]"
  49. .. "image[7,0.5;1,1;" .. itrash .. "]"
  50. -- Positions for individual buttons.
  51. local positions = {
  52. armor = {0, 1},
  53. bags = {1, 1},
  54. skins = {0, 0},
  55. zcg = {1, 0},
  56. }
  57. local ox, oy = 0.0, 0.5
  58. -- Install buttons.
  59. local buttons = inventory_plus.buttons
  60. for k, v in pairs(buttons) do
  61. -- 'k' is element name, 'v' is display label.
  62. if positions[k] then
  63. local x, y = ox + positions[k][1], oy + positions[k][2]
  64. formspec = formspec .. "image_button[" .. x .. "," .. y ..
  65. ";1,1;inventory_plus_" .. k .. ".png;" .. k .. ";]" ..
  66. "tooltip[" .. k .. ";" .. v .. "]" -- Give each button a tooltip.
  67. end
  68. end
  69. return formspec
  70. end
  71. function inventory_plus.on_joinplayer(player)
  72. inventory_plus.set_inventory_formspec(player,
  73. inventory_plus.get_formspec())
  74. end
  75. function inventory_plus.on_receive_fields(player, formname, fields)
  76. -- Main. This button should exist in any formspec that replaces this one.
  77. -- Otherwise, players will not be able to navigate back!
  78. if fields.main then
  79. inventory_plus.set_inventory_formspec(player,
  80. inventory_plus.get_formspec())
  81. return
  82. end
  83. end
  84. -- This piece of code just rebuilds the inventory formspecs whenever the mod is
  85. -- reloaded. This helps with formspec/code development.
  86. local function rebuild_all()
  87. local players = minetest.get_connected_players()
  88. for k, v in ipairs(players) do
  89. inventory_plus.set_inventory_formspec(v,
  90. inventory_plus.get_formspec())
  91. end
  92. end
  93. -- Execute!
  94. rebuild_all()
  95. if not inventory_plus.registered then
  96. minetest.register_on_joinplayer(function(...)
  97. return inventory_plus.on_joinplayer(...)
  98. end)
  99. minetest.register_on_player_receive_fields(function(...)
  100. return inventory_plus.on_receive_fields(...)
  101. end)
  102. local c = "inventory_plus:core"
  103. local f = inventory_plus.modpath .. "/init.lua"
  104. reload.register_file(c, f, false)
  105. inventory_plus.registered = true
  106. end