Raised This Month: $ Target: $400
 0% 

Completely new to coding and trying to make plugins for a server


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
FelixFTW
New Member
Join Date: Jan 2025
Old 01-20-2025 , 19:39   Completely new to coding and trying to make plugins for a server
Reply With Quote #1

Here is the code for the plugins I have so far (made using ChatGPT)


This code block is supposed to replace engineer's shotgun with sniper's SMG and take it away when engineer has a sentry built. If engineer has no sentry, access to the SMG is granted again
PHP Code:
#include <sourcemod>
#include <tf2_stocks>
#include <tf2items>

// Define constants for slot positions if needed
#define SLOT_PRIMARY 0
#define SLOT_SECONDARY 1

// SMG weapon class name
#define TF_WEAPON_SMG "tf_weapon_smg"

// Plugin initialization
public void OnPluginStart()
{
    
HookEvent("player_spawn"OnPlayerSpawn);
    
PrintToServer("Engineer Weapon Replacement Plugin loaded successfully.");
}

/**
 * Handles the player spawn event to replace the Engineer's Shotgun with an SMG.
 */
public void OnPlayerSpawn(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    
    
// Ensure the client is valid and playing as Engineer
    
if (!IsClientInGame(client) || TF2_GetPlayerClass(client) != TFClass_Engineer)
        return;

    
// Remove Engineer's Shotgun (Secondary)
    
int shotgun GetPlayerWeaponSlot(clientSLOT_SECONDARY); // Get the shotgun entity index
    
if (shotgun != -1)
    {
        
RemovePlayerItem(clientshotgun); // Remove the shotgun
    
}

    
// Give Engineer the Sniper's SMG in the Secondary Slot
    
GivePlayerItem(clientTF_WEAPON_SMGSLOT_SECONDARY);

This code block is supposed to make the heavy immobile when his minigun is revved
PHP Code:
#include <sourcemod>
#include <tf2_stocks>

public void OnPluginStart()
{
    
HookEvent("player_shoot"OnPlayerShoot);
    
PrintToServer("Heavy Movement Restriction Plugin loaded successfully.");
}

public 
Action OnPlayerShoot(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    if (
IsClientInGame(client) && TF2_GetPlayerClass(client) == TFClass_Heavy)
    {
        
TF2_SetPlayerCondition(clientTFCond_Slowed);
        return 
Plugin_Handled;
    }
    return 
Plugin_Continue;

This code block was sent to me by Chat and I believe it's supposed to be a "wack" version of the previous one
PHP Code:
#include <sourcemod>
#include <tf2_stocks> // Ensure this is available

// Define TF2 class constants if missing
#define TFClass_Heavy 6

public void OnPluginStart()
{
    
HookEvent("player_shoot"OnPlayerShoot); // Trigger when a player shoots
    
PrintToServer("Heavy Immobility Plugin loaded successfully.");
}

public 
Action OnPlayerShoot(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));

    
// Check if client is valid and is playing as Heavy
    
if (IsClientInGame(client) && TF2_GetPlayerClass(client) == TFClass_Heavy)
    {
        
// Stop the player's movement by resetting velocity
        
TeleportEntity(clientNULL_VECTORNULL_VECTORNULL_VECTOR);
        return 
Plugin_Handled// Prevent further processing of the event
    
}

    return 
Plugin_Continue;

This is supposed to make engineer's buildings only upgradable to level 2 and disable dispensers from being built. The buildings should not be able to be wacked with the wrench to quicken the initial building process, but said process' time should be cut in half. teleporters' build time shouldn't be affected, but their only level is 2
PHP Code:
#include <sourcemod>
#include <tf2_stocks>

#define TF_OBJECT_DISPENSER 0
#define TF_OBJECT_SENTRY 2
#define TF_OBJECT_TELEPORTER 1

public void OnPluginStart()
{
    
AddCommandListener(OnBuildCommand"build");
    
HookEvent("object_built"OnObjectBuilt);
    
HookEvent("player_hurt"OnPlayerHurt); // To intercept wrench hits
}

public 
Action OnBuildCommand(int client, const char[] commandint argc)
{
    if (
IsClientInGame(client) && argc)
    {
        
char arg1[11];
        
GetCmdArg(1arg1sizeof(arg1));
        
int building StringToInt(arg1);

        
// Block dispensers for all teams
        
if (building == TF_OBJECT_DISPENSER)
        {
            
PrintToChat(client"Building dispensers is disabled on this server.");
            return 
Plugin_Handled// Ensure proper Action return type
        
}
    }

    return 
Plugin_Continue;
}

public 
void OnObjectBuilt(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    
int objType GetEventInt(event"object");
    
int objEnt GetEventInt(event"object_entindex");

    if (!
IsValidEntity(objEnt) || !IsClientInGame(client))
        return;

    
// Modify build time behavior
    
if (objType == TF_OBJECT_SENTRY || objType == TF_OBJECT_DISPENSER)
    {
        
// Double the initial build speed
        
SetEntPropFloat(objEntProp_Send"m_flConstructProgress"0.5); // Start at 50%
    
}
    else if (
objType == TF_OBJECT_TELEPORTER)
    {
        
// Teleporters should build at normal speed but start at level 2
        
SetEntProp(objEntProp_Send"m_iUpgradeLevel"2); // Set level to 2
    
}
}

public 
Action OnPlayerHurt(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    
int attacker GetClientOfUserId(GetEventInt(event"attacker"));

    if (!
IsClientInGame(client) || !IsClientInGame(attacker))
        return 
Plugin_Continue;

    if (
TF2_GetPlayerClass(attacker) == TFClass_Engineer)
    {
        
int weapon GetPlayerWeaponSlot(attackerTFWeaponSlot_Melee);
        if (
weapon && IsValidEntity(weapon))
        {
            
char weaponName[64];
            
GetEdictClassname(weaponweaponNamesizeof(weaponName));

            
// Block wrench hits during the initial build process for non-teleporters
            
if (StrEqual(weaponName"tf_weapon_wrench"))
            {
                
int targetBuilding GetEventInt(event"object"); // The object being hit
                
if (targetBuilding == TF_OBJECT_SENTRY || targetBuilding == TF_OBJECT_DISPENSER)
                {
                    
PrintToChat(attacker"Speeding up construction with the wrench is disabled.");
                    return 
Plugin_Handled// Ensure proper Action return type
                
}
            }
        }
    }

    return 
Plugin_Continue;

This is supposed to make medic, demoman, spy and pyro unpickable as classes. If the player tries to choose them in the character selection screen, it just puts them back in the screen
PHP Code:
#include <sourcemod>
#include <tf2_stocks>

#define TFClass_Medic 5
#define TFClass_Demoman 4
#define TFClass_Spy 8
#define TFClass_Pyro 7

public void OnPluginStart()
{
    
AddCommandListener(OnClassChangeCommand"changeclass");
    
PrintToServer("Class Restriction Plugin loaded successfully.");
}

public 
Action OnClassChangeCommand(int client, const char[] commandint argc)
{
    if (!
IsClientInGame(client) || argc 1)
        return 
Plugin_Continue;

    
char className[32];
    
GetCmdArg(1classNamesizeof(className));

    
// Check for restricted classes
    
if (StrEqual(className"medic") || StrEqual(className"demoman") || StrEqual(className"spy") || StrEqual(className"pyro"))
    {
        
PrintToChat(client"Medic, Demoman, Spy, and Pyro are disabled on this server.");
        return 
Plugin_Handled// Block the command
    
}

    return 
Plugin_Continue;

This is supposed to replace sniper's SMG with spy's revolver
PHP Code:
#include <sourcemod>
#include <tf2_stocks>
#include <tf2items>

#define SLOT_SECONDARY 1
#define TF_WEAPON_REVOLVER "weapon_revolver" // Spy's Revolver class name

public void OnPluginStart()
{
    
HookEvent("player_spawn"OnPlayerSpawn);
    
PrintToServer("Custom Sniper Weapon Plugin loaded successfully.");
}

public 
void OnPlayerSpawn(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    
    
// Ensure the client is valid and playing as Sniper
    
if (!IsClientInGame(client) || TF2_GetPlayerClass(client) != TFClass_Sniper)
        return;

    
// Remove Sniper's SMG (Secondary)
    
int smg GetPlayerWeaponSlot(clientSLOT_SECONDARY); // Get the SMG weapon entity index
    
if (smg != -1)
    {
        
RemovePlayerItem(clientsmg); // Remove the item
    
}

    
// Give Sniper the Spy's Revolver in the Secondary Slot
    
GivePlayerItem(clientTF_WEAPON_REVOLVER);

And finally, this is supposed to replace the soldier's shotgun with the grenade launcher
PHP Code:
#include <sourcemod>
#include <tf2_stocks>
#include <tf2items>

#define SLOT_SECONDARY 1
#define TFWeapon_GrenadeLauncher "weapon_grenadelauncher" // Soldier's Grenade Launcher class name

public void OnPluginStart()
{
    
HookEvent("player_spawn"OnPlayerSpawn);
    
PrintToServer("Soldier Grenade Launcher Plugin loaded successfully.");
}

public 
void OnPlayerSpawn(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    
    
// Ensure the client is valid and playing as Soldier
    
if (!IsClientInGame(client) || TF2_GetPlayerClass(client) != TFClass_Soldier)
        return;

    
// Remove Soldier's Shotgun (Secondary)
    
int shotgun GetPlayerWeaponSlot(clientSLOT_SECONDARY); // Get the shotgun entity index
    
if (shotgun != -1)
    {
        
RemovePlayerItem(clientshotgun); // Remove the item
    
}

    
// Give Soldier the Grenade Launcher in the Secondary Slot
    
GivePlayerItem(clientTFWeapon_GrenadeLauncher);

One more code block request; how can I cut the time to kick due to inactivity in half?

The scripts remove the assigned weapons at first but they can be retreived by hitting a resupply locker, dispensers can still be built and sentries can still be upgraded.

Let me know what I can fix and improve or even merge, considering I have two engineer scripts. It would be cool if I could merge all of them into one big script for example, but I'm not necessarily asking for that if it's not doable. Like I said, I'm totally new to this and I'm coming here for script help. If there's a discord or somewhere else I should go instead, let me know. Thanks!
FelixFTW 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 20:13.


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