quantum-space-buddies/QSB/Player/Events/ServerSendPlayerStatesEvent.cs

39 lines
1.3 KiB
C#
Raw Normal View History

2020-11-04 09:34:01 +00:00
using OWML.Common;
2020-12-14 16:24:52 +00:00
using QSB.Events;
2021-04-11 16:05:02 +00:00
using QSB.TransformSync;
2020-11-04 09:34:01 +00:00
using QSB.Utility;
namespace QSB.Player.Events
{
2020-12-02 21:23:01 +00:00
public class ServerSendPlayerStatesEvent : QSBEvent<PlayerStateMessage>
{
public override EventType Type => EventType.PlayerState;
2020-12-02 21:23:01 +00:00
public override void SetupListener() => GlobalMessenger.AddListener(EventNames.QSBServerSendPlayerStates, Handler);
public override void CloseListener() => GlobalMessenger.RemoveListener(EventNames.QSBServerSendPlayerStates, Handler);
2020-12-02 21:23:01 +00:00
private void Handler()
{
foreach (var player in QSBPlayerManager.PlayerList)
{
DebugLog.DebugWrite($" - Sending playerstate of player ID {player.PlayerId}", MessageType.Info);
SendEvent(CreateMessage(player));
}
}
2020-12-02 21:23:01 +00:00
private PlayerStateMessage CreateMessage(PlayerInfo player) => new PlayerStateMessage
{
AboutId = player.PlayerId,
PlayerName = player.Name,
2021-04-18 17:42:22 +00:00
PlayerState = player.PlayerStates
2020-12-02 21:23:01 +00:00
};
2020-12-11 22:42:21 +00:00
public override void OnReceiveRemote(bool server, PlayerStateMessage message)
2020-12-02 21:23:01 +00:00
{
DebugLog.DebugWrite($"Received playerstate of player ID {message.AboutId}", MessageType.Info);
2021-03-13 10:17:52 +00:00
QSBCore.UnityEvents.RunWhen(
2021-04-11 16:05:02 +00:00
() => QSBPlayerManager.GetSyncObject<SyncObjectTransformSync>(message.AboutId) != null,
2020-12-02 21:23:01 +00:00
() => QSBPlayerManager.HandleFullStateMessage(message));
}
}
2020-12-03 08:28:05 +00:00
}