formspecs-legacy.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. local travelnet_form_name = "travelnet:show"
  2. local player_formspec_data = travelnet.player_formspec_data
  3. -- minetest.chat_send_player is sometimes not so well visible
  4. function travelnet.show_message(pos, player_name, title, message)
  5. if not pos or not player_name or not message then
  6. return
  7. end
  8. local formspec = travelnet.formspecs.error_message({
  9. title = title,
  10. message = message
  11. })
  12. travelnet.show_formspec(player_name, formspec)
  13. end
  14. -- show the player the formspec they would see when right-clicking the node;
  15. -- needs to be simulated this way as calling on_rightclick would not do
  16. function travelnet.show_current_formspec(pos, meta, player_name)
  17. player_formspec_data[player_name] = player_formspec_data[player_name] or {}
  18. player_formspec_data[player_name].pos = pos
  19. local node = minetest.get_node(pos)
  20. travelnet.show_formspec(player_name,
  21. travelnet.formspecs.current({
  22. station_network = meta:get_string("station_network"),
  23. station_name = meta:get_string("station_name"),
  24. owner_name = meta:get_string("owner"),
  25. is_elevator = travelnet.is_elevator(node.name)
  26. }, player_name))
  27. end
  28. -- a player clicked on something in the formspec hse was manually shown
  29. -- (back from help page, moved travelnet up or down etc.)
  30. function travelnet.form_input_handler(player, formname, fields)
  31. if formname ~= travelnet_form_name then return end
  32. if not player then return end
  33. local name = player:get_player_name()
  34. player_formspec_data[name] = player_formspec_data[name] or {}
  35. local pos = player_formspec_data[name].pos
  36. return travelnet.on_receive_fields(pos, nil, fields, player)
  37. end
  38. -- most formspecs the travelnet uses are stored in the travelnet node itself,
  39. -- but some may require some "back"-button functionality (i.e. help page,
  40. -- move up/down etc.)
  41. minetest.register_on_player_receive_fields(travelnet.form_input_handler)
  42. function travelnet.reset_formspec()
  43. minetest.log("warning",
  44. "[travelnet] the travelnet.reset_formspec method is deprecated. "..
  45. "Run meta:set_string('station_network', '') to reset the travelnet.")
  46. end
  47. function travelnet.edit_formspec(pos, meta, player_name)
  48. if not pos or not meta or not player_name then
  49. return
  50. end
  51. local node = minetest.get_node_or_nil(pos)
  52. if not node then return end
  53. if travelnet.is_elevator(node.name) then
  54. return travelnet.edit_formspec_elevator(pos, meta, player_name)
  55. end
  56. -- request changed data
  57. local formspec = travelnet.formspecs.edit_travelnet({
  58. owner_name = meta:get_string("owner"),
  59. station_network = meta:get_string("station_network"),
  60. station_name = meta:get_string("station_name")
  61. }, player_name)
  62. -- show the formspec manually
  63. travelnet.show_formspec(player_name, formspec)
  64. end
  65. function travelnet.edit_formspec_elevator(pos, meta, player_name)
  66. if not pos or not meta or not player_name then
  67. return
  68. end
  69. -- request changed data
  70. local formspec = travelnet.formspecs.edit_elevator(
  71. { station_name = meta:get_string("station_name") },
  72. player_name
  73. )
  74. -- show the formspec manually
  75. travelnet.show_formspec(player_name, formspec)
  76. end
  77. function travelnet.show_formspec(player_name, formspec)
  78. if formspec and formspec ~= "" then
  79. minetest.show_formspec(player_name, travelnet_form_name, formspec)
  80. return true
  81. else
  82. minetest.show_formspec(player_name, "", "")
  83. return false
  84. end
  85. end