quantum-space-buddies/QSB/Tools/ProbeLauncherTool/Messages/EquipProbeLauncherMessage.cs
2021-12-26 18:55:54 -08:00

51 lines
1.4 KiB
C#

using OWML.Common;
using QSB.Messaging;
using QSB.Player;
using QSB.Player.TransformSync;
using QSB.Utility;
namespace QSB.Tools.ProbeLauncherTool.Messages
{
public class EquipProbeLauncherMessage : QSBBoolMessage
{
static EquipProbeLauncherMessage()
{
GlobalMessenger<ProbeLauncher>.AddListener(OWEvents.ProbeLauncherEquipped, launcher => Handle(launcher, true));
GlobalMessenger<ProbeLauncher>.AddListener(OWEvents.ProbeLauncherUnequipped, launcher => Handle(launcher, false));
}
private static bool _nonPlayerLauncherEquipped;
private static void Handle(ProbeLauncher launcher, bool equipped)
{
if (PlayerTransformSync.LocalInstance)
{
if (launcher != QSBPlayerManager.LocalPlayer.LocalProbeLauncher)
{
_nonPlayerLauncherEquipped = equipped;
return;
}
if (_nonPlayerLauncherEquipped)
{
DebugLog.ToConsole($"Warning - Trying to equip/unequip player launcher whilst non player launcher is still equipped?", MessageType.Warning);
return;
}
new EquipProbeLauncherMessage(equipped).Send();
}
}
private EquipProbeLauncherMessage(bool equipped) => Value = equipped;
public override void OnReceiveRemote()
{
var player = QSBPlayerManager.GetPlayer(From);
player.ProbeLauncherEquipped = Value;
player.ProbeLauncher?.ChangeEquipState(Value);
}
public override void OnReceiveLocal() =>
QSBPlayerManager.LocalPlayer.ProbeLauncherEquipped = Value;
}
}