minetest-fs_themes/init.lua

122 lines
3.6 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 = {}
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 players theme by a registered themes name.
-- If dont_save is provided, the players default theme wont 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))
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])
return true
2024-08-05 20:52:12 -05:00
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 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
return true
2024-08-05 20:52:12 -05:00
end
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)
local player_name = player:get_player_name()
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)
end
2024-08-05 20:52:12 -05:00
end
)
-- Register all themes in the ./themes/ directory, one by one.
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