roflmuffin / CounterStrikeSharp

CounterStrikeSharp allows you to write server plugins in C# for Counter-Strike 2/Source2/CS2
https://docs.cssharp.dev
Other
769 stars 119 forks source link

Help with Getting Started #475

Closed yabanavarsheyleo closed 2 months ago

yabanavarsheyleo commented 4 months ago

I wanted to make plugin like it was before in CSGO which was playing sounds after round end. so with my 0 experience in C# I texted ChatGPT for help so it provided me this code and I did something... well not as well as I was expecting and now I have some errors that I don't even know how to fix.

using System; using System.Collections.Generic; using System.IO; using CounterStrikeSharp.API.Core;

namespace PostMatchMusic { public class PostMatchMusicPlugin : BasePlugin { private readonly Random _random = new Random(); private readonly string _musicDirectory = "sound/custom_music"; private readonly Dictionary<int, bool> _musicEnabled = new Dictionary<int, bool>();

    public override string ModuleName => "PostMatchMusicPlugin";
    public override string ModuleVersion => "1.0";

    public override void Load(bool hotReload)
    {
        base.Load(hotReload);

        // Register events and commands
        EventManager.RegisterListener<RoundEndEvent>(OnRoundEnd);
        CommandManager.RegisterCommand("!music", OnMusicCommand);
    }

    private void OnRoundEnd(RoundEndEvent e)
    {
        PlayRandomMusic();
    }

    private void PlayRandomMusic()
    {
        var musicFiles = Directory.GetFiles(_musicDirectory, "*.mp3");
        if (musicFiles.Length == 0)
        {
            LogError("No music files found in the specified directory.");
            return;
        }

        var randomMusic = musicFiles[_random.Next(musicFiles.Length)];
        foreach (var player in PlayerManager.Players)
        {
            if (_musicEnabled.TryGetValue(player.ClientID, out bool isEnabled) && isEnabled)
            {
                player.EmitSound(randomMusic);
            }
        }
    }

    private void OnMusicCommand(string[] args)
    {
        if (args.Length != 1)
        {
            LogMessage("Usage: !music <on/off>");
            return;
        }

        if (args[0].Equals("on", StringComparison.OrdinalIgnoreCase))
        {
            _musicEnabled[CurrentPlayer.ClientID] = true;
            LogMessage("Post-match music enabled.");
        }
        else if (args[0].Equals("off", StringComparison.OrdinalIgnoreCase))
        {
            _musicEnabled[CurrentPlayer.ClientID] = false;
            LogMessage("Post-match music disabled.");
        }
        else
        {
            LogMessage("Usage: !music <on/off>");
        }
    }
}

}

C:\Users\YABOO\Desktop\addons\PostMatchMusic\Class1.cs(26,33): error CS0246: The type or namespace name 'RoundEndEvent' could not be found (are you missing a using directive or an assembly reference?) [C:\ Users\YABOO\Desktop\addons\PostMatchMusic\PostMatchMusic.csproj]

Build FAILED.

C:\Users\YABOO\Desktop\addons\PostMatchMusic\Class1.cs(26,33): error CS0246: The type or namespace name 'RoundEndEvent' could not be found (are you missing a using directive or an assembly reference?) [C:\ Users\YABOO\Desktop\addons\PostMatchMusic\PostMatchMusic.csproj] 0 Warning(s) 1 Error(s)

Time Elapsed 00:00:01.42 PS C:\Users\YABOO\Desktop\addons\PostMatchMusic>

so if there is someone who can finish this code or help me to finish this I will appreciate that cuz I really miss that plugin lol

THANKS FOR ATTENTION

ianlucas commented 4 months ago

Hello,

Follow this guide to learn how to build a plugin: https://docs.cssharp.dev/docs/guides/hello-world-plugin.html

I'd suggest you to join CSSharp's Discord if you want to look for help, other than that you are on your own, i.e. learn C# and look at existing open source plugins that have similar features for what you want to do. (Like MVP-Sounds-GoldKingZ.)

yabanavarsheyleo commented 4 months ago

ok thank you so much cuz that is the plugin I was looking for 😭

can you recommend me some page where I'll search some plugins for cs2 ?

I have only https://ghostcap.com/cs2-plugins-list/ and looks like there is not much plugins 🤧

ianlucas commented 4 months ago

You can find plugins in CSSharp's Discord, GitHub "counterstrikesharp" topic, or by searching for GitHub repositories with the term "cs2".

Search engines may work but unfortunately we don't have an easily indexable list of plugins besides the one accessible in Discord.

yabanavarsheyleo commented 4 months ago

thank you so much ❤️