1- using QSB . Messaging ;
1+ using System . Collections . Generic ;
2+ using QSB . Messaging ;
23using UnityEngine ;
34using System . Linq ;
45using QSB . Utility ;
5- using UnityEngine . SceneManagement ;
66
77namespace QSB . TransformSync
88{
99 public class SectorSync : MonoBehaviour
1010 {
11- private Sector [ ] _allSectors ;
11+ private readonly List < Sector > _allSectors = new List < Sector > ( ) ;
1212 private MessageHandler < SectorMessage > _sectorHandler ;
1313
1414 private readonly Sector . Name [ ] _sectorBlacklist =
@@ -19,7 +19,19 @@ public class SectorSync : MonoBehaviour
1919
2020 private void Awake ( )
2121 {
22- SceneManager . sceneLoaded += OnSceneLoaded ;
22+ QSB . Helper . Events . Subscribe < Sector > ( OWML . Common . Events . AfterAwake ) ;
23+ QSB . Helper . Events . Event += OnEvent ;
24+ }
25+
26+ private void OnEvent ( MonoBehaviour behaviour , OWML . Common . Events ev )
27+ {
28+ if ( behaviour is Sector sector && ev == OWML . Common . Events . AfterAwake )
29+ {
30+ if ( ! _allSectors . Contains ( sector ) )
31+ {
32+ _allSectors . Add ( sector ) ;
33+ }
34+ }
2335 }
2436
2537 private void Start ( )
@@ -29,11 +41,6 @@ private void Start()
2941 _sectorHandler . OnServerReceiveMessage += OnServerReceiveMessage ;
3042 }
3143
32- private void OnSceneLoaded ( Scene scene , LoadSceneMode mode )
33- {
34- _allSectors = FindObjectsOfType < Sector > ( ) ;
35- }
36-
3744 private void SendSector ( uint id , Sector sector )
3845 {
3946 DebugLog . ToScreen ( $ "Sending sector { sector . name } for id { id } ") ;
@@ -50,23 +57,24 @@ private void SendSector(uint id, Sector sector)
5057 private Sector FindSectorByName ( Sector . Name sectorName , string goName )
5158 {
5259 return _allSectors ?
53- . FirstOrDefault ( sector => sector . GetName ( ) == sectorName && sector . name == goName ) ;
60+ . FirstOrDefault ( sector => sector != null &&
61+ sector . GetName ( ) == sectorName &&
62+ sector . name == goName ) ;
5463 }
5564
5665 private void OnClientReceiveMessage ( SectorMessage message )
5766 {
58- var sectorName = ( Sector . Name ) message . SectorId ;
5967 DebugLog . ToScreen ( $ "Received sector { message . SectorName } for id { message . SenderId } ") ;
6068
61- var sector = FindSectorByName ( sectorName , message . SectorName ) ;
69+ var sector = FindSectorByName ( ( Sector . Name ) message . SectorId , message . SectorName ) ;
6270
6371 if ( sector == null )
6472 {
65- DebugLog . ToScreen ( $ "Sector { sectorName } not found") ;
73+ DebugLog . ToScreen ( $ "Sector { message . SectorName } not found") ;
6674 return ;
6775 }
6876
69- DebugLog . ToScreen ( $ "Found sector { sectorName } for { message . SenderId } ") ;
77+ DebugLog . ToScreen ( $ "Found sector { message . SectorName } for { message . SenderId } ") ;
7078 PlayerRegistry . GetTransformSync ( message . SenderId ) . ReferenceTransform = sector . transform ;
7179 }
7280
@@ -105,7 +113,8 @@ private void UpdateTransformSync(TransformSync transformSync)
105113 private Sector GetClosestSector ( Transform trans )
106114 {
107115 return _allSectors ?
108- . Where ( sector => ! _sectorBlacklist . Contains ( sector . GetName ( ) ) )
116+ . Where ( sector => sector != null &&
117+ ! _sectorBlacklist . Contains ( sector . GetName ( ) ) )
109118 . OrderBy ( sector => Vector3 . Distance ( sector . transform . position , trans . position ) )
110119 . First ( ) ;
111120 }
0 commit comments