From cbee3e334676ffe671b7b1670b75680ab14e6d71 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Thu, 30 May 2024 18:23:47 -0500 Subject: [PATCH] Finish initial implementation of setting tuning with get/set commands --- mods/CORE/vl_tuning/init.lua | 97 +++++++++++++++++++++++++++++++----- mods/HUD/awards/api.lua | 3 ++ mods/HUD/awards/mod.conf | 2 +- 3 files changed, 88 insertions(+), 14 deletions(-) diff --git a/mods/CORE/vl_tuning/init.lua b/mods/CORE/vl_tuning/init.lua index a7e7df597..ee99f0121 100644 --- a/mods/CORE/vl_tuning/init.lua +++ b/mods/CORE/vl_tuning/init.lua @@ -1,27 +1,76 @@ local modname = minetest.get_current_modname() local S = minetest.get_translator(modname) - +local storage = minetest.get_mod_storage() local mod = {} vl_tuning = mod +-- local tunables = {} -function mod.register_tunable(name, default, setting, setting_type) - local tunable = { - [1] = default, - setting = setting, - setting_type = setting_type, - } - if setting then - if setting_type == "bool" then - tunable[1] = minetest.settings:get_bool(.setting) - else - tunable[1] = minetest.settings:get(setting) +-- Supported variable types +local tunable_types = { + bool = { + to_string = tostring, + from_string = function(value) + return (value == "true") end + }, + number = { + to_string = tostring, + from_string = tonumber, + }, + string = { + to_string = function(v) return v end, + from_string = function(v) return v end, + }, +} + +-- Tunable metatable functions +local tunable_class = {} +function tunable_class:set(value) + local self_type = self.type + if type(value) == "string" then + self[1] = self_type.from_string(value) + else + self[1] = value + end + + local setting = self.setting + if setting then + storage:set_string(setting,self_type.to_string(self[1])) end end +function tunable_class:get_string() + print(dump(self)) + return self.type.to_string(self[1]) +end -minetest.register_chatcommand("tune", { +function mod.get_server_setting(name, description, default, setting, setting_type, options ) + local tunable = tunables[name] + if tunable then return tunable end + + tunable = { + name = name, + setting = setting, + description = description, + type = tunable_types[setting_type], + options = options, + } + tunable[1] = default + print(dump(tunable)) + setmetatable(tunable, {__index=tunable_class}) + if setting then + local setting_value = storage:get_string(setting) + if setting_value and setting_value ~= "" then + tunable:set(setting_value) + end + end + print(dump(tunable)) + tunables[name] = tunable + return tunable +end + +minetest.register_chatcommand("set_setting", { description = S("Admin tool to tune settings and game rules"), params = S(" "), privs = { debug = true }, @@ -35,6 +84,28 @@ minetest.register_chatcommand("tune", { if #params ~= 2 then return false, S("Usage: /tune ") end + + local tunable = tunables[params[1]] + if tunable then + minetest.log("action", "[vl_tuning] "..name.." set ".. params[1] .." to "..params[2]) + tunable:set(params[2]) + return true + else + return false, S("Setting @1 doesn't exist", params[1]) + end + end +}) +minetest.register_chatcommand("get_setting", { + description = S("Admin tool to view settings and game rules"), + params = S(""), + privs = { debug = true }, + func = function(name, param) + local tunable = tunables[param] + if tunable then + return true, tunable:get_string() + else + return false, S("Setting @1 doesn't exist", param) + end end }) diff --git a/mods/HUD/awards/api.lua b/mods/HUD/awards/api.lua index ca87a812c..9299fc3fd 100644 --- a/mods/HUD/awards/api.lua +++ b/mods/HUD/awards/api.lua @@ -18,6 +18,9 @@ local modname = minetest.get_current_modname() local modpath = minetest.get_modpath(modname) local S = minetest.get_translator(modname) +-- Tunable parameters +local notif_delay = vl_tuning.get_server_setting("award_display_time", S("Amount of time award notification are displayed"), 3, "awards_notif_delay", "number", { min = 2, max = 10 }) + -- The global award namespace awards = { show_mode = "hud", diff --git a/mods/HUD/awards/mod.conf b/mods/HUD/awards/mod.conf index 1657323e2..3b16dc2ca 100644 --- a/mods/HUD/awards/mod.conf +++ b/mods/HUD/awards/mod.conf @@ -6,4 +6,4 @@ license = LGPL 2.1 or later forum = https://forum.minetest.net/viewtopic.php?t=4870 version = 2.3.0 optional_depends = sfinv, unified_inventory -depends = mcl_colors +depends = mcl_colors, vl_tuning