#define PLUGIN_NAME		   "SetCollisionGroup Interface"
#define PLUGIN_AUTHOR		 "Pan32 (Thanks organ-harvester)"
#define PLUGIN_DESCRIPTION	"Properly sets the collision group of a player by using the source SDK"
#define PLUGIN_VERSION		"1.0"
#define PLUGIN_URL			"unloze.gay"

#include <sourcemod>
#include <sdktools>

#pragma semicolon 1

//https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/public/const.h#L397
#define MAX_GROUP_VALUE 20

Handle g_hSetCollisionGroupCall = INVALID_HANDLE;


public Plugin:myinfo =
{
	name = PLUGIN_NAME,
	author = PLUGIN_AUTHOR,
	description = PLUGIN_DESCRIPTION,
	version = PLUGIN_VERSION,
	url = PLUGIN_URL
};

public OnPluginStart()
{
	Handle temp = LoadGameConfigFile("SetCollisionGroup.games");
	
	if(temp == INVALID_HANDLE)
	{
		SetFailState("Gamedata file SetCollisionGroup.games.txt not found or invalid.");
	}
	
	//https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/game/shared/baseentity_shared.cpp#L2493
	StartPrepSDKCall(SDKCall_Entity);
	if(!PrepSDKCall_SetFromConf(temp, SDKConf_Signature, "SetCollisionGroup"))
	{
		SetFailState("Function signature for CBaseEntity::SetCollisionGroup not found or invalid.");
	}
	
	PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
	g_hSetCollisionGroupCall = EndPrepSDKCall();
	
	if(g_hSetCollisionGroupCall == INVALID_HANDLE)
	{
		SetFailState("Failed to create a SDKCall for CBaseEntity::SetCollisionGroup.");
	}
}

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
	CreateNative("SetCollisionGroup", Native_SetCollisionGroup);

	RegPluginLibrary("SetCollisionGroup");
	return APLRes_Success;
}

public void SetCollisionGroup(int entity, int group)
{
	SDKCall(g_hSetCollisionGroupCall, entity, group);
}

public int Native_SetCollisionGroup(Handle hPlugin, int numParams)
{
	int entity = GetNativeCell(1);
	int group = GetNativeCell(2);
	
	//Check if valid entity (sanity check)
	//https://github.com/ValveSoftware/source-sdk-2013/blob/master/mp/src/public/const.h#L397
	if (IsValidEntity(entity) && (group < MAX_GROUP_VALUE))
	{
		SetCollisionGroup(entity, group);
	}
}