Raised This Month: $12 Target: $400
 3% 

Solved KeyValues not working


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 02-24-2021 , 03:50   KeyValues not working
Reply With Quote #1

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?
__________________

Last edited by SpirT; 02-24-2021 at 07:41.
SpirT is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 02-24-2021 , 04:03   Re: KeyValues not working
Reply With Quote #2

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.
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 02-24-2021 , 04:12   Re: KeyValues not working
Reply With Quote #3

Quote:
Originally Posted by DJ Tsunami View Post
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;
    }

__________________

Last edited by SpirT; 02-24-2021 at 04:12.
SpirT is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 02-24-2021 , 04:17   Re: KeyValues not working
Reply With Quote #4

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.
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 02-24-2021 , 04:25   Re: KeyValues not working
Reply With Quote #5

Quote:
Originally Posted by DJ Tsunami View Post
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 is offline
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 02-24-2021 , 05:04   Re: KeyValues not working
Reply With Quote #6

Quote:
Originally Posted by DJ Tsunami View Post
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?
__________________

Last edited by SpirT; 02-24-2021 at 05:10.
SpirT is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 02-24-2021 , 05:54   Re: KeyValues not working
Reply With Quote #7

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.
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.

Last edited by DJ Tsunami; 02-24-2021 at 05:58.
DJ Tsunami is offline
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 02-24-2021 , 06:13   Re: KeyValues not working
Reply With Quote #8

Quote:
Originally Posted by DJ Tsunami View Post
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;

__________________
SpirT is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 02-24-2021 , 07:07   Re: KeyValues not working
Reply With Quote #9

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-"
	}
}
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 02-24-2021 , 07:40   Re: KeyValues not working
Reply With Quote #10

Quote:
Originally Posted by DJ Tsunami View Post
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!
__________________
SpirT 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 19:31.


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