Good Little Panda
|
08-23-2010
, 11:30
Re: Hey Need help with my plugin.
|
#6
|
Hehe we all have to start somewhere after all. Although your code is reset per client connect (or rather, should be), I went ahead and wrote up the code using cookies so that the setting is persistent, meaning clients won't have to use the command every time they join.
You can then expand upon this code and even start saving the client's kills/deaths using the basic cookie system should you wish, but you wouldn't be able to work it into any type of ranking system. I haven't tested the code, but it compiled fine. Let me know if you get any errors and I'll fix them up.
PHP Code:
#include <sourcemod> #include <clientprefs>
#pragma semicolon 1 #define VERSION "0.0.1"
public Plugin:myinfo = { name = "Kill Counter", author = "NakashimaKun", description = "Counts up your kills and headshots.", version = VERSION, url = "N/A" }
//Create a handle for your cookie. new Handle:g_displayStatus; new g_canDisplay[MAXPLAYERS+1];
new Handle:counter=INVALID_HANDLE; new kill_counts[MAXPLAYERS+1][2];
public OnPluginStart() { //Register the cookie for the client; this will appear when a user types !settings g_displayStatus = RegClientCookie("Kill_Counter", "Display Kill Counter", CookieAccess_Protected); SetCookieMenuItem(Menu_Status, 0, "Display Kill Counter"); //create new cvars counter = CreateConVar("counters", "1", "0: Off. 1: On. 2: Headshots only.",FCVAR_REPLICATED|FCVAR_GAMEDLL|FCVAR_NOTIFY,true,0.0,true,2.0); AutoExecConfig(true, "Kill_Counter"); //hook events HookEvent("player_death", Event_Player_Death, EventHookMode_Pre); //create command RegConsoleCmd("sm_counter", Command_Counter); }
//If for whatever reason something wiggy happens later, default the setting to on for the client first. public OnClientConnected(client) { g_canDisplay[client] = 1; }
//Called after the player has been authorized and fully in-game public OnClientPostAdminCheck(client) { //Check to ensure the clients are cached from the database before using them if (AreClientCookiesCached(client)) { //Get the content of the cookie and store it into a string decl String:cookie[3]; GetClientCookie(client, g_displayStatus, cookie, sizeof(cookie)); //Check to see if there is any data in the cookie, and if not, set the default value to on (1) if (StrEqual(cookie, "")) { SetClientCookie(client, g_displayStatus, "1"); cookie = "1"; } //Throw their status into a cvar, so that the plug-in doesn't have to keep checking the client's cookie g_canDisplay[client] = StringToInt(cookie); } }
public Action:Event_Player_Death(Handle:event, const String:name[], bool:dontBroadcast) { new attacker = GetClientOfUserId(GetEventInt(event, "attacker")); //Only process if the player is a legal attacker (i.e., a player) if(0 < attacker <= MaxClients) { new bool:headshot = GetEventBool(event, "headshot"); printkillinfo(attacker, headshot); }
return Plugin_Continue; }
printkillinfo(attacker, bool:headshot) { new intbroad = GetConVarInt(counter); new murder; if ((intbroad >= 1) && headshot) { murder = kill_counts[attacker][0]; //Only display if they want it if(g_canDisplay[attacker]) { if(murder>1) PrintHintText(attacker, "HEADSHOTS +%d", murder); else PrintHintText(attacker, "HEADSHOT!"); } kill_counts[attacker][0] = murder+1; } else if (intbroad == 1) { murder = kill_counts[attacker][1]; //Only display if they want it if(g_canDisplay[attacker]) { if(murder>=1) PrintHintText(attacker, "KILLS +%d", murder); else PrintHintText(attacker, "KILL!"); } kill_counts[attacker][1] = murder+1; } }
//The code that is fired should a client type !counter or /counter in chat, or sm_counter in console public Action:Command_Counter(client, args) { //Since their status is already saved, use it to determine the change if(g_canDisplay[client]) { //Display is on, they want off SetClientCookie(client, g_displayStatus, "0"); PrintToChat(client, "Yo what up with you, you don't like my counter? Fine then, I'll hide it from you"); } else { //Display is off, turn on SetClientCookie(client, g_displayStatus, "1"); PrintToChat(client, "You love my counter, don't you? Admit it! I shall show it to you then!"); } }
//Used for showing the client their counter status should they type !settings public Menu_Status(client, CookieMenuAction:action, any:info, String:buffer[], maxlen) { if (action == CookieMenuAction_DisplayOption) Format(buffer, maxlen, "Display Counter Setting"); else if (action == CookieMenuAction_SelectOption) CreateMenuStatus(client); }
//Menu that appears when a user types !settings stock CreateMenuStatus(client) { new Handle:menu = CreateMenu(Menu_StatusDisplay); decl String:text[64];
//The title of the menu Format(text, sizeof(text), "Counter Setting"); SetMenuTitle(menu, text);
//Since their status is already saved, use it to determine the change if(g_canDisplay[client]) AddMenuItem(menu, "Kill_Counter", "Counter ~ Enabled!", ITEMDRAW_DISABLED); else AddMenuItem(menu, "Kill_Counter", "Counter ~ Disabled!", ITEMDRAW_DISABLED);
//Give the menu a back button, and make it display on the client SetMenuExitBackButton(menu, true); SetMenuExitButton(menu, true); DisplayMenu(menu, client, 15); }
//Determines if the menu should be opened or closed (i.e. if the client types !settings twice) public Menu_StatusDisplay(Handle:menu, MenuAction:action, param1, param2) { new client = param1; switch (action) { case MenuAction_Cancel: { switch (param2) { case MenuCancel_ExitBack: { ShowCookieMenu(client); } } } case MenuAction_End: { CloseHandle(menu); } } }
__________________
Last edited by thetwistedpanda; 08-23-2010 at 11:32.
|
|