From d4286ff5352b0f04a27a2a6d999713256e9fd4a0 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Fri, 31 May 2024 18:53:13 -0500 Subject: [PATCH] Implement doDaylightCycle, add on_change hook for tunables and make sure they are not called when loaded --- mods/CORE/vl_tuning/init.lua | 18 ++++++++++------ mods/ENVIRONMENT/mcl_weather/skycolor.lua | 25 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/mods/CORE/vl_tuning/init.lua b/mods/CORE/vl_tuning/init.lua index 5555b9c1d..3991f8ccf 100644 --- a/mods/CORE/vl_tuning/init.lua +++ b/mods/CORE/vl_tuning/init.lua @@ -27,22 +27,27 @@ local tunable_types = { -- Tunable metatable functions local tunable_class = {} -function tunable_class:set(value) +function tunable_class:set(value, no_hook) local self_type = self.type if type(value) == "string" then local new_value = self_type.from_string(value) if new_value == nil then new_value = self.default end - minetest.log("action","new_value="..dump(new_value)) self[1] = new_value else self[1] = value end - local setting = self.setting - if setting then - storage:set_string(setting,self_type.to_string(self[1])) + minetest.log("action", "[vl_tuning] Set "..self.setting.." to "..dump(self[1])) + + -- Call on_change hook + if not no_hook then + local hook = self.on_change + if hook then hook(self) end end + + -- Persist value + storage:set_string(self.setting,self_type.to_string(self[1])) end function tunable_class:get_string() 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 local setting_value = storage:get_string(setting) 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 -- Add to the list of all available settings diff --git a/mods/ENVIRONMENT/mcl_weather/skycolor.lua b/mods/ENVIRONMENT/mcl_weather/skycolor.lua index 87952845b..72aac0ab8 100644 --- a/mods/ENVIRONMENT/mcl_weather/skycolor.lua +++ b/mods/ENVIRONMENT/mcl_weather/skycolor.lua @@ -1,3 +1,6 @@ +local modname = minetest.get_current_modname() +local S = minetest.get_translator(modname) + local mods_loaded = false local NIGHT_VISION_RATIO = 0.45 @@ -7,6 +10,28 @@ local water_color = "#3F76E4" local mg_name = minetest.get_mapgen_setting("mg_name") +-- 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) local pos = player:get_pos() if minetest.get_item_group(minetest.get_node(vector.new(pos.x,pos.y+1.5,pos.z)).name, "water") ~= 0 then return end