Save a “default” theme, inherited from other mods

Most likely minetest_game’s `default` mod.
This commit is contained in:
Jaidyn Ann 2024-08-06 22:07:14 -05:00
parent f336172dfb
commit 07c7618d3d
2 changed files with 20 additions and 16 deletions

View File

@ -19,6 +19,7 @@ fs_themes.themes = {}
local storage = minetest.get_mod_storage() local storage = minetest.get_mod_storage()
local modpath = minetest.get_modpath(minetest.get_current_modname()) local modpath = minetest.get_modpath(minetest.get_current_modname())
-- Set a players theme by a registered themes name. -- Set a players theme by a registered themes name.
-- If dont_save is provided, the players default theme wont change. -- If dont_save is provided, the players default theme wont change.
function fs_themes.set_theme(player, theme_name, dont_save) function fs_themes.set_theme(player, theme_name, dont_save)
@ -34,16 +35,9 @@ function fs_themes.set_theme(player, theme_name, dont_save)
end end
-- Register a theme, given a name and a formspec-files path. -- Register a theme, given a name and a formspec string.
function fs_themes.register_theme(name, formspecs_file_path) function fs_themes.register_theme(name, formspec_str)
local fs_file, open_err = io.open(formspecs_file_path, "r") fs_themes.themes[name] = formspec_str
if fs_file == nil then
minetest.log("warning", string.format("Theme “%s” could not be opened: %s", name, open_err))
return false
end
local fs_file_contents = fs_file:read("*all")
fs_themes.themes[name] = fs_file_contents
return true return true
end end
@ -61,11 +55,9 @@ minetest.register_chatcommand(
ret = ret .. ", " .. name ret = ret .. ", " .. name
end end
end end
if ret == nil then if ret == nil then
ret = "No themes are installed!" ret = "No themes are installed!"
end end
return true, ret return true, ret
end end
} }
@ -94,9 +86,14 @@ minetest.register_chatcommand(
minetest.register_on_joinplayer( minetest.register_on_joinplayer(
function(player) function(player)
local player_name = player:get_player_name() local player_name = player:get_player_name()
local default_theme = minetest.settings:get("fs_default_theme") or "restaurant" local default_theme = minetest.settings:get("fs_default_theme")
local player_theme = storage:get_string(player_name) local player_theme = storage:get_string(player_name)
-- Set the “default” theme (inherited from whatever mods load before fs_themes)
if not fs_themes.themes["default"] then
fs_themes.themes["default"] = player:get_formspec_prepend()
end
-- If a player has a preferred theme we cannot use, complain & use default. -- If a player has a preferred theme we cannot use, complain & use default.
if player_theme ~= "" and not fs_themes.set_theme(player, player_theme) then if player_theme ~= "" and not fs_themes.set_theme(player, player_theme) then
minetest.chat_send_player( minetest.chat_send_player(
@ -106,7 +103,7 @@ minetest.register_on_joinplayer(
storage:set_string(player_name, "") storage:set_string(player_name, "")
end end
if player_theme == "" then if player_theme == "" and default_theme and default_theme ~= "" then
fs_themes.set_theme(player, default_theme) fs_themes.set_theme(player, default_theme)
end end
end end
@ -117,5 +114,12 @@ minetest.register_on_joinplayer(
local theme_dir_path = modpath .. "/themes/" local theme_dir_path = modpath .. "/themes/"
local theme_file_paths = minetest.get_dir_list(theme_dir_path, false) local theme_file_paths = minetest.get_dir_list(theme_dir_path, false)
for _, file_name in pairs(theme_file_paths) do for _, file_name in pairs(theme_file_paths) do
fs_themes.register_theme(file_name, theme_dir_path .. file_name) local fs_file, open_err = io.open(theme_dir_path .. file_name, "r")
if fs_file == nil then
minetest.log("warning", string.format("Theme “%s” could not be opened: %s", name, open_err))
return false
end
local fs_file_contents = fs_file:read("*all")
fs_themes.register_theme(file_name, fs_file_contents)
end end

View File

@ -1 +1 @@
fs_default_theme (Default theme for Formspecs) string restaurant fs_default_theme (Default theme for Formspecs) string