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

Solved [TF2] Force weapons apon a class


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
FyreLite07
New Member
Join Date: Nov 2021
Old 11-05-2021 , 19:23   [TF2] Force weapons apon a class
Reply With Quote #1

I recently started up a TF2 server, and I want to force certain weapons onto a class. For example, scout with the enforcer, thermal thruster, etc. Since I am new to all this, is there a way I could do this and make it permanent on the server? Thanks!

Last edited by FyreLite07; 11-07-2021 at 09:00.
FyreLite07 is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 11-05-2021 , 23:33   Re: [TF2] Force weapons apon a class
Reply With Quote #2

Yes. There are a couple of ways to do this.

Do you have the "[TF2Items] Give Weapon" plugin installed and working?

1. If so then it will be pretty easy to do. It would mean making a plugin that uses the features of the plugin above to do most of the work.

2. If not then you'd have to do a make a plugin that does a bit more work to give weapons to players. An example of this is 'Give Bots Weapons' or 'Give Bots More Weapons'.

One issue: Using a weapon not designed for a specific class (such as the Thermal Thruster for scout) will not properly display weapon animations will look weird.

Here is an example plugin that I made for you. It forces specific weapons for each class:
PHP Code:
#include <sourcemod>
#include <tf2_stocks>

#pragma semicolon 1
#pragma newdecls required

#define PLUGIN_VERSION "1.0"

bool g_bMedieval;

Handle g_hEquipWearable;

public 
Plugin myinfo 
{
    
name "Force Weapons by Class",
    
author "PC Gamer, with code by luki1412",
    
description "Makes players use specific weapons for each class",
    
version PLUGIN_VERSION,
    
url "https://forums.alliedmods.net"
}

public 
void OnPluginStart()
{
    
HookEvent("post_inventory_application"player_inv);
    
    
GameData hTF2 = new GameData("sm-tf2.games"); // sourcemod's tf2 gamedata

    
if (!hTF2)
    
SetFailState("This plugin is designed for a TF2 dedicated server only.");

    
StartPrepSDKCall(SDKCall_Player);
    
PrepSDKCall_SetVirtual(hTF2.GetOffset("RemoveWearable") - 1);    // EquipWearable offset is always behind RemoveWearable, subtract its value by 1
    
PrepSDKCall_AddParameter(SDKType_CBaseEntitySDKPass_Pointer);
    
g_hEquipWearable EndPrepSDKCall();

    if (!
g_hEquipWearable)
    
SetFailState("Failed to create call: CBasePlayer::EquipWearable");

    
delete hTF2
}

public 
void OnMapStart()
{
    if (
GameRules_GetProp("m_bPlayingMedieval"))
    {
        
g_bMedieval true;
    }    
}

