#include <sourcemod>
#include <sdkhooks>
#include <sdktools>

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin:myinfo =
{
	name = "Button Presser",
	description = "Notify admins when buttons are pressed",
	author = "zaCade (lost the original source btw)",
	version = "1.0",
	url = ""
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	int iEntity = INVALID_ENT_REFERENCE;

	while ((iEntity = FindEntityByClassname(iEntity, "*")) != INVALID_ENT_REFERENCE)
	{
		OnEntityCreated(iEntity, "");
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnEntityCreated(int iEntity, const char[] sClassname)
{
	if (!IsValidEdict(iEntity))
		return;

	char sClassname2[64];
	GetEdictClassname(iEntity, sClassname2, sizeof(sClassname2))

	if (StrEqual(sClassname2, "func_button") || StrEqual(sClassname2, "func_rot_button"))
		SDKHook(iEntity, SDKHook_Use, OnPressed);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action OnPressed(int iEntity, int iActivator, int iCaller, UseType type, float fValue)
{
	if (IsValidClient(iActivator))
	{
		char sSteamID[32];
		if (!GetClientAuthId(iActivator, AuthId_Steam2, sSteamID, sizeof(sSteamID)))
		{
			sSteamID = "UNKNOWN";
		}
		char sTargetname[128];
		GetEntPropString(iEntity, Prop_Data, "m_iName", sTargetname, sizeof(sTargetname));

		for(int i = 1; i <= MaxClients; i++)
		{
			if(IsValidClient(i) && CheckCommandAccess(i, "", ADMFLAG_GENERIC))
				PrintToConsole(i, "%N (%s) Pressed button: %s", iActivator, sSteamID, sTargetname);
		}
	}
	return Plugin_Continue;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock int IsValidClient(int client, bool nobots = true)
{
	if (client <= 0 || client > MaxClients || !IsClientConnected(client) || (nobots && IsFakeClient(client)))
		return false;

	return IsClientInGame(client);
}