Skip to content

Commit

Permalink
Save creative_mode and enable_damage setting for each world in world.mt
Browse files Browse the repository at this point in the history
Create Parameters on world initialisation and set settings of old worlds
  • Loading branch information
fz72 authored and nerzhul committed Mar 18, 2015
1 parent 2f0107f commit 8ca08a8
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 5 deletions.
20 changes: 19 additions & 1 deletion builtin/mainmenu/common.lua
Expand Up @@ -189,14 +189,23 @@ end

--------------------------------------------------------------------------------
function menu_handle_key_up_down(fields,textlist,settingname)

if fields["key_up"] then
local oldidx = core.get_textlist_index(textlist)

if oldidx ~= nil and oldidx > 1 then
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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion builtin/mainmenu/modmgr.lua
Expand Up @@ -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

Expand Down
44 changes: 44 additions & 0 deletions builtin/mainmenu/tab_server.lua
Expand Up @@ -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
Expand All @@ -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

Expand Down
48 changes: 46 additions & 2 deletions builtin/mainmenu/tab_singleplayer.lua
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion src/subgame.cpp
Expand Up @@ -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;

Expand Down

3 comments on commit 8ca08a8

@ShadowNinja
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has a lot of copypasta.

@nerzhul
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.

@nerzhul
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common.lua is now clean. I will make a PR later for the tabs

Please sign in to comment.