Fix bugs in mainmenu
authorjeanpatrick.guerrero@gmail.com <jeanpatrick.guerrero@gmail.com>
Mon, 29 Jun 2015 13:41:05 +0000 (15:41 +0200)
committerest31 <MTest31@outlook.com>
Tue, 30 Jun 2015 16:11:38 +0000 (18:11 +0200)
builtin/mainmenu/common.lua
builtin/mainmenu/tab_server.lua
builtin/mainmenu/tab_singleplayer.lua

index 6126c61a41b213f7abe77291585fd4cef595a3ce..bcb075f99842ff7765881a1f01a299f57f74b7bc 100644 (file)
@@ -300,3 +300,34 @@ function is_server_protocol_compat_or_error(proto_min, proto_max)
 
        return true
 end
+--------------------------------------------------------------------------------
+function menu_worldmt(selected, setting, value)
+       local world = menudata.worldlist:get_list()[selected]
+       if world then
+               local filename = world.path .. DIR_DELIM .. "world.mt"
+               local world_conf = Settings(filename)
+
+               if value then
+                       if not world_conf:write() then
+                               core.log("error", "Failed to write world config file")
+                       end
+                       return world_conf:set(setting, value)
+               else
+                       return world_conf:get(setting)
+               end
+       else
+               return nil
+       end
+end
+
+function menu_worldmt_legacy()
+       local modes = {"creative_mode", "enable_damage"}
+       for _, mode in pairs(modes) do
+               local mode = menu_worldmt(selected, ""..mode.."")
+               if mode then
+                       core.setting_set(""..mode.."", mode)
+               else
+                       menu_worldmt(selected, ""..mode.."", core.setting_get(""..mode..""))
+               end
+       end
+end
index d78af768fdd3c7b848a75417faf5d6409928104f..2362d32a1ad7983274c601277d1dfb0654e9e74c 100644 (file)
@@ -67,32 +67,9 @@ local function main_button_handler(this, fields, name, tabdata)
 
        if fields["srv_worlds"] ~= nil then
                local event = core.explode_textlist_event(fields["srv_worlds"])
-
                local selected = core.get_textlist_index("srv_worlds")
-               if selected ~= nil then
-                       local filename = menudata.worldlist:get_list()[selected].path
-                       local worldconfig = modmgr.get_worldconfig(filename)
-                       filename = filename .. DIR_DELIM .. "world.mt"
-
-                       if worldconfig.creative_mode ~= nil then
-                               core.setting_set("creative_mode", worldconfig.creative_mode)
-                       else
-                               local worldfile = Settings(filename)
-                               worldfile:set("creative_mode", core.setting_get("creative_mode"))
-                               if not worldfile:write() then
-                                       core.log("error", "Failed to write world config file")
-                               end
-                       end
-                       if worldconfig.enable_damage ~= nil then
-                               core.setting_set("enable_damage", worldconfig.enable_damage)
-                       else
-                               local worldfile = Settings(filename)
-                               worldfile:set("enable_damage", core.setting_get("enable_damage"))
-                               if not worldfile:write() then
-                                       core.log("error", "Failed to write world config file")
-                               end
-                       end
-               end
+
+               menu_worldmt_legacy()
 
                if event.type == "DCL" then
                        world_doubleclick = true
@@ -111,28 +88,16 @@ local function main_button_handler(this, fields, name, tabdata)
        if fields["cb_creative_mode"] then
                core.setting_set("creative_mode", fields["cb_creative_mode"])
                local selected = core.get_textlist_index("srv_worlds")
-               local filename = menudata.worldlist:get_list()[selected].path ..
-                               DIR_DELIM .. "world.mt"
+               menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
 
-               local worldfile = Settings(filename)
-               worldfile:set("creative_mode", fields["cb_creative_mode"])
-               if not worldfile:write() then
-                       core.log("error", "Failed to write world config file")
-               end
                return true
        end
 
        if fields["cb_enable_damage"] then
                core.setting_set("enable_damage", fields["cb_enable_damage"])
                local selected = core.get_textlist_index("srv_worlds")
