using OWML.Common; using QSB.Events; using QSB.Messaging; using QSB.TransformSync; using QSB.Utility; using System.Linq; namespace QSB.Player.Events { public class PlayerReadyEvent : QSBEvent { public override EventType Type => EventType.PlayerReady; public override void SetupListener() => GlobalMessenger.AddListener(EventNames.QSBPlayerReady, Handler); public override void CloseListener() => GlobalMessenger.RemoveListener(EventNames.QSBPlayerReady, Handler); private void Handler(bool ready) => SendEvent(CreateMessage(ready)); private ToggleMessage CreateMessage(bool ready) => new ToggleMessage { AboutId = LocalPlayerId, ToggleValue = ready }; public override void OnServerReceive(ToggleMessage message) { DebugLog.DebugWrite($"[S] Get ready event from {message.FromId}", MessageType.Success); if (message.FromId == QSBPlayerManager.LocalPlayerId) { return; } QSBPlayerManager.GetPlayer(message.AboutId).IsReady = message.ToggleValue; PlayerStateEvent.LocalInstance.Send(); } public override void OnReceiveRemote(ToggleMessage message) { DebugLog.DebugWrite($"Get ready event from {message.FromId}", MessageType.Success); foreach (var item in QSBPlayerManager.GetSyncObjects() .Where(x => x != null && x.IsReady && x.ReferenceSector != null && x.PlayerId == LocalPlayerId)) { GlobalMessenger.FireEvent(EventNames.QSBSectorChange, item.netId.Value, item.ReferenceSector); } } } }