AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Need Some Help (https://forums.alliedmods.net/showthread.php?t=94001)

dRk13 06-05-2009 10:07

Need Some Help
 
the ideea is that if a player connects with a name like: server.ro to my server i want to make his name: unnamed and disconnect him and after disconnect him, to print in client console a message asking him to connect after he changes his name... ( and also if the word "server" is part of his name it would react the same...) also i want the plugin to see if the player changes his name in game, search thru the list of restricted names and if is the same as with one of the restricted names turn it back to the old one...

here is what i made until now..
and pelase don`t tell me not to reinvent a plugin... i just want to learn and this i can learn only redoing some plugins...[yeah, i know a lot of plugins that do almost the same thing.. but i want to know what i`m doing wrong... here ]

PHP Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "Name Restriction"
#define VERSION "0.1"
#define AUTHOR "DarKMaN"
//---------DEFINES------------

//Define max names from file.
#define MAXNAMES 60
new NumberOfNamesFromFile 0
//Rember names from file to compare with players.
new names[MAXNAMES][]
//If user is with name from list, change it to:
#define SETNAME "unnamed"
//---------END OF DEFINES ----


public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
LoadNameRestFile()
    
}

stock LoadNameRestFile()
{
    new 
filepath[64]
    
get_configsdir(filepath63);
    
format(filepath63"%s/namerestriction.ini"filepath);
    if(
file_exists(filepath))
    {
        new 
name[32];
        
        
//Open file
        
new fHandle fopen(filepath"rt");
        
        
//Checks for failure
        
if(!fHandle)
            return 
PLUGIN_HANDLED;
        
//LOOP THRU LINES
        
for(new 0MAXNAMES && !feof(fHandle); a++)
        {
            
//Get line
            
fgets(fHandlename31);
            
            
//Work away comments
            
if(name[0] == ';' || !name[0] || name[0] == ' ' || name[0] == 10
            {
                
//Line is not counted
                
a--;
                continue;
            }
            
copy(names[a], 31name)    
            
NumberOfNamesFromFile++;    
        }
        
    }
    return 
PLUGIN_CONTINUE;

}

public 
client_connects(id)
{
    if(
NumberOfNamesFromFile == )
        return 
PLUGIN_HANDLED
    
else
    {
        new 
playerconnects[32]
        
get_user_info(id"name"playerconnects31)
        for(new 
i=0NumberOfNamesFromFile i++)
            if(
equali(playerconnectsnames[i][]))
                
changeName(id)
            else
            {
            
checkIFnameISpartial(names[i], playerconnectsid)
            }
    }
    return 
PLUGIN_HANDLED
}

public 
client_infochanged(id)
{

    new 
newname[32],oldname[32]
    
get_user_info(id"name"newname,31)
    
get_user_name(idoldname31)
    if(
equali(newname,oldname))
        return 
PLUGIN_HANDLED;
    else
    {
        for(new 
0NumberOfNamesFromFile i++ )
        if(
equali(names[i],newname))
         
changeNameInGame(idoldname)
        else
         
checkIFnameISpartialINGAME(names[i], newnameidoldname)
    }
    return 
PLUGIN_HANDLED
}

stock changeName(id)
{
        
client_cmd(id"name %s"SETNAME)
        
client_cmd(id"echo [DNK BOT] Kicked because:")
        
client_cmd(id"echo [DNK BOT] Numele tau a fost schimbat in ^"%s^". Te rog conecteaza-te dupa ce ti-ai schimbat numele."SETNAME)
        
client_cmd(id"disconnect")
        return 
PLUGIN_HANDLED
}

stock changeNameInGame(idChangeNameTo[])
{
        
client_cmd(id"name %s"ChangeNameTo)
        
client_print(idprint_chat,"[DNK BOT] Nume interzis! Numele tau a fost schimbat in ^"%s^"."ChangeNameTo)
        return 
PLUGIN_HANDLED
}

stock checkIFnameISpartial(Vector1[], Vector2[], id)
{
            new 
pcnum charsmaxVector2 )
            new 
numb charsmaxVector1 )
            new 
