using OWML.Common; using QSB.Events; using QSB.QuantumSync.WorldObjects; using QSB.Utility; using QSB.WorldSync; using UnityEngine; namespace QSB.QuantumSync.Events { public class SocketStateChangeEvent : QSBEvent { public override void SetupListener() => GlobalMessenger.AddListener(EventNames.QSBSocketStateChange, Handler); public override void CloseListener() => GlobalMessenger.RemoveListener(EventNames.QSBSocketStateChange, Handler); private void Handler(int objid, int socketid, Quaternion localRotation) => SendEvent(CreateMessage(objid, socketid, localRotation)); private SocketStateChangeMessage CreateMessage(int objid, int socketid, Quaternion localRotation) => new() { AboutId = LocalPlayerId, ObjectId = objid, SocketId = socketid, LocalRotation = localRotation }; public override void OnReceiveRemote(bool server, SocketStateChangeMessage message) { if (!WorldObjectManager.AllObjectsReady) { return; } var obj = QSBWorldSync.GetWorldFromId(message.ObjectId); if (obj.ControllingPlayer != message.FromId) { DebugLog.ToConsole($"Error - Got SocketStateChangeEvent for {obj.Name} from {message.FromId}, but it's currently controlled by {obj.ControllingPlayer}!", MessageType.Error); return; } obj.MoveToSocket(message); } } }