AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Ban Player from Menu (https://forums.alliedmods.net/showthread.php?t=316606)

Andriis 05-31-2019 13:03

Ban Player from Menu
 
I am trying to make a menu with fixed ban types.. like swearing, i made the menu with some samples but its not working.. Can someone help me with this?

Code:

public BPlayerMenu0(id){
        new menu = menu_create("\rthS\d | \yChoose a Player:", "BPlayerMenu_Handler0");
       
        new players[32], pnum, tempid;
       
        new szName[32], szUserId[32];
       
        get_players( players, pnum, "a");
       
        for (new i; i<pnum; i++){
        tempid = players[i];
       
        get_user_name(tempid, szName, charsmax(szName));
        formatex(szUserId, charsmax(szUserId), "%d", get_user_userid(tempid));
       
        menu_additem(menu, szName, szUserId, 0);
        }
       
        menu_display(id, menu, 0);
}
public BPlayerMenu_Handler0(id, menu, item){       
        if (item == MENU_EXIT){
        menu_destroy(menu);
        return PLUGIN_HANDLED;
        }
        new data[5];
        new szIP[40];
        new szData[6], szName[64], AdminName[64];
        new _access, item_callback;
        menu_item_getinfo(menu, item, _access, szData,charsmax(szData), szName,charsmax(szName), item_callback);
       
        new tempid = str_to_num(data)
        get_user_name(tempid, szName, charsmax(szName ));
        get_user_ip(tempid, szIP, charsmax(szIP), 1)
        get_user_ip(id, AdminName, charsmax(AdminName), 1)
       
        server_cmd("amx_ban ^"%s^" ^"30^" ^"Do not swear^"",szName)
        console_print(id, "Banned name: ^"%s^"", szName)
        console_print(id, "Banned IP: ^"%s^"", szIP)
        console_print(id, "Ban reason: Swearing")
        console_print(id, "Admin: ^"%s^"", AdminName)

        menu_destroy( menu );
        return PLUGIN_HANDLED;
}

And also.. I'm not sure if I correctly defined the admin who is banning the player :/

<VeCo> 05-31-2019 13:15

Re: Ban Player from Menu
 
PHP Code:

formatex(szUserIdcharsmax(szUserId), "%d"get_user_userid(tempid)); 

You are getting the #userid here, not the client id which would be used for the name/ip natives below. Use find_player to find his client id from #userid.
You already send the player name in szName trough the menu, no need to get it again.
AdminName is an IP address, obviously.

Andriis 05-31-2019 15:28

Re: Ban Player from Menu
 
Code:

public BPlayerMenu0(id){
        new menu = menu_create( "\rthS\d | \yChoose Player:", "BPlayerMenu_Handler0" );
        new players[32], pnum, tempid;
        new szName[32], szUserId[32];
        get_players(players, pnum, "a");
        for (new i; i<pnum; i++){
        tempid = players[i];
        get_user_name(tempid, szName, charsmax(szName));
        formatex(szUserId, charsmax(szUserId), "%d", get_user_userid(tempid));
        menu_additem(menu, szName, szUserId, 0);
        }
        menu_display( id, menu, 0 );
}
public BPlayerMenu_Handler0(id, menu, item){
        if (item == MENU_EXIT){
        menu_destroy(menu);
        return PLUGIN_HANDLED;
        }
        new szIP[40];
        new szData[6], szName[64], AdminName[64];
        new _access, item_callback;
        menu_item_getinfo(menu, item, _access, szData,charsmax(szData), szName,charsmax(szName), item_callback);
        new tempid = str_to_num(szData)
        get_user_name(tempid, szName, charsmax(szName))
        get_user_ip(tempid, szIP, charsmax(szIP), 1)
        get_user_name(id, AdminName, charsmax(AdminName))
        new player = find_player("k", tempid)
        server_cmd("amx_ban ^"%s^" ^"30^" ^"Do not swear^"",player)
        console_print(id, "Banned name: ^"%s^"", szName)
        console_print(id, "Banned IP: ^"%s^"", szIP)
        console_print(id, "Ban reason: Swearing")
        console_print(id, "Ban Time: 30 minutes")
        console_print(id, "Admin: ^"%s^"", AdminName)
       
        menu_destroy( menu );
        return PLUGIN_HANDLED;
}

I just don't get it. How to use things and ..

<VeCo> 05-31-2019 19:15

Re: Ban Player from Menu
 
Use "player" as the index in the get_user_ip native. Of course, make sure the player is connected first. You already have the name saved in szName from the menu option, no need for get_user_name there.
Use tempid as the userid in the amx_ban command ("%s" should be "#%d").
//You can also use num_to_str instead of formatex.

Andriis 06-01-2019 12:18

Re: Ban Player from Menu
 
Now it's almost working like it supposed to, but still something is wrong
Here is the code:
Code:

public BPlayerMenu0(id){
        new menu = menu_create( "\rthS\d | \yChoose Player:", "BPlayerMenu_Handler0" );
        new players[32], pnum, tempid;
        new szName[32], szUserId[32];
        get_players(players, pnum, "a");
        for (new i; i<pnum; i++){
                tempid = players[i];
                get_user_name(tempid, szName, charsmax(szName));
                // formatex(szUserId, charsmax(szUserId), "%d", get_user_userid(tempid));
                new szUserId = str_to_num(tempid)
                menu_additem(menu, szName, szUserId, 0);
        }
        menu_display( id, menu, 0 );
}
public BPlayerMenu_Handler0(id, menu, item){
        if (item == MENU_EXIT){
        menu_destroy(menu);
        return PLUGIN_HANDLED;
        }
        new szIP[40];
        new szData[6], szName[64], AdminName[64];
        new _access, item_callback;
        menu_item_getinfo(menu, item, _access, szData,charsmax(szData), szName,charsmax(szName), item_callback);
        new tempid = str_to_num(szData)
        new player = find_player("k", tempid)
        get_user_ip(player, szIP, charsmax(szIP), 1)
        get_user_name(id, AdminName, charsmax(AdminName))
        if (player && is_user_alive(player)){
                server_cmd("amx_ban ^"#%d^" ^"30^" ^"Do not swear^"",player)
                console_print(id, "Banned name: ^"%s^"", szName)
                console_print(id, "Banned IP: ^"%s^"", szIP)
                console_print(id, "Ban reason: Swearing")
                console_print(id, "Ban Time: 30 minutes")
                console_print(id, "Admin: ^"%s^"", AdminName)
        }
        menu_destroy( menu );
        return PLUGIN_HANDLED;
}

Problem is, that I can ban myself (when I'm on the server by my self), but only once, after un ban i have to restart the server to be able to ban myself again, and, if I try to ban someone else, than the console says that player can't be found ..

fysiks 06-01-2019 13:11

Re: Ban Player from Menu
 
You shouldn't be using the id from get_players in your menu data. You should be using the userid (from get_user_userid()) because if someone leaves after you open the menu and another person joins, they will get that same id and you might ban someone that wasn't supposed to be banned.

Also, to use the "#" in the amx_ban command requires the userid (from get_user_userid()), not the entity id (from get_players()). So, I think you only need to make the following changes:

Code:
public BPlayerMenu0(id) {     new menu = menu_create( "\rthS\d | \yChoose Player:", "BPlayerMenu_Handler0" )     new players[32], pnum, tempid;     new szName[32], szUserId[32]     get_players(players, pnum, "a")     for (new i; i<pnum; i++)     {         tempid = players[i];         get_user_name(tempid, szName, charsmax(szName))         // formatex(szUserId, charsmax(szUserId), "%d", get_user_userid(tempid));         new szUserId[12]         num_to_str(get_user_userid(tempid), szUserId, charsmax(szUserId))         menu_additem(menu, szName, szUserId, 0);     }     menu_display( id, menu, 0 ) } public BPlayerMenu_Handler0(id, menu, item) {     if (item == MENU_EXIT)     {         menu_destroy(menu)         return PLUGIN_HANDLED     }     new szIP[40];     new szData[6], szName[64], AdminName[64];     new _access, item_callback;     menu_item_getinfo(menu, item, _access, szData,charsmax(szData), szName,charsmax(szName), item_callback);     new tempid = str_to_num(szData)     new player = find_player("k", tempid)     if (player && is_user_alive(player))     {         get_user_ip(player, szIP, charsmax(szIP), 1)         get_user_name(id, AdminName, charsmax(AdminName))         server_cmd("amx_ban ^"#%d^" ^"30^" ^"Do not swear^"",tempid)         console_print(id, "Banned name: ^"%s^"", szName)         console_print(id, "Banned IP: ^"%s^"", szIP)         console_print(id, "Ban reason: Swearing")         console_print(id, "Ban Time: 30 minutes")         console_print(id, "Admin: ^"%s^"", AdminName)     }     menu_destroy( menu );     return PLUGIN_HANDLED; }

Andriis 06-01-2019 14:33

Re: Ban Player from Menu
 
Now I can't compile it..
Error: Argument type mismatch (argument 1) on line xy .. The line which contains this: new szUserId = str_to_num(get_user_userid(tempid))
and also: Error: Argument type mismatch (argument 3) on line xy .. The line with this: menu_additem(menu, szName, szUserId, 0);

OciXCrom 06-01-2019 14:36

Re: Ban Player from Menu
 
Quote:

new szUserId = str_to_num(get_user_userid(tempid))
This function requires a string as the first argument, but you're giving it an integer. get_user_userid is already an integer, why are you trying to convert it?
You need to convert it to a string in order to use it with menu_additem. You need num_to_str, not str_to_num.

Quote:

menu_additem(menu, szName, szUserId, 0)
Again - szUserId should be a string, in your case it's an integer.

fysiks 06-01-2019 15:38

Re: Ban Player from Menu
 
Fixed above. Since I was basing it on his original code, it wasn't as obvious to me at the time. Had I written it from scratch, it would have been much easier.


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

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