OK 0
            
new 0
            
for(new 0pcnumi++)
            {
            if(
equali(Vector2[i], Vector1[k]))
            
k++
            
OK++
            }
            if(
equali(OK,numb))
            
changeName(id)
            return 
PLUGIN_CONTINUE;
}
         
stock checkIFnameISpartialINGAME(Vector1[], Vector2[], idoldname2[])
{
            new 
pcnum charsmaxVector2 )
            new 
numb charsmaxVector1 )
            new 
OK 0
            
new 0
            
for(new 0pcnumi++)
            {
            if(
equali(Vector2[i], Vector1[k])
            
k++
            
OK++
            }
            if(
equali(OK,numb))
            
changeNameInGame(idoldname2)
            return 
PLUGIN_CONTINUE;



fysiks 06-05-2009 18:43

Re: Need Some Help
 
Are you saying that it doesn't work at all? Are there any errors, that is where I start to get things working.

Anyways, The first thing that I noticed is your for loop. I was taught to never change the incrementing variable inside the for loop. So, I would use a while loop and get rid of "a" completey:

PHP Code:

        while( NumberOfNamesFromFile MAXNAMES && !feof(fHandle) ) 
        { 
            
//Get line 
            
fgets(fHandlename31); 
            
            
trim(name)
            
            
//Work away comments and blank lines
            
if( name[0] == ';' || !name[0] )  
            { 
                
//Line is not counted 
                
continue; 
            } 
            
copy(names[NumberOfNamesFromFile], 31name)     
            
NumberOfNamesFromFile++;     
        } 

Also, I would always use braces, Always. It makes it much easier to read the code. I see some places that I can't tell if they are supposed to go with the if statement or not.

And, always make sure that all of your parentheses are closed.

dRk13 06-07-2009 09:53

Re: Need Some Help
 
oke, 10x

one more question...
PHP Code:

 if( name[0] == ';' || !name[0] )   
            {  
                
//Line is not counted  
                
continue;  
            }  
            
copy(names[NumberOfNamesFromFile], 31name)      
            
NumberOfNamesFromFile++;      
        } 

shouldn`t be
PHP Code:

"else copy()" 

? or fi it appears continue just jumps over the rest of code from block and checks again at a new line?

Bugsy 06-07-2009 10:00

Re: Need Some Help
 
I personally always enclose any multi-operand conditional in parenthesis. It is not required but it makes code easier to edit\read [IMO]

ie.
PHP Code:

if( ( name[0] == ';' ) || !name[0] ) 


fysiks 06-07-2009 18:31

Re: Need Some Help
 
Yes, continue causes it to skip any following lines in the while loop.

client_connects -> client_connect (fyi)

client_infochanged() is called serveral times (in my experience) during connection so there wouldn't be a need to have client_connect.

This won't work if executed when the player is dead/spectating/noteam which is the case when they are connecting:
PHP Code:

client_cmd(id"name %s"SETNAME


Also, I would try to make things more modular. Meaning, I would make a function that looks for [partial] matches of the supplied string and one of the names found in the list of names retrieved from the ini file.

My example:
PHP Code:

bool:does_contain_restricted_name(username[])
{
    for(new 
0NumberOfNamesFromFilei++)
    {
        
// Use containi or even your partial name check code ( I can't comprehend what it does exactly at the moment :)
        
if(containi(usernamenames[i]))
        {
            return 
true
        
}
    }
    return 
false


Then I would use (in client_infochanged) something like:

PHP Code:

if( does_contain_restricted_name(newname) )
{
    
// Change name/kick person here.
    
return PLUGIN_HANDLED
}

return 
PLUGIN_CONTINUE 

EDIT: Oh, and I think you will need to declare the string length for your names loaded from the file:

PHP Code:

new names[MAXNAMES][] 

:arrow:
PHP Code:

new names[MAXNAMES][32



All times are GMT -4. The time now is 13:53.

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