quantum-space-buddies/QSB/GeyserSync/GeyserEvent.cs

42 lines
1.2 KiB
C#
Raw Normal View History

2020-08-13 13:32:58 +00:00
using QSB.Events;
using QSB.Messaging;
using QSB.WorldSync;
namespace QSB.GeyserSync
{
public class GeyserEvent : QSBEvent<GeyserMessage>
{
public override MessageType Type => MessageType.Geyser;
public override void SetupListener()
{
2020-08-15 19:52:43 +00:00
GlobalMessenger<int, bool>.AddListener(EventNames.QSBGeyserState, Handler);
2020-08-13 13:32:58 +00:00
}
2020-08-15 19:55:25 +00:00
2020-08-15 19:32:58 +00:00
public override void CloseListener()
{
2020-08-15 19:52:43 +00:00
GlobalMessenger<int, bool>.RemoveListener(EventNames.QSBGeyserState, Handler);
2020-08-15 19:32:58 +00:00
}
2020-08-15 19:52:43 +00:00
private void Handler(int id, bool state) => SendEvent(CreateMessage(id, state));
2020-08-13 18:43:47 +00:00
private GeyserMessage CreateMessage(int id, bool state) => new GeyserMessage
2020-08-13 13:32:58 +00:00
{
2020-08-17 15:51:56 +00:00
FromId = PlayerRegistry.LocalPlayer.NetId,
2020-08-17 16:59:13 +00:00
AboutId = PlayerRegistry.LocalPlayer.NetId,
2020-08-13 18:47:23 +00:00
ObjectId = id,
2020-08-13 13:32:58 +00:00
State = state
};
public override void OnReceiveRemote(GeyserMessage message)
{
2020-08-16 20:39:21 +00:00
if (!QSBSceneManager.IsInUniverse)
2020-08-13 13:32:58 +00:00
{
return;
}
2020-08-13 18:47:23 +00:00
var geyser = WorldRegistry.GetObject<QSBGeyser>(message.ObjectId);
2020-08-16 20:39:21 +00:00
geyser?.SetState(message.State);
2020-08-13 13:32:58 +00:00
}
}
}