Raised This Month: $32 Target: $400
 8% 

Solved Am I using SM cookies correctly?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
LiveAndLetDie
Junior Member
Join Date: Sep 2019
Location: Finland
Old 04-26-2020 , 22:05   Am I using SM cookies correctly?
Reply With Quote #1

Hey, so I am learning how to use cookies and came up with a test plugin like below. You can use command /announce and it should enable cookie and print to chat "Hello world" after client joins if client has this cookie. And then disable it with command /noannounce.

So does it look correct?

Code:
#include <sourcemod>
#include <clientprefs>

bool AnnounceEnabled[MAXPLAYERS +1];

Handle TestCookie;

public void OnPluginStart()
{
    RegConsoleCmd("sm_announce", Announce);
    RegConsoleCmd("sm_noannounce", NoAnnounce);
    TestCookie = RegClientCookie("Announce", "Announce on/off", CookieAccess_Protected);
}

public Action Announce(client, args)
{
    char CookieValue[8]; 
    AnnounceEnabled[client] = true;
    IntToString(AnnounceEnabled[client], CookieValue, sizeof(CookieValue));
    SetClientCookie(client, TestCookie, CookieValue);
    return Plugin_Handled;
}

public Action NoAnnounce(client, args)
{
    char CookieValue[8];
    PrintToChat(client, "[SM] Announce disabled!");
    AnnounceEnabled[client] = false;
    IntToString(AnnounceEnabled[client], CookieValue, sizeof(CookieValue));
    SetClientCookie(client, TestCookie, CookieValue);
    return Plugin_Handled;
}


public void OnClientConnected(client)
{
    if(AnnounceEnabled[client])
    {
        CreateTimer(10.0, TestTimer);
    }
}

public Action TestTimer(Handle timer)
{
    PrintToChatAll("Hello world!");
}

public void OnClientCookiesCached(client)
{
    char CookieValue[8];
    GetClientCookie(client, TestCookie, CookieValue, sizeof(CookieValue));
    AnnounceEnabled[client] = (CookieValue[0] != '\0' && StringToInt(CookieValue));
}

Last edited by LiveAndLetDie; 04-27-2020 at 08:22.
LiveAndLetDie is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-27-2020 , 05:12   Re: Am I using SM cookies correctly?
Reply With Quote #2

Pretty much, yes.
To fill small details, give data types to callback variables. example:
Code:
public void OnClientConnected(int client)
{
...

And it always good to try test with output messages, how code would work.
For example, when player connect to server:
Code:
Not Valve ds: Direct-connect is allowed
Client "Bacardi" connected (192.168.1.20:27006).
OnClientConnected 1
Sending server info signon packet for 192.168.1.20:27006: 289800 / 524284 buffer
"Bacardi<3><STEAM_1:1:14163588><>" STEAM USERID validated
OnClientCookiesCached 1
PutClientInServer: no info_player_start on level
Sending full update to Client Bacardi (Bacardi can't find frame from tick -1)
...OnClientConnected works first than OnClientCookiesCached


You could merge these together.
PHP Code:
public void OnClientCookiesCached(int client)
{
    
PrintToServer("OnClientCookiesCached %i"client);

    
char CookieValue[8];
    
GetClientCookie(clientTestCookieCookieValuesizeof(CookieValue));
    
AnnounceEnabled[client] = (CookieValue[0] != '\0' && StringToInt(CookieValue));


    if(
AnnounceEnabled[client])
    {
        
CreateTimer(10.0TestTimer);
    }



*edit
If you want use sourcemod Client Preference "!settings" menu. It's old Pawn code, tough. (2013)
[Tutorial] ClientPrefs
https://forums.alliedmods.net/showthread.php?t=228244
__________________
Do not Private Message @me

Last edited by Bacardi; 04-27-2020 at 05:20.
Bacardi is offline
LiveAndLetDie
Junior Member
Join Date: Sep 2019
Location: Finland
Old 04-27-2020 , 08:23   Re: Am I using SM cookies correctly?
Reply With Quote #3

Thank you @Bacardi for feedback.
LiveAndLetDie is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 23:59.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode