View Single Post
ferret
SourceMod Developer
Join Date: Dec 2004
Location: Atlanta, GA
Old 07-08-2007 , 14:37   Re: VoteManager API
Reply With Quote #3

native bool:VM_UnregisterVote(index)

If you register a vote, you should unregister it. Use this in OnPluginUnload(). Unfortunately, this once again causes VMgr to become a required plugin, until "Non-required Natives" are a reality. This will return false if the index is invalid.

Code:
public OnPluginUnload()
{
 if (g_MyVote != -1)
  VM_UnregisterVote(g_MyVote);
}
VM_RegisterVote

Now we're down to the most important part of all this. Registering your vote.

Code:
stock VM_RegisterVote(const String:name[],
   const String:title[],
   admflag = VM_ADMINFLAG,
   VoteType:vType = VoteType:YesNo,
   TargetType:tType = TargetType:None,
   bool:say = true)
The above stock handles creating the vote. It uses several other natives to accomplish this. I won't detail them because you should use the stock, and never those natives directly. If you're interested in exactly how it works, read the include file.

VM_RegisterVote does several things. It gives your vote's information to the Core, and registers a command and a convar. The first parameter, name, should be a no-spaces single word for the vote, such as "kick". Title should be the title of the vote, such as "Kick Player". admflag is the admin flag you want the command to be restricted to. It defaults to ADMFLAG_VOTE.

vType and tType control how the vote works. I will detail both below. The last parameter, 'say', controls whether or not this vote will work from chat. It defaults to true. If you change this to false, your vote will be console command only, regardless of the value of vm_sayvotes.

Code:
g_MyVote = VM_RegisterVote("kick", "Kick", _, _, TargetType:Client, true);
g_MyVote = VM_RegisterVote("gravity", "Change Gravity", _, VoteType:MultiInt, _, true);
g_MyVote = VM_RegisterVote("ff", "Change FriendlyFire");
g_MyVote = VM_RegisterVote("burn", "Burn", _, VoteType:YesNo, TargetType:Client, true);
g_MyVote = VM_RegisterVote("map", "Change Map", _, VoteType:MultiMap, _, true);
vType and tType

Here's the enums:

Code:
enum VoteType
{
 YesNo = 0,
 MultiInt,
 MultiFloat,
 MultiString,
 MultiMap
}
 
enum TargetType
{
 None = 0,  // No target
 Client,    // Any client, including Bots
 Player     // Only players, no fake clients
}
TargetType is pretty straight forward. No target, any client, or only actual players, no fake clients.

VoteType currently has 5 options. I will probably be adding a sixth soon.

The first is the default, YesNo. In this case your vote will take a single argument, and the options displayed to the player will be Yes and No. For a vote of this type, item[] will be "Yes" or "No" when VM_OnVoteEnd is called.

The next are all MultiVotes. It should be noted that a MultiVote is ALWAYS successful. There is no failure. Each MultiVote has a type associated with it, such as Int, Float, String and Map. VMgr_Core checks the arguments supplied with the vote command against this. For example, all arguments sent with a MultiMap vote are ran through IsMapValid(). Int and Float votes are tested as well. MultiString's receive no validation. When only a single argument is provided to a MultiVote, the vote will display like a YesNo vote. The difference is that when VM_OnVoteEnd is called, item[] will be set to that argument's value, rather than Yes or No.

That's just about everything. So as an example, here is the complete vmgr_kick plugin:

Code:
#pragma semicolon 1
 
#include <sourcemod>
#include <vmgr>
 
#define PLUGIN_VERSION "1.0"
 
public Plugin:myinfo =
{
name = "VM VoteKick",
author = "ferret",
description = "VM Votekick",
version = PLUGIN_VERSION,
url = "http://www.sourcemod.net/"
};
 
new g_MyVote = -1;
 
public OnPluginStart()
{
LoadTranslations("common.phrases");
 
if (VM_IsCoreReady() && g_MyVote == -1)
g_MyVote = VM_RegisterVote("kick", "Kick", _, _, TargetType:Client, true);
}
 
public OnPluginUnload()
{
if (g_MyVote != -1)
VM_UnregisterVote(g_MyVote);
}
 
public VM_OnCoreReady()
{
if (g_MyVote == -1)
g_MyVote = VM_RegisterVote("kick", "Kick", _, _, TargetType:Client, true);
}
 
public VM_OnVoteStart(vote)
{
//Nothing
}
 
public VM_OnVoteEnd(vote, bool:result, String:item[], client, userid, String:name[], String:authid[])
{
if (vote != g_MyVote)
return;
 
if (result)
{
PrintToChatAll("%t", "Kicked player", name);
LogMessage("Votekick success, kicking \"%L\"", client);
ServerCommand("kickid %d \"Votekicked.\"", userid);
}
}
__________________
I'm a blast from the past!

Last edited by ferret; 07-08-2007 at 23:33.
ferret is offline