-
Notifications
You must be signed in to change notification settings - Fork 3
/
KnifeSelector.cs
332 lines (250 loc) · 11.9 KB
/
KnifeSelector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System.Xml;
using System.Xml.XPath;
using System.Xml.Linq;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.Modules.Cvars;
using Newtonsoft.Json.Linq;
namespace KnifeSelector;
[MinimumApiVersion(126)]
public class KnifeSelector : BasePlugin, IPluginConfig<Config>
{
public override string ModuleName => "Knife Selector";
public override string ModuleAuthor => "spancer";
public override string ModuleDescription => "Allows players to select and use knifes.";
public override string ModuleVersion => "1.1.0";
private Database? _database;
private readonly DateTime[] _commandCooldown = new DateTime[Server.MaxPlayers];
public Config Config { get; set; } = new();
public void OnConfigParsed(Config config)
{
Config = config;
}
public override void Load(bool hotReload)
{
_database = new Database();
_database.Initialize(ModuleDirectory);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterEventHandler<EventRoundStart>(OnRoundStart, HookMode.Pre);
RegisterEventHandler<EventRoundEnd>(OnRoundEnd, HookMode.Pre);
RegisterEventHandler<EventRoundAnnounceWarmup>(OnRoundAnnounceWarmup, HookMode.Pre);
RegisterListener<Listeners.OnClientConnect>(Listener_OnClientConnectHandler);
RegisterConsoleCommands();
}
private void RegisterConsoleCommands()
{
AddCommand($"css_{Config.KnifeCommand}", "Gives player the desired knife that exist in game.", [CommandHelper(minArgs: 1, usage: "<knife name>", whoCanExecute: CommandUsage.CLIENT_ONLY)] (player, info) => { if (player == null) return; KnifeCmd(player, info); } );
AddCommand($"css_{Config.KnifeListCommand}", "Prints the console which knifes are available.", [CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)] (player, info) => { if (player == null) return; KnifeListCmd(player, info); } );
}
private HookResult OnRoundAnnounceWarmup(EventRoundAnnounceWarmup @event, GameEventInfo info)
{
NativeAPI.IssueServerCommand("mp_t_default_melee \"\"");
NativeAPI.IssueServerCommand("mp_ct_default_melee \"\"");
float warmupTime = ConVar.Find("mp_warmuptime")!.GetPrimitiveValue<float>();
AddTimer(warmupTime, () =>
{
NativeAPI.IssueServerCommand("mp_t_default_melee \"\"");
NativeAPI.IssueServerCommand("mp_ct_default_melee \"\"");
});
return HookResult.Continue;
}
[GameEventHandler]
private void Listener_OnClientConnectHandler(int playerSlot, string name, string ipAddress)
{
AddTimer(2.0f, () =>
{
CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot);
if (!player.IsValid || player.IsBot) return;
if (_database != null && !_database.ClientExistsInDatabase(player.SteamID)) _database.AddNewClientToDatabase(player);
});
}
private void OnMapStart(string mapName)
{
AddTimer(2.0f, () =>
{
NativeAPI.IssueServerCommand("mp_t_default_melee \"\"");
NativeAPI.IssueServerCommand("mp_ct_default_melee \"\"");
});
}
private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
NativeAPI.IssueServerCommand("mp_t_default_melee \"\"");
NativeAPI.IssueServerCommand("mp_ct_default_melee \"\"");
return HookResult.Continue;
}
[GameEventHandler]
private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
NativeAPI.IssueServerCommand("mp_t_default_melee \"\"");
NativeAPI.IssueServerCommand("mp_ct_default_melee \"\"");
return HookResult.Continue;
}
private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
var player = @event.Userid;
if(!player.IsValid || !player.PlayerPawn.IsValid || player.IsBot) return HookResult.Continue;
string? knife = _database?.GetPlayerKnifeFromDb(player);
if (knife != null) GiveKnifeToPlayerOnSpawn(player, knife);
return HookResult.Continue;
}
private async Task<bool> IsUserGroupMember(ulong steamId)
{
CCSPlayerController? player = Utilities.GetPlayers().FirstOrDefault(p => p.SteamID == steamId);
using (HttpClient client = new HttpClient())
{
// get the current time as a timestamp
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
// append the timestamp to the URL as a query parameter
string url = $"https://steamcommunity.com/profiles/{steamId}/?xml=1&ts={timestamp}";
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
try
{
// get a stream from the URL
Stream stream = await client.GetStreamAsync(url);
// load the XML document from the stream
XDocument doc = await XDocument.LoadAsync(stream, LoadOptions.None, CancellationToken.None);
// XPath ile bir düğüm seç
XElement? node = doc.XPathSelectElement($"/profile/groups/group[groupID64=\"{Config.SteamGroupId}\"]");
XElement? node1 = doc.XPathSelectElement("/profile/privacyState");
if (node1?.Value == "public")
{
if (node != null)
{
return true;
}
else
{
Server.NextFrame(() => {
player?.PrintToChat($" {Localizer["ks.ServerPrefix"]} {Localizer["ks.UserNotInSteamGroup"]}");
});
return false;
}
}else{
Server.NextFrame(() => {
player?.PrintToChat($" {Localizer["ks.ServerPrefix"]} {Localizer["ks.UserProfileNotPublic"]}");
});
return false;
}
}
catch (XmlException ex)
{
Server.NextFrame(() => {Server.PrintToConsole("XML failed to load: " + ex.Message);
});
}
}
return false;
}
}
public bool KnifeExist(string knife)
{
string json = File.ReadAllText(@$"{ModuleDirectory}/knifenames.json");
JToken token = JToken.Parse (json);
JToken? value = token.SelectToken(knife);
if (value != null)return true;
return false;
}
public async void KnifeCmd(CCSPlayerController? player, CommandInfo command)
{
if (player == null || !player.IsValid || !player.PawnIsAlive) return;
string knife = command.ArgByIndex(1);
int playerIndex = (int)player.Index;
if (DateTime.UtcNow >= _commandCooldown[playerIndex].AddSeconds(5))
{
_commandCooldown[playerIndex] = DateTime.UtcNow;
if (KnifeExist(knife))
{
if (Config.CheckSteamGroup)
{
if (await IsUserGroupMember(player.SteamID)) Server.NextFrame(() => {GiveKnifeToPlayer(player, knife);});
}else{
GiveKnifeToPlayer(player, knife);
}
}else{
player.PrintToChat($" {Localizer["ks.ServerPrefix"]} {Localizer["ks.UnknownKnife", Config.KnifeListCommand]}");
}
}else{
player.PrintToChat($" {Localizer["ks.ServerPrefix"]} {Localizer["ks.UserSpammed"]}");
}
}
public void KnifeListCmd(CCSPlayerController? player, CommandInfo command)
{
string json = File.ReadAllText(@$"{ModuleDirectory}/knifenames.json");
JToken token = JToken.Parse(json);
if (player == null || !player.IsValid)
{
player?.PrintToChat($" {Localizer["ks.ServerPrefix"]} {Localizer["ks.UserCantUseCommand"]}");
}else{
player.PrintToChat($" {Localizer["ks.ServerPrefix"]} {Localizer["ks.SeeConsoleOutput"]}");
player.PrintToConsole(new string ('\u2029', 15));
player.PrintToConsole(new string ('*', 30));
player.PrintToConsole($"{Localizer["ks.ConsoleInstructor"]} !{Config.KnifeCommand} karambit");
player.PrintToConsole(new string ('*', 30));
foreach (var jToken in token)
{
var property = (JProperty)jToken;
string key = property.Name;
JArray value = (JArray)property.Value;
player.PrintToConsole($"for ★{value[1]} use !{Config.KnifeCommand} {key}");
}
}
player?.PrintToConsole(new string ('*', 30));
player?.PrintToConsole("Plugin created by spancer");
player?.PrintToConsole("https://github.com/spancer35");
player?.PrintToConsole(new string ('*', 30));
}
private void RemoveKnifeFromPlayer(CCSPlayerController player)
{
var weapons = player.PlayerPawn.Value?.WeaponServices!.MyWeapons;
if (weapons != null)
{
foreach (var weapon in weapons)
{
if (weapon.Value != null && weapon.IsValid && weapon.Value.IsValid)
{
//if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59)
if (weapon.Value.DesignerName.Contains("knife"))
{
weapon.Value.Remove();
return;
}
}
}
}
}
private void GiveKnifeToPlayer(CCSPlayerController player, string knifeArg)
{
string json = File.ReadAllText(@$"{ModuleDirectory}/knifenames.json");
JToken token = JToken.Parse (json);
JToken? value = token.SelectToken(knifeArg);
if (knifeArg == "default") {
RemoveKnifeFromPlayer(player);
player.GiveNamedItem((CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife");
}else{
if (KnifeExist(knifeArg))
{
string weaponKnifeName = value![0]!.ToString();
string weaponDisplayName = value[1]!.ToString();
player.PrintToChat($" {Localizer["ks.ServerPrefix"]} {Localizer["ks.SelectedKnife", weaponDisplayName]}");
RemoveKnifeFromPlayer(player);
player.GiveNamedItem(weaponKnifeName);
_database?.SaveCurrentKnife(player, weaponKnifeName);
}
else
{
player.PrintToChat($" {Localizer["ks.ServerPrefix"]} {Localizer["ks.UnknownKnife", Config.KnifeListCommand]}");
}
}
}
private void GiveKnifeToPlayerOnSpawn(CCSPlayerController player, string knifeName)
{
RemoveKnifeFromPlayer(player);
player.GiveNamedItem(knifeName);
}
}