Skip to content

Commit 8ec8989

Browse files
authored
Added server command handling back to in-game chat (#1044)
* Added server command handling back to in-game chat * Removed redundant check
1 parent b73d55d commit 8ec8989

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

NitroxClient/GameLogic/ChatUI/PlayerChatManager.cs

+19-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public PlayerChatManager(IMultiplayerSession multiplayerSession)
1818
private PlayerChat playerChat;
1919
public Transform PlayerChaTransform => playerChat.transform;
2020
private readonly IMultiplayerSession multiplayerSession;
21+
private const char SERVER_COMMAND_PREFIX = '/';
2122

2223
public void ShowChat() => Player.main.StartCoroutine(ShowChatAsync());
2324
private IEnumerator ShowChatAsync()
@@ -51,12 +52,26 @@ private IEnumerator AddMessageAsync(string playerName, string message, Color col
5152

5253
public void SendMessage()
5354
{
54-
if (playerChat.inputText.Trim() != "")
55+
if (string.IsNullOrWhiteSpace(playerChat.InputText))
5556
{
56-
multiplayerSession.Send(new ChatMessage(multiplayerSession.Reservation.PlayerId, playerChat.inputText));
57-
playerChat.WriteLogEntry(multiplayerSession.AuthenticationContext.Username, playerChat.inputText, multiplayerSession.PlayerSettings.PlayerColor);
58-
playerChat.inputText = "";
57+
playerChat.Select();
58+
return;
5959
}
60+
61+
string trimmedInput = playerChat.InputText.Trim();
62+
if (trimmedInput[0] == SERVER_COMMAND_PREFIX)
63+
{
64+
// Server command
65+
multiplayerSession.Send(new ServerCommand(trimmedInput.Substring(1)));
66+
playerChat.InputText = "";
67+
playerChat.Select();
68+
return;
69+
}
70+
71+
// Chat message
72+
multiplayerSession.Send(new ChatMessage(multiplayerSession.Reservation.PlayerId, trimmedInput));
73+
playerChat.WriteLogEntry(multiplayerSession.AuthenticationContext.Username, playerChat.InputText, multiplayerSession.PlayerSettings.PlayerColor);
74+
playerChat.InputText = "";
6075
playerChat.Select();
6176
}
6277

NitroxClient/MonoBehaviours/Gui/Chat/PlayerChat.cs

+12-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using NitroxClient.GameLogic.ChatUI;
55
using NitroxModel.Core;
66
using UnityEngine;
7-
using UnityEngine.EventSystems;
87
using UnityEngine.UI;
98

109
namespace NitroxClient.MonoBehaviours.Gui.Chat
@@ -16,29 +15,29 @@ public class PlayerChat : uGUI_InputGroup
1615
private const float TOGGLED_TRANSPARENCY = 0.4f;
1716
public const float CHAT_VISIBILITY_TIME_LENGTH = 10f;
1817

19-
public static bool IsReady { get; private set; }
20-
21-
private PlayerChatManager playerChatManager;
18+
private static readonly Queue<ChatLogEntry> entries = new Queue<ChatLogEntry>();
19+
private Image[] backgroundImages;
2220
private CanvasGroup canvasGroup;
23-
private HorizontalOrVerticalLayoutGroup[] layoutGroups;
21+
private InputField inputField;
2422
private GameObject logEntryPrefab;
25-
private Image[] backgroundImages;
23+
24+
private PlayerChatManager playerChatManager;
2625
private bool transparent;
27-
private InputField inputField;
28-
public string inputText
26+
27+
public static bool IsReady { get; private set; }
28+
29+
public string InputText
2930
{
3031
get { return inputField.text; }
3132
set { inputField.text = value; }
3233
}
3334

34-
private static readonly Queue<ChatLogEntry> entries = new Queue<ChatLogEntry>();
35-
3635
public IEnumerator SetupChatComponents()
3736
{
3837
playerChatManager = NitroxServiceLocator.LocateService<PlayerChatManager>();
3938

4039
canvasGroup = GetComponent<CanvasGroup>();
41-
layoutGroups = GetComponentsInChildren<HorizontalOrVerticalLayoutGroup>();
40+
GetComponentsInChildren<HorizontalOrVerticalLayoutGroup>();
4241

4342
logEntryPrefab = GameObject.Find("ChatLogEntryPrefab");
4443
logEntryPrefab.AddComponent<PlayerChatLogItem>();
@@ -51,10 +50,7 @@ public IEnumerator SetupChatComponents()
5150
inputField.gameObject.AddComponent<PlayerChatInputField>().InputField = inputField;
5251
inputField.GetComponentInChildren<Button>().onClick.AddListener(playerChatManager.SendMessage);
5352

54-
backgroundImages = new[]
55-
{
56-
transform.GetChild(0).GetComponent<Image>(), transform.GetChild(1).GetComponent<Image>(), transform.GetChild(3).GetComponent<Image>()
57-
};
53+
backgroundImages = new[] { transform.GetChild(0).GetComponent<Image>(), transform.GetChild(1).GetComponent<Image>(), transform.GetChild(3).GetComponent<Image>() };
5854

5955
yield return new WaitForEndOfFrame(); //Needed so Select() works on initialization
6056
IsReady = true;
@@ -92,6 +88,7 @@ public void Show()
9288
PlayerChatInputField.ResetTimer();
9389
StartCoroutine(ToggleChatFade(true));
9490
}
91+
9592
public void Hide()
9693
{
9794
StartCoroutine(ToggleChatFade(false));

0 commit comments

Comments
 (0)