Skip to content

Commit 69ef3ec

Browse files
committed
Messages can be a string or array
1 parent cb450bc commit 69ef3ec

File tree

5 files changed

+74
-14
lines changed

5 files changed

+74
-14
lines changed

Diff for: CustomCommands/Commands.example.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
{
2121
"Title": "Enable Surf",
2222
"Command": "surf",
23-
"Message": "Surf is now enabled",
23+
"Message": [
24+
"Surf is now:",
25+
"{GREEN}Enabled"
26+
],
2427
"PrintTo": 0,
2528
"Description": "Command for Surf gamemode",
2629
"ServerCommands": [

Diff for: CustomCommands/Functions.cs

+48-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Nodes;
13
using CounterStrikeSharp.API;
24
using CounterStrikeSharp.API.Core;
35
using CounterStrikeSharp.API.Modules.Admin;
46
using CounterStrikeSharp.API.Modules.Cvars;
57
using CounterStrikeSharp.API.Modules.Entities;
6-
using CounterStrikeSharp.API.Modules.Entities.Constants;
78
using CounterStrikeSharp.API.Modules.Utils;
89
using CustomCommands.Model;
910
using Microsoft.Extensions.Logging;
10-
using Serilog;
11-
1211
namespace CustomCommands;
1312
public partial class CustomCommands
1413
{
@@ -105,14 +104,14 @@ private bool RequiresPermissions(CCSPlayerController player, Permission permissi
105104
if (AdminManager.PlayerHasPermissions(player, new string[]{permission}))
106105
return true;
107106
}
108-
PrintToChat(Receiver.Client, player, "You don't have the required permissions to execute this command");
107+
player.PrintToChat($"{PrefixCache}You don't have the required permissions to execute this command");
109108
return false;
110109
}
111110
else
112111
{
113112
if (!AdminManager.PlayerHasPermissions(player, permissions.PermissionList.ToArray()))
114113
{
115-
PrintToChat(Receiver.Client, player, "You don't have the required permissions to execute this command");
114+
player.PrintToChat($"{PrefixCache}You don't have the required permissions to execute this command");
116115
return false;
117116
}
118117
return true;
@@ -160,9 +159,51 @@ private void TriggerMessage(CCSPlayerController player, Commands cmd)
160159
break;
161160
}
162161
}
163-
private string[] WrappedLine(string input)
162+
private string[] WrappedLine(dynamic input)
164163
{
165-
return input.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
164+
List<string> output = new List<string>();
165+
166+
if (input is JsonElement jsonElement)
167+
{
168+
switch (jsonElement.ValueKind)
169+
{
170+
case JsonValueKind.String:
171+
string result = jsonElement.GetString()!;
172+
return result?.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None) ?? Array.Empty<string>();
173+
174+
case JsonValueKind.Array:
175+
foreach (var arrayElement in jsonElement.EnumerateArray())
176+
{
177+
string[] lines = arrayElement.GetString()?.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None) ?? Array.Empty<string>();
178+
output.AddRange(lines);
179+
}
180+
break;
181+
182+
default:
183+
Logger.LogError($"{Config.LogPrefix} Message is not a string or array");
184+
return Array.Empty<string>();
185+
}
186+
}
187+
else
188+
{
189+
Logger.LogError($"{Config.LogPrefix} Invalid input type");
190+
return Array.Empty<string>();
191+
}
192+
193+
return output.ToArray();
194+
}
195+
196+
private string[] ReplaceTags(string[] input, CCSPlayerController player)
197+
{
198+
string[] output = new string[input.Length];
199+
200+
for (int i = 0; i < input.Length; i++)
201+
{
202+
output[i] = ReplaceMessageTags(input[i], player);
203+
output[i] = ReplaceColorTags(output[i]);
204+
}
205+
206+
return output;
166207
}
167208

168209
private string ReplaceMessageTags(string input, CCSPlayerController player)

Diff for: CustomCommands/Model/CommandsConfig.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Commands
55
public required string Title { get; set; } = "";
66
public string Description { get; set; } = "Description";
77
public required string Command { get; set; } = "testtesttest";
8-
public string Message { get; set; } = "";
8+
public dynamic Message { get; set; } = "";
99
public CenterElement CenterMessage { get; set; } = new();
1010
public required Sender PrintTo { get; set; } = Sender.ClientChat;
1111
public List<string> ServerCommands { get; set; } = new();
@@ -32,4 +32,3 @@ public enum Sender
3232
AllChatClientCenter = 6,
3333
AllChatAllCenter = 7
3434
}
35-

Diff for: CustomCommands/PrintFunctions.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ private void PrintToChatAndAllCenter(Receiver receiver, CCSPlayerController play
3737
PrintToAllCenter(cmd);
3838
}
3939

40-
private void PrintToChat(Receiver printToChat, CCSPlayerController player, string message)
40+
private void PrintToChat(Receiver printToChat, CCSPlayerController player, dynamic message)
4141
{
42-
string replaceTags = ReplaceMessageTags(message, player);
43-
string replaceColorTags = ReplaceColorTags(replaceTags);
44-
string[] msg = WrappedLine(replaceColorTags);
42+
string[] msg = WrappedLine(message);
43+
msg = ReplaceTags(msg, player);
4544

4645
switch (printToChat)
4746
{

Diff for: Examples/NewLines.json

+18
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,23 @@
1212
"Command": "numbers",
1313
"Message": "1\n2\n3\n4\n5\n6\n7\n8\n9\r\n10", // \n or \r is a new line
1414
"PrintTo": 0
15+
},
16+
{
17+
"Title": "Numbers",
18+
"Description": "From one to ten with arrays as new lines",
19+
"Command": "numbers2",
20+
"Message": [ // If you want to use arrays as new lines, do this
21+
"1",
22+
"2",
23+
"3",
24+
"4",
25+
"5",
26+
"6",
27+
"7",
28+
"8",
29+
"9",
30+
"10"
31+
],
32+
"PrintTo": 0
1533
}
1634
]

0 commit comments

Comments
 (0)