Raised This Month: $ Target: $400
 0% 

[TF2Items] Extended Stocks


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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, 1301 views)
File Type: sp Get Plugin or Get Source (tf2itemsinfo.sp - 936 views - 93.7 KB)
File Type: smx tf2itemsinfo.smx (34.2 KB, 2094 views)

Last edited by ReFlexPoison; 01-19-2015 at 22:30.
ReFlexPoison is offline
 



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 23:39.


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