2024-08-05 20:52:12 -05:00
|
|
|
|
-- Copyright © 2024 Jaidyn Ann <jadedctrl@posteo.at>
|
|
|
|
|
--
|
|
|
|
|
-- This program is free software: you can redistribute it and/or
|
|
|
|
|
-- modify it under the terms of the GNU Lesser General Public License
|
|
|
|
|
-- as published by the Free Software Foundation, either version 3 of
|
|
|
|
|
-- the License, or (at your option) any later version.
|
|
|
|
|
--
|
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
-- GNU Lesser General Public License for more details.
|
|
|
|
|
--
|
|
|
|
|
-- You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
fs_themes = {}
|
|
|
|
|
fs_themes.themes = {}
|
|
|
|
|
|
2024-08-05 23:01:04 -05:00
|
|
|
|
local storage = minetest.get_mod_storage()
|
|
|
|
|
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
2024-08-05 20:52:12 -05:00
|
|
|
|
|
|
|
|
|
-- Set a player’s theme by a registered theme’s name.
|
2024-08-05 23:01:04 -05:00
|
|
|
|
-- If dont_save is provided, the player’s default theme won’t change.
|
|
|
|
|
function fs_themes.set_theme(player, theme_name, dont_save)
|
2024-08-05 20:52:12 -05:00
|
|
|
|
if not fs_themes.themes[theme_name] then
|
|
|
|
|
minetest.log("warning", string.format("Theme “%s” was not found.", theme_name))
|
2024-08-05 23:01:04 -05:00
|
|
|
|
return false
|
|
|
|
|
elseif not dont_save then
|
|
|
|
|
storage:set_string(player:get_player_name(), theme_name)
|
2024-08-05 20:52:12 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
player:set_formspec_prepend(fs_themes.themes[theme_name])
|
2024-08-05 23:01:04 -05:00
|
|
|
|
return true
|
2024-08-05 20:52:12 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Register a theme, given a name and a formspec-file’s path.
|
|
|
|
|
function fs_themes.register_theme(name, formspecs_file_path)
|
|
|
|
|
local fs_file, open_err = io.open(formspecs_file_path, "r")
|
|
|
|
|
if fs_file == nil then
|
|
|
|
|
minetest.log("warning", string.format("Theme “%s” could not be opened: %s", name, open_err))
|
2024-08-05 23:01:04 -05:00
|
|
|
|
return false
|
2024-08-05 20:52:12 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local fs_file_contents = fs_file:read("*all")
|
|
|
|
|
fs_themes.themes[name] = fs_file_contents
|
2024-08-05 23:01:04 -05:00
|
|
|
|
return true
|
2024-08-05 20:52:12 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
2024-08-05 23:01:04 -05:00
|
|
|
|
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.
|
2024-08-05 20:52:12 -05:00
|
|
|
|
minetest.register_on_joinplayer(
|
|
|
|
|
function(player)
|
2024-08-05 23:01:04 -05:00
|
|
|
|
local player_name = player:get_player_name()
|
2024-08-05 22:09:23 -05:00
|
|
|
|
local default_theme = minetest.settings:get("fs_default_theme") or "restaurant"
|
2024-08-05 23:01:04 -05:00
|
|
|
|
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)
|
|
|
|
|
end
|
2024-08-05 20:52:12 -05:00
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Register all themes in the ./themes/ directory, one by one.
|
2024-08-05 23:01:04 -05:00
|
|
|
|
local theme_dir_path = modpath .. "/themes/"
|
2024-08-05 20:52:12 -05:00
|
|
|
|
local theme_file_paths = minetest.get_dir_list(theme_dir_path, false)
|
|
|
|
|
for _, file_name in pairs(theme_file_paths) do
|
|
|
|
|
fs_themes.register_theme(file_name, theme_dir_path .. file_name)
|
|
|
|
|
end
|