Add /themes and /set_theme commands

Allowing the user to set their preferred theme!
This commit is contained in:
Jaidyn Ann 2024-08-05 23:01:04 -05:00
parent 26e655e2e2
commit f336172dfb

View File

@ -15,17 +15,22 @@
fs_themes = {} fs_themes = {}
fs_themes.themes = {} fs_themes.themes = {}
fs_themes.modpath = minetest.get_modpath(minetest.get_current_modname())
local storage = minetest.get_mod_storage()
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.
function fs_themes.set_theme(player, theme_name) -- If dont_save is provided, the players default theme wont change.
function fs_themes.set_theme(player, theme_name, dont_save)
if not fs_themes.themes[theme_name] then if not fs_themes.themes[theme_name] then
minetest.log("warning", string.format("Theme “%s” was not found.", theme_name)) minetest.log("warning", string.format("Theme “%s” was not found.", theme_name))
return return false
elseif not dont_save then
storage:set_string(player:get_player_name(), theme_name)
end end
player:set_formspec_prepend(fs_themes.themes[theme_name]) player:set_formspec_prepend(fs_themes.themes[theme_name])
return true
end end
@ -34,25 +39,82 @@ function fs_themes.register_theme(name, formspecs_file_path)
local fs_file, open_err = io.open(formspecs_file_path, "r") local fs_file, open_err = io.open(formspecs_file_path, "r")
if fs_file == nil then if fs_file == nil then
minetest.log("warning", string.format("Theme “%s” could not be opened: %s", name, open_err)) minetest.log("warning", string.format("Theme “%s” could not be opened: %s", name, open_err))
return return false
end end
local fs_file_contents = fs_file:read("*all") local fs_file_contents = fs_file:read("*all")
fs_themes.themes[name] = fs_file_contents fs_themes.themes[name] = fs_file_contents
return true
end end
-- Whenever a play joins, set their formspecs theme. minetest.register_chatcommand(
"themes",
{
description = "Lists available GUI (Formspecs) themes.",
func = function(name, param)
local ret = nil
for name,fs in pairs(fs_themes.themes) do
if ret == nil then
ret = name
else
ret = ret .. ", " .. name
end
end
if ret == nil then
ret = "No themes are installed!"
end
return true, ret
end
}
)
minetest.register_chatcommand(
"set_theme",
{
params = "<theme_name>",
description = "Sets your theme for GUI (Formspecs) windows.",
func = function(name, param)
if fs_themes.set_theme(minetest.get_player_by_name(name), param) then
return true, "Theme set!"
elseif param ~= "" then
return false, string.format("No such theme “%s”.", param)
else
return false
end
end
}
)
-- Whenever a player joins, set their formspecs theme.
minetest.register_on_joinplayer( minetest.register_on_joinplayer(
function(player) function(player)
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") or "restaurant"
local player_theme = storage:get_string(player_name)
-- 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
minetest.chat_send_player(
player_name,
string.format("The theme “%s” no longer exists! Resetting preference.", player_theme))
player_theme = ""
storage:set_string(player_name, "")
end
if player_theme == "" then
fs_themes.set_theme(player, default_theme) fs_themes.set_theme(player, default_theme)
end end
end
) )
-- Register all themes in the ./themes/ directory, one by one. -- Register all themes in the ./themes/ directory, one by one.
local theme_dir_path = fs_themes.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) fs_themes.register_theme(file_name, theme_dir_path .. file_name)