minetest-fs_themes/init.lua

60 lines
2.1 KiB
Lua
Raw Normal View History

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 = {}
fs_themes.modpath = minetest.get_modpath(minetest.get_current_modname())
-- Set a players theme by a registered themes name.
function fs_themes.set_theme(player, theme_name)
if not fs_themes.themes[theme_name] then
minetest.log("warning", string.format("Theme “%s” was not found.", theme_name))
return
end
player:set_formspec_prepend(fs_themes.themes[theme_name])
end
-- Register a theme, given a name and a formspec-files 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))
return
end
local fs_file_contents = fs_file:read("*all")
fs_themes.themes[name] = fs_file_contents
end
-- Whenever a play joins, set their formspecs theme.
minetest.register_on_joinplayer(
function(player)
local default_theme = minetest.settings:get("fs_default_theme") or "restaurant"
2024-08-05 20:52:12 -05:00
fs_themes.set_theme(player, default_theme)
end
)
-- Register all themes in the ./themes/ directory, one by one.
local theme_dir_path = fs_themes.modpath .. "/themes/"
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