Skip to content

Commit 85f3d57

Browse files
kilbithest31
authored andcommitted
Fix bugs in mainmenu
1 parent 91f95fd commit 85f3d57

File tree

3 files changed

+45
-82
lines changed

3 files changed

+45
-82
lines changed

builtin/mainmenu/common.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,34 @@ function is_server_protocol_compat_or_error(proto_min, proto_max)
300300

301301
return true
302302
end
303+
--------------------------------------------------------------------------------
304+
function menu_worldmt(selected, setting, value)
305+
local world = menudata.worldlist:get_list()[selected]
306+
if world then
307+
local filename = world.path .. DIR_DELIM .. "world.mt"
308+
local world_conf = Settings(filename)
309+
310+
if value then
311+
if not world_conf:write() then
312+
core.log("error", "Failed to write world config file")
313+
end
314+
return world_conf:set(setting, value)
315+
else
316+
return world_conf:get(setting)
317+
end
318+
else
319+
return nil
320+
end
321+
end
322+
323+
function menu_worldmt_legacy()
324+
local modes = {"creative_mode", "enable_damage"}
325+
for _, mode in pairs(modes) do
326+
local mode = menu_worldmt(selected, ""..mode.."")
327+
if mode then
328+
core.setting_set(""..mode.."", mode)
329+
else
330+
menu_worldmt(selected, ""..mode.."", core.setting_get(""..mode..""))
331+
end
332+
end
333+
end

builtin/mainmenu/tab_server.lua

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,9 @@ local function main_button_handler(this, fields, name, tabdata)
6767

6868
if fields["srv_worlds"] ~= nil then
6969
local event = core.explode_textlist_event(fields["srv_worlds"])
70-
7170
local selected = core.get_textlist_index("srv_worlds")
72-
if selected ~= nil then
73-
local filename = menudata.worldlist:get_list()[selected].path
74-
local worldconfig = modmgr.get_worldconfig(filename)
75-
filename = filename .. DIR_DELIM .. "world.mt"
76-
77-
if worldconfig.creative_mode ~= nil then
78-
core.setting_set("creative_mode", worldconfig.creative_mode)
79-
else
80-
local worldfile = Settings(filename)
81-
worldfile:set("creative_mode", core.setting_get("creative_mode"))
82-
if not worldfile:write() then
83-
core.log("error", "Failed to write world config file")
84-
end
85-
end
86-
if worldconfig.enable_damage ~= nil then
87-
core.setting_set("enable_damage", worldconfig.enable_damage)
88-
else
89-
local worldfile = Settings(filename)
90-
worldfile:set("enable_damage", core.setting_get("enable_damage"))
91-
if not worldfile:write() then
92-
core.log("error", "Failed to write world config file")
93-
end
94-
end
95-
end
71+
72+
menu_worldmt_legacy()
9673

9774
if event.type == "DCL" then
9875
world_doubleclick = true
@@ -111,28 +88,16 @@ local function main_button_handler(this, fields, name, tabdata)
11188
if fields["cb_creative_mode"] then
11289
core.setting_set("creative_mode", fields["cb_creative_mode"])
11390
local selected = core.get_textlist_index("srv_worlds")
114-
local filename = menudata.worldlist:get_list()[selected].path ..
115-
DIR_DELIM .. "world.mt"
91+
menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
11692

117-
local worldfile = Settings(filename)
118-
worldfile:set("creative_mode", fields["cb_creative_mode"])
119-
if not worldfile:write() then
120-
core.log("error", "Failed to write world config file")
121-
end
12293
return true
12394
end
12495

12596
if fields["cb_enable_damage"] then
12697
core.setting_set("enable_damage", fields["cb_enable_damage"])
12798
local selected = core.get_textlist_index("srv_worlds")
128-
local filename = menudata.worldlist:get_list()[selected].path ..
129-
DIR_DELIM .. "world.mt"
99+
menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
130100

