Skip to content

Commit 47c1461

Browse files
Hugues RossWuzzy2Warr1024rubenwardy
authoredAug 12, 2021
Add disable_settings to game.conf to get rid of "Enable Damage"/"Creative Mode"/"Host Server" checkboxes (#11524)
This adds support for disable_settings to game.conf. In this you can specify a list of settings that should not be visible in the "local game" (or however it is called nowadays) tab. Enable Damage, Creative Mode and Host Server are supported. Co-authored-by: Wuzzy <Wuzzy2@mail.ru> Co-authored-by: Aaron Suen <warr1024@gmail.com> Co-authored-by: rubenwardy <rw@rubenwardy.com>
1 parent 442e48b commit 47c1461

File tree

2 files changed

+89
-13
lines changed

2 files changed

+89
-13
lines changed
 

‎builtin/mainmenu/tab_local.lua

+80-12
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@
1818

1919
local enable_gamebar = PLATFORM ~= "Android"
2020
local current_game, singleplayer_refresh_gamebar
21+
local valid_disabled_settings = {
22+
["enable_damage"]=true,
23+
["creative_mode"]=true,
24+
["enable_server"]=true,
25+
}
2126

2227
if enable_gamebar then
28+
-- Currently chosen game in gamebar for theming and filtering
2329
function current_game()
2430
local last_game_id = core.settings:get("menu_last_game")
2531
local game = pkgmgr.find_by_gameid(last_game_id)
@@ -102,37 +108,87 @@ if enable_gamebar then
102108
btnbar:add_button("game_open_cdb", "", plus_image, fgettext("Install games from ContentDB"))
103109
end
104110
else
111+
-- Currently chosen game in gamebar: no gamebar -> no "current" game
105112
function current_game()
106113
return nil
107114
end
108115
end
109116

117+
local function get_disabled_settings(game)
118+
if not game then
119+
return {}
120+
end
121+
122+
local gameconfig = Settings(game.path .. "/game.conf")
123+
local disabled_settings = {}
124+
if gameconfig then
125+
local disabled_settings_str = (gameconfig:get("disabled_settings") or ""):split()
126+
for _, value in pairs(disabled_settings_str) do
127+
local state = false
128+
value = value:trim()
129+
if string.sub(value, 1, 1) == "!" then
130+
state = true
131+
value = string.sub(value, 2)
132+
end
133+
if valid_disabled_settings[value] then
134+
disabled_settings[value] = state
135+
else
136+
core.log("error", "Invalid disabled setting in game.conf: "..tostring(value))
137+
end
138+
end
139+
end
140+
return disabled_settings
141+
end
142+
110143
local function get_formspec(tabview, name, tabdata)
111144
local retval = ""
112145

113146
local index = filterlist.get_current_index(menudata.worldlist,
114-
tonumber(core.settings:get("mainmenu_last_selected_world"))
115-
)
147+
tonumber(core.settings:get("mainmenu_last_selected_world")))
148+
local list = menudata.worldlist:get_list()
149+
local world = list and index and list[index]
150+
local gameid = world and world.gameid
151+
local game = gameid and pkgmgr.find_by_gameid(gameid)
152+
local disabled_settings = get_disabled_settings(game)
153+
154+
local creative, damage, host = "", "", ""
155+
156+
-- Y offsets for game settings checkboxes
157+
local y = -0.2
158+
local yo = 0.45
159+
160+
if disabled_settings["creative_mode"] == nil then
161+
creative = "checkbox[0,"..y..";cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
162+
dump(core.settings:get_bool("creative_mode")) .. "]"
163+
y = y + yo
164+
end
165+
if disabled_settings["enable_damage"] == nil then
166+
damage = "checkbox[0,"..y..";cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
167+
dump(core.settings:get_bool("enable_damage")) .. "]"
168+
y = y + yo
169+
end
170+
if disabled_settings["enable_server"] == nil then
171+
host = "checkbox[0,"..y..";cb_server;".. fgettext("Host Server") ..";" ..
172+
dump(core.settings:get_bool("enable_server")) .. "]"
173+
y = y + yo
174+
end
116175

