Skip to content

Instantly share code, notes, and snippets.

@StarterX4
Created June 26, 2022 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StarterX4/9b71edf123d92078fcad56f9ed02c92c to your computer and use it in GitHub Desktop.
Save StarterX4/9b71edf123d92078fcad56f9ed02c92c to your computer and use it in GitHub Desktop.
/*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <builtinvotes>
#include <colors>
#undef REQUIRE_PLUGIN
#define PLUGIN_VERSION "0.1"
public Plugin myinfo =
{
name = "[L4D2] Vote To Spec",
author = "StarterX4",
version = PLUGIN_VERSION,
description = "Vote to move selected player to spectators.",
url = "https://github.com/StarterX4"
}
public void OnPluginStart()
{
LoadTranslations("common.phrases");
RegConsoleCmd("sm_votetospec", VoteToSpecCmd, "sm_votetospec <#userid|name>"); // Allows players to vote for moving player to spectators
RegConsoleCmd("sm_vts", VoteToSpecCmd, "sm_vts <#userid|name>"); // Allows players to vote for moving player to spectators (short)
RegAdminCmd("sm_fts", ForceToSpecCommand, ADMFLAG_BAN, "sm_fts <#userid|name>"); // Let admins instatly move a player to spectators
RegAdminCmd("sm_fmts", ForceToSpecCommand, ADMFLAG_BAN, "sm_fmts <#userid|name>"); // As above ↑
return;
}
bool RunVoteChecks(int client)
{
if (GetClientTeam(client) == 1)
{
CPrintToChat(client, "{blue}<{green}VoteToSpec{blue}>{default} Voting is not available for spectators."); // Abuse safeguard
return false;
}
if (!IsNewBuiltinVoteAllowed())
{
CPrintToChat(client, "{blue}<{green}VoteToSpec{blue}>{default} Voting cannot be called right now...");
return false;
}
return true;
}
public Action VoteToSpecCmd(int client, int args)
{
if (!RunVoteChecks(client)) {
return Plugin_Handled;
}
if (args != 1)
{
CReplyToCommand(client, "{blue}<{green}VoteToSpec{blue}>{default} Usage: !vts {olive}<{default}player{olive}>.");
return Plugin_Handled;
}
// Get all non-spectating players
int iNumPlayers;
int[] iPlayers = new int[MaxClients];
for (int i=1; i<=MaxClients; i++)
{
if (!IsClientInGame(i) || IsFakeClient(i) || (GetClientTeam(i) == 1))
{
continue;
}
iPlayers[iNumPlayers++] = i;
}
char bv_voteTitle[64];
// Set vote title
{
Format(bv_voteTitle, 64, "Move player %p to spectators?", client);
}
// Start the vote!
Handle bv_hVote = CreateBuiltinVote(VoteToSpecActionHandler, BuiltinVoteType_Custom_YesNo, BuiltinVoteAction_Cancel | BuiltinVoteAction_VoteEnd | BuiltinVoteAction_End);
SetBuiltinVoteArgument(bv_hVote, bv_voteTitle);
SetBuiltinVoteInitiator(bv_hVote, client);
SetBuiltinVoteResultCallback(bv_hVote, VoteToSpecResultHandler);
DisplayBuiltinVote(bv_hVote, iPlayers, iNumPlayers, 20);
FakeClientCommand(client, "Vote Yes");
return Plugin_Handled;
}
public void VoteToSpecActionHandler(Handle vote, BuiltinVoteAction action, int param1, int param2)
{
switch (action)
{
case BuiltinVoteAction_End:
{
CloseHandle(vote);
}
case BuiltinVoteAction_Cancel:
{
DisplayBuiltinVoteFail(vote, view_as<BuiltinVoteFailReason>(param1));
}
}
}
public void VoteToSpecResultHandler(Handle vote, int num_votes, int num_clients, const int[][] client_info, int num_items, const int[][] item_info)
{
Event event;
int client = GetClientOfUserId(event.GetInt("userid"));
if (!client) return;
for (int i=0; i<num_items; i++)
{
if (item_info[i][BUILTINVOTEINFO_ITEM_INDEX] == BUILTINVOTES_VOTE_YES)
{
if (item_info[i][BUILTINVOTEINFO_ITEM_VOTES] > (num_clients / 2))
{
DisplayBuiltinVotePass(vote, "Player %p has been moved to spectators.", client);
ChangeClientTeam(client, 1);
return;
}
}
}
// Vote Failed
DisplayBuiltinVoteFail(vote, BuiltinVoteFail_Loses);
return;
}
public Action ForceToSpecCommand(int client, int args)
{
FakeClientCommand(client, "sm_swapto 1 %p", client);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment