Fix bugs in mainmenu

This commit is contained in:
kilbith 2015-06-17 13:49:45 +02:00
parent a1a2ac7954
commit ad3a78764f
3 changed files with 66 additions and 79 deletions

31
builtin/mainmenu/common.lua Normal file → Executable file
View File

@ -300,3 +300,34 @@ function is_server_protocol_compat_or_error(proto_min, proto_max)
return true
end
--------------------------------------------------------------------------------
local function menu_load_worldmt(selected)
if menudata.worldlist:get_list()[selected] ~= nil then
local world = menudata.worldlist:get_list()[selected]
return world
else
return nil
end
end
function menu_get_worldmt(selected, setting)
local world = menu_load_worldmt(selected)
local filename = world.path .. DIR_DELIM .. "world.mt"
local world_conf = Settings(filename)
return world_conf:get(setting)
end
function menu_set_worldmt(selected, setting, value)
local world = menu_load_worldmt(selected)
if not world then return nil end
local filename = world.path .. DIR_DELIM .. "world.mt"
local world_conf = Settings(filename)
if not world_conf:write() then
core.log("error", "Failed to write world config file")
end
return world_conf:set(setting, value)
end

View File

@ -67,31 +67,20 @@ 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"
local creative_mode = menu_get_worldmt(selected, "creative_mode")
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
if creative_mode ~= nil then
core.setting_set("creative_mode", creative_mode)
else
menu_set_worldmt(selected, "creative_mode", core.setting_get("creative_mode"))
end
local enable_damage = menu_get_worldmt(selected, "enable_damage")
if enable_damage ~= nil then
core.setting_set("enable_damage", enable_damage)
else
menu_set_worldmt(selected, "enable_damage", core.setting_get("enable_damage"))
end
if event.type == "DCL" then
@ -111,28 +100,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_set_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_set_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 +136,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 ~= nil 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

57
builtin/mainmenu/tab_singleplayer.lua Normal file → Executable file
View 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,31 +105,20 @@ 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"
local creative_mode = menu_get_worldmt(selected, "creative_mode")
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
if creative_mode ~= nil then
core.setting_set("creative_mode", creative_mode)
else
menu_set_worldmt(selected, "creative_mode", core.setting_get("creative_mode"))
end
local enable_damage = menu_get_worldmt(selected, "enable_damage")
if enable_damage ~= nil then
core.setting_set("enable_damage", enable_damage)
else
menu_set_worldmt(selected, "enable_damage", core.setting_get("enable_damage"))
end
if event.type == "DCL" then
@ -150,28 +139,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_set_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_set_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