#include <sourcemod>
#include <sdktools>
#include <emitsoundany>

public Plugin myinfo = 
{
    name = "Defuse Effects",
    author = "Vortéx!",
    version = "1.0",
    url = "https://forums.alliedmods.net/showthread.php?t=299964"
}

public void OnPluginStart()
{
    HookEvent("bomb_defused", Event_Defuse); 
}

public void OnMapStart()
{
    PrecacheSoundAny("weapons/party_horn_01.wav");
}

public Action Event_Defuse(Handle event, const char[] name, bool dontBroadcast) 
{
    int id = GetClientOfUserId(GetEventInt(event,"userid"));
    CreateParticle(id, "weapon_confetti_balloons", 5.0);
}

stock void CreateParticle(int ent, char[] particleType, float time)
{
    int particle = CreateEntityByName("info_particle_system");
    
    char name[64];
    
    if (IsValidEdict(particle))
    {
        float position[3];
        GetEntPropVector(ent, Prop_Send, "m_vecOrigin", position);
        TeleportEntity(particle, position, NULL_VECTOR, NULL_VECTOR);
        GetEntPropString(ent, Prop_Data, "m_iName", name, sizeof(name));
        DispatchKeyValue(particle, "targetname", "tf2particle");
        DispatchKeyValue(particle, "parentname", name);
        DispatchKeyValue(particle, "effect_name", particleType);
        DispatchSpawn(particle);
        SetVariantString(name);
        AcceptEntityInput(particle, "SetParent", particle, particle, 0);
        ActivateEntity(particle);
        AcceptEntityInput(particle, "start");
        CreateTimer(time, DeleteParticle, particle);
    }
    EmitSoundToAllAny("weapons/party_horn_01.wav");  
}

public Action DeleteParticle(Handle timer, any particle)
{
    if (IsValidEntity(particle))
    {
        char classN[64];
        GetEdictClassname(particle, classN, sizeof(classN));
        if (StrEqual(classN, "info_particle_system", false))
        {
            RemoveEdict(particle);
        }
    }
}