Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sourcemod>
- #include <sdktools>
- #define PLUGIN_VERSION "1.0"
- new Float:g_fAFKTime[MAXPLAYERS + 1] = {0.0, ...};
- new Float:g_fEyeAngles[MAXPLAYERS + 1][3];
- new Float:g_fMapPosition[MAXPLAYERS + 1][3];
- new bool:g_AFK[MAXPLAYERS + 1] = {false, ...};
- new Handle:g_AFKTimer[MAXPLAYERS + 1];
- new Handle:g_MaxAFKTime = INVALID_HANDLE;
- new bool:g_bKickOnFull[MAXPLAYERS + 1] = {false, ...};
- new Handle:g_MaxPlayers = INVALID_HANDLE;
- new Handle:g_KickAFKPlayers_Timer;
- public Plugin:myinfo =
- {
- name = "AFK Manager",
- author = "Darkness",
- description = "Manages AFK players",
- version = PLUGIN_VERSION,
- url = "https://forums.alliedmods.net/showthread.php?t=254430"
- }
- public OnPluginStart()
- {
- HookEvent("player_spawn", Event_PlayerSpawn);
- HookEvent("round_end", Event_RoundEnd);
- HookEvent("player_death", Event_PlayerDeath);
- HookEvent("round_end", Event_RoundEnd);
- AddCommandListener(Command_Say, "say");
- AddCommandListener(Command_Say, "say_team");
- g_MaxAFKTime = CreateConVar("maxafktime", "45.0", "Maximum amount of time in seconds that a player can be AFK.");
- g_MaxPlayers = CreateConVar("maxplayers", "32", "When player count reaches this number - afk players will be kicked.");
- g_KickAFKPlayers_Timer = CreateTimer(30.0, KickAFKSpec, INVALID_HANDLE, TIMER_REPEAT);
- AutoExecConfig(true, "afkmanager");
- }
- public Action:Event_RoundEnd(Handle:event, const String:name[], bool:dontbroadcast)
- {
- // HERE YOU GO
- }
- public Action:Event_PlayerDeath(Handle:event, const String:name[], bool:dontbroadcast)
- {
- new client = GetClientOfUserId(GetEventInt(event, "userid"));
- if (IsValidClient(client))
- {
- g_fAFKTime[client] = 0.0;
- if (g_AFKTimer[client] != INVALID_HANDLE)
- {
- KillTimer(g_AFKTimer[client])
- g_AFKTimer[client] = INVALID_HANDLE;
- }
- }
- }
- public OnMapEnd()
- {
- if (g_KickAFKPlayers_Timer != INVALID_HANDLE)
- {
- KillTimer(g_KickAFKPlayers_Timer);
- g_KickAFKPlayers_Timer = INVALID_HANDLE;
- }
- for (new i = 1; i <= MaxClients; i++)
- {
- if (IsValidClient(i))
- {
- if (g_AFKTimer[i] != INVALID_HANDLE)
- {
- KillTimer(g_AFKTimer[i]);
- g_AFKTimer[i] = INVALID_HANDLE;
- }
- }
- }
- }
- public Action:KickAFKSpec(Handle:timer)
- {
- if (GetClientCount(false) >= GetConVarInt(g_MaxPlayers))
- {
- for (new i = 1; i <= MaxClients; i++)
- {
- if (IsValidClient(i))
- {
- if (g_bKickOnFull[i])
- {
- KickClient(i, "You have been kicked for being AFK too long on a full server.");
- }
- }
- }
- }
- }
- public Action:Command_Say(client, const String:command[], args)
- {
- g_fAFKTime[client] = 0.0;
- g_AFK[client] = false;
- g_bKickOnFull[client] = false;
- }
- public OnClientPutInServer(client)
- {
- g_fAFKTime[client] = 0.0;
- g_bKickOnFull[client] = false;
- }
- public OnClientDisconnect(client)
- {
- g_fAFKTime[client] = 0.0;
- g_bKickOnFull[client] = false;
- if (g_AFKTimer[client] != INVALID_HANDLE)
- {
- KillTimer(g_AFKTimer[client]);
- g_AFKTimer[client] = INVALID_HANDLE;
- }
- }
- public Action:Event_PlayerSpawn(Handle:event, const String:name[], bool:dontbroadcast)
- {
- new client = GetClientOfUserId(GetEventInt(event, "userid"));
- if (IsValidClient(client))
- {
- if (IsPlayerAlive(client))
- {
- GetClientEyeAngles(client, g_fEyeAngles[client]);
- GetClientAbsOrigin(client, g_fMapPosition[client]);
- g_fAFKTime[client] = 0.0;
- if (g_AFKTimer[client] != INVALID_HANDLE)
- {
- KillTimer(g_AFKTimer[client])
- g_AFKTimer[client] = CreateTimer(1.0, CheckAFK, client, TIMER_REPEAT);
- }
- if (g_AFKTimer[client] == INVALID_HANDLE)
- {
- g_AFKTimer[client] = CreateTimer(1.0, CheckAFK, client, TIMER_REPEAT);
- }
- }
- }
- }
- public Action:CheckAFK(Handle:timer, any:client)
- {
- if (IsValidClient(client))
- {
- decl Float:f_Map_Location[3];
- f_Map_Location = g_fMapPosition[client];
- decl Float:f_Eye_Angles[3];
- f_Eye_Angles = g_fEyeAngles[client];
- GetClientEyeAngles(client, g_fEyeAngles[client]);
- GetClientAbsOrigin(client, g_fMapPosition[client]);
- if ((f_Map_Location[0] == g_fMapPosition[client][0]) &&
- (f_Map_Location[1] == g_fMapPosition[client][1]) &&
- (f_Map_Location[2] == g_fMapPosition[client][2]))
- {
- if ((f_Eye_Angles[0] == g_fEyeAngles[client][0]) &&
- (f_Eye_Angles[1] == g_fEyeAngles[client][1]) &&
- (f_Eye_Angles[2] == g_fEyeAngles[client][2]))
- {
- g_AFK[client] = true;
- }
- else
- {
- g_AFK[client] = false;
- g_fAFKTime[client] = 0.0;
- g_bKickOnFull[client] = false;
- }
- }
- else
- {
- g_AFK[client] = false;
- g_fAFKTime[client] = 0.0;
- g_bKickOnFull[client] = false;
- }
- if (g_AFK[client])
- {
- g_fAFKTime[client]++;
- }
- if (g_fAFKTime[client] >= GetConVarFloat(g_MaxAFKTime))
- {
- MoveToSpectate(client);
- }
- }
- return Plugin_Continue;
- }
- MoveToSpectate(client)
- {
- if (IsValidClient(client))
- {
- if (IsPlayerAlive(client))
- {
- if (GetClientTeam(client) > 1)
- {
- PrintToChatAll("[\x04SG JB\x01] %N has been moved to \x08Spectator\x01 for being AFK too long!", client);
- ForcePlayerSuicide(client);
- ChangeClientTeam(client, 1);
- g_bKickOnFull[client] = true;
- }
- }
- }
- }
- stock bool:IsValidClient(client)
- {
- if (client < 1 || client > MaxClients)
- {
- return false;
- }
- if (!IsClientConnected(client))
- {
- return false;
- }
- if (!IsClientInGame(client))
- {
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment