AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved KeyValues not working (https://forums.alliedmods.net/showthread.php?t=330862)

SpirT 02-24-2021 03:50

KeyValues not working
 
Hey.

I've been building a plugin where it needs to check on client authorized if his steamid is on the file as keyvalues. It is suposed to create one if it doesn't exist.

Current code part where I want to create it:
PHP Code:

char authID[MAXPLAYERS 1][32];
char scoreTag[MAXPLAYERS 1][16];
char chatColor[MAXPLAYERS 1][16];
char nameColor[MAXPLAYERS 1][16];
char filePath[256];

public 
void OnPluginStart()
{
    
BuildPath(Path_SMfilePathsizeof(filePath), "configs/SpirT/VIP-Tags/tags.cfg");
}

public 
void OnClientAuthorized(int client, const char[] auth)
{
    
GetClientAuthId(clientAuthId_Steam2authID[client], sizeof(authID[]));
    
WaitingForScoreboardTag[client] = false;
    
CheckForKeyValues(clientauthID[client]);
    
FetchCookieInfo(client);
}

void CheckForKeyValues(int clientchar[] steamID)
{
    
KeyValues kv = new KeyValues("VIPTags");
    
kv.ImportFromFile(filePath);
    
    
KvGotoFirstSubKey(kv);
    
    if(!
kv.JumpToKey(steamIDtrue))
    {
        
PrintToServer("Couldn't find a key for %s but we'll try to create it."steamID);
        
KvSetString(kv"scoretag""Teste - ");
        
KvSetString(kv"chatcolor""{red}");
        
KvSetString(kv"namecolor""{blue}");
        
kv.ExportToFile(filePath);
        
delete kv;
        return;
    }
    else
    {
        
PrintToServer("FOUND A FCKNG KEY FOR: %s."steamID);
        
KvGetString(kv"scoretag"scoreTag[client], sizeof(scoreTag[]));
        
KvGetString(kv"chatcolor"chatColor[client], sizeof(chatColor[]));
        
KvGetString(kv"namecolor"nameColor[client], sizeof(nameColor[]));
        
delete kv;
        return;
    }


When I connect or when I use !check (to execute the void CheckForKeyValues) it always send to the console that found the key when it doesn't exist

Current tags.cfg content:
Code:

"VIPTags"
{
       
}

If I type my steam there it does work and it gets the variables that I have typed there. How can I make the plugin to create the key itself?

DJ Tsunami 02-24-2021 04:03

Re: KeyValues not working
 
You're passing true to JumpToKey to create the key if it doesn't exist, so naturally it will always return true. Then you need to use ExportToFile to make the key show up in the file.

SpirT 02-24-2021 04:12

Re: KeyValues not working
 
Quote:

Originally Posted by DJ Tsunami (Post 2738086)
You're passing true to JumpToKey to create the key if it doesn't exist, so naturally it will always return true. Then you need to use ExportToFile to make the key show up in the file.

Thanks for the reply DJ Tsunami. Therefore, should it just be like this? :
PHP Code:

{
        
KeyValues kv = new KeyValues("VIPTags");
    
kv.ImportFromFile(filePath);
    
    
KvGotoFirstSubKey(kv);
    
    if(
kv.JumpToKey(steamIDtrue))
    {
        
PrintToServer("Couldn't find a key for %s but we'll try to create it."steamID);
        
KvGetString(kv"scoretag"scoreTag[client], sizeof(scoreTag[]), "Teste - ");
        
KvSetString(kv"chatcolor"chatColor[client], sizeof(chatColor[]), "{red}");
        
KvSetString(kv"namecolor"nameColor[client], sizeof(nameColor[]), "{blue}");
        
kv.ExportToFile(filePath);
        
delete kv;
        return;
    }



DJ Tsunami 02-24-2021 04:17

Re: KeyValues not working
 
Yes, although I don't see any reason why JumpToKey would ever return false here, so I think the if statement is unnecessary.

return; at the end of a function is also unnecessary.

SpirT 02-24-2021 04:25

Re: KeyValues not working
 
Quote:

Originally Posted by DJ Tsunami (Post 2738088)
Yes, although I don't see any reason why JumpToKey would ever return false here, so I think the if statement is unnecessary.

return; at the end of a function is also unnecessary.

Thanks once more. I'll give it a try later and I'll leave some feedback here.

SpirT 02-24-2021 05:04

Re: KeyValues not working
 
Quote:

Originally Posted by DJ Tsunami (Post 2738088)
Yes, although I don't see any reason why JumpToKey would ever return false here, so I think the if statement is unnecessary.

return; at the end of a function is also unnecessary.

Well, it didn't work... I have noticed a error on my code for the kv.JumpToKey. It has only 1 KvGetString and the other two are kvSetString. I have changed that but still doesn't work.

PHP Code:

void CheckForKeyValues(int clientchar[] steamID)
{
    
KeyValues kv = new KeyValues("VIPTags");
    
kv.ImportFromFile(filePath);
    
    
KvGotoFirstSubKey(kv);
    
    if(
kv.JumpToKey(steamIDtrue))
    {
        
KvGetString(kv"scoretag"scoreTag[client], sizeof(scoreTag[]), "NEW SETUP - ");
        
KvGetString(kv"chatcolor"chatColor[client], sizeof(chatColor[]), "{red}");
        
KvGetString(kv"namecolor"nameColor[client], sizeof(nameColor[]), "{blue}");
        
kv.ExportToFile(filePath);
        
delete kv;
        return;
    }
    
    return;


Any idea to fix this?

DJ Tsunami 02-24-2021 05:54

Re: KeyValues not working
 
See the last example on the wiki: https://wiki.alliedmods.net/KeyValue...Value_Creation

You need to use SetString to store values under the steamID key. An empty section does not seem to be exported.

You also need to use Rewind before ExportToFile.

SpirT 02-24-2021 06:13

Re: KeyValues not working
 
Quote:

Originally Posted by DJ Tsunami (Post 2738096)
See the last example on the wiki: https://wiki.alliedmods.net/KeyValue...Value_Creation

You need to use SetString to store values under the steamID key. An empty section does not seem to be exported.

You also need to use Rewind before ExportToFile.

Sorry for the "spam". Still doesn't work. I have changed the code to:
PHP Code:

void CheckForKeyValues(int clientchar[] steamID)
{
    
KeyValues kv = new KeyValues("VIPTags");
    
kv.ImportFromFile(filePath);
    
    
KvGotoFirstSubKey(kv);
    
    if(
kv.JumpToKey(steamIDtrue))
    {
        
KvGetString(kv"scoretag"scoreTag[client], sizeof(scoreTag[]));
        
KvGetString(kv"chatcolor"chatColor[client], sizeof(chatColor[]));
        
KvGetString(kv"namecolor"nameColor[client], sizeof(nameColor[]));
        
        if(
StrEqual(scoreTag[client], "") && StrEqual(chatColor[client], "") && StrEqual(nameColor[client], ""))
        {
               
KvSetString(kv"scoretag""-change-");
            
KvSetString(kv"chatcolor""-change-");
            
KvSetString(kv"namecolor""-change-");
            
kv.Rewind();
            
kv.ExportToFile(filePath);
            
delete kv;
            return;
           }
           
        
delete kv;
        return;
    }
    
    return;



DJ Tsunami 02-24-2021 07:07

Re: KeyValues not working
 
I don't know, works fine for me:
PHP Code:

char filePath[PLATFORM_MAX_PATH 1];
char scoreTag[MAXPLAYERS 1][64];
char chatColor[MAXPLAYERS 1][64];
char nameColor[MAXPLAYERS 1][64];

public 
void OnPluginStart()
{
    
BuildPath(Path_SMfilePathsizeof(filePath), "configs/viptags.cfg");

    
CheckForKeyValues(1"STEAM_0:1:2");
}

void CheckForKeyValues(int clientchar[] steamID)
{
    
KeyValues kv = new KeyValues("VIPTags");
    
kv.ImportFromFile(filePath);
    
kv.JumpToKey(steamIDtrue);

    
kv.GetString("scoretag"scoreTag[client], sizeof(scoreTag[]));
    
kv.GetString("chatcolor"chatColor[client], sizeof(chatColor[]));
    
kv.GetString("namecolor"nameColor[client], sizeof(nameColor[]));

    if (
StrEqual(scoreTag[client], "") && StrEqual(chatColor[client], "") && StrEqual(nameColor[client], ""))
    {
        
kv.SetString("scoretag""-change-");
        
kv.SetString("chatcolor""-change-");
        
kv.SetString("namecolor""-change-");
        
kv.Rewind();
        
kv.ExportToFile(filePath);
    }

    
delete kv;


Output:
Code:

"VIPTags"
{
        "STEAM_0:1:2"
        {
                "scoretag"                "-change-"
                "chatcolor"                "-change-"
                "namecolor"                "-change-"
        }
}


SpirT 02-24-2021 07:40

Re: KeyValues not working
 
Quote:

Originally Posted by DJ Tsunami (Post 2738108)
I don't know, works fine for me:
PHP Code:

char filePath[PLATFORM_MAX_PATH 1];
char scoreTag[MAXPLAYERS 1][64];
char chatColor[MAXPLAYERS 1][64];
char nameColor[MAXPLAYERS 1][64];

public 
void OnPluginStart()
{
    
BuildPath(Path_SMfilePathsizeof(filePath), "configs/viptags.cfg");

    
CheckForKeyValues(1"STEAM_0:1:2");
}

void CheckForKeyValues(int clientchar[] steamID)
{
    
KeyValues kv = new KeyValues("VIPTags");
    
kv.ImportFromFile(filePath);
    
kv.JumpToKey(steamIDtrue);

    
kv.GetString("scoretag"scoreTag[client], sizeof(scoreTag[]));
    
kv.GetString("chatcolor"chatColor[client], sizeof(chatColor[]));
    
kv.GetString("namecolor"nameColor[client], sizeof(nameColor[]));

    if (
StrEqual(scoreTag[client], "") && StrEqual(chatColor[client], "") && StrEqual(nameColor[client], ""))
    {
        
kv.SetString("scoretag""-change-");
        
kv.SetString("chatcolor""-change-");
        
kv.SetString("namecolor""-change-");
        
kv.Rewind();
        
kv.ExportToFile(filePath);
    }

    
delete kv;


Output:
Code:

"VIPTags"
{
        "STEAM_0:1:2"
        {
                "scoretag"                "-change-"
                "chatcolor"                "-change-"
                "namecolor"                "-change-"
        }
}


Thanks for the reply. Also tried your code and worked as well. I have modified my code and it worked.

The current source is:
PHP Code:

void CheckForKeyValues(int clientchar[] steamID)
{
    
KeyValues kv = new KeyValues("VIPTags");
    
kv.ImportFromFile(filePath);
    
    
kv.JumpToKey(steamIDtrue);
    
KvGetString(kv"scoretag"scoreTag[client], sizeof(scoreTag[]));
    
KvGetString(kv"chatcolor"chatColor[client], sizeof(chatColor[]));
    
KvGetString(kv"namecolor"nameColor[client], sizeof(nameColor[]));
        
    if(
StrEqual(scoreTag[client], "") && StrEqual(chatColor[client], "") && StrEqual(nameColor[client], ""))
    {
           
KvSetString(kv"scoretag""-change-");
        
KvSetString(kv"chatcolor""-change-");
           
KvSetString(kv"namecolor""-change-");
        
kv.Rewind();
        
kv.ExportToFile(filePath);
        
delete kv;
        return;
    }
           
    
delete kv;
    return;


I don't think it was the if statement because it was returning true. What I think that was the issue was the KVGoToFirstSubKey(kv);. I think I shouldn't use that if I'm on the main tree.

Thanks for your help!


All times are GMT -4. The time now is 17:50.

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