Skip to content

Commit

Permalink
Feat(syncweather): use player states for disabling syncing, refactor …
Browse files Browse the repository at this point in the history
…qb-compat
  • Loading branch information
FjamZoo committed Nov 19, 2023
1 parent 57b7602 commit 88afa5e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 79 deletions.
35 changes: 32 additions & 3 deletions client/time.lua
Original file line number Diff line number Diff line change
@@ -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'
71 changes: 44 additions & 27 deletions client/weather.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local serverWeather = GlobalState.weather
local hadSnow = false
local playerState = LocalPlayer.state

local function resetWeatherParticles()
if hadSnow then
Expand All @@ -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)

Expand All @@ -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)
55 changes: 6 additions & 49 deletions compatability/qb/client.lua
Original file line number Diff line number Diff line change
@@ -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)
RegisterNetEvent('qb-weathersync:client:EnableSync', function()
playerState.syncWeather = true
end)

0 comments on commit 88afa5e

Please sign in to comment.