on_receive_fields.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. local S = minetest.get_translator("travelnet")
  2. local player_formspec_data = travelnet.player_formspec_data
  3. local function validate_travelnet(pos, meta)
  4. local owner_name = meta:get_string("owner")
  5. local station_network = meta:get_string("station_network")
  6. local station_name = meta:get_string("station_name")
  7. -- if there is something wrong with the data
  8. if not owner_name or not station_network or not station_name then
  9. print(
  10. "ERROR: The travelnet at " .. minetest.pos_to_string(pos) .. " has a problem: " ..
  11. " DATA: owner: " .. (owner_name or "?") ..
  12. " station_name: " .. (station_name or "?") ..
  13. " station_network: " .. (station_network or "?") .. "."
  14. )
  15. return false, S("Error") .. ": " ..
  16. S("There is something wrong with the configuration of this station.") ..
  17. " DEBUG DATA: owner: " .. (owner_name or "?") ..
  18. " station_name: " .. (station_name or "?") ..
  19. " station_network: " .. (station_network or "?") .. "."
  20. end
  21. -- TODO: This check seems odd, re-think this. Don't get node twice, don't hard-code node names.
  22. local description = travelnet.node_description(pos)
  23. if not description then
  24. return false, S("Unknown node.")
  25. end
  26. return true, {
  27. description = description,
  28. owner_name = owner_name,
  29. station_network = station_network,
  30. station_name = station_name
  31. }
  32. end
  33. local function decide_action(fields, props)
  34. -- the player wants to quit/exit the formspec; do not save/update anything
  35. if (fields.station_exit and fields.station_exit ~= "") or (fields.quit and fields.quit ~= "") then
  36. return travelnet.actions.end_input
  37. end
  38. -- back button leads back to the previous form
  39. if fields.back and fields.back ~= "" then
  40. return travelnet.actions.return_to_form
  41. end
  42. -- if paging is enabled and the player wants to change pages
  43. if (travelnet.MAX_STATIONS_PER_NETWORK == 0 or travelnet.MAX_STATIONS_PER_NETWORK > 24)
  44. and fields.page_number
  45. and (
  46. fields.next_page
  47. or fields.prev_page
  48. or fields.last_page
  49. or fields.first_page
  50. )
  51. then
  52. return travelnet.actions.navigate_page
  53. end
  54. -- the player wants to remove the station
  55. if fields.station_dig then
  56. return travelnet.actions.remove_station
  57. end
  58. -- the player wants to open the edit form
  59. if fields.station_edit then
  60. return travelnet.actions.edit_station
  61. end
  62. -- if the box has not been configured yet
  63. if travelnet.is_falsey_string(props.station_network) then
  64. return travelnet.actions.add_station
  65. end
  66. -- save pressed after editing
  67. if fields.station_set then
  68. return travelnet.actions.update_station
  69. end
  70. -- pressed the "open door" button
  71. if fields.open_door then
  72. return travelnet.actions.toggle_door
  73. end
  74. -- the owner or players with the travelnet_attach priv can move stations up or down in the list
  75. if fields.move_up or fields.move_down then
  76. return travelnet.actions.change_order
  77. end
  78. if not fields.target then
  79. return travelnet.actions.instruct_player
  80. end
  81. local network = travelnet.get_network(props.owner_name, props.station_network)
  82. if not network then
  83. return travelnet.actions.add_station
  84. end
  85. return travelnet.actions.transport_player
  86. end
  87. function travelnet.on_receive_fields(pos, _, fields, player)
  88. if not player then
  89. return
  90. end
  91. local name = player:get_player_name()
  92. player_formspec_data[name] = player_formspec_data[name] or {}
  93. if pos then
  94. player_formspec_data[name].pos = pos
  95. else
  96. pos = player_formspec_data[name].pos
  97. end
  98. local action_args = {
  99. pos = pos,
  100. props = {}
  101. }
  102. if not pos then
  103. travelnet.actions.end_input(action_args, fields or {}, player)
  104. travelnet.show_formspec(name, false)
  105. return
  106. end
  107. local node = minetest.get_node(pos)
  108. action_args.node = node
  109. local meta = minetest.get_meta(pos)
  110. action_args.meta = meta
  111. if not fields then
  112. travelnet.actions.end_input(action_args, {}, player)
  113. travelnet.show_formspec(name, false)
  114. return
  115. end
  116. -- Validate node's meta data
  117. local valid, props = validate_travelnet(pos, meta)
  118. if not valid then
  119. minetest.chat_send_player(name, props)
  120. travelnet.actions.end_input(action_args, fields, player)
  121. travelnet.show_formspec(name, false)
  122. return
  123. end
  124. props.is_elevator = travelnet.is_elevator(node.name)
  125. action_args.props = props
  126. -- Decide which action to run based on fields given
  127. local action = decide_action(fields, props)
  128. if not action then
  129. travelnet.actions.end_input(action_args, fields, player)
  130. travelnet.show_formspec(name, false)
  131. return
  132. end
  133. -- Perform the action
  134. local success, result = action(action_args, fields, player)
  135. -- Respond with a formspec
  136. if success then
  137. if result and result.formspec then
  138. if result.formspec ~= travelnet.formspecs.current then
  139. player_formspec_data[name].current_form = result.formspec
  140. end
  141. if result.options then
  142. for k,v in pairs(result.options) do
  143. props[k] = v
  144. end
  145. end
  146. travelnet.show_formspec(name, result.formspec(props, name))
  147. else
  148. travelnet.actions.end_input(action_args, fields, player)
  149. travelnet.show_formspec(name, false)
  150. end
  151. else
  152. travelnet.show_formspec(name, travelnet.formspecs.error_message({ message = result }))
  153. end
  154. end