Skip to content

Instantly share code, notes, and snippets.

@Mr-Robot-ops
Last active April 27, 2024 09:58
Show Gist options
  • Save Mr-Robot-ops/05571ea014abf33ea8e472a62b1a8173 to your computer and use it in GitHub Desktop.
Save Mr-Robot-ops/05571ea014abf33ea8e472a62b1a8173 to your computer and use it in GitHub Desktop.
DecoyPlugin
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Utils;
namespace DecoyPlugin
{
public class DecoyPlugin : BasePlugin
{
public override string ModuleName => "Decoy & Smoke Refill Plugin";
public override string ModuleVersion => "2.0";
private HashSet<string> ctWeapons = new HashSet<string> { "weapon_decoy", "weapon_smokegrenade", "weapon_knife", "weapon_flashbang" };
private HashSet<string> tWeapons = new HashSet<string> { "weapon_knife", "weapon_molotov", "weapon_hegrenade" };
public override void Load(bool hotReload)
{
// Register EventHandlers for both MapStart and RoundStart events
RegisterListener<Listeners.OnMapStart>(name =>
{
// Set sv_cheats to false when the map starts
Server.ExecuteCommand("sv_cheats false");
// Check weapons for all players on map start
CheckMissingWeaponsForAllPlayers();
});
RegisterEventHandler<EventRoundStart>((@event, info) =>
{
// Prevent triggering cheat commands here
Server.ExecuteCommand("sv_cheats false");
// Check weapons for all players on round start
CheckMissingWeaponsForAllPlayers();
return HookResult.Continue;
});
// Start a timer that runs every 30 seconds
AddTimer(30.0f, () =>
{
// Give missing weapons to all players every 30 seconds
foreach (var player in Utilities.GetPlayers())
{
if (player.PawnIsAlive)
{
if (player.TeamNum == (byte)CsTeam.CounterTerrorist)
{
GiveMissingWeapons(player, ctWeapons);
}
else if (player.TeamNum == (byte)CsTeam.Terrorist)
{
GiveMissingWeapons(player, tWeapons);
}
}
}
}, TimerFlags.REPEAT);
// Start a timer that runs every 2 seconds and sets sv_cheats to false
AddTimer(2.0f, () =>
{
Server.ExecuteCommand("sv_cheats false");
}, TimerFlags.REPEAT);
}
// Check for missing weapons for all players
private void CheckMissingWeaponsForAllPlayers()
{
foreach (var player in Utilities.GetPlayers())
{
if (player.PawnIsAlive)
{
if (player.TeamNum == (byte)CsTeam.CounterTerrorist)
{
GiveMissingWeapons(player, ctWeapons);
}
else if (player.TeamNum == (byte)CsTeam.Terrorist)
{
GiveMissingWeapons(player, tWeapons);
}
}
}
}
// Give missing weapons to a specific player
private void GiveMissingWeapons(CCSPlayerController player, HashSet<string> expectedWeapons)
{
var playerPawn = player.PlayerPawn.Value;
if (playerPawn != null && playerPawn.WeaponServices != null)
{
foreach (var weapon in expectedWeapons)
{
if (!PlayerHasWeapon(player, weapon))
{
player.GiveNamedItem(weapon);
}
}
}
}
// Check if a player already has a specific weapon
private bool PlayerHasWeapon(CCSPlayerController player, string weaponName)
{
var playerPawn = player.PlayerPawn.Value;
if (playerPawn != null && playerPawn.WeaponServices != null)
{
foreach (var weapon in playerPawn.WeaponServices.MyWeapons)
{
if (weapon.Value.DesignerName.Contains(weaponName))
{
return true;
}
}
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment