2020-08-22 17:08:25 +00:00
|
|
|
|
using OWML.Common;
|
2020-11-03 21:42:14 +00:00
|
|
|
|
using QSB.EventsCore;
|
2020-08-08 11:23:23 +00:00
|
|
|
|
using QSB.Utility;
|
|
|
|
|
|
2020-11-03 21:33:48 +00:00
|
|
|
|
namespace QSB.Player.Events
|
2020-08-08 11:23:23 +00:00
|
|
|
|
{
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public class PlayerJoinEvent : QSBEvent<PlayerJoinMessage>
|
|
|
|
|
{
|
|
|
|
|
public override EventType Type => EventType.PlayerJoin;
|
2020-08-08 11:23:23 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public override void SetupListener() => GlobalMessenger<string>.AddListener(EventNames.QSBPlayerJoin, Handler);
|
|
|
|
|
public override void CloseListener() => GlobalMessenger<string>.RemoveListener(EventNames.QSBPlayerJoin, Handler);
|
2020-08-15 19:32:58 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
private void Handler(string name) => SendEvent(CreateMessage(name));
|
2020-08-08 11:23:23 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
private PlayerJoinMessage CreateMessage(string name) => new PlayerJoinMessage
|
|
|
|
|
{
|
|
|
|
|
AboutId = LocalPlayerId,
|
|
|
|
|
PlayerName = name
|
|
|
|
|
};
|
2020-08-10 16:17:54 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public override void OnReceiveRemote(PlayerJoinMessage message)
|
|
|
|
|
{
|
|
|
|
|
var player = QSBPlayerManager.GetPlayer(message.AboutId);
|
|
|
|
|
player.Name = message.PlayerName;
|
|
|
|
|
DebugLog.ToHud($"{player.Name} joined!");
|
|
|
|
|
DebugLog.DebugWrite($"{player.Name} joined as id {player.PlayerId}", MessageType.Info);
|
|
|
|
|
}
|
2020-08-08 11:23:23 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public override void OnReceiveLocal(PlayerJoinMessage message)
|
|
|
|
|
{
|
|
|
|
|
var player = QSBPlayerManager.GetPlayer(QSBPlayerManager.LocalPlayerId);
|
|
|
|
|
player.Name = message.PlayerName;
|
|
|
|
|
var text = $"Connected to server as {player.Name}.";
|
|
|
|
|
DebugLog.ToAll(text, MessageType.Info);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
}
|