PHP Code:
#include <tf2>
#include <tf2_stocks>
ConVar g_hClassRestrict;
bool bRestrict;
public Plugin myinfo =
{
name = "Team Class Restriction",
author = "Cyriv",
description = "A plugin that restricts the blue team to demoman and the red team to soldier",
version = "1.0",
}
public void OnPluginStart()
{
// Register a command that can be used by admins to toggle the plugin on or off
RegAdminCmd("sm_classrestrict", Command_ClassRestrict, ADMFLAG_CONVARS, "Toggle class restriction plugin");
// Create a global variable to store the plugin state
g_hClassRestrict = CreateConVar("sm_classrestrict_enabled", "1", "Enable or disable class restriction plugin", _, true, _, true, 1.0);
g_hClassRestrict.AddChangeHook(CVarChange);
bRestrict = g_hClassRestrict.BoolValue;
// Hook the event that fires when a player spawns
HookEvent("player_spawn", Event_PlayerSpawn);
}
public void CVarChange(ConVar cvar, const char[] oldValue, const char[] newValue)
{
bRestrict = cvar.BoolValue;
}
public Action Command_ClassRestrict(int client, int args)
{
PrintToChatAll("Class restriction plugin is now %s.", bRestrict ? "enabled" : "disabled");
SetConVarBool(g_hClassRestrict, !bRestrict);
return Plugin_Handled;
}
public Action Event_PlayerSpawn(Event event, const char[] name, bool dontBroadcast)
{
if (!bRestrict)
return Plugin_Continue;
int client = GetClientOfUserId(event.GetInt("userid"));
if(!client || !IsClientInGame(client))
return Plugin_Continue;
// Get the player's team and class
TFTeam team = TF2_GetClientTeam(client);
TFClassType class = TF2_GetPlayerClass(client);
// Check if the player's team and class are valid
if(team == TFTeam_Unassigned || team == TFTeam_Spectator || class == TFClass_Unknown)
return Plugin_Continue;
// Check if the player's class matches the team restriction
if((team == TFTeam_Blue && class != TFClass_DemoMan) || (team == TFTeam_Red && class != TFClass_Soldier))
{
// Force the player to change class
TF2_SetPlayerClass(client, team == TFTeam_Blue ? TFClass_DemoMan : TFClass_Soldier);
// Print a message to the player
PrintToChat(client, "You can only play as %s on this team.", team == TFTeam_Blue ? "demoman" : "soldier");
// Return Plugin_Handled to prevent other plugins from interfering
return Plugin_Handled;
}
// Return Plugin_Continue to allow other plugins to process the event
return Plugin_Continue;
}
__________________