main.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. local S = minetest.get_translator("travelnet")
  2. local player_formspec_data = travelnet.player_formspec_data
  3. travelnet.actions = {}
  4. function travelnet.actions.navigate_page(node_info, fields, _)
  5. local page = 1
  6. local network = travelnet.get_network(node_info.props.owner_name, node_info.props.station_network)
  7. local station_count = 0
  8. for _ in pairs(network) do
  9. station_count = station_count+1
  10. end
  11. local page_size = 7*3
  12. local pages = math.ceil(station_count/page_size)
  13. if fields.last_page then
  14. page = pages
  15. else
  16. local current_page = tonumber(fields.page_number)
  17. if current_page then
  18. if fields.next_page then
  19. page = math.min(current_page+1, pages)
  20. elseif fields.prev_page then
  21. page = math.max(current_page-1, 1)
  22. end
  23. end
  24. end
  25. return true, { formspec = travelnet.formspecs.primary, options = { page_number = page } }
  26. end
  27. function travelnet.actions.remove_station(node_info, _, player)
  28. local player_name = player:get_player_name()
  29. -- abort if protected by another mod
  30. if minetest.is_protected(node_info.pos, player_name)
  31. and not minetest.check_player_privs(player_name, { protection_bypass = true })
  32. then
  33. minetest.record_protection_violation(node_info.pos, player_name)
  34. return false,
  35. S("This @1 belongs to @2. You can't remove it.", node_info.props.description, node_info.props.owner_name)
  36. end
  37. -- players with travelnet_remove priv can dig the station
  38. if
  39. not minetest.get_player_privs(player_name)[travelnet.remove_priv]
  40. -- the function travelnet.allow_dig(..) may allow additional digging
  41. and not travelnet.allow_dig(player_name, node_info.props.owner_name, node_info.props.station_network, node_info.pos)
  42. -- the owner can remove the station
  43. and node_info.props.owner_name ~= player_name
  44. -- stations without owner can be removed/edited by anybody
  45. and node_info.props.owner_name ~= ""
  46. then
  47. return false,
  48. S("This @1 belongs to @2. You can't remove it.", node_info.props.description, node_info.props.owner_name)
  49. end
  50. -- remove station
  51. local player_inventory = player:get_inventory()
  52. if not player_inventory:room_for_item("main", node_info.node.name) then
  53. return false, S("You do not have enough room in your inventory.")
  54. end
  55. -- give the player the box
  56. player_inventory:add_item("main", node_info.node.name)
  57. -- remove the box from the data structure
  58. travelnet.remove_box(node_info.pos, nil, node_info.meta:to_table(), player)
  59. -- remove the node as such
  60. minetest.remove_node(node_info.pos)
  61. return true
  62. end
  63. function travelnet.actions.edit_station(node_info, _, player)
  64. local player_name = player:get_player_name()
  65. -- abort if protected by another mod
  66. if minetest.is_protected(node_info.pos, player_name)
  67. and not minetest.check_player_privs(player_name, { protection_bypass=true })
  68. then
  69. minetest.record_protection_violation(node_info.pos, player_name)
  70. return false, S("This @1 belongs to @2. You can't edit it.",
  71. node_info.props.description,
  72. tostring(node_info.props.owner_name)
  73. )
  74. end
  75. return true, {
  76. formspec = node_info.props.is_elevator
  77. and travelnet.formspecs.edit_elevator
  78. or travelnet.formspecs.edit_travelnet
  79. }
  80. end
  81. function travelnet.actions.update_station(node_info, fields, player)
  82. if node_info.props.is_elevator then
  83. return travelnet.actions.update_elevator(node_info, fields, player)
  84. else
  85. return travelnet.actions.update_travelnet(node_info, fields, player)
  86. end
  87. end
  88. function travelnet.actions.toggle_door(node_info, _, player)
  89. travelnet.open_close_door(node_info.pos, player, "toggle")
  90. return true
  91. end
  92. function travelnet.actions.instruct_player(_, _, player)
  93. minetest.chat_send_player(player:get_player_name(), S("Please click on the target you want to travel to."))
  94. return true
  95. end
  96. function travelnet.actions.end_input(_, _, player)
  97. player_formspec_data[player:get_player_name()] = nil
  98. return true
  99. end
  100. function travelnet.actions.return_to_form()
  101. return true, { formspec = travelnet.formspecs.current }
  102. end
  103. travelnet.actions.repair_station = dofile(travelnet.path .. "/actions/repair_station.lua")
  104. travelnet.actions.change_order = dofile(travelnet.path .. "/actions/change_order.lua")
  105. travelnet.actions.add_station = dofile(travelnet.path .. "/actions/add_station.lua")
  106. travelnet.actions.transport_player = dofile(travelnet.path .. "/actions/transport_player.lua")
  107. travelnet.actions.update_elevator = dofile(travelnet.path .. "/actions/update_elevator.lua")
  108. travelnet.actions.update_travelnet = dofile(travelnet.path .. "/actions/update_travelnet.lua")