View Single Post
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 09-13-2019 , 11:13   Re: [HELP] Get All Cmd Args
Reply With Quote #10

Quote:
Originally Posted by Crasher_3637 View Post
In your code, you're not exporting the new data to the file, so any phrase you set isn't being stored. You're also not closing the KV handle afterward, so you have Handle leaks.

Here's how I would store a phrase:

PHP Code:
#include <sourcemod>

public void OnPluginStart()
{
    
RegAdminCmd("sm_phrase"cmdPhraseADMFLAG_ROOT"Set a phrase");
}

public 
Action cmdPhrase(int clientint args)
{
    
char sPhrase[64];
    
GetCmdArgString(sPhrasesizeof(sPhrase));
    
StorePhrase(clientsPhrase);

    return 
Plugin_Handled;
}

stock void StorePhrase(int client, const char[] phrase)
{
    
char sFilePath[PLATFORM_MAX_PATH];
    
BuildPath(Path_SMsFilePathsizeof(sFilePath), "configs/phrases.cfg");

    
KeyValues kvm = new KeyValues("Player Phrases");
    
kvm.ImportFromFile(sFilePath);

    
char sSteamID[32];
    if (
GetClientAuthId(clientAuthId_Steam2sSteamIDsizeof(sSteamID)))
    {
        if (
kvm.JumpToKey(sSteamIDtrue))
        {
            
char sCheck[64];
            
kvm.GetString("phrase"sChecksizeof(sCheck), "");

            if (!
StrEqual(sCheckphrase))
            {
                
kvm.SetString("phrase"phrase);
                
kvm.ExportToFile(sFilePath);

                
ReplyToCommand(client"[SM] You set your new phrase to: \"%s\""phrase);
            }
            else
            {
                
ReplyToCommand(client"[SM] The phrase you entered is the same one that is already saved on the server.");
            }
        }
    }

    
delete kvm;

Example output:

PHP Code:
Crasher_3637 :  !phrase Hello there!
[
SMYou set your new phrase to"Hello there!" 
For a "clean way" instead of
PHP Code:
if (!StrEqual(sCheckphrase))
            {
                
kvm.SetString("phrase"phrase);
                
kvm.ExportToFile(sFilePath);

                
ReplyToCommand(client"[SM] You set your new phrase to: \"%s\""phrase);
            }
            else
            {
                
ReplyToCommand(client"[SM] The phrase you entered is the same one that is already saved on the server.");
            } 
Can we use
PHP Code:
if (!StrEqual(sCheckphrase))
            {
                
kvm.SetString("phrase"phrase);
                
kvm.ExportToFile(sFilePath);

                
ReplyToCommand(client"[SM] You set your new phrase to: \"%s\""phrase);
            }
            
             
ReplyToCommand(client"[SM] The phrase you entered is the same one that is already saved on the server."); 
And the kvm.ExportToFile(FILEPATH) is it needed? It doesn't export the values to file itself?
__________________
SpirT is offline