131-
local worldfile = Settings(filename)
132-
worldfile:set("enable_damage", fields["cb_enable_damage"])
133-
if not worldfile:write() then
134-
core.log("error", "Failed to write world config file")
135-
end
136101
return true
137102
end
138103

@@ -159,9 +124,11 @@ local function main_button_handler(this, fields, name, tabdata)
159124

160125
--update last game
161126
local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
127+
if world then
128+
local game, index = gamemgr.find_by_gameid(world.gameid)
129+
core.setting_set("menu_last_game", game.id)
130+
end
162131

163-
local game,index = gamemgr.find_by_gameid(world.gameid)
164-
core.setting_set("menu_last_game",game.id)
165132
core.start()
166133
return true
167134
end

builtin/mainmenu/tab_singleplayer.lua

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ local function current_game()
2323
end
2424

2525
local function singleplayer_refresh_gamebar()
26-
26+
2727
local old_bar = ui.find_by_name("game_button_bar")
28-
28+
2929
if old_bar ~= nil then
3030
old_bar:delete()
3131
end
@@ -76,7 +76,7 @@ end
7676

7777
local function get_formspec(tabview, name, tabdata)
7878
local retval = ""
79-
79+
8080
local index = filterlist.get_current_index(menudata.worldlist,
8181
tonumber(core.setting_get("mainmenu_last_selected_world"))
8282
)
@@ -105,32 +105,9 @@ local function main_button_handler(this, fields, name, tabdata)
105105

106106
if fields["sp_worlds"] ~= nil then
107107
local event = core.explode_textlist_event(fields["sp_worlds"])
108-
109108
local selected = core.get_textlist_index("sp_worlds")
110-
if selected ~= nil then
111-
local filename = menudata.worldlist:get_list()[selected].path
112-
local worldconfig = modmgr.get_worldconfig(filename)
113-
filename = filename .. DIR_DELIM .. "world.mt"
114109

115-
if worldconfig.creative_mode ~= nil then
116-
core.setting_set("creative_mode", worldconfig.creative_mode)
117-
else
118-
local worldfile = Settings(filename)
119-
worldfile:set("creative_mode", core.setting_get("creative_mode"))
120-
if not worldfile:write() then
121-
core.log("error", "Failed to write world config file")
122-
end
123-
end
124-
if worldconfig.enable_damage ~= nil then
125-
core.setting_set("enable_damage", worldconfig.enable_damage)
126-
else
127-
local worldfile = Settings(filename)
128-
worldfile:set("enable_damage", core.setting_get("enable_damage"))
129-
if not worldfile:write() then
130-
core.log("error", "Failed to write world config file")
131-
end
132-
end
133-
end
110+
menu_worldmt_legacy()
134111

135112
if event.type == "DCL" then
136113
world_doubleclick = true
@@ -150,28 +127,16 @@ local function main_button_handler(this, fields, name, tabdata)
150127
if fields["cb_creative_mode"] then
151128
core.setting_set("creative_mode", fields["cb_creative_mode"])
152129
local selected = core.get_textlist_index("sp_worlds")
153-
local filename = menudata.worldlist:get_list()[selected].path ..
154-
DIR_DELIM .. "world.mt"
130+
menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
155131

156-
local worldfile = Settings(filename)
157-
worldfile:set("creative_mode", fields["cb_creative_mode"])
158-
if not worldfile:write() then
159-
core.log("error", "Failed to write world config file")
160-
end
161132
return true
162133
end
163134

164135
if fields["cb_enable_damage"] then
165136
core.setting_set("enable_damage", fields["cb_enable_damage"])
166137
local selected = core.get_textlist_index("sp_worlds")
167-
local filename = menudata.worldlist:get_list()[selected].path ..
168-
DIR_DELIM .. "world.mt"
138+
menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
169139

170-
local worldfile = Settings(filename)
171-
worldfile:set("enable_damage", fields["cb_enable_damage"])
172-
if not worldfile:write() then
173-
core.log("error", "Failed to write world config file")
174-
end
175140
return true
176141
end
177142

0 commit comments

Comments
 (0)