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

57 lines
1.6 KiB
C#
Raw Normal View History

2021-03-09 16:43:41 +00:00
using QSB.Events;
using QSB.Menus;
2021-03-09 16:43:41 +00:00
using QSB.Messaging;
using QSB.Utility;
using QuantumUNET;
using System.Linq;
namespace QSB.Player.Events
{
2021-03-09 16:44:14 +00:00
internal class PlayerKickEvent : QSBEvent<EnumMessage<KickReason>>
2021-03-09 16:43:41 +00:00
{
public override EventType Type => EventType.PlayerKick;
public override void SetupListener() => GlobalMessenger<uint, KickReason>.AddListener(EventNames.QSBPlayerKick, Handler);
public override void CloseListener() => GlobalMessenger<uint, KickReason>.RemoveListener(EventNames.QSBPlayerKick, Handler);
private void Handler(uint player, KickReason reason) => SendEvent(CreateMessage(player, reason));
private EnumMessage<KickReason> CreateMessage(uint player, KickReason reason) => new EnumMessage<KickReason>
{
AboutId = player,
2021-03-29 13:36:16 +00:00
EnumValue = reason
2021-03-09 16:43:41 +00:00
};
public override void OnReceiveLocal(bool server, EnumMessage<KickReason> message)
{
if (!server)
{
return;
}
2021-06-18 21:38:32 +00:00
2021-03-13 10:17:52 +00:00
QSBCore.UnityEvents.FireInNUpdates(() => KickPlayer(message.AboutId), 10);
2021-03-09 16:43:41 +00:00
}
2021-03-09 16:44:14 +00:00
private void KickPlayer(uint id)
2021-03-09 16:43:41 +00:00
=> QNetworkServer.connections.First(x => x.GetPlayerId() == id).Disconnect();
public override void OnReceiveRemote(bool server, EnumMessage<KickReason> message)
{
if (message.AboutId != QSBPlayerManager.LocalPlayerId)
{
if (QSBPlayerManager.PlayerExists(message.AboutId))
{
DebugLog.ToAll($"{QSBPlayerManager.GetPlayer(message.AboutId).Name} was kicked.");
return;
}
2021-06-18 21:38:32 +00:00
2021-03-09 16:43:41 +00:00
DebugLog.ToAll($"Player id:{message.AboutId} was kicked.");
return;
}
2021-06-18 21:38:32 +00:00
2021-03-29 13:36:16 +00:00
DebugLog.ToAll($"Kicked from server. Reason : {message.EnumValue}");
MenuManager.Instance.OnKicked(message.EnumValue);
2021-03-09 16:43:41 +00:00
}
}
}