Repository navigation
Expand file tree
/
Copy pathJoinSound.cs
More file actions
100 lines (87 loc) · 3.16 KB
/
Copy pathJoinSound.cs
File metadata and controls
100 lines (87 loc) · 3.16 KB
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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities;
using System;
using System.Linq;
using System.Collections.Generic;
namespace CS2_JoinSound
{
public class JoinSound : BasePlugin, IPluginConfig<PluginConfig>
{
public override string ModuleName => "CS2_JoinSound";
public override string ModuleVersion => "0.0.2";
public override string ModuleAuthor => "MeowKus";
public PluginConfig Config { get; set; } = new PluginConfig();
private Dictionary<CCSPlayerController, int> playerSoundIndex = new Dictionary<CCSPlayerController, int>();
public void OnConfigParsed(PluginConfig config)
{
Config = config;
Console.WriteLine("Configuration loaded successfully.");
}
public override void Load(bool hotReload)
{
base.Load(hotReload);
RegisterListener<Listeners.OnClientPutInServer>(OnClientPutInServer);
}
private void OnClientPutInServer(int playerSlot)
{
var player = Utilities.GetPlayerFromSlot(playerSlot);
if (player != null)
{
PlaySound(player);
}
else
{
Console.WriteLine($"Player with slot {playerSlot} not found.");
}
}
private void PlaySound(CCSPlayerController player)
{
try
{
string soundPath = GetSoundPath(Config.SoundMode, Config.MusicList, player);
if (!string.IsNullOrEmpty(soundPath))
{
player.ExecuteClientCommand($"play {soundPath}");
Console.WriteLine($"Playing sound for player: {soundPath}");
}
else
{
Console.WriteLine($"No sound found for play event.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error while playing sound: {ex.Message}");
}
}
private string GetSoundPath(string mode, List<SoundItem> soundList, CCSPlayerController player)
{
if (soundList.Count == 0) return string.Empty;
if (mode == "random")
{
Random random = new Random();
int index = random.Next(soundList.Count);
return soundList[index].Path;
}
else // "order"
{
if (!playerSoundIndex.ContainsKey(player))
{
playerSoundIndex[player] = 0;
}
int currentIndex = playerSoundIndex[player];
string soundPath = soundList[currentIndex].Path;
playerSoundIndex[player] = (currentIndex + 1) % soundList.Count;
return soundPath;
}
}
[ConsoleCommand("jtest")]
public void PlayJoinCommand(CCSPlayerController controller, CommandInfo info)
{
PlaySound(controller);
}
}
}