117176
retval = retval ..
118177
"button[3.9,3.8;2.8,1;world_delete;".. fgettext("Delete") .. "]" ..
119178
"button[6.55,3.8;2.8,1;world_configure;".. fgettext("Select Mods") .. "]" ..
120179
"button[9.2,3.8;2.8,1;world_create;".. fgettext("New") .. "]" ..
121180
"label[3.9,-0.05;".. fgettext("Select World:") .. "]"..
122-
"checkbox[0,-0.20;cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
123-
dump(core.settings:get_bool("creative_mode")) .. "]"..
124-
"checkbox[0,0.25;cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
125-
dump(core.settings:get_bool("enable_damage")) .. "]"..
126-
"checkbox[0,0.7;cb_server;".. fgettext("Host Server") ..";" ..
127-
dump(core.settings:get_bool("enable_server")) .. "]" ..
181+
creative ..
182+
damage ..
183+
host ..
128184
"textlist[3.9,0.4;7.9,3.45;sp_worlds;" ..
129185
menu_render_worldlist() ..
130186
";" .. index .. "]"
131187

132-
if core.settings:get_bool("enable_server") then
188+
if core.settings:get_bool("enable_server") and disabled_settings["enable_server"] == nil then
133189
retval = retval ..
134190
"button[7.9,4.75;4.1,1;play;".. fgettext("Host Game") .. "]" ..
135-
"checkbox[0,1.15;cb_server_announce;" .. fgettext("Announce Server") .. ";" ..
191+
"checkbox[0,"..y..";cb_server_announce;" .. fgettext("Announce Server") .. ";" ..
136192
dump(core.settings:get_bool("server_announce")) .. "]" ..
137193
"field[0.3,2.85;3.8,0.5;te_playername;" .. fgettext("Name") .. ";" ..
138194
core.formspec_escape(core.settings:get("name")) .. "]" ..
@@ -227,9 +283,21 @@ local function main_button_handler(this, fields, name, tabdata)
227283

228284
-- Update last game
229285
local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
286+
local game_obj
230287
if world then
231-
local game = pkgmgr.find_by_gameid(world.gameid)
232-
core.settings:set("menu_last_game", game.id)
288+
game_obj = pkgmgr.find_by_gameid(world.gameid)
289+
core.settings:set("menu_last_game", game_obj.id)
290+
end
291+
292+
local disabled_settings = get_disabled_settings(game_obj)
293+
for k, _ in pairs(valid_disabled_settings) do
294+
local v = disabled_settings[k]
295+
if v ~= nil then
296+
if k == "enable_server" and v == true then
297+
error("Setting 'enable_server' cannot be force-enabled! The game.conf needs to be fixed.")
298+
end
299+
core.settings:set_bool(k, disabled_settings[k])
300+
end
233301
end
234302

235303
if core.settings:get_bool("enable_server") then

‎doc/lua_api.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,16 @@ The game directory can contain the following files:
7777
`disallowed_mapgens`.
7878
* `disallowed_mapgen_settings= <comma-separated mapgen settings>`
7979
e.g. `disallowed_mapgen_settings = mgv5_spflags`
80-
These settings are hidden for this game in the world creation
80+
These mapgen settings are hidden for this game in the world creation
8181
dialog and game start menu.
82+
* `disabled_settings = <comma-separated settings>`
83+
e.g. `disabled_settings = enable_damage, creative_mode`
84+
These settings are hidden for this game in the "Start game" tab
85+
and will be initialized as `false` when the game is started.
86+
Prepend a setting name with an exclamation mark to initialize it to `true`
87+
(this does not work for `enable_server`).
88+
Only these settings are supported:
89+
`enable_damage`, `creative_mode`, `enable_server`.
8290
* `author`: The author of the game. It only appears when downloaded from
8391
ContentDB.
8492
* `release`: Ignore this: Should only ever be set by ContentDB, as it is

0 commit comments

Comments
 (0)
Please sign in to comment.