public 
void player_inv(Handle event, const char[] namebool dontBroadcast
{
    
int userd GetEventInt(event"userid");
    
int client GetClientOfUserId(userd);

    if (
IsClientInGame(client))
    {
        
CreateTimer(1.0Timer_GiveWeaponsuserd);
    }
}

public 
Action Timer_GiveWeapons(Handle timerany data)
{
    
int client GetClientOfUserId(data);
    
    
TFClassType class = TF2_GetPlayerClass(client);

    switch (class)
    {
    case 
TFClass_Scout:
        {
            if (!
g_bMedieval//Only give primary and secondary weapons if map is not medieval mode
            
{
                
TF2_RemoveWeaponSlot(client0); //first we remove existing weapon
                
CreateWeapon(client"tf_weapon_scattergun"151066); //Blue Mew

                
TF2_RemoveWeaponSlot(client1);
                
CreateWeapon(client"tf_weapon_pistol"306666); //CAPPER
            
}

            
TF2_RemoveWeaponSlot(client2);  //always give melee weapon
            
CreateWeapon(client"tf_weapon_bat_fish"9996); //Festive Holy Mackerel
        
}

    case 
TFClass_Soldier:
        {
            if (!
g_bMedieval)
            {
                
TF2_RemoveWeaponSlot(client0);
                
CreateWeapon(client"tf_weapon_rocketlauncher"2286); //Black Box

                
TF2_RemoveWeaponSlot(client1);
                
CreateWeapon(client"tf_wearable"1336); //Gunboats
            
}

            
TF2_RemoveWeaponSlot(client2);
            
CreateWeapon(client"tf_weapon_shovel"4166); //Market Gardener
        
}
        
    case 
TFClass_Pyro:
        {
            if (!
g_bMedieval)
            {
                
TF2_RemoveWeaponSlot(client0);
                
CreateWeapon(client"tf_weapon_rocketlauncher_fireball"11786); //Dragon's Fury

                
TF2_RemoveWeaponSlot(client1);
                
CreateWeapon(client"tf_weapon_shotgun_pyro"150476); //Lightning Rod
            
}

            
TF2_RemoveWeaponSlot(client2);
            
CreateWeapon(client"tf_weapon_slap"11816); //Hot Hand
        
}

    case 
TFClass_DemoMan:
        {
            if (!
g_bMedieval)
            {
                
TF2_RemoveWeaponSlot(client0);
                
CreateWeapon(client"tf_weapon_grenadelauncher"151426); //Warhawk

                
TF2_RemoveWeaponSlot(client1);
                
CreateWeapon(client"tf_weapon_pipebomblauncher"150486); //Pink Elephant
            
}

            
TF2_RemoveWeaponSlot(client2);
            
CreateWeapon(client"tf_weapon_katana"3576); //Half-Zatoichi
        
}

    case 
TFClass_Heavy:
        {
            if (!
g_bMedieval)
            {
                
TF2_RemoveWeaponSlot(client0);
                
CreateWeapon(client"tf_weapon_minigun"150996); //Mr Cuddles

                
TF2_RemoveWeaponSlot(client1);
                
CreateWeapon(client"tf_weapon_shotgun_hwg"151526); //Red Bear
            
}

            
TF2_RemoveWeaponSlot(client2);
            
CreateWeapon(client"tf_weapon_fists"5876); //Apoco-Fists
        
}

    case 
TFClass_Engineer:
        {
            if (!
g_bMedieval)
            {
                
TF2_RemoveWeaponSlot(client0);
                
CreateWeapon(client"tf_weapon_shotgun_building_rescue"9976); //Rescue Ranger

                
TF2_RemoveWeaponSlot(client1);
                
CreateWeapon(client"tf_weapon_pistol"150136); //Red Rock Roscoe
            
}

            
TF2_RemoveWeaponSlot(client2);
            
CreateWeapon(client"tf_weapon_wrench"151566); //Airwolf
        
}
        
    case 
TFClass_Medic:
        {
            if (!
g_bMedieval)
            {
                
TF2_RemoveWeaponSlot(client0);
                
CreateWeapon(client"tf_weapon_crossbow"3056); //Crusaders Crossbow

                
TF2_RemoveWeaponSlot(client1);
                
CreateWeapon(client"tf_weapon_medigun"4116); //Quick Fix
            
}

            
TF2_RemoveWeaponSlot(client2);
            
CreateWeapon(client"tf_weapon_bonesaw"4136); //Solemn Vow
        
}

    case 
TFClass_Sniper:
        {
            if (!
g_bMedieval)
            {
                
TF2_RemoveWeaponSlot(client0);
                
CreateWeapon(client"tf_weapon_sniperrifle"2306); //Sydney Sleeper

                
TF2_RemoveWeaponSlot(client1);
                
CreateWeapon(client"tf_weapon_jar"586); //Jarate
            
}

            
TF2_RemoveWeaponSlot(client2);
            
CreateWeapon(client"tf_weapon_club"2326); //Bushwacka
        
}

    case 
TFClass_Spy:
        {
            if (!
g_bMedieval)
            {
                
TF2_RemoveWeaponSlot(client0);
                
CreateWeapon(client"tf_weapon_revolver"150646); //Macabre Web

                
TF2_RemoveWeaponSlot(client1);
                
CreateWeapon(client"tf_weapon_sapper"9336); //Ap-Sap
            
}

            
TF2_RemoveWeaponSlot(client2);
            
CreateWeapon(client"tf_weapon_knife"7276); //Black Rose
        
}
    }    
}

bool CreateWeapon(int clientchar[] classnameint itemindexint qualityint level 0)
{
    
int weapon CreateEntityByName(classname);

    if (!
IsValidEntity(weapon))
    {
        return 
false;
    }
    
    
char entclass[64];
    
GetEntityNetClass(weaponentclasssizeof(entclass));
    
SetEntProp(weaponProp_Send"m_iItemDefinitionIndex"itemindex);     
    
SetEntProp(weaponProp_Send"m_bInitialized"1);
    
SetEntData(weaponFindSendPropInfo(entclass"m_iEntityQuality"), quality);        

    if (
level)
    {
        
SetEntProp(weaponProp_Send"m_iEntityLevel"level);
    }
    else
    {
        
SetEntProp(weaponProp_Send"m_iEntityLevel"GetRandomInt(1,99));
    }

    switch (
itemindex)
    {
    case 
2526:
        {
            
DispatchSpawn(weapon);
            
EquipPlayerWeapon(clientweapon); 

            return 
true;             
        }
    case 
73573681093310801102:
        {
            
SetEntProp(weaponProp_Send"m_iObjectType"3);
            
SetEntProp(weaponProp_Data"m_iSubType"3);
            
SetEntProp(weaponProp_Send"m_aBuildableObjectTypes"0_0);
            
SetEntProp(weaponProp_Send"m_aBuildableObjectTypes"0_1);
            
SetEntProp(weaponProp_Send"m_aBuildableObjectTypes"0_2);
            
SetEntProp(weaponProp_Send"m_aBuildableObjectTypes"1_3);
        }    
    case 
998:
        {
            
SetEntProp(weaponProp_Send"m_nChargeResistType"GetRandomInt(0,2));
        }
    }
    
    if (
itemindex == 405 || itemindex == 608 || itemindex == 1101 || itemindex == 133 || itemindex == 444 || itemindex == 57 || itemindex == 231 || itemindex == 642 || itemindex == 131 || itemindex == 406 || itemindex == 1099 || itemindex == 1144)
    {
        
DispatchSpawn(weapon);
        
SDKCall(g_hEquipWearableclientweapon);
        
CreateTimer(0.1TimerHealthclient);
    }

    else
    {
        
DispatchSpawn(weapon);
        
EquipPlayerWeapon(clientweapon); 
    }


    if (!
g_bMedieval)
    {
        
TF2_SwitchtoSlot(client0);
    }
    
    return 
true;
}

public 
Action TimerHealth(Handle timerany client)
{
    
int hp GetPlayerMaxHp(client);
    
    if (
hp 0)
    {
        
SetEntityHealth(clienthp);
    }
}

int GetPlayerMaxHp(int client)
{
    if (!
IsClientConnected(client))
    {
        return -
1;
    }

    
int entity GetPlayerResourceEntity();

    if (
entity == -1)
    {
        return -
1;
    }

    return 
GetEntProp(entityProp_Send"m_iMaxHealth"_client);
}

stock void TF2_SwitchtoSlot(int clientint slot)
{
    if (
slot >= && slot <= && IsClientInGame(client) && IsPlayerAlive(client))
    {
        
char wepclassname[64];
        
int wep GetPlayerWeaponSlot(clientslot);
        if (
wep MaxClients && IsValidEdict(wep) && GetEdictClassname(wepwepclassnamesizeof(wepclassname)))
        {
            
FakeClientCommandEx(client"use %s"wepclassname);
            
SetEntPropEnt(clientProp_Send"m_hActiveWeapon"wep);
        }
    }


Last edited by PC Gamer; 11-06-2021 at 04:09.
PC Gamer is offline
FyreLite07
New Member
Join Date: Nov 2021
Old 11-06-2021 , 09:20   Re: [TF2] Force weapons apon a class
Reply With Quote #3

I have tried the [TF2Items] Give Weapon Plugin and the one you sent, I cant seem to get it working correctly. I put it in my Plugins Folder and everything. I'm unsure how to do this. Still a tad bit difficult.
FyreLite07 is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 11-06-2021 , 19:52   Re: [TF2] Force weapons apon a class
Reply With Quote #4

This plugin doesn't require' [TF2Items] Give Weapon'. Simply click on the 'Get Plugin' link on this post to download the compiled plugin called 'forceweaponsbyclass.smx'. It will have a file extension of .smx which tells you it is a compiled sourcemod plugin. Place the forceweaponsbyclass.smx file in your /addons/sourcemod/plugins directory. Change the map and your plugin will be active.

To edit the plugin, click on the 'Get Source' link on this post to download the sourcecode for the uncompiled plugin called 'forceweaponsbyclass.sp'. It will have a file extension of .sp which tells you it is an uncompiled sourcemod script. Place the forceweaponsbyclass.sp file in your /addons/sourcemod/scripting directory. Use a good editing tool like Notepad++ to open and edit the forceweaponsbyclass.sp file. Change the weapon ids within the script to your liking. Save the file. Go to DOS prompt. Change into the /addons/sourcemod/scripting directory. Type: "spcomp forceweaponsbyclass.sp" without the quotes. That will compile the script and create a new file called 'forceweaponsbyclass.smx'. Move that file into the /addons/sourcemod/plugins folder. Change map. Your newly edited plugin will now be active. The sourcecode will still be available for you in the /scripting folder so you can change it any time you want to.

I hope this helps!
Attached Files
File Type: sp Get Plugin or Get Source (forceweaponsbyclass.sp - 53 views - 8.0 KB)
PC Gamer is offline
FyreLite07
New Member
Join Date: Nov 2021
Old 11-07-2021 , 08:59   Re: [TF2] Force weapons apon a class
Reply With Quote #5

After a bit I finally got the hang of it. Thank you so much! Works fine, just gotta figure a couple other things out but I can manage. Once again, thank you!
FyreLite07 is offline
Reply



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 08:45.


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