/*
	* Admin knife by OverGame
	* Bhop https://forums.alliedmods.net/showthread.php?t=57900
*/

#include < sourcemod >
#include < sdktools >

#pragma semicolon 1

new knife [ MAXPLAYERS + 1 ], VelocityOffset_0, VelocityOffset_1, BaseVelocityOffset;
public Plugin:myinfo = {
	name = "Admin knife",
	author = "Overgame",
	description = "Admin knife menu",
	version = "1.0.0"
}

public OnPluginStart () {
	RegAdminCmd ( "say !aknife", func_admin_knife, ADMFLAG_CHEATS );
	
	HookEvent ( "player_spawn", PlayerSpawn );
	HookEvent ( "player_jump", PlayerJumpEvent );
	VelocityOffset_0 = FindSendPropOffs ( "CBasePlayer", "m_vecVelocity [ 0 ]" );
	if ( VelocityOffset_0 == -1 ) SetFailState ( "[BunnyHop] Error: Failed to find Velocity[0] offset, aborting" );
	VelocityOffset_1 = FindSendPropOffs ( "CBasePlayer", "m_vecVelocity [ 1 ]" );
	if ( VelocityOffset_1 == -1 ) SetFailState ( "[BunnyHop] Error: Failed to find Velocity[1] offset, aborting" );
	BaseVelocityOffset = FindSendPropOffs ( "CBasePlayer", "m_vecBaseVelocity" );
	if ( BaseVelocityOffset == -1 ) SetFailState ( "[BunnyHop] Error: Failed to find the BaseVelocity offset, aborting" );
}

public PlayerJumpEvent ( Handle:event,const String:name [],bool:dontBroadcast ) {
	new id = GetClientOfUserId ( GetEventInt ( event, "userid" ) );
	if ( knife [ id ] ) {
		new Float:finalvec [ 3 ];
		finalvec [ 0 ] = GetEntDataFloat ( id, VelocityOffset_0 ) * 1.0 / 2.0;
		finalvec [ 1 ] = GetEntDataFloat ( id, VelocityOffset_1 ) * 1.0 / 2.0;
		finalvec [ 2 ] = 1.0 * 50.0;
		SetEntDataVector ( id, BaseVelocityOffset, finalvec, true );
	}
}

public Action:PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast) {
	new id = GetClientOfUserId ( GetEventInt ( event, "userid" ) );
	if ( !IsPlayerAlive ( id ) ) return;

	CreateTimer ( 2.0, SpawnKnife, id );
}

public Action:SpawnKnife ( Handle:timer, any:id ) {
	Giveknife ( id );
}

public Action:func_admin_knife ( id, args ) {
	if ( knife [ id ] ) knife [ id ] = false;
	else knife [ id ] = true;
	
	if ( knife [ id ] ) Giveknife ( id );
}

Giveknife ( id ) {
	if ( !IsClientInGame ( id ) || !IsPlayerAlive ( id ) ) return;
	new iWeapon = GetPlayerWeaponSlot ( id, 2 );
	if ( iWeapon == INVALID_ENT_REFERENCE ) return;
	if ( knife [ id ] ) {
		new iItem = GivePlayerItem ( id, "weapon_knife_m9_bayonet" );
		EquipPlayerWeapon ( id, iItem );
	} else {
		new iItem = GivePlayerItem ( id, "weapon_knife" );
		EquipPlayerWeapon ( id, iItem );
	}
}