Code:
#pragma semicolon 1
#define DEBUG
#define PLUGIN_AUTHOR "Grandpa -a"
#define PLUGIN_VERSION "1.14"
#include <sourcemod>
#include <sdktools>
#include <cstrike>
#include <sdkhooks>
#include <smlib>
new bool:DisableSwitch[MAXPLAYERS + 1];
new g_iWeapon[MAXPLAYERS + 1]; // STORES EACH CLIENT'S WEP.
public Plugin myinfo =
{
name = "takeweapon",
author = PLUGIN_AUTHOR,
description = "",
version = PLUGIN_VERSION,
url = ""
};
public void OnPluginStart()
{
regcmds();
if (!IsCSGO())
{
SetFailState("CS:GO Only.");
}
}
public OnClientPutInServer(client)
{
DisableSwitch[client] = false; // automatically sets false when client joins server.
}
public Action:Command_Switch(client, args)
{
DisableSwitch[client] = !DisableSwitch[client];
if (DisableSwitch[client])
{
PrintToChat(client, "[\x0ESwitch Weapon\x01] You have \x0Fdisabled\x01 switching!");
}
else
{
PrintToChat(client, "[\x0ESwitch Weapom\x01] You have \x06enabled\x01 switching!");
}
return Plugin_Handled; // after setting, end the plugin.
}
stock bool:IsCSGO()
{
new String:bufferString[16];
GetGameFolderName(bufferString, 16);
if (StrEqual(bufferString, "csgo", false))
{
LogMessage("Success!");
return true;
}
else
{
LogMessage("Failed to load plugin, CS:GO ony!");
return false;
}
}
bool:IsValidClient(client)
{
if (!(1 <= client <= MaxClients) || !IsClientInGame(client))
return false;
return true;
}
stock bool:exec_PreTakeWeapon(player1, player2)
{
if (IsValidClient(player1))
{
g_iWeapon[player1] = GetPlayerWeaponSlot(player1, 0);
if (g_iWeapon[player1] == -1) { return false; }
new String:szClClassID[64];
GetEdictClassname(g_iWeapon[player2], szClClassID, sizeof(szClClassID));
Client_RemoveWeapon(player1, szClClassID);
return true;
}
}
stock regcmds()
{
RegConsoleCmd("sm_takeweapon", command_takeWeapon, "self-explanatory...");
RegConsoleCmd("sm_takeweaponoff", Command_Switch, "disables the ability for other people to grab your weapon.");
}
public Action:command_takeWeapon(client, args)
{
if (args < 1)
{
PrintToChat(client, "[\x0ESwitch Weapon\x01] Usage: sm_switchknife <player>");
return Plugin_Handled;
}
new String:arg[32];
GetCmdArg(1, arg, sizeof(arg)); // get args
new target = FindTarget(client, arg, false, false); // args = player
/** Possible Errors go here**/
if (target == -1)
{
PrintToChat(client, "[\x0ESwitch Weapon\x01] \x07Can't find player!");
return Plugin_Handled;
}
else if (DisableSwitch[target])
{
PrintToChat(client, "[\x0ESwitch Weapon\x01] \x07That person has disabled switching!");
return Plugin_Handled;
}
else if (!IsPlayerAlive(target))
{
PrintToChat(client, "[\x0ESwitch Weapon\x01] \x07That player is dead!");
return Plugin_Handled;
}
else if (!IsPlayerAlive(client))
{
PrintToChat(client, "[\x0ESwitch Weapon\x01] \x07You are dead!");
return Plugin_Handled;
}
else if (!GetPlayerWeaponSlot(target, 0))
{
PrintToChat(client, "[\x0ESwitch Weapon\x01] \x07Target doesn't have a weapon to switch!");
return Plugin_Handled;
}
/** Exec command**/
if (!IsPlayerAlive(client))
{
PrintToChat(client, "[\x0EAlert!\x01]\x07 Cancelled: You are dead!");
return Plugin_Handled;
}
else if (!IsPlayerAlive(target))
{
PrintToChat(target, "[\x0EAlert!\x01]\x07 Cancelled: That player is dead!");
return Plugin_Handled;
}
if (PreTakeWeapon(client, target))
{
RemovePlayerItem(client, g_iWeapon[client]);
AcceptEntityInput(g_iWeapon[client], "Kill");
exec_TakeWeapon(client, target);
}
else if (!PreTakeWeapon(client, target))
{
exec_TakeWeapon(client, target);
}
}
stock bool:PreTakeWeapon(player1, player2)
{
g_iWeapon[player1] = GetPlayerWeaponSlot(player1, 0);
if (g_iWeapon[player1] == -1) { return false; } else { return true; }
}
exec_TakeWeapon(player1, player2)
{
new Handle:DataPack = CreateDataPack();
WritePackCell(DataPack, player1);
WritePackCell(DataPack, player2);
g_iWeapon[player2] = GetPlayerWeaponSlot(player2, 0);
if (g_iWeapon[player2] == -1) { return; }
new String:szClassID[64];
GetEdictClassname(g_iWeapon[player2], szClassID, sizeof(szClassID));
WritePackString(DataPack, szClassID);
CS_DropWeapon(player2, g_iWeapon[player2], false, true);
CreateDataTimer(0.1, EquipWeapon, DataPack);
}
// trying to pass the target into a timer callback
public Action:EquipWeapon(Handle:timer, Handle:DataPack)
{
new client;
new target;
decl String:szClassID[64];
ResetPack(DataPack);
client = ReadPackCell(DataPack);
target = ReadPackCell(DataPack);
ReadPackString(DataPack, szClassID, sizeof(szClassID));
if (IsPlayerAlive(client))
{
//decl String:newWeapon[64];
//Format(newWeapon, sizeof(newWeapon), szClassID);
PrintToChatAll("client: %s", client);
PrintToChatAll("target: %s", target);
PrintToChatAll("string: %s", szClassID);
new Float:clientvec[3];
GetClientAbsOrigin(client, clientvec);
//TeleportEntity(newWeapon, clientvec, NULL_VECTOR, NULL_VECTOR);
EquipPlayerWeapon(client, g_iWeapon[client]);
EquipPlayerWeapon(target, g_iWeapon[target]);
//SetEntPropEnt(client, Prop_Send, "m_hActiveWeapon", szClassID); // maybe change this variable to szClassID idk
//SetEntPropEnt(target, Prop_Send, "m_hActiveWeapon", szClassID); // maybe change this variable to szClassID idk
CloseHandle(DataPack);
}
}