-               local filename = menudata.worldlist:get_list()[selected].path ..
-                               DIR_DELIM .. "world.mt"
+               menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
 
-               local worldfile = Settings(filename)
-               worldfile:set("enable_damage", fields["cb_enable_damage"])
-               if not worldfile:write() then
-                       core.log("error", "Failed to write world config file")
-               end
                return true
        end
 
@@ -159,9 +124,11 @@ local function main_button_handler(this, fields, name, tabdata)
 
                        --update last game
                        local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
+                       if world then
+                               local game, index = gamemgr.find_by_gameid(world.gameid)
+                               core.setting_set("menu_last_game", game.id)
+                       end
                        
-                       local game,index = gamemgr.find_by_gameid(world.gameid)
-                       core.setting_set("menu_last_game",game.id)
                        core.start()
                        return true
                end
index c2b850e449507c4394ff31bc1617d950eb0b4078..916b0c1887b1249f85e694746f9d75d9f91aab7a 100644 (file)
@@ -23,9 +23,9 @@ local function current_game()
 end
 
 local function singleplayer_refresh_gamebar()
-       
+
        local old_bar = ui.find_by_name("game_button_bar")
-       
+
        if old_bar ~= nil then
                old_bar:delete()
        end
@@ -76,7 +76,7 @@ end
 
 local function get_formspec(tabview, name, tabdata)
        local retval = ""
-       
+
        local index = filterlist.get_current_index(menudata.worldlist,
                                tonumber(core.setting_get("mainmenu_last_selected_world"))
                                )
@@ -105,32 +105,9 @@ local function main_button_handler(this, fields, name, tabdata)
 
        if fields["sp_worlds"] ~= nil then
                local event = core.explode_textlist_event(fields["sp_worlds"])
-
                local selected = core.get_textlist_index("sp_worlds")
-               if selected ~= nil then
-                       local filename = menudata.worldlist:get_list()[selected].path
-                       local worldconfig = modmgr.get_worldconfig(filename)
-                       filename = filename .. DIR_DELIM .. "world.mt"
 
-                       if worldconfig.creative_mode ~= nil then
-                               core.setting_set("creative_mode", worldconfig.creative_mode)
-                       else
-                               local worldfile = Settings(filename)
-                               worldfile:set("creative_mode", core.setting_get("creative_mode"))
-                               if not worldfile:write() then
-                                       core.log("error", "Failed to write world config file")
-                               end
-                       end
-                       if worldconfig.enable_damage ~= nil then
-                               core.setting_set("enable_damage", worldconfig.enable_damage)
-                       else
-                               local worldfile = Settings(filename)
-                               worldfile:set("enable_damage", core.setting_get("enable_damage"))
-                               if not worldfile:write() then
-                                       core.log("error", "Failed to write world config file")
-                               end
-                       end
-               end
+               menu_worldmt_legacy()
 
                if event.type == "DCL" then
                        world_doubleclick = true
@@ -150,28 +127,16 @@ local function main_button_handler(this, fields, name, tabdata)
        if fields["cb_creative_mode"] then
                core.setting_set("creative_mode", fields["cb_creative_mode"])
                local selected = core.get_textlist_index("sp_worlds")
-               local filename = menudata.worldlist:get_list()[selected].path ..
-                               DIR_DELIM .. "world.mt"
+               menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
 
-               local worldfile = Settings(filename)
-               worldfile:set("creative_mode", fields["cb_creative_mode"])
-               if not worldfile:write() then
-                       core.log("error", "Failed to write world config file")
-               end
                return true
        end
 
        if fields["cb_enable_damage"] then
                core.setting_set("enable_damage", fields["cb_enable_damage"])
                local selected = core.get_textlist_index("sp_worlds")
-               local filename = menudata.worldlist:get_list()[selected].path ..
-                               DIR_DELIM .. "world.mt"
+               menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
 
-               local worldfile = Settings(filename)
-               worldfile:set("enable_damage", fields["cb_enable_damage"])
-               if not worldfile:write() then
-                       core.log("error", "Failed to write world config file")
-               end
                return true
        end