Skip to content

Commit

Permalink
Merge pull request #186 from Cruze03/master
Browse files Browse the repository at this point in the history
Added 2 new natives (Weapons_GetClientKnife and Weapons_SetClientKnife)
  • Loading branch information
kgns authored Aug 3, 2020
2 parents fb093ef + 9c712f1 commit 2452fc0
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
10 changes: 10 additions & 0 deletions addons/sourcemod/scripting/include/weapons.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#if defined _weapons_included_
#endinput
#endif
#define _weapons_included_

//Retrieves and stores client's knife entity name in 'sKnife'. returns 'weapon_knife' if selection is 'OwnKnife'.
native void Weapons_GetClientKnife(int client, char[] sKnife, int Size);

//Sets client's knife to what is stored in 'sKnife'. Throws Native Error if knife name isn't valid. Update = Store the 'sKnife' in client's mysql table?
native void Weapons_SetClientKnife(int client, char[] sKnife, bool update);
62 changes: 62 additions & 0 deletions addons/sourcemod/scripting/weapons.sp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <sdkhooks>
#include <cstrike>
#include <PTaH>
#include <weapons>

#pragma semicolon 1
#pragma newdecls required
Expand All @@ -31,6 +32,9 @@
#include "weapons/database.sp"
#include "weapons/config.sp"
#include "weapons/menus.sp"
#include "weapons/natives.sp"

//#define DEBUG

public Plugin myinfo =
{
Expand All @@ -41,6 +45,13 @@ public Plugin myinfo =
url = "https://www.oyunhost.net"
};

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
CreateNative("Weapons_SetClientKnife", Weapons_SetClientKnife_Native);
CreateNative("Weapons_GetClientKnife", Weapons_GetClientKnife_Native);
return APLRes_Success;
}

public void OnPluginStart()
{
if(GetEngineVersion() != Engine_CSGO)
Expand Down Expand Up @@ -96,8 +107,59 @@ public void OnPluginStart()
AddCommandListener(ChatListener, "say");
AddCommandListener(ChatListener, "say2");
AddCommandListener(ChatListener, "say_team");

#if defined DEBUG
RegAdminCmd("sm_setknife", Command_SetKnife, ADMFLAG_ROOT, "Sets knife of specific player.");
RegAdminCmd("sm_getknife", Command_GetClientKnife, ADMFLAG_ROOT, "Gets specific player's knife class name.");
#endif
}
#if defined DEBUG
public Action Command_SetKnife(int client, int args)
{
if(args != 2)
{
ReplyToCommand(client, "[SM] Usage: sm_setknife <playername> <weaponname>");
return Plugin_Handled;
}
char buffer[64];
GetCmdArg(1, buffer, sizeof(buffer));
int target = FindTarget(client, buffer);
if(target == -1)
{
ReplyToCommand(client, "[SM] Please enter valid playername!");
return Plugin_Handled;
}
GetCmdArg(2, buffer, sizeof(buffer));
if(SetClientKnife(target, buffer) == -1)
{
ReplyToCommand(client, "[SM] Knife %s is not valid.", buffer);
return Plugin_Handled;
}
ReplyToCommand(client, "[SM] Successfully set %N's knife.", target);
return Plugin_Handled;
}

public Action Command_GetClientKnife(int client, int args)
{
if(args != 1)
{
ReplyToCommand(client, "[SM] Usage: sm_getknife <playername>");
return Plugin_Handled;
}
char buffer[32];
GetCmdArg(1, buffer, sizeof(buffer));
int target = FindTarget(client, buffer);
if(target == -1)
{
ReplyToCommand(client, "[SM] Please enter valid playername!");
return Plugin_Handled;
}
char sKnife[64];
GetClientKnife(client, sKnife, sizeof(sKnife));
ReplyToCommand(client, "[SM] %N's knife is %s.", target, sKnife);
return Plugin_Handled;
}
#endif
public Action CommandWeaponSkins(int client, int args)
{
if (IsValidClient(client))
Expand Down
54 changes: 54 additions & 0 deletions addons/sourcemod/scripting/weapons/helpers.sp
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,57 @@ stock int GetRemainingGracePeriodSeconds(int client)
return remaining > 0 ? remaining : -1;
}
}

void GetClientKnife(int client, char[] KnifeName, int Size)
{
if(g_iKnife[client] == 0)
{
Format(KnifeName, Size, "weapon_knife");
}
else
{
Format(KnifeName, Size, g_WeaponClasses[g_iKnife[client]]);
}
}

int SetClientKnife(int client, char[] sKnife, bool Native = false, bool update = true)
{
int knife;
if(strcmp(sKnife, "weapon_knife") == 0)
{
knife = 0;
}
else
{
int count = -1;
for(int i = 33; i < sizeof(g_WeaponClasses); i++)
{
if(strcmp(sKnife, g_WeaponClasses[i]) == 0)
{
count = i;
break;
}
}
if(count == -1)
{
if(Native)
{
return ThrowNativeError(25, "Knife (%s) is not valid.", sKnife);
}
else
{
return -1;
}
}
knife = count;
}
g_iKnife[client] = knife;
if(update)
{
char updateFields[16];
Format(updateFields, sizeof(updateFields), "knife = %d", knife);
UpdatePlayerData(client, updateFields);
}
RefreshWeapon(client, knife, knife == 0);
return 0;
}
34 changes: 34 additions & 0 deletions addons/sourcemod/scripting/weapons/natives.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public int Weapons_GetClientKnife_Native(Handle plugin, int numparams)
{
int client = GetNativeCell(1);
if (client < 1 || client > MaxClients)
{
return ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index (%d).", client);
}
if(!IsClientInGame(client))
{
return ThrowNativeError(SP_ERROR_NATIVE, "Client (%d) is not in game.", client);
}
char KnifeName[64];
GetClientKnife(client, KnifeName, sizeof(KnifeName));
SetNativeString(2, KnifeName, GetNativeCell(3));
return 0;
}

public int Weapons_SetClientKnife_Native(Handle plugin, int numparams)
{
int client = GetNativeCell(1);
if (client < 1 || client > MaxClients)
{
return ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index (%d).", client);
}
if(!IsClientInGame(client))
{
return ThrowNativeError(SP_ERROR_NATIVE, "Client (%d) is not in game.", client);
}
char KnifeName[64];
GetNativeString(2, KnifeName, 64);
bool update = !!GetNativeCell(3);
SetClientKnife(client, KnifeName, true, update);
return 0;
}

0 comments on commit 2452fc0

Please sign in to comment.