Raised This Month: $27 Target: $400
 6% 

Solved Add Prefix to Player Name based on Admin Flags


Post New Thread Reply   
 
Thread Tools Display Modes
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 04-13-2025 , 09:45   Re: Add Prefix to Player Name based on Admin Flags
Reply With Quote #11

Cthulhu88, better in this way:
PHP Code:
static const char prefixes[][] = {
    
"☆",
    
"★"
};

// Can replace this with a LUT (lookup table), depending on the conditions required
int prefix = (owners) ? : (uppers) ? : -1;

if(
prefix >= 0)
{
    
char name[MAX_NAME_LENGTH];
    
GetClientName(clientnamesizeof(name));
    
// http://www.sourcemod.net/new-api/string/ReplaceString
    // Return Value
    //     Number of replacements that were performed.
    
if(ReplaceString(namesizeof(name), prefixes[0], "") || ReplaceString(namesizeof(name), prefixes[1], ""))
    {
        
// need to use Format, not FormatEx, if the string buffers overlap
        
Format(namesizeof(name), "%s %s"prefixes[prefix], name);
        
SetClientName(clientname);
    }

__________________
Grey83 is offline
Cthulhu88
Junior Member
Join Date: Mar 2025
Old 04-13-2025 , 11:53   Re: Add Prefix to Player Name based on Admin Flags
Reply With Quote #12

Four issues here:
  • ReplaceString will replace ALL occurences, not just the first.
  • ReplaceString will look for the substring anywhere in the string, not just the beginning.
  • You need for it to replace "prefix ", rather than "prefix", else an extra whitespace will be appended before the real name.
  • ReplaceString is O(N), where N is the length of the string. The function has to traverse the whole string comparing it against the substring. The strncmp approach is O(3) at worst as both prefixes encode to 3 bytes (UTF-8).

EDIT: You are also not adding the prefix, if neither of the prefixes are found.
EDIT: Fixed my post to check for a zero return value from strncmp (equal), rather than non zero (not equal).

Last edited by Cthulhu88; 04-13-2025 at 12:28.
Cthulhu88 is offline
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 04-13-2025 , 13:09   Re: Add Prefix to Player Name based on Admin Flags
Reply With Quote #13

Quote:
Originally Posted by Cthulhu88 View Post
ReplaceString will replace ALL occurences, not just the first.
http://www.sourcemod.net/new-api/string/ReplaceStringEx
__________________
Grey83 is offline
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 04-13-2025 , 13:14   Re: Add Prefix to Player Name based on Admin Flags
Reply With Quote #14

Quote:
Originally Posted by Cthulhu88 View Post
You need for it to replace "prefix ", rather than "prefix", else an extra whitespace will be appended before the real name.
http://www.sourcemod.net/new-api/string/TrimString
__________________
Grey83 is offline
Cthulhu88
Junior Member
Join Date: Mar 2025
Old 04-13-2025 , 15:18   Re: Add Prefix to Player Name based on Admin Flags
Reply With Quote #15

I never said there aren't other ways around it. I merely pointed the problems with the solution you gave as "better".

All those rmw string operations will be much slower than just passing the offset to where the actual name starts (random/direct access, once we know where the actual name starts). In x86, this can translate to a simple:
Code:
lea eax,[eax+ecx] // eax == string pointer, ecx == offset
push eax // to Format call
Even then, messing with strings, especially when a client can edit them, is not ideal. The ideal approach was my second example.

Last edited by Cthulhu88; 04-13-2025 at 15:22.
Cthulhu88 is offline
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 04-15-2025 , 10:30   Re: Add Prefix to Player Name based on Admin Flags
Reply With Quote #16

With removing all invalid prefixes before name (like "★☆ ★☆☆ ★★☆")
Attached Files
File Type: sp Get Plugin or Get Source (admin_prefixes 1.0.0_15.04.2025.sp - 20 views - 2.8 KB)
__________________

Last edited by Grey83; 04-15-2025 at 10:31.
Grey83 is offline
doublejz
Member
Join Date: Mar 2022
Old 04-15-2025 , 19:49   Re: Add Prefix to Player Name based on Admin Flags
Reply With Quote #17

Thanks for all the input/help here "guys"

Quote:
Originally Posted by Grey83 View Post
With removing all invalid prefixes before name (like "★☆ ★☆☆ ★★☆")
I can't seem to get this to add a prefix. It strips it regardless if I have no admin flag, custom admin flag 5 or 6
doublejz is offline
101
Senior Member
Join Date: Nov 2023
Old 04-16-2025 , 04:02   Re: Add Prefix to Player Name based on Admin Flags
Reply With Quote #18

Shared ONLY for public benefit and develepment :
https://forums.alliedmods.net/showthread.php?t=350943
101 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 00:48.


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