change_order.lua 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. local S = minetest.get_translator("travelnet")
  2. return function (node_info, fields, player)
  3. local player_name = player:get_player_name()
  4. -- does the player want to move this station one position up in the list?
  5. -- only the owner and players with the travelnet_attach priv can change the order of the list
  6. -- Note: With elevators, only the "G"(round) marking is actually moved
  7. if fields and (fields.move_up or fields.move_down)
  8. and not travelnet.is_falsey_string(node_info.props.owner_name)
  9. and (
  10. (node_info.props.owner_name == player_name)
  11. or (minetest.get_player_privs(player_name)[travelnet.attach_priv])
  12. )
  13. then
  14. local travelnets = travelnet.get_travelnets(node_info.props.owner_name)
  15. local network = travelnets[node_info.props.station_network]
  16. if not network then
  17. return false, S("This station does not have a network.")
  18. end
  19. local stations = travelnet.get_ordered_stations(
  20. node_info.props.owner_name,
  21. node_info.props.station_network,
  22. node_info.props.is_elevator
  23. )
  24. local current_pos = -1
  25. for index, k in ipairs(stations) do
  26. if k == node_info.props.station_name then
  27. current_pos = index
  28. break
  29. end
  30. end
  31. local swap_with_pos
  32. if fields.move_up then
  33. swap_with_pos = current_pos-1
  34. else
  35. swap_with_pos = current_pos+1
  36. end
  37. -- handle errors
  38. if swap_with_pos < 1 then
  39. return false, S("This station is already the first one on the list.")
  40. elseif swap_with_pos > #stations then
  41. return false, S("This station is already the last one on the list.")
  42. else
  43. local current_station = stations[current_pos]
  44. local swap_with_station = stations[swap_with_pos]
  45. -- swap the actual data by which the stations are sorted
  46. local old_timestamp = network[swap_with_station].timestamp
  47. network[swap_with_station].timestamp = network[current_station].timestamp
  48. network[current_station].timestamp = old_timestamp
  49. -- for elevators, only the "G"(round) marking is moved; no point in swapping stations
  50. if not node_info.props.is_elevator then
  51. -- actually swap the stations
  52. stations[swap_with_pos] = current_station
  53. stations[current_pos] = swap_with_station
  54. end
  55. -- store the changed order
  56. travelnet.log("action", "saving changed order for network '" .. node_info.props.station_network ..
  57. "' player '" .. node_info.props.owner_name .. "'")
  58. travelnet.set_travelnets(node_info.props.owner_name, travelnets)
  59. return true, { formspec = travelnet.formspecs.primary }
  60. end
  61. end
  62. return false, S("This @1 belongs to @2. You can't edit it.",
  63. node_info.props.description,
  64. tostring(node_info.props.owner_name)
  65. )
  66. end