From 8ec89892a1d2e80f21508ed74a770d8daf1e6b2d Mon Sep 17 00:00:00 2001 From: Measurity Date: Sat, 18 Apr 2020 22:38:45 +0200 Subject: [PATCH] Added server command handling back to in-game chat (#1044) * Added server command handling back to in-game chat * Removed redundant check --- .../GameLogic/ChatUI/PlayerChatManager.cs | 23 +++++++++++++--- .../MonoBehaviours/Gui/Chat/PlayerChat.cs | 27 +++++++++---------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/NitroxClient/GameLogic/ChatUI/PlayerChatManager.cs b/NitroxClient/GameLogic/ChatUI/PlayerChatManager.cs index ac0686433d..9a088641b3 100644 --- a/NitroxClient/GameLogic/ChatUI/PlayerChatManager.cs +++ b/NitroxClient/GameLogic/ChatUI/PlayerChatManager.cs @@ -18,6 +18,7 @@ public PlayerChatManager(IMultiplayerSession multiplayerSession) private PlayerChat playerChat; public Transform PlayerChaTransform => playerChat.transform; private readonly IMultiplayerSession multiplayerSession; + private const char SERVER_COMMAND_PREFIX = '/'; public void ShowChat() => Player.main.StartCoroutine(ShowChatAsync()); private IEnumerator ShowChatAsync() @@ -51,12 +52,26 @@ private IEnumerator AddMessageAsync(string playerName, string message, Color col public void SendMessage() { - if (playerChat.inputText.Trim() != "") + if (string.IsNullOrWhiteSpace(playerChat.InputText)) { - multiplayerSession.Send(new ChatMessage(multiplayerSession.Reservation.PlayerId, playerChat.inputText)); - playerChat.WriteLogEntry(multiplayerSession.AuthenticationContext.Username, playerChat.inputText, multiplayerSession.PlayerSettings.PlayerColor); - playerChat.inputText = ""; + playerChat.Select(); + return; } + + string trimmedInput = playerChat.InputText.Trim(); + if (trimmedInput[0] == SERVER_COMMAND_PREFIX) + { + // Server command + multiplayerSession.Send(new ServerCommand(trimmedInput.Substring(1))); + playerChat.InputText = ""; + playerChat.Select(); + return; + } + + // Chat message + multiplayerSession.Send(new ChatMessage(multiplayerSession.Reservation.PlayerId, trimmedInput)); + playerChat.WriteLogEntry(multiplayerSession.AuthenticationContext.Username, playerChat.InputText, multiplayerSession.PlayerSettings.PlayerColor); + playerChat.InputText = ""; playerChat.Select(); } diff --git a/NitroxClient/MonoBehaviours/Gui/Chat/PlayerChat.cs b/NitroxClient/MonoBehaviours/Gui/Chat/PlayerChat.cs index 4a5b1355f8..85660306ee 100644 --- a/NitroxClient/MonoBehaviours/Gui/Chat/PlayerChat.cs +++ b/NitroxClient/MonoBehaviours/Gui/Chat/PlayerChat.cs @@ -4,7 +4,6 @@ using NitroxClient.GameLogic.ChatUI; using NitroxModel.Core; using UnityEngine; -using UnityEngine.EventSystems; using UnityEngine.UI; namespace NitroxClient.MonoBehaviours.Gui.Chat @@ -16,29 +15,29 @@ public class PlayerChat : uGUI_InputGroup private const float TOGGLED_TRANSPARENCY = 0.4f; public const float CHAT_VISIBILITY_TIME_LENGTH = 10f; - public static bool IsReady { get; private set; } - - private PlayerChatManager playerChatManager; + private static readonly Queue entries = new Queue(); + private Image[] backgroundImages; private CanvasGroup canvasGroup; - private HorizontalOrVerticalLayoutGroup[] layoutGroups; + private InputField inputField; private GameObject logEntryPrefab; - private Image[] backgroundImages; + + private PlayerChatManager playerChatManager; private bool transparent; - private InputField inputField; - public string inputText + + public static bool IsReady { get; private set; } + + public string InputText { get { return inputField.text; } set { inputField.text = value; } } - private static readonly Queue entries = new Queue(); - public IEnumerator SetupChatComponents() { playerChatManager = NitroxServiceLocator.LocateService(); canvasGroup = GetComponent(); - layoutGroups = GetComponentsInChildren(); + GetComponentsInChildren(); logEntryPrefab = GameObject.Find("ChatLogEntryPrefab"); logEntryPrefab.AddComponent(); @@ -51,10 +50,7 @@ public IEnumerator SetupChatComponents() inputField.gameObject.AddComponent().InputField = inputField; inputField.GetComponentInChildren