Change API so most parameters are in a definition table, add /gamerule chatcommand, implement gamerule announceAdvancements

This commit is contained in:
teknomunk 2024-05-30 21:04:37 -05:00
parent c98f2e63ff
commit d02ddae55b
2 changed files with 62 additions and 27 deletions

@ -4,7 +4,7 @@ local storage = minetest.get_mod_storage()
local mod = {}
vl_tuning = mod
--
-- All registered tunable parameters
local tunables = {}
-- Supported variable types
@ -45,29 +45,28 @@ function tunable_class:get_string()
return self.type.to_string(self[1])
end
function mod.get_server_setting(name, description, default, setting, setting_type, options )
local tunable = tunables[name]
function mod.setting(setting, setting_type, def )
-- return the existing setting if it was previously registered. Don't update the definition
local tunable = tunables[setting]
if tunable then return tunable end
tunable = {
name = name,
default = default,
setting = setting,
description = description,
type = tunable_types[setting_type],
options = options,
}
tunable[1] = default
print(dump(tunable))
-- Setup the tunable data
tunable = table.copy(def)
tunable.setting = setting
tunable.type = tunable_types[setting_type]
tunable[1] = tunable.default
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
-- Load the setting value from mod storage
local setting_value = storage:get_string(setting)
if setting_value and setting_value ~= "" then
tunable:set(setting_value)
end
print(dump(tunable))
tunables[name] = tunable
-- Add to the list of all available settings
tunables[setting] = tunable
-- Provide it so that the current value in [1] can be accessed without having to call into this API again
return tunable
end
@ -87,13 +86,13 @@ minetest.register_chatcommand("set_setting", {
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
if not tunable then
return false, S("Setting @1 doesn't exist", params[1])
end
minetest.log("action", "[vl_tuning] "..name.." set ".. params[1] .." to "..params[2])
tunable:set(params[2])
return true
end
})
minetest.register_chatcommand("get_setting", {
@ -110,3 +109,34 @@ minetest.register_chatcommand("get_setting", {
end
})
minetest.register_chatcommand("gamerule", {
description = S("Display or set customizable options"),
params = S("<rule> [<value>]"),
privs = { server = true },
func = function(name, params_raw)
-- Split apart the params
local params = {}
for str in string.gmatch(params_raw, "([^ ]+)") do
params[#params + 1] = str
end
if #params < 1 or #params > 2 then
return false, S("Usage: /gamerule <rule> [<value>]")
end
local tunable = tunables["gamerule:"..params[1]]
if not tunable then
return false, S("Game rule @1 doesn't exist", params[1])
end
local value = params[2]
if value then
minetest.log("action", "[vl_tuning] Setting game rule "..params[1].." to "..params[2])
tunable:set(params[2])
return true
else
return true, tunable:get_string()
end
end
})

@ -19,7 +19,12 @@ 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 })
local notif_delay = vl_tuning.setting("award_display_time", "number", {
description = S("Amount of time award notification are displayed"), default = 3, min = 2, max = 10
})
local announce_in_chat = vl_tuning.setting("gamerule:announceAdvancements", "bool", {
description = S("Whether advancements should be announced in chat"), default = true,
})
-- The global award namespace
awards = {
@ -220,7 +225,7 @@ function awards.unlock(name, award)
-- Get award
minetest.log("action", name.." has gotten award "..award)
if minetest.settings:get_bool("mcl_showAdvancementMessages", true) then
if minetest.settings:get_bool("mcl_showAdvancementMessages", true) or announce_in_chat[1] then
minetest.chat_send_all(S("@1 has made the advancement @2", name, minetest.colorize(mcl_colors.GREEN, "[" .. (awdef.title or award) .. "]")))
end
data.unlocked[award] = award