Raised This Month: $12 Target: $400
 3% 

[TF2Items] Extended Stocks


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ReFlexPoison
☠☠☠
Join Date: Jul 2011
Location: ☠☠☠
Old 01-19-2015 , 22:28   [TF2Items] Extended Stocks
Reply With Quote #1

I'm very thankful for having Voided explain to me how to use the OnGiveNamedItem forward in multiple plugins to have full customization over weapons, miscs, and hates. I've developed these stocks to help simplify creating custom weapons and items in my mods so they can work between each other (ex: One plugin adds an unusual effect while the other enhances the item's attributes by 10)

Some of these stocks use tf2itemsinfo. You can download a working one below.

PHP Code:
/**
 * vim: set ts=4 :
 * =============================================================================
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
 * =============================================================================
 *
 * This file is part of the SourceMod/SourcePawn SDK.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, version 3.0, as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * As a special exception, AlliedModders LLC gives you permission to link the
 * code of this program (as well as its derivative works) to "Half-Life 2," the
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
 * by the Valve Corporation.  You must obey the GNU General Public License in
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
 * this exception to all derivative works.  AlliedModders LLC defines further
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
 * or <http://www.sourcemod.net/license.php>.
 *
 * Version: $Id$
 */

#if defined _tf2items_extended_stocks
 #endinput
#endif
#define _tf2items_extended_stocks

#include <tf2itemsinfo>

/**
 * Finds an attribute and its index within a TF2Items item handle.
 *
 * @param hItem            Handle to the TF2Items object that we'll be operating with.
 * @param iAttribute    Item attribute to find.
 * @return                Attribute index if found, -1 otherwise.
 */
stock TF2Items_FindAttributeIndex(Handle:hItemiAttribute)
{
    new 
iNumAttributes TF2Items_GetNumAttributes(hItem);
    for(new 
0iNumAttributesi++)
    {
        if(
TF2Items_GetAttributeId(hItemi) == iAttribute)
            return 
i;
    }
    return -
1;
}

/**
 * Replaces (or inserts) an attribute within a TF2Items item handle.
 *
 * @param hItem            Handle to the TF2Items object that we'll be operating with.
 * @param iAttribute    Item attribute replace/insert.
 * @param flValue        Value of the attribute.
 * @param bForce        Insert a new attribute if attribute can't be replaced?.
 * @return                True if replaced or inserted, false otherwise.
 */
stock TF2Items_ReplaceAttribute(Handle:hItemiAttributeFloat:flValuebool:bInsert true)
{
    new 
iNumAttributes TF2Items_GetNumAttributes(hItem);
    for(new 
0iNumAttributesi++)
    {
        if(
TF2Items_GetAttributeId(hItemi) == iAttribute)
        {
            
TF2Items_SetAttribute(hItemiiAttributeflValue);
            return 
true;
        }
    }

    if(
bInsert && iNumAttributes <= 15)
    {
        
TF2Items_SetNumAttributes(hItemiNumAttributes 1);
        
TF2Items_SetAttribute(hItemiNumAttributesiAttributeflValue);
        return 
true;
    }

    return 
false;
}

/**
 * Replaces (or inserts) an attribute within a TF2Items item handle.
 *
 * @param hItem            Handle to the TF2Items object that we'll be operating with.
 * @param strAttribute    Name of the attribute to replace/insert.
 * @param flValue            Value of the attribute.
 * @param bForce            Insert a new attribute if attribute can't be replaced?.
 * @return                    True if replaced or inserted, false otherwise.
 */
stock TF2Items_ReplaceAttributeByName(Handle:hItem, const String:strAttribute[], Float:flValuebool:bInsert true)
{
    new 
iNumAttributes TF2Items_GetNumAttributes(hItem);
    
decl String:strName[56];
    for(new 
0iNumAttributesi++)
    {
        new 
iAttribute TF2Items_GetAttributeId(hItemi);
        
TF2II_GetAttributeNameByID(iAttributestrNamesizeof(strName));
        if(
StrEqual(strAttributestrName))
        {
            
TF2Items_SetAttribute(hItemiiAttributeflValue);
            return 
true;
        }
    }

    if(
bInsert && iNumAttributes <= 15)
    {
        
TF2Items_SetNumAttributes(hItemiNumAttributes 1);
        
TF2Items_SetAttribute(hItemiNumAttributesTF2II_GetAttributeIDByName(strAttribute), flValue);
        
PrintToServer("%i - %f"TF2II_GetAttributeIDByName(strAttribute), flValue);
        return 
true;
    }

    return 
false;
}

/**
 * Adds to an attribute within a TF2Items item handle.
 *
 * @param hItem            Handle to the TF2Items object that we'll be operating with.
 * @param iAttribute        Index of the attribute to add to.
 * @param flValue            How much to add to the attribute value.
 * @return                    True if added, false otherwise.
 */
stock bool:TF2Items_AddAttribute(Handle:hItemiAttributeFloat:flAmount)
{
    for(new 
0TF2Items_GetNumAttributes(hItem); i++)
    {
        if(
TF2Items_GetAttributeId(hItemi) == iAttribute)
        {
            new 
Float:flCurrent TF2Items_GetAttributeValue(hItemi);
            
TF2Items_SetAttribute(hItemiiAttributeflCurrent flAmount);
            return 
true;
        }
    }
    return 
false;
}

/**
 * Adds to an attribute within a TF2Items item handle.
 *
 * @param hItem            Handle to the TF2Items object that we'll be operating with.
 * @param strAttribute    Name of the attribute to add to.
 * @param flValue            How much to add to the attribute value.
 * @return                    True if added, false otherwise.
 */
stock bool:TF2Items_AddAttributeByName(Handle:hItem, const String:strAttribute[], Float:flAmount)
{
    
decl String:strName[56];
    for(new 
0TF2Items_GetNumAttributes(hItem); i++)
    {
        
TF2II_GetAttribName(TF2Items_GetAttributeId(hItemi), strNamesizeof(strName));
        if(
StrEqual(strAttributestrName))
        {
            new 
Float:flCurrent TF2Items_GetAttributeValue(hItemi);
            
TF2Items_SetAttribute(hItemiiAttributeflCurrent flAmount);
            return 
true;
        }
    }
    return 
false;
}

/**
 * Subtracts from an attribute within a TF2Items item handle.
 *
 * @param hItem            Handle to the TF2Items object that we'll be operating with.
 * @param iAttribute        Index of the attribute to subtract from.
 * @param flValue            How much to subtract from the attribute value.
 * @return                    True if subtracted, false otherwise.
 */
stock bool:TF2Items_SubtractAttribute(Handle:hItemiAttributeFloat:flAmount)
{
    for(new 
0TF2Items_GetNumAttributes(hItem); i++)
    {
        if(
TF2Items_GetAttributeId(hItemi) == iAttribute)
        {
            new 
Float:flCurrent TF2Items_GetAttributeValue(hItemi);
            
TF2Items_SetAttribute(hItemiiAttributeflCurrent flAmount);
            return 
true;
        }
    }
    return 
false;
}

/**
 * Subtracts from an attribute within a TF2Items item handle.
 *
 * @param hItem            Handle to the TF2Items object that we'll be operating with.
 * @param iAttribute        Name of the attribute to subtract from.
 * @param flValue            How much to subtract from the attribute value.
 * @return                    True if subtracted, false otherwise.
 */
stock bool:TF2Items_SubtractAttributeByName(Handle:hItem, const String:strAttribute[], Float:flAmount)
{
    
decl String:strName[56];
    for(new 
0TF2Items_GetNumAttributes(hItem); i++)
    {
        
TF2II_GetAttribName(TF2Items_GetAttributeId(hItemi), strNamesizeof(strName));
        if(
StrEqual(strAttributestrName))
        {
            new 
Float:flCurrent TF2Items_GetAttributeValue(hItemi);
            
TF2Items_SetAttribute(hItemiiAttributeflCurrent flAmount);
            return 
true;
        }
    }
    return 
false;
}

/**
 * Removes an attribute within a TF2Items item handle.
 *
 * @param hItem            Handle to the TF2Items object that we'll be operating with.
 * @param strAttribute    Name of the attribute to remove.
 * @return                    True if removed, false otherwise.
 */
stock TF2Items_RemoveAttributeByName(Handle:hItem, const String:strAttribute[])
{
    
decl String:strName[56];
    
decl String:strFormat[56];
    for(new 
0TF2Items_GetNumAttributes(hItem); i++)
    {
        new 
iAttribute TF2Items_GetAttributeId(hItemi);
        
TF2II_GetAttribName(iAttributestrNamesizeof(strName));
        if(
StrContains(strName"eater"false) != -1)
            continue;

        if(
StrContains(strAttributestrName))
        {
            
TF2II_GetAttribDescrFormat(iAttributestrFormatsizeof(strFormat));
            if(
StrContains(strFormat"percentage"false) != -1)
                
TF2Items_ReplaceAttribute(hItemiAttribute1.0);
            else
                
TF2Items_ReplaceAttribute(hItemiAttribute0.0);

            return 
true;
        }
    }

    return 
false;
}

/**
 * Removes all default attributes within a TF2Items item handle,
 * (default attributes found from items_game.txt).
 *
 * @param hItem            Handle to the TF2Items object that we'll be operating with.
 * @noreturn
 */
stock TF2Items_RemoveDefaultAttributes(Handle:hItem)
{
    
decl String:strName[56];
    
decl String:strFormat[56];

    for(new 
0TF2Items_GetNumAttributes(hItem); i++)
    {
        new 
iAttribute TF2Items_GetAttributeId(hItemi);
        
TF2II_GetAttribName(iAttributestrNamesizeof(strName));
        if(
StrContains(strName"eater"false) != -1)
            continue;

        
TF2II_GetAttribDescrFormat(iAttributestrFormatsizeof(strFormat));
        if(
StrContains(strFormat"percentage"false) != -1)
            
TF2Items_ReplaceAttribute(hItemiAttribute1.0);
        else
            
TF2Items_ReplaceAttribute(hItemiAttribute0.0);
    }

Here is an example of using two plugins together.

One plugin will change/add (replace) attributes of weapons:
PHP Code:
public Action:TF2Items_OnGiveNamedItem(iClientString:strClassname[], iItemDefinitionIndex, &Handle:hItem)
{
    static 
Handle:hNewItem INVALID_HANDLE;
    if(
hNewItem != INVALID_HANDLE)
    {
        
CloseHandle(hNewItem);
        
hNewItem INVALID_HANDLE;
    }

    switch(
iItemDefinitionIndex)
    {
        case 
39351:
        {
            if(
hItem == INVALID_HANDLE)
            {
                
hItem TF2Items_CreateItem(OVERRIDE_ATTRIBUTES PRESERVE_ATTRIBUTES);
                
hNewItem hItem;
            }

            new 
iFlags TF2Items_GetFlags(hItem);
            if(!(
iFlags OVERRIDE_ATTRIBUTES)) iFlags |= OVERRIDE_ATTRIBUTES;
            
TF2Items_SetFlags(hItemiFlags);

            
TF2Items_ReplaceAttribute(hItem250.5);
            
TF2Items_ReplaceAttribute(hItem2071.33);
            
TF2Items_ReplaceAttribute(hItem1441.0);
            
TF2Items_ReplaceAttribute(hItem583.2);
        }
    }

The other will simply remove default attributes while preserving custom ones (such as killstreak effects)

PHP Code:
public Action:TF2Items_OnGiveNamedItem(iClientString:strClassname[], iItemDefinitionIndex, &Handle:hItem)
{
    static 
Handle:hNewItem INVALID_HANDLE;
    if(
hNewItem != INVALID_HANDLE)
    {
        
CloseHandle(hNewItem);
        
hNewItem INVALID_HANDLE;
    }

    switch(
iItemDefinitionIndex)
    {
        case 
39351:
        {
            if(
hItem == INVALID_HANDLE)
            {
                
hItem TF2Items_CreateItem(OVERRIDE_ATTRIBUTES PRESERVE_ATTRIBUTES);
                
hNewItem hItem;
            }

            new 
iFlags TF2Items_GetFlags(hItem);
            if(!(
iFlags OVERRIDE_ATTRIBUTES)) iFlags |= OVERRIDE_ATTRIBUTES;
            if(
iFlags PRESERVE_ATTRIBUTESiFlags &= ~PRESERVE_ATTRIBUTES;
            
TF2Items_SetFlags(hItemiFlags);

            
TF2Items_RemoveDefaultAttributes(hItem);
        }
    }

Attached Files
File Type: inc tf2itemsextended.inc (9.1 KB, 1280 views)
File Type: sp Get Plugin or Get Source (tf2itemsinfo.sp - 918 views - 93.7 KB)
File Type: smx tf2itemsinfo.smx (34.2 KB, 2076 views)

Last edited by ReFlexPoison; 01-19-2015 at 22:30.
ReFlexPoison is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 01-19-2015 , 22:30   Re: [TF2Items] Extended Stocks
Reply With Quote #2

Here's some more useful stuff
Spoiler

You might notice spy doesn't spawn sappers. That's because I never figured out how it works.

Would your thing be useful for determining whether or not an item has X attribute? And its value. I tried TF2Attributes related natives but I must've done it wrong. (I was trying to check if a player had attribute 252 and get the float value it's set to).
__________________

Last edited by Chdata; 01-20-2015 at 00:00.
Chdata is offline
ReFlexPoison
☠☠☠
Join Date: Jul 2011
Location: ☠☠☠
Old 01-19-2015 , 22:37   Re: [TF2Items] Extended Stocks
Reply With Quote #3

Quote:
Originally Posted by Chdata View Post
Would your thing be useful for determining whether or not an item has X attribute? And its value. I tried TF2Attributes related natives but I must've done it wrong. (I was trying to check if a player had attribute 252 and get the float value it's set to).
Use TF2Items_FindAttributeIndex and check if it is not equal to -1.
You can then retrieve the attributes value via TF2Items_GetAttributeValue.
I tend to avoid using TF2Attributes since several of its core functionality could already be accessed though OnGiveNamedItem by using a little brain power.

Last edited by ReFlexPoison; 01-19-2015 at 22:40.
ReFlexPoison is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 01-19-2015 , 22:46   Re: [TF2Items] Extended Stocks
Reply With Quote #4

I hear the getattribute stuff doesn't work on a weapons "default" attributes (well, the bottom function I listed there at least) and only works on extra attributes that were added by tf2attributes or modified by tf2items.

(Except Flam has yet to push out a version that can lul).

hmm

I mean, I guess I already have a hard coded array that I populate every time I use spawnweapon and after post_inventoryapplication. But eh.
__________________

Last edited by Chdata; 01-19-2015 at 22:48.
Chdata is offline
ReFlexPoison
☠☠☠
Join Date: Jul 2011
Location: ☠☠☠
Old 01-19-2015 , 22:49   Re: [TF2Items] Extended Stocks
Reply With Quote #5

Quote:
Originally Posted by Chdata View Post
I hear the getattribute stuff doesn't work on a weapons "default" attributes (well, the bottom function I listed there at least) and only works on extra attributes that were added by tf2attributes or modified by tf2items.

(Except Flam has yet to push out a version that can lul).

hmm

I mean, I guess I already have a hard coded array that I populate every time I use spawnweapon and after post_inventoryapplication. But eh.
You can loop though attributes found with tf2itemsinfo, and use TF2Items_ReplaceAttribute to completly overwrite it. I've been using TF2Items_ReplaceAttribute for everything now, even if it may a bit excessive.
ReFlexPoison is offline
VoiDeD
AlliedModders Donor
Join Date: Mar 2009
Location: Illinois, USA
Old 01-21-2015 , 14:39   Re: [TF2Items] Extended Stocks
Reply With Quote #6

Iterating the non-networked SOC attributes of items is fairly difficult and requires an extension to do the proper way. Generally I'd say avoid that and tf2attribs entirely if you can manage, unless you enjoy the pain that is gamedata updating every few updates.

And it really goes without saying that tf2itemsinfo is a bloated trainwreck.
__________________

Last edited by VoiDeD; 01-21-2015 at 14:40.
VoiDeD is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 01-24-2015 , 04:42   Re: [TF2Items] Extended Stocks
Reply With Quote #7

I'd suggest not naming your functions the same as the extension, since it's different.

As for tf2attributes, can we get setbyname and removebyname added to tf2stocks along with automatic gd updates? These 2 are very useful. There are a few others in tf2attributes plugin, like getvalue and such, but they're not used nearly as often.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
Michalplyoutube
Veteran Member
Join Date: Jan 2013
Location: Tank Carrier in Mannhatt
Old 01-30-2015 , 10:36   Re: [TF2Items] Extended Stocks
Reply With Quote #8

TF2BWRR Can spawn a sapper using tf2itemsinfo
__________________
The plugin developer of TF2BWR Reborn
And a TF2 Player
Michalplyoutube is offline
Uyuzgamer
Junior Member
Join Date: Jul 2015
Old 08-08-2015 , 15:06   Re: [TF2Items] Extended Stocks
Reply With Quote #9

L 08/08/2015 - 22:07:42: [SM] Displaying call stack trace for plugin "tf2itemsinfo.smx":
L 08/08/2015 - 22:07:42: [SM] [0] Line 1652, C:\Server\tf\addons\sourcemod\scripting\tf2it emsinfo.sp::GetAttribIDByName()
L 08/08/2015 - 22:07:42: [SM] [1] Line 1526, C:\Server\tf\addons\sourcemod\scripting\tf2it emsinfo.sp:: PrecacheItemSchema()
L 08/08/2015 - 22:07:42: [SM] [2] Line 252, C:\Server\tf\addons\sourcemod\scripting\tf2it emsinfo.sp::OnPluginStart()

on Centos
Pls help

Last edited by Uyuzgamer; 08-08-2015 at 15:08.
Uyuzgamer is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 08-09-2015 , 02:29   Re: [TF2Items] Extended Stocks
Reply With Quote #10

You cut off the most important part of the error message. Likely TF2ItemsInfo got killed by the watchdog timer.
Potato Uno is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:40.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode