stations.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. -- Crafting Mod - semi-realistic crafting in minetest
  2. -- Copyright (C) 2018 rubenwardy <rw@rubenwardy.com>
  3. --
  4. -- This library is free software; you can redistribute it and/or
  5. -- modify it under the terms of the GNU Lesser General Public
  6. -- License as published by the Free Software Foundation; either
  7. -- version 2.1 of the License, or (at your option) any later version.
  8. --
  9. -- This library is distributed in the hope that it will be useful,
  10. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. -- Lesser General Public License for more details.
  13. --
  14. -- You should have received a copy of the GNU Lesser General Public
  15. -- License along with this library; if not, write to the Free Software
  16. -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. crafting.register_type("inv")
  18. if minetest.global_exists("sfinv") then
  19. local player_inv_hashes = {}
  20. sfinv.override_page("sfinv:crafting", {
  21. get = function(self, player, context)
  22. player_inv_hashes[player:get_player_name()] =
  23. crafting.calc_inventory_list_hash(player:get_inventory(), "main")
  24. local formspec = crafting.make_result_selector(player, "inv", 1, { x = 8, y = 3 }, context)
  25. return sfinv.make_formspec(player, context, formspec, true)
  26. end,
  27. on_player_receive_fields = function(self, player, context, fields)
  28. if crafting.result_select_on_receive_results(player, "inv", 1, context, fields) then
  29. sfinv.set_player_inventory_formspec(player)
  30. end
  31. return true
  32. end
  33. })
  34. local function check_for_changes()
  35. for _, player in pairs(minetest.get_connected_players()) do
  36. if sfinv.get_or_create_context(player).page == "sfinv:crafting" then
  37. local hash = crafting.calc_inventory_list_hash(player:get_inventory(), "main")
  38. local old_hash = player_inv_hashes[player:get_player_name()]
  39. if hash ~= old_hash then
  40. sfinv.set_page(player, "sfinv:crafting")
  41. end
  42. end
  43. end
  44. minetest.after(1, check_for_changes)
  45. end
  46. check_for_changes()
  47. end