From: AeonLucid Date: Mon, 26 Oct 2020 19:31:05 +0000 (+0100) Subject: Add more API methods to InnerPlayerControl X-Git-Tag: v1.2.2~66 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=d399207d573c0c4e44c071006fe05012c423bc7f;p=rhonda%2Fimpostor.git Add more API methods to InnerPlayerControl --- diff --git a/src/Impostor.Api/Net/Inner/Objects/IInnerPlayerControl.cs b/src/Impostor.Api/Net/Inner/Objects/IInnerPlayerControl.cs index 4d53d2c..e862147 100644 --- a/src/Impostor.Api/Net/Inner/Objects/IInnerPlayerControl.cs +++ b/src/Impostor.Api/Net/Inner/Objects/IInnerPlayerControl.cs @@ -4,10 +4,58 @@ namespace Impostor.Api.Net.Inner.Objects { public interface IInnerPlayerControl { + /// + /// Gets the of the . + /// Contains metadata about the player. + /// IInnerPlayerInfo PlayerInfo { get; } + /// + /// Sets the name of the current . + /// Visible to all players. + /// + /// A name for the player. + /// Task that must be awaited. ValueTask SetNameAsync(string name); + /// + /// Sets the color of the current . + /// Visible to all players. + /// + /// A color for the player. + /// Task that must be awaited. + ValueTask SetColorAsync(byte colorId); + + /// + /// Sets the hat of the current . + /// Visible to all players. + /// + /// An hat for the player. + /// Task that must be awaited. + ValueTask SetHatAsync(uint hatId); + + /// + /// Sets the pet of the current . + /// Visible to all players. + /// + /// An hat for the player. + /// Task that must be awaited. + ValueTask SetPetAsync(uint petId); + + /// + /// Sets the skin of the current . + /// Visible to all players. + /// + /// An hat for the player. + /// Task that must be awaited. + ValueTask SetSkinAsync(uint skinId); + + /// + /// Send a chat message as the current . + /// Visible to all players. + /// + /// The message to send. + /// Task that must be awaited. ValueTask SendChatAsync(string text); } -} \ No newline at end of file +} diff --git a/src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs b/src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs index 2b1b130..f3b8dfb 100644 --- a/src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs +++ b/src/Impostor.Plugins.Example/Handlers/PlayerEventListener.cs @@ -7,10 +7,39 @@ namespace Impostor.Plugins.Example.Handlers { public class PlayerEventListener : IEventListener { + private static readonly Random Random = new Random(); + [EventListener] public void OnPlayerSpawned(IPlayerSpawnedEvent e) { Console.WriteLine(e.PlayerControl.PlayerInfo.PlayerName + " spawned"); + + // Need to make a local copy because it might be possible that + // the event gets changed after being handled. + var clientPlayer = e.ClientPlayer; + var playerControl = e.PlayerControl; + + Task.Run(async () => + { + Console.WriteLine("Starting player task."); + + // Give the player time to load. + await Task.Delay(TimeSpan.FromSeconds(3)); + + while (clientPlayer.Client.Connection != null && + clientPlayer.Client.Connection.IsConnected) + { + // Modify player properties. + await playerControl.SetColorAsync((byte) Random.Next(1, 9)); + await playerControl.SetHatAsync((uint) Random.Next(1, 9)); + await playerControl.SetSkinAsync((uint) Random.Next(1, 9)); + await playerControl.SetPetAsync((uint) Random.Next(1, 9)); + + await Task.Delay(TimeSpan.FromMilliseconds(500)); + } + + Console.WriteLine("Stopping player task."); + }); } [EventListener] diff --git a/src/Impostor.Server/Net/Hazel/HazelConnection.cs b/src/Impostor.Server/Net/Hazel/HazelConnection.cs index 9a9a071..1d6f35c 100644 --- a/src/Impostor.Server/Net/Hazel/HazelConnection.cs +++ b/src/Impostor.Server/Net/Hazel/HazelConnection.cs @@ -54,7 +54,6 @@ namespace Impostor.Server.Net.Hazel { if (Client == null) { - _logger.LogWarning("Client was null."); return; } @@ -69,4 +68,4 @@ namespace Impostor.Server.Net.Hazel } } } -} \ No newline at end of file +} diff --git a/src/Impostor.Server/Net/Inner/Objects/InnerPlayerControl.Api.cs b/src/Impostor.Server/Net/Inner/Objects/InnerPlayerControl.Api.cs index 6a9fa45..d3e8d80 100644 --- a/src/Impostor.Server/Net/Inner/Objects/InnerPlayerControl.Api.cs +++ b/src/Impostor.Server/Net/Inner/Objects/InnerPlayerControl.Api.cs @@ -9,20 +9,54 @@ namespace Impostor.Server.Net.Inner.Objects public async ValueTask SetNameAsync(string name) { - using (var writer = _game.StartRpc(NetId, RpcCalls.SetName)) - { - writer.Write(name); - await _game.FinishRpcAsync(writer); - } + PlayerInfo.PlayerName = name; + + using var writer = _game.StartRpc(NetId, RpcCalls.SetName); + writer.Write(name); + await _game.FinishRpcAsync(writer); + } + + public async ValueTask SetColorAsync(byte colorId) + { + PlayerInfo.ColorId = colorId; + + using var writer = _game.StartRpc(NetId, RpcCalls.SetColor); + writer.Write(colorId); + await _game.FinishRpcAsync(writer); + } + + public async ValueTask SetHatAsync(uint hatId) + { + PlayerInfo.HatId = hatId; + + using var writer = _game.StartRpc(NetId, RpcCalls.SetHat); + writer.WritePacked(hatId); + await _game.FinishRpcAsync(writer); + } + + public async ValueTask SetPetAsync(uint petId) + { + PlayerInfo.PetId = petId; + + using var writer = _game.StartRpc(NetId, RpcCalls.SetPet); + writer.WritePacked(petId); + await _game.FinishRpcAsync(writer); + } + + public async ValueTask SetSkinAsync(uint skinId) + { + PlayerInfo.SkinId = skinId; + + using var writer = _game.StartRpc(NetId, RpcCalls.SetSkin); + writer.WritePacked(skinId); + await _game.FinishRpcAsync(writer); } public async ValueTask SendChatAsync(string text) { - using (var writer = _game.StartRpc(NetId, RpcCalls.SendChat)) - { - writer.Write(text); - await _game.FinishRpcAsync(writer); - } + using var writer = _game.StartRpc(NetId, RpcCalls.SendChat); + writer.Write(text); + await _game.FinishRpcAsync(writer); } } -} \ No newline at end of file +}