Member
|
09-07-2023
, 10:10
Re: Resetting your name to Steam name does not work
|
#12
|
Quote:
Originally Posted by Peter Brev
Additional question: How would I go about preventing a player from changing their name temporarily after an admin used sm_rename on them?
|
I have tried something, but after testing the following code with a friend, it is not working as I intended it to. I am probably doing it wrong, though. The problem is that if you rename a second player while another one is in cooldown, it takes the time left of the timer applied to the first player and applies it to the second player, which is not what I want. If multiple people get renamed, I want them all to have a unique cooldown timer. This is what I tried so far (I used the default sm_rename code as a base and modified it):
PHP Code:
Handle changename_adminrename_cooldown = INVALID_HANDLE;
char g_targetnewname[MAXPLAYERS+1][MAX_NAME_LENGTH];
public void OnPluginStart()
{
BuildPath(Path_SM, adminrenamedfile, sizeof(adminrenamedfile), "configs/admin_renamed_temp.ini");
changename_adminrename_cooldown = CreateConVar("sm_rename_cooldown", "60", "Controls how long a player needs to wait before changing their name again after an admin renamed them.", FCVAR_NOTIFY);
RegAdminCmd("sm_rename", Command_Rename, ADMFLAG_SLAY, "Renames a player manually and apply a temporary cooldown before being able to change names again.");
}
public void OnMapStart()
{
for (int x = 0; x < adminrenamedlines; x++)
{
adminrenamedid[x] = "";
}
adminrenamedlines = 0;
if (changename_adminrename_cooldown == INVALID_HANDLE)
{
SetFailState("%s You did not set a valid value for \"sm_rename_cooldown\".", TAG);
}
}
public void OnClientPostAdminCheck(int client)
{
char getsteamid[64];
RenameCheck(getsteamid, client);
}
public bool AdminRenamed()
{
Handle file = OpenFile(adminrenamedfile, "rt");
if (file == INVALID_HANDLE)
{
LogError("[NAME] Renamed players file could not be opened.", adminrenamedfile);
if (GetConVarBool(changename_debug))
{
LogToFile(LOGPATH, "%s Renamed players file could not be opened. Check that the file is placed in the \"configs\" folder.", LOGTAG);
}
return false;
}
if (file != INVALID_HANDLE)
{
PrintToServer("[NAME] Successfully loaded admin_renamed_temp.ini", adminrenamedfile);
if (GetConVarBool(changename_debug))
{
LogToFile(LOGPATH, "%s Renamed players file loaded.", LOGTAG);
}
}
while(!IsEndOfFile(file))
{
char line[64];
if (!ReadFileLine(file, line, sizeof(line)))
{
break;
}
TrimString(line);
ReplaceString(line, 64, " ", "");
if (strlen(line) == 0 || (line[0] == '/' && line[1] == '/'))
{
continue;
}
strcopy(adminrenamedid[adminrenamedlines], sizeof(adminrenamedlines[]), line);
adminrenamedlines++;
}
CloseHandle(file);
return true;
}
void RenameCheck(char getsteamid[64], char client)
{
AdminId clientAdmin = GetUserAdmin(client);
if(GetAdminFlag(clientAdmin, Admin_Generic, Access_Effective))
{
return;
}
ReplaceString(getsteamid, 64, " ", "");
return;
}
public Action adminrenamecheck(Event event, const char[] name, bool dontBroadcast)
{
char PlayerName[64];
GetEventString(event, "steamid2", PlayerName, 64);
RenameCheck(PlayerName, GetClientOfUserId(GetEventInt(event, "userid")));
return Plugin_Handled;
}
public Action Command_Rename(int client, int args)
{
if (args < 2)
{
PrintToChat(client, "%s%s %sUsage: %ssm_rename <#userid|name> <new name>", CTAG, TAG, CUSAGE, CLIME);
return Plugin_Handled;
}
char arg[MAX_NAME_LENGTH], arg2[MAX_NAME_LENGTH];
GetCmdArg(1, arg, sizeof(arg));
if (args > 1)
{
GetCmdArg(2, arg2, sizeof(arg2));
}
char target_name[MAX_TARGET_LENGTH];
int target_list[MAXPLAYERS], target_count;
bool tn_is_ml;
if ((target_count = ProcessTargetString(
arg,
client,
target_list,
MAXPLAYERS,
COMMAND_TARGET_NONE,
target_name,
sizeof(target_name),
tn_is_ml)) > 0)
{
for (int i = 0; i < target_count; i++)
{
if (target_count > 1) // getting rid of that dumb randomizer
{
PrintToChat(client, "%s%s %sMore than one player cannot be renamed at a time.", CTAG, TAG, CUSAGE);
return Plugin_Handled;
}
PrintToChatAll("%s%s %s%s %shas been renamed by an admin to %s%s%s.", CTAG, TAG, CPLAYER, target_name, CUSAGE, CPLAYER, arg2, CUSAGE);
Format(g_targetnewname[target_list[i]], MAX_NAME_LENGTH, "%s", arg2);
RenamePlayer(target_list[i]);
if (GetConVarBool(changename_debug))
{
LogToFile(LOGPATH, "%s %s has been renamed by %s to %s.", LOGTAG, target_name, client, arg2);
}
}
}
else
{
ReplyToTargetError(client, target_count);
}
return Plugin_Handled;
}
void RenamePlayer(int target)
{
SetClientName(target, g_targetnewname[target]);
g_targetnewname[target][0] = '\0';
char buffer[32];
GetClientAuthId(target, AuthId_Steam2, buffer, sizeof(buffer));
Handle nfile = OpenFile(adminrenamedfile, "a+");
/*for (int i = 0; i < adminrenamedlines; i++)
{
if (StrContains(buffer, adminrenamedid[i], false) != -1)
{
return;
}
}*/
WriteFileLine(nfile, buffer);
int timeleft = GetConVarInt(changename_adminrename_cooldown);
int mins, secs;
if (timeleft > 0)
{
mins = timeleft / 60;
secs = timeleft % 60;
PrintToChat(target, "%s%s %sAn admin has renamed you. You have been temporarily banned from changing names for %s%d:%02d%s.", CTAG, TAG, CUSAGE, CPLAYER, mins, secs, CUSAGE);
CheckTimer(target);
}
Handle DP = CreateDataPack();
WritePackString(DP, buffer);
WritePackCell(DP, target);
CreateTimer(GetConVarFloat(changename_adminrename_cooldown), name_temp_ban, DP);
CloseHandle(nfile);
OnMapStart();
if (GetConVarBool(changename_debug))
{
LogToFile(LOGPATH, "%s %s temporarily banned from changing names.", LOGTAG, target);
}
return;
}
public Action name_temp_ban(Handle timer, any DP)
{
ResetPack(DP);
char buffer[32];
ReadPackString(DP, buffer, 32);
CloseHandle(DP);
PrintToServer("Value of buffer is %s", buffer);
ArrayList fileArray = CreateArray(32);
DeleteFile(adminrenamedfile);
File newFile = OpenFile(adminrenamedfile, "a+");
if (newFile == null)
{
LogError("%s Could not open admin_renamed_temp.ini", TAG);
PrintToServer("%s Could not open admin_renamed_temp.ini", TAG);
return Plugin_Handled;
}
PrintToServer("%s %s has been removed from the temporary banned name list.", TAG, buffer);
if (GetConVarBool(changename_debug))
{
LogToFile(LOGPATH,"%s %s removed from temporary banned names list.", TAG, buffer);
}
for (int i = 0; i < GetArraySize(fileArray); i++)
{
char writeLine[32];
fileArray.GetString(i, writeLine, sizeof(writeLine));
newFile.WriteLine(writeLine);
if (GetConVarBool(changename_debug))
{
LogToFile(LOGPATH,"%s %s removed from temporary banned names list.", TAG, buffer);
}
}
delete newFile;
delete fileArray;
OnMapStart();
return Plugin_Handled;
//PrintToChat(target, "%s%s %sThis name has been added to the banned names list.", CTAG, TAG, CUSAGE);
}
public void CheckTimer(int client)
{
int iNow = GetTime(), iCooldown = GetConVarInt(changename_adminrename_cooldown);
if(iCooldown > 0)
{
int iTimeLeft = g_iLastUsed[client] + iCooldown - iNow;
int mins, secs;
if (iTimeLeft > 0)
{
mins = iTimeLeft / 60;
secs = iTimeLeft % 60;
//char timeleftbuffer[128];
PrintToChat(client, "%s%s %sAn admin recently renamed you. You will only be able to change your name in %s%d:%02d%s.", CTAG, TAG, CUSAGE, CPLAYER, mins, secs, CUSAGE);
return;
}
}
g_iLastUsed[client] = iNow;
}
public Action Command_Name(int client, int args)
{
gB_HideNameChange = true;
AdminId playerAdmin = GetUserAdmin(client);
if (args == 0)
{
char id[32], buffer[MAX_NAME_LENGTH], currentname[MAX_NAME_LENGTH];
GetClientAuthId(client, AuthId_Steam2, id, sizeof(id));
g_names.GetString(id, buffer, sizeof(buffer));
GetClientName(client, currentname, sizeof(currentname));
if(!GetAdminFlag(playerAdmin, Admin_Generic, Access_Effective))
{
for (int z = 0; z < adminrenamedlines; z++)
{
if (StrContains(id, adminrenamedid[z], false) != -1)
{
CheckTimer(client);
if (GetConVarBool(changename_debug))
{
LogToFile(LOGPATH, "%s %s attempted to change their name, but they have been temporarily suspended from doing so following an admin sm_rename usage.", LOGTAG, client);
}
return Plugin_Handled;
}
}
}
// Name stuff
}
}
|
|