View Single Post
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 11-18-2019 , 09:16   Re: [TF2] Client game crash when the client get a weapon
Reply With Quote #4

I managed to get this working with the [TF2] Econ Data plugin written by the talented nosoop (link:https://forums.alliedmods.net/showthread.php?t=315011 )

It works good but has a 3 problems I need help with.

1. I have a tag mismatch with this piece of code:
Quote:
int iTargetClassType = view_as<int>(TF2_GetPlayerClass(iTarget));
int iSlot = TF2Econ_GetItemSlot(iItemID, iTargetClassType); //I get a tag mismatch warning on this line
2: I can't seem to find a way to remove only ONE existing cosmetic item before giving a new one. I can either remove all cosmetics first, or don't remove any. I opted to not remove any.

3. For some reason it will not allow me to give any saxxy class item. It gives error "Invalid entity (-1 - -1)" so I added a check for that.

PHP Code:
#include <tf2attributes>
#include <tf_econ_data>

#pragma semicolon 1

#define DEBUG 0
#define PLUGIN_VERSION "1.0"

Handle g_hWeaponEquip;
Handle g_hWWeaponEquip;
Handle g_hIsWearable;
Handle g_hGameConfig;

public 
Plugin myinfo 
{
    
name "Give Stuff",
    
author "Whai and PC Gamer",
    
description "Give Weapon and Cosmetic items to players",
    
version PLUGIN_VERSION,
    
url "https://forums.alliedmods.net"
}

public 
void OnPluginStart() 
{
    
g_hGameConfig LoadGameConfigFile("give.stuff");
    
    if (!
g_hGameConfig)
    {
        
SetFailState("Failed to find give.stuff.txt gamedata! Can't continue.");
    }    
    
    
StartPrepSDKCall(SDKCall_Player);
    
PrepSDKCall_SetFromConf(g_hGameConfigSDKConf_Virtual"WeaponEquip");
    
PrepSDKCall_AddParameter(SDKType_CBaseEntitySDKPass_Pointer);
    
g_hWeaponEquip EndPrepSDKCall();
    
    if (!
g_hWeaponEquip)
    {
        
SetFailState("Failed to prepare the SDKCall for giving weapons. Try updating gamedata or restarting your server.");
    }
    
    
StartPrepSDKCall(SDKCall_Player);
    
PrepSDKCall_SetFromConf(g_hGameConfigSDKConf_Virtual"EquipWearable");
    
PrepSDKCall_AddParameter(SDKType_CBaseEntitySDKPass_Pointer);
    
g_hWWeaponEquip EndPrepSDKCall();
    
    if (!
g_hWWeaponEquip)
    {
        
SetFailState("Failed to prepare the SDKCall for giving wearable weapons. Try updating gamedata or restarting your server.");
    }
    
    
StartPrepSDKCall(SDKCall_Player);
    
PrepSDKCall_SetFromConf(g_hGameConfigSDKConf_Virtual"IsWearable");
    
PrepSDKCall_AddParameter(SDKType_CBaseEntitySDKPass_Pointer);
    
g_hIsWearable EndPrepSDKCall();
    
    if (!
g_hIsWearable)
    {
        
SetFailState("Failed to prepare the SDKCall for Determining if item is wearable. Try updating gamedata or restarting your server.");
    }    
    
    
RegAdminCmd("sm_give"Command_GiveADMFLAG_SLAY"Have a Great Day!");    
}

public 
Action:Command_Give(clientargs)
{
    if (
args != 2)
    {
        
ReplyToCommand(client"[SM] Usage: sm_give <client> <index>");
        return 
Plugin_Handled;
    }
    
    new 
String:arg1[32];
    
GetCmdArg(1arg1sizeof(arg1));
    
    new 
String:arg2[32];
    
GetCmdArg(2arg2sizeof(arg2));

    
decl String:target_name[MAX_TARGET_LENGTH];
    
decl target_list[MAXPLAYERS], target_countbool:tn_is_ml;
    
    if ((
target_count ProcessTargetString(
                    
arg1,
                    
client,
                    
target_list,
                    
MAXPLAYERS,
                    
COMMAND_FILTER_CONNECTED,
                    
target_name,
                    
sizeof(target_name),
                    
tn_is_ml)) <= 0)
    {
        
ReplyToTargetError(clienttarget_count);
        return 
Plugin_Handled;
    }
    
    new 
itemID StringToInt(arg2);

    for (new 
0target_counti++)
    {
        
TF2_GivePlayerItem(clienttarget_list[i], itemID);        
    }
    
    return 
Plugin_Handled;
}

public 
Action:TF2_GivePlayerItem(int iClientint iTargetint iItemID)
{
    if (!(
IsValidClient(iTarget) || IsPlayerAlive(iTarget)))
    {
        
PrintToChat(iClient"Target is Not Alive");
        return 
Plugin_Handled;
    }
    
    if(!
TF2Econ_IsValidItemDefinition(iItemID))
    {
        
PrintToChat(iClient"[SM] Invalid item definition index");
        return 
Plugin_Handled;
    }

    
int iLevel GetRandomInt(1,99);
    
int iQuality GetRandomInt(0,14);

    
char strClassname[32];
    
TF2Econ_GetItemClassName(iItemIDstrClassnamesizeof(strClassname));
    
int weaponprimary CreateEntityByName(strClassname);
    if (!
IsValidEntity(weaponprimary))
    {
        
PrintToChat(iClient,"Invalid Entity: Item Classname : %s"strClassname);    
        return 
Plugin_Handled;
    }

    
char entclass[64];
    
GetEntityNetClass(weaponprimaryentclasssizeof(entclass));
    
SetEntData(weaponprimaryFindSendPropInfo(entclass"m_iItemDefinitionIndex"), iItemID);
    
SetEntData(weaponprimaryFindSendPropInfo(entclass"m_bInitialized"), 1);
    
SetEntData(weaponprimaryFindSendPropInfo(entclass"m_iEntityLevel"), iLevel);
    
SetEntData(weaponprimaryFindSendPropInfo(entclass"m_iEntityQuality"), iQuality);
    
TF2Attrib_SetByDefIndex(weaponprimary7250.2);
    
    
int iTargetClassType view_as<int>(TF2_GetPlayerClass(iTarget));
    
int iSlot TF2Econ_GetItemSlot(iItemIDiTargetClassType);  //I get a tag mismatch warning on this line
    
if (iSlot <0)
    {
        
PrintToChat(iClient"Item Index %i is invalid for target class"iItemID);
        return 
Plugin_Handled;
    }

    if (
iSlot >6)
    {
        
//        TF2_RemoveAllWearables(iTarget);
        
        
DispatchSpawn(weaponprimary);
        
SDKCall(g_hWWeaponEquipiTargetweaponprimary);
    }
    else
    {
        
TF2_RemoveWeaponSlot(iTargetiSlot);
        
DispatchSpawn(weaponprimary);
        
SDKCall(g_hWeaponEquipiTargetweaponprimary);
    }
    
    
char strItemName[64];
    
TF2Econ_GetItemName(iItemIDstrItemNamesizeof(strItemName));
    
PrintToChat(iClient"Gave %s to %N"strItemNameiTarget);    
    
    
#if DEBUG
    
char strQualityName[32];
    
TF2Econ_GetQualityName(iQualitystrQualityNamesizeof(strQualityName));

    
char strItemSlotName[32];
    
TF2Econ_TranslateLoadoutSlotIndexToName(iSlotstrItemSlotNamesizeof(strItemSlotName));

    
PrintToChat(iClient,"[Give Debug]: Item Name : %s"strItemName);
    
PrintToChat(iClient,"[Give Debug]: Item Classname : %s"strClassname);
    
PrintToChat(iClient,"[Give Debug]: iSlot : %i"iSlot);
    
PrintToChat(iClient,"[Give Debug]: iSlotName : %s"strItemSlotName);    
    
PrintToChat(iClient,"[Give Debug]: iQuality : %i"iQuality);
    
PrintToChat(iClient,"[Give Debug]: iQualityName : %s"strQualityName);    
    
PrintToChat(iClient,"[Give Debug]: iLevel : %i"iLevel);

    
#endif

    
return Plugin_Handled;
}

stock bool:IsValidClient(client)
{
    if (
client <= 0) return false;
    if (
client MaxClients) return false;
    return 
IsClientInGame(client);
}

stock TF2_RemoveAllWearables(client)
{
    
RemoveWearable(client"tf_wearable""CTFWearable");
    
RemoveWearable(client"tf_powerup_bottle""CTFPowerupBottle");
}

stock RemoveWearable(clientString:classname[], String:networkclass[])
{
    if (
IsValidClient(client) && IsPlayerAlive(client))
    {
        new 
edict MaxClients+1;
        while((
edict FindEntityByClassname(edictclassname)) != -1)
        {
            
decl String:netclass[32];
            if (
GetEntityNetClass(edictnetclasssizeof(netclass)) && StrEqual(netclassnetworkclass))
            {
                if (
GetEntPropEnt(edictProp_Send"m_hOwnerEntity") == client)
                {
                    
AcceptEntityInput(edict"Kill"); 
                }
            }
        }
    }

Attached Files
File Type: txt give.stuff.txt (349 Bytes, 56 views)
File Type: sp Get Plugin or Get Source (give.sp - 53 views - 6.1 KB)
PC Gamer is offline