Implement doDaylightCycle, add on_change hook for tunables and make sure they are not called when loaded

This commit is contained in:
teknomunk 2024-05-31 18:53:13 -05:00
parent 38ed457211
commit d8af2d4ced
2 changed files with 37 additions and 6 deletions

View file

@ -27,22 +27,27 @@ local tunable_types = {
-- Tunable metatable functions -- Tunable metatable functions
local tunable_class = {} local tunable_class = {}
function tunable_class:set(value) function tunable_class:set(value, no_hook)
local self_type = self.type local self_type = self.type
if type(value) == "string" then if type(value) == "string" then
local new_value = self_type.from_string(value) local new_value = self_type.from_string(value)
if new_value == nil then new_value = self.default end if new_value == nil then new_value = self.default end
minetest.log("action","new_value="..dump(new_value))
self[1] = new_value self[1] = new_value
else else
self[1] = value self[1] = value
end end
local setting = self.setting minetest.log("action", "[vl_tuning] Set "..self.setting.." to "..dump(self[1]))
if setting then
storage:set_string(setting,self_type.to_string(self[1])) -- Call on_change hook
if not no_hook then
local hook = self.on_change
if hook then hook(self) end
end end
-- Persist value
storage:set_string(self.setting,self_type.to_string(self[1]))
end end
function tunable_class:get_string() function tunable_class:get_string()
return self.type.to_string(self[1]) return self.type.to_string(self[1])
@ -63,7 +68,8 @@ function mod.setting(setting, setting_type, def )
-- Load the setting value from mod storage -- Load the setting value from mod storage
local setting_value = storage:get_string(setting) local setting_value = storage:get_string(setting)
if setting_value and setting_value ~= "" then if setting_value and setting_value ~= "" then
tunable:set(setting_value) tunable:set(setting_value, true)
minetest.log("action", "[vl_tuning] Loading "..setting.." = "..dump(setting_value).." ("..dump(tunable[1])..")")
end end
-- Add to the list of all available settings -- Add to the list of all available settings

View file

@ -1,6 +1,9 @@
-- Constants -- Constants
local modname = minetest.get_current_modname() local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname) local modpath = minetest.get_modpath(modname)
local S = minetest.get_translator(modname)
local mods_loaded = false
local NIGHT_VISION_RATIO = 0.45 local NIGHT_VISION_RATIO = 0.45
local DEBUG = false local DEBUG = false
@ -10,6 +13,28 @@ local minimum_update_interval = { 250e3 }
-- Module state -- Module state
local mods_loaded = false local mods_loaded = false
-- Daylight cycle handling
local fixed_time = vl_tuning.setting("fixed_daylight_time", "number", {
description = S("Time of day to use when gamerule:doDaylightCycle == false"),
default = 0.5
})
local gamerule_doDaylightCycle = vl_tuning.setting("gamerule:doDaylightCycle", "bool",{
description = S("Whether the daylight cycle and moon phases progress"),
default = true,
on_change = function(self)
if not self[1] then
fixed_time:set(minetest.get_timeofday())
end
end
})
local function daylightCycle()
if not gamerule_doDaylightCycle[1] and fixed_time[1] then
minetest.set_timeofday(fixed_time[1])
end
minetest.after(1, daylightCycle)
end
minetest.after(1, daylightCycle)
function mcl_weather.set_sky_box_clear(player, sky, fog) function mcl_weather.set_sky_box_clear(player, sky, fog)
-- Make sure the player's head isn't in water before changing the skybox -- Make sure the player's head isn't in water before changing the skybox
local node_head = mcl_playerinfo[player:get_player_name()].node_head local node_head = mcl_playerinfo[player:get_player_name()].node_head