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 f0d9c5c83c
commit d4286ff535
2 changed files with 37 additions and 6 deletions

@ -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

@ -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