init.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. --[[
  2. Inventory Plus+ for Minetest
  3. Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
  4. Copyright (c) 2013 Zeg9 <dazeg9@gmail.com>
  5. Source Code: https://github.com/Zeg9/minetest-inventory_plus
  6. License: GPLv3
  7. ]]--
  8. --[[
  9. TODO:
  10. * Include a few button textures, especially for "abandoned" mods
  11. -> Done: bags
  12. -> Todo: home GUI, mobf settings (if it still exists),...
  13. * Limit the number of buttons displayed, and then:
  14. * Multiple button pages (inventory can only display 9 buttons, and creative 6)
  15. * Fallback to text if no image is present ?
  16. ]]--
  17. -- expose api
  18. inventory_plus = {}
  19. -- tell that we are inventory++, not inventory_plus
  20. -- ...so mods know if they can use our functions like remove_button
  21. inventory_plus.plusplus = true
  22. -- define buttons
  23. inventory_plus.buttons = {}
  24. -- default inventory page
  25. inventory_plus.default = minetest.settings:get("inventory_default") or "main"
  26. -- original inventory formspec, per player
  27. inventory_plus.inventory = {}
  28. -- register_button
  29. inventory_plus.register_button = function(player,name,...)
  30. local player_name = player:get_player_name()
  31. if inventory_plus.buttons[player_name] == nil then
  32. inventory_plus.buttons[player_name] = {}
  33. end
  34. for _, i in ipairs(inventory_plus.buttons[player_name]) do
  35. if i == name then return end -- only register buttons once
  36. end
  37. table.insert(inventory_plus.buttons[player_name], name)
  38. end
  39. inventory_plus.remove_button = function(player,name)
  40. local player_name = player:get_player_name()
  41. if inventory_plus.buttons[player_name] == nil then
  42. inventory_plus.buttons[player_name] = {}
  43. end
  44. local index = nil
  45. for i, n in ipairs(inventory_plus.buttons[player_name]) do
  46. if n == name then
  47. index = i
  48. end
  49. end
  50. table.remove(inventory_plus.buttons[player_name], index)
  51. end
  52. -- set_inventory_formspec
  53. inventory_plus.set_inventory_formspec = function(player,formspec)
  54. if minetest.settings:get_bool("creative_mode") then
  55. -- if creative mode is on then wait a bit
  56. minetest.after(0.01,function()
  57. player:set_inventory_formspec(formspec)
  58. end)
  59. else
  60. player:set_inventory_formspec(formspec)
  61. end
  62. end
  63. -- get_formspec
  64. inventory_plus.get_formspec = function(player,page)
  65. local get_buttons = function(ox,oy,mx) -- origin x, origin y, max x
  66. if not inventory_plus.buttons[player:get_player_name()] then
  67. return ""
  68. end
  69. local formspec = ""
  70. local x,y=ox,oy
  71. for _, i in ipairs(inventory_plus.buttons[player:get_player_name()]) do
  72. formspec = formspec .. "image_button["..x..","..y..";1,1;inventory_plus_"..i..".png;"..i..";]"
  73. x=x+1
  74. if x >= ox+mx then
  75. y = y+1
  76. x = ox
  77. end
  78. end
  79. return formspec
  80. end
  81. -- craft page
  82. if page=="main" then
  83. if minetest.settings:get_bool("creative_mode") then
  84. return player:get_inventory_formspec()
  85. .. get_buttons(6,0,2)
  86. else
  87. return inventory_plus.inventory[player:get_player_name()]
  88. .. get_buttons(0,0,3)
  89. end
  90. end
  91. end
  92. -- register_on_joinplayer
  93. minetest.register_on_joinplayer(function(player)
  94. if inventory_plus.inventory[player:get_player_name()] == nil then
  95. inventory_plus.inventory[player:get_player_name()] = player:get_inventory_formspec()
  96. end
  97. minetest.after(1,function()
  98. if not player:is_player() then return end
  99. inventory_plus.set_inventory_formspec(player,inventory_plus.get_formspec(player, inventory_plus.default))
  100. end)
  101. end)
  102. -- register_on_player_receive_fields
  103. minetest.register_on_player_receive_fields(function(player, formname, fields)
  104. -- main
  105. if fields.main then
  106. if minetest.settings:get_bool("creative_mode") then
  107. minetest.after(0.01,function()
  108. inventory_plus.set_inventory_formspec(player, inventory_plus.get_formspec(player,"main"))
  109. end)
  110. else
  111. inventory_plus.set_inventory_formspec(player, inventory_plus.get_formspec(player,"main"))
  112. end
  113. return
  114. end
  115. if fields.creative_prev or fields.creative_next then
  116. minetest.after(0.01,function()
  117. inventory_plus.set_inventory_formspec(player, inventory_plus.get_formspec(player,"main"))
  118. end)
  119. return
  120. end
  121. end)
  122. -- log that we started
  123. minetest.log("action", "[MOD]"..minetest.get_current_modname().." -- loaded from "..minetest.get_modpath(minetest.get_current_modname()))