2
0

tab_server.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. --Minetest
  2. --Copyright (C) 2014 sapier
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program 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
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. --------------------------------------------------------------------------------
  18. local function get_formspec(tabview, name, tabdata)
  19. local index = menudata.worldlist:get_current_index(
  20. tonumber(core.setting_get("mainmenu_last_selected_world"))
  21. )
  22. local retval =
  23. "button[4,4.15;2.6,0.5;world_delete;".. fgettext("Delete") .. "]" ..
  24. "button[6.5,4.15;2.8,0.5;world_create;".. fgettext("New") .. "]" ..
  25. "button[9.2,4.15;2.55,0.5;world_configure;".. fgettext("Configure") .. "]" ..
  26. "button[8.5,4.9;3.25,0.5;start_server;".. fgettext("Start Game") .. "]" ..
  27. "label[4,-0.25;".. fgettext("Select World:") .. "]"..
  28. "vertlabel[0,-0.25;".. fgettext("START SERVER") .. "]" ..
  29. "checkbox[0.5,0.25;cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
  30. dump(core.setting_getbool("creative_mode")) .. "]"..
  31. "checkbox[0.5,0.7;cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
  32. dump(core.setting_getbool("enable_damage")) .. "]"..
  33. "checkbox[0.5,1.15;cb_server_announce;".. fgettext("Public") .. ";" ..
  34. dump(core.setting_getbool("server_announce")) .. "]"..
  35. "field[0.8,3.2;3.5,0.5;te_playername;".. fgettext("Name") .. ";" ..
  36. core.setting_get("name") .. "]" ..
  37. "pwdfield[0.8,4.2;3.5,0.5;te_passwd;".. fgettext("Password") .. "]"
  38. local bind_addr = core.setting_get("bind_address")
  39. if bind_addr ~= nil and bind_addr ~= "" then
  40. retval = retval ..
  41. "field[0.8,5.2;2.25,0.5;te_serveraddr;".. fgettext("Bind Address") .. ";" ..
  42. core.setting_get("bind_address") .."]" ..
  43. "field[3.05,5.2;1.25,0.5;te_serverport;".. fgettext("Port") .. ";" ..
  44. core.setting_get("port") .."]"
  45. else
  46. retval = retval ..
  47. "field[0.8,5.2;3.5,0.5;te_serverport;".. fgettext("Server Port") .. ";" ..
  48. core.setting_get("port") .."]"
  49. end
  50. retval = retval ..
  51. "textlist[4,0.25;7.5,3.7;srv_worlds;" ..
  52. menu_render_worldlist() ..
  53. ";" .. index .. "]"
  54. return retval
  55. end
  56. --------------------------------------------------------------------------------
  57. local function main_button_handler(this, fields, name, tabdata)
  58. local world_doubleclick = false
  59. if fields["srv_worlds"] ~= nil then
  60. local event = core.explode_textlist_event(fields["srv_worlds"])
  61. if event.type == "DCL" then
  62. world_doubleclick = true
  63. end
  64. if event.type == "CHG" then
  65. core.setting_set("mainmenu_last_selected_world",
  66. menudata.worldlist:get_raw_index(core.get_textlist_index("srv_worlds")))
  67. end
  68. end
  69. if menu_handle_key_up_down(fields,"srv_worlds","mainmenu_last_selected_world") then
  70. return true
  71. end
  72. if fields["cb_creative_mode"] then
  73. core.setting_set("creative_mode", fields["cb_creative_mode"])
  74. end
  75. if fields["cb_enable_damage"] then
  76. core.setting_set("enable_damage", fields["cb_enable_damage"])
  77. end
  78. if fields["cb_server_announce"] then
  79. core.setting_set("server_announce", fields["cb_server_announce"])
  80. end
  81. if fields["start_server"] ~= nil or
  82. world_doubleclick or
  83. fields["key_enter"] then
  84. local selected = core.get_textlist_index("srv_worlds")
  85. if selected ~= nil then
  86. gamedata.playername = fields["te_playername"]
  87. gamedata.password = fields["te_passwd"]
  88. gamedata.port = fields["te_serverport"]
  89. gamedata.address = ""
  90. gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
  91. core.setting_set("port",gamedata.port)
  92. if fields["te_serveraddr"] ~= nil then
  93. core.setting_set("bind_address",fields["te_serveraddr"])
  94. end
  95. --update last game
  96. local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
  97. local game,index = gamemgr.find_by_gameid(world.gameid)
  98. core.setting_set("menu_last_game",game.id)
  99. core.start()
  100. end
  101. end
  102. if fields["world_create"] ~= nil then
  103. local create_world_dlg = create_create_world_dlg(true)
  104. create_world_dlg:set_parent(this)
  105. create_world_dlg:show()
  106. this:hide()
  107. return true
  108. end
  109. if fields["world_delete"] ~= nil then
  110. local selected = core.get_textlist_index("srv_worlds")
  111. if selected ~= nil and
  112. selected <= menudata.worldlist:size() then
  113. local world = menudata.worldlist:get_list()[selected]
  114. if world ~= nil and
  115. world.name ~= nil and
  116. world.name ~= "" then
  117. local index = menudata.worldlist:get_raw_index(selected)
  118. local delete_world_dlg = create_delete_world_dlg(world.name,index)
  119. delete_world_dlg:set_parent(this)
  120. delete_world_dlg:show()
  121. this:hide()
  122. end
  123. end
  124. return true
  125. end
  126. if fields["world_configure"] ~= nil then
  127. local selected = core.get_textlist_index("srv_worlds")
  128. if selected ~= nil then
  129. local configdialog =
  130. create_configure_world_dlg(
  131. menudata.worldlist:get_raw_index(selected))
  132. if (configdialog ~= nil) then
  133. configdialog:set_parent(this)
  134. configdialog:show()
  135. this:hide()
  136. end
  137. end
  138. return true
  139. end
  140. return false
  141. end
  142. --------------------------------------------------------------------------------
  143. tab_server = {
  144. name = "server",
  145. caption = fgettext("Server"),
  146. cbf_formspec = get_formspec,
  147. cbf_button_handler = main_button_handler,
  148. on_change = nil
  149. }