Save creative_mode and enable_damage setting for each world in world.mt

Create Parameters on world initialisation and set settings of old worlds
master
fz72 2015-03-15 14:52:10 +01:00 committed by Loic Blot
parent 2f0107f4a7
commit 8ca08a850f
5 changed files with 115 additions and 5 deletions

View File

@ -189,7 +189,6 @@ end
--------------------------------------------------------------------------------
function menu_handle_key_up_down(fields,textlist,settingname)
if fields["key_up"] then
local oldidx = core.get_textlist_index(textlist)
@ -197,6 +196,16 @@ function menu_handle_key_up_down(fields,textlist,settingname)
local newidx = oldidx -1
core.setting_set(settingname,
menudata.worldlist:get_raw_index(newidx))
local worldconfig = modmgr.get_worldconfig(
menudata.worldlist:get_list()[newidx].path)
if worldconfig.creative_mode ~= nil then
core.setting_set("creative_mode", worldconfig.creative_mode)
end
if worldconfig.enable_damage ~= nil then
core.setting_set("enable_damage", worldconfig.enable_damage)
end
end
return true
end
@ -208,6 +217,15 @@ function menu_handle_key_up_down(fields,textlist,settingname)
local newidx = oldidx + 1
core.setting_set(settingname,
menudata.worldlist:get_raw_index(newidx))
local worldconfig = modmgr.get_worldconfig(
menudata.worldlist:get_list()[newidx].path)
if worldconfig.creative_mode ~= nil then
core.setting_set("creative_mode", worldconfig.creative_mode)
end
if worldconfig.enable_damage ~= nil then
core.setting_set("enable_damage", worldconfig.enable_damage)
end
end
return true

View File

@ -321,8 +321,10 @@ function modmgr.get_worldconfig(worldpath)
for key,value in pairs(worldfile:to_table()) do
if key == "gameid" then
worldconfig.id = value
else
elseif key:sub(0, 9) == "load_mod_" then
worldconfig.global_mods[key] = core.is_yes(value)
else
worldconfig[key] = value
end
end

View File

@ -68,6 +68,32 @@ 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
if event.type == "DCL" then
world_doubleclick = true
end
@ -84,11 +110,29 @@ 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"
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"
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

View File

@ -106,13 +106,39 @@ 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
if event.type == "DCL" then
world_doubleclick = true
end
if event.type == "CHG" then
if event.type == "CHG" and selected ~= nil then
core.setting_set("mainmenu_last_selected_world",
menudata.worldlist:get_raw_index(core.get_textlist_index("sp_worlds")))
menudata.worldlist:get_raw_index(selected))
return true
end
end
@ -123,11 +149,29 @@ 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"
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"
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

View File

@ -283,7 +283,9 @@ bool initializeWorld(const std::string &path, const std::string &gameid)
std::string worldmt_path = path + DIR_DELIM "world.mt";
if (!fs::PathExists(worldmt_path)) {
std::ostringstream ss(std::ios_base::binary);
ss << "gameid = " << gameid << "\nbackend = sqlite3\n";
ss << "gameid = " << gameid << "\nbackend = sqlite3\n"
<< "creative_mode = " << g_settings->get("creative_mode")
<< "\nenable_damage = " << g_settings->get("enable_damage") << "\n";
if (!fs::safeWriteToFile(worldmt_path, ss.str()))
return false;