diff --git a/client/time.lua b/client/time.lua index c8baa1c..ea07fae 100644 --- a/client/time.lua +++ b/client/time.lua @@ -1,25 +1,54 @@ local timeScale = GlobalState.timeScale +local currentTime = GlobalState.currentTime +local timeFrozen = GlobalState.freezeTime +local playerState = LocalPlayer.state AddStateBagChangeHandler('currentTime', nil, function(bagName, _, value) if bagName == 'global' and value and next(value) then - NetworkOverrideClockTime(value.hour, value.minute, 0) + + if playerState.syncWeather then + NetworkOverrideClockTime(value.hour, value.minute, 0) + end + + currentTime = value end end) AddStateBagChangeHandler('timeScale', nil, function(bagName, _, value) if bagName == 'global' and value then - NetworkOverrideClockMillisecondsPerGameMinute(value) + + if playerState.syncWeather then + NetworkOverrideClockMillisecondsPerGameMinute(value) + end + timeScale = value end end) AddStateBagChangeHandler('freezeTime', nil, function(bagName, _, value) if bagName == 'global' then - NetworkOverrideClockMillisecondsPerGameMinute(value and 99999999 or timeScale) + + if playerState.syncWeather then + NetworkOverrideClockMillisecondsPerGameMinute(value and 99999999 or timeScale) + end + + timeFrozen = value end end) NetworkOverrideClockMillisecondsPerGameMinute(timeScale) +AddStateBagChangeHandler('syncWeather', ('player:%s'):format(cache.serverId), function(_, _, value) + if value then + NetworkOverrideClockTime(currentTime.hour, currentTime.minute, 0) + if not timeFrozen then + NetworkOverrideClockMillisecondsPerGameMinute(timeScale) + end + else + NetworkOverrideClockMillisecondsPerGameMinute(99999999) + NetworkOverrideClockTime(18, 0, 0) + end +end) + require 'compatability.qb.client' \ No newline at end of file diff --git a/client/weather.lua b/client/weather.lua index 30c7f98..a5b7694 100644 --- a/client/weather.lua +++ b/client/weather.lua @@ -1,5 +1,6 @@ local serverWeather = GlobalState.weather local hadSnow = false +local playerState = LocalPlayer.state local function resetWeatherParticles() if hadSnow then @@ -26,29 +27,39 @@ local function setWeatherParticles() end end -AddStateBagChangeHandler('weather', nil, function(bagName, _, value) - if value and bagName == 'global' then - SetRainLevel(-1.0) +local function setWeather(forceSwap) + SetRainLevel(-1.0) - SetWeatherTypeOvertimePersist(value.weather, 60.0) + if forceSwap then + SetWeatherTypeNowPersist(serverWeather.weather) + else + SetWeatherTypeOvertimePersist(serverWeather.weather, 60.0) + end - if value.windDirection then - SetWindDirection(math.rad(value.WindDirection)) - end + if serverWeather.windDirection then + SetWindDirection(math.rad(serverWeather.WindDirection)) + end - if value.windSpeed then - SetWind(value.windSpeed / 2) - end + if serverWeather.windSpeed then + SetWind(serverWeather.windSpeed / 2) + end - if value.hasSnow then - setWeatherParticles() - end + if serverWeather.hasSnow then + setWeatherParticles() + end - if not value.hasSnow and hadSnow then - resetWeatherParticles() - end + if not serverWeather.hasSnow and hadSnow then + resetWeatherParticles() + end +end +AddStateBagChangeHandler('weather', nil, function(bagName, _, value) + if value and bagName == 'global' then serverWeather = value + + if playerState.syncWeather then + setWeather() + end end end) @@ -64,18 +75,24 @@ CreateThread(function () SetWind(0.1) WaterOverrideSetStrength(0.5) - if serverWeather.windDirection then - SetWindDirection(math.rad(serverWeather.windDirection)) - end + setWeather(true) - if serverWeather.windSpeed then - SetWind(serverWeather.windSpeed / 2) - end + playerState.syncWeather = true +end) - if serverWeather.hasSnow then - setWeatherParticles() +AddStateBagChangeHandler('syncWeather', ('player:%s'):format(cache.serverId), function(_, _, value) + if value then + SetTimeout(0, function() + while not playerState.syncWeather do + SetRainLevel(0.0) + SetWeatherTypePersist('CLEAR') + SetWeatherTypeNow('CLEAR') + SetWeatherTypeNowPersist('CLEAR') + NetworkOverrideClockTime(18, 0, 0) + Wait(5000) + end + end) + else + setWeather(true) end - - SetRainLevel(-1.0) - SetWeatherTypeNowPersist(serverWeather.weather) end) \ No newline at end of file diff --git a/compatability/qb/client.lua b/compatability/qb/client.lua index 963ce91..b1f8ecd 100644 --- a/compatability/qb/client.lua +++ b/compatability/qb/client.lua @@ -1,53 +1,10 @@ if GetResourceState('qb-core') == 'missing' and GetResourceState('qbx_core') == 'missing' then return end +local playerState = LocalPlayer.state -local disablesync = false -local currentTime = GlobalState.currentTime -local currentWeather = GlobalState.weather - -AddStateBagChangeHandler('currentTime', nil, function(bagName, _, value) - if bagName == 'global' and value and next(value) then - currentTime = value - end -end) - -AddStateBagChangeHandler('weather', nil, function(bagName, _, value) - if value and bagName == 'global' then - currentWeather = value - end +RegisterNetEvent('qb-weathersync:client:DisableSync', function() + playerState.syncWeather = false end) -local function disabledSync() - if not disablesync then - disablesync = true - NetworkOverrideClockMillisecondsPerGameMinute(99999999) - CreateThread(function() - while disablesync do - SetRainLevel(0.0) - SetWeatherTypePersist('CLEAR') - SetWeatherTypeNow('CLEAR') - SetWeatherTypeNowPersist('CLEAR') - NetworkOverrideClockTime(18, 0, 0) - Wait(5000) - end - end) - end -end - -local function enabledSync() - if disablesync then - NetworkOverrideClockTime(currentTime.hour, currentTime.minute, 0) - - if not GlobalState.freezeTime then - NetworkOverrideClockMillisecondsPerGameMinute(GlobalState.timeScale) - end - - SetWeatherTypePersist(currentWeather.weather) - SetWeatherTypeNow(currentWeather.weather) - SetWeatherTypeNowPersist(currentWeather.weather) - - disablesync = false - end -end - -RegisterNetEvent('qb-weathersync:client:DisableSync', disabledSync) -RegisterNetEvent('qb-weathersync:client:EnableSync', enabledSync) \ No newline at end of file +RegisterNetEvent('qb-weathersync:client:EnableSync', function() + playerState.syncWeather = true +end) \ No newline at end of file