mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-26 18:35:34 +00:00
42 lines
1.2 KiB
C#
42 lines
1.2 KiB
C#
using QSB.Events;
|
|
using QSB.Messaging;
|
|
using QSB.Player;
|
|
using QSB.Utility;
|
|
|
|
namespace QSB.ClientServerStateSync.Events
|
|
{
|
|
internal class ClientStateEvent : QSBEvent<EnumMessage<ClientState>>
|
|
{
|
|
public override bool RequireWorldObjectsReady => false;
|
|
|
|
public override void SetupListener()
|
|
=> GlobalMessenger<ClientState>.AddListener(EventNames.QSBClientState, Handler);
|
|
|
|
public override void CloseListener()
|
|
=> GlobalMessenger<ClientState>.RemoveListener(EventNames.QSBClientState, Handler);
|
|
|
|
private void Handler(ClientState state) => SendEvent(CreateMessage(state));
|
|
|
|
private EnumMessage<ClientState> CreateMessage(ClientState state) => new()
|
|
{
|
|
AboutId = LocalPlayerId,
|
|
EnumValue = state
|
|
};
|
|
|
|
public override void OnReceiveLocal(bool server, EnumMessage<ClientState> message)
|
|
=> ClientStateManager.Instance.ChangeClientState(message.EnumValue);
|
|
|
|
public override void OnReceiveRemote(bool server, EnumMessage<ClientState> message)
|
|
{
|
|
if (message.AboutId == uint.MaxValue)
|
|
{
|
|
DebugLog.ToConsole($"Error - ID is uint.MaxValue!", OWML.Common.MessageType.Error);
|
|
return;
|
|
}
|
|
|
|
var player = QSBPlayerManager.GetPlayer(message.AboutId);
|
|
player.State = message.EnumValue;
|
|
}
|
|
}
|
|
}
|