PDA

View Full Version : Simple announce plugin. need help


kostazs
02-18-2012, 02:19
I have made a plugin that posts chat messages to client when client connected.
The problem is when client reconnects plugin post the same message twice, afret one more reconnect - 3 times and so on... so how to make plugin to "forget" a client and not to post the same things many times.. ? :3
Im just trying to learn how to write plugins. this is my first try. here is the code:
#include <sourcemod>

#define PL_VERSION "1.0"

public Plugin:myinfo =
{
name = "Announce",
author = "7wolf",
description = "Text Output",
version = PL_VERSION,
url = "http://www.nacpat.clan.su/"
};

new Handle:g_announce = INVALID_HANDLE;
new Handle:g_hTimer;

public OnPluginStart()
{
LoadTranslations ("sm_announce.phrases");

g_announce = CreateConVar("sm_announce","1");
}

public OnPluginEnd()
{
CloseHandle(g_announce);
}

public OnClientPutInServer(client)
{
g_hTimer = CreateTimer (30.0, announceMsg, client);
g_hTimer = CreateTimer (60.0, announceMsg1, client);
g_hTimer = CreateTimer (90.0, announceMsg2, client);
g_hTimer = CreateTimer (120.0, announceMsg3, client);
g_hTimer = CreateTimer (150.0, announceMsg4, client);
g_hTimer = CreateTimer (180.0, announceMsg5, client);
}

public OnClientDisconnecting(client)
{
if (g_hTimer != INVALID_HANDLE)
{
KillTimer(g_hTimer);
}
}


public Action:announceMsg (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[^_^]\x01 %T", "Announce", client);
}
return Plugin_Continue;
}
public Action:announceMsg1 (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[o_O]\x01 %T", "Announce1", client);
}
return Plugin_Continue;
}
public Action:announceMsg2 (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[O_o]\x01 %T", "Announce2", client);
}
return Plugin_Continue;
}
public Action:announceMsg3 (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[O_O]\x01 %T", "Announce3", client);
}
return Plugin_Continue;
}
public Action:announceMsg4 (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[o_O]\x01 %T", "Announce4", client);
}
return Plugin_Continue;
}
public Action:announceMsg5 (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[O_o]\x01 %T", "Announce5", client);
}
return Plugin_Continue;
}

kostazs
02-19-2012, 11:59
anyone ? -_-

Dr. McKay
02-20-2012, 12:34
You're going to have to use a database. Databases are kinda complex for a first plugin.

kostazs
02-20-2012, 13:59
by saying "forget" a client i meant not to duplicate the output.
for example the message is "welcome" must show up after 30 seconds
Client connects and the map changes 5 sec after he joined.
So.. the map changed.. he enters the game and after 25 sec he sees "welcome" and 5 sec later one more "welcome".... thats what i mean... i dont know how to clear the timer.
Once client joined timer starts working... so if i rejoin 10 times i will see 10 welcome messages in a raw. -_-
So how to make timer off on client disconnect ?

TnTSCS
02-20-2012, 14:00
is public OnClientDisconnecting(client) valid? I searched for it in API but it doesn't exist

kostazs
02-20-2012, 14:28
it is not the point.. i just want to know HOW to do it... i will find another event.. maybe onmapchange or something like that :)

kostazs
02-20-2012, 14:30
And yeah.. this thing not working and i am not sure if KillTimer(g_hTimer); is what i need...
public OnClientDisconnecting(client)
{
if (g_hTimer != INVALID_HANDLE)
{
KillTimer(g_hTimer);
}
}

necavi
02-20-2012, 14:31
#include <sourcemod>

#define PL_VERSION "1.0"

public Plugin:myinfo =
{
name = "Announce",
author = "7wolf",
description = "Text Output",
version = PL_VERSION,
url = "http://www.nacpat.clan.su/"
};

new Handle:g_announce = INVALID_HANDLE;
new Handle:g_hClientTimers[MAXPLAYERS+1][6];

public OnPluginStart()
{
LoadTranslations ("sm_announce.phrases");
for(new j=0;j<MAXPLAYERS+1;j++)
{
for(new i=0;i<6;i++)
{
g_hClientTimers[j][i] = INVALID_HANDLE;
}
}

g_announce = CreateConVar("sm_announce","1");
}

public OnPluginEnd()
{
CloseHandle(g_announce);
}

public OnClientPutInServer(client)
{
g_hClientTimers[client][0] = CreateTimer (30.0, announceMsg, client);
g_hClientTimers[client][1] = CreateTimer (60.0, announceMsg1, client);
g_hClientTimers[client][2] = CreateTimer (90.0, announceMsg2, client);
g_hClientTimers[client][3] = CreateTimer (120.0, announceMsg3, client);
g_hClientTimers[client][4] = CreateTimer (150.0, announceMsg4, client);
g_hClientTimers[client][5] = CreateTimer (180.0, announceMsg5, client);
}

public OnClientDisconnecting(client)
{
for(new i;i<6;i++)
{
if(g_hClientTimers[client][i]!=INVALID_HANDLE)
{
KillTimer(g_hClientTimers[client][i]);
g_hClientTimers[client][i] = INVALID_HANDLE;
}
}
}


public Action:announceMsg (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[^_^]\x01 %T", "Announce", client);
}
return Plugin_Continue;
}
public Action:announceMsg1 (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[o_O]\x01 %T", "Announce1", client);
}
return Plugin_Continue;
}
public Action:announceMsg2 (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[O_o]\x01 %T", "Announce2", client);
}
return Plugin_Continue;
}
public Action:announceMsg3 (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[O_O]\x01 %T", "Announce3", client);
}
return Plugin_Continue;
}
public Action:announceMsg4 (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[o_O]\x01 %T", "Announce4", client);
}
return Plugin_Continue;
}
public Action:announceMsg5 (Handle:timer, any:client)
{
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[O_o]\x01 %T", "Announce5", client);
}
return Plugin_Continue;
}
Excuse the way I handled the multidimensional array, I'm pretty tired at the moment, but it should work (may not be the absolute fastest, however).

TnTSCS
02-20-2012, 15:02
for player disconnecting, it should be something like:


public OnClientDisconnect(client)
{
if(IsClientInGame(client))
{
for(new i=0;i<6;i++)
{
if(g_hClientTimers[client][i] != INVALID_HANDLE)
{
KillTimer(g_hClientTimers[client][i]);
g_hClientTimers[client][i] = INVALID_HANDLE;
}
}
}
}


And make sure you set each clienttimer handle to INVALID_HANDLE when the timer expires...
public Action:announceMsg (Handle:timer, any:client)
{
g_hClientTimer[client][0] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))
{
PrintToChat(client,"\x05[^_^]\x01 %T", "Announce", client);
}
return Plugin_Continue;
}

and also, might have to have IsClientInGame before IsClientConnected otherwise I think, and I could be wrong, you'll throw an error on the IsClientConnected if the client isn't in game (invalid client id)...
if (GetConVarInt (g_announce) == 1 && IsClientConnected (client) && IsClientInGame (client))



Also, you could have <= MaxClients, instead of <MAXPLAYERS+1 (and you should start at 1, not 0 (I think)
for(new j=0;j<MAXPLAYERS+1;j++)should be for(new j=1;j<=MaxClients;j++)

kostazs
02-20-2012, 15:35
so.. this is what we have now:
#include <sourcemod>

#define PL_VERSION "1.0"

public Plugin:myinfo =
{
name = "Announce",
author = "7wolf",
description = "Text Output",
version = PL_VERSION,
url = "http://www.nacpat.clan.su/"
};

new Handle:g_announce = INVALID_HANDLE;
new Handle:g_hClientTimers[MAXPLAYERS+1][6];

public OnPluginStart()
{
LoadTranslations ("sm_announce.phrases");
for(new j=1;j<=MaxClients;j++)
{
for(new i=0;i<6;i++)
{
g_hClientTimers[j][i] = INVALID_HANDLE;
}
}

g_announce = CreateConVar("sm_announce","1");
}

public OnPluginEnd()
{
CloseHandle(g_announce);
}

public OnClientPutInServer(client)
{
g_hClientTimers[client][0] = CreateTimer (30.0, announceMsg, client);
g_hClientTimers[client][1] = CreateTimer (60.0, announceMsg1, client);
g_hClientTimers[client][2] = CreateTimer (90.0, announceMsg2, client);
g_hClientTimers[client][3] = CreateTimer (120.0, announceMsg3, client);
g_hClientTimers[client][4] = CreateTimer (150.0, announceMsg4, client);
g_hClientTimers[client][5] = CreateTimer (180.0, announceMsg5, client);
}

public OnClientDisconnect(client)
{
if(IsClientInGame(client))
{
for(new i=0;i<6;i++)
{
if(g_hClientTimers[client][i] != INVALID_HANDLE)
{
KillTimer(g_hClientTimers[client][i]);
g_hClientTimers[client][i] = INVALID_HANDLE;
}
}
}
}


public Action:announceMsg (Handle:timer, any:client)
{
g_hClientTimers[client][0] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[^_^]\x01 %T", "Announce", client);
}
return Plugin_Continue;
}
public Action:announceMsg1 (Handle:timer, any:client)
{
g_hClientTimers[client][0] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[o_O]\x01 %T", "Announce1", client);
}
return Plugin_Continue;
}
public Action:announceMsg2 (Handle:timer, any:client)
{
g_hClientTimers[client][0] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[O_o]\x01 %T", "Announce2", client);
}
return Plugin_Continue;
}
public Action:announceMsg3 (Handle:timer, any:client)
{
g_hClientTimers[client][0] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[O_O]\x01 %T", "Announce3", client);
}
return Plugin_Continue;
}
public Action:announceMsg4 (Handle:timer, any:client)
{
g_hClientTimers[client][0] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[o_O]\x01 %T", "Announce4", client);
}
return Plugin_Continue;
}
public Action:announceMsg5 (Handle:timer, any:client)
{
g_hClientTimers[client][0] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[O_o]\x01 %T", "Announce5", client);
}
return Plugin_Continue;
}
Compiler says:
loose indentation
6 warnings all about this:
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
plugin works the same way.. nothing have changed :(

necavi
02-20-2012, 16:15
Unfortunately you're not right about MaxClients, anything before OnMapStart() is supposed to use MAXPLAYERS (and in this case, MAXPLAYERS+1, if I remember correctly).

However, you are correct on all other counts! I was simply too sleepy to code properly.

TnTSCS
02-20-2012, 18:15
coo... I wasn't thinking when I suggested MaxClients in that function..

the loose indentation warnings just mean that the indents are not consistent. I'm not sure what you use, but make it consistent... either use 4 spaces or 1 tab...

kostazs... g_hClientTimers[client][0] = INVALID_HANDLE; is good for the first timer callback, but you need to increase the number for each one... match up the timer creation with the timer conclusion

g_hClientTimers[client][1] = INVALID_HANDLE; // for the second
g_hClientTimers[client][2] = INVALID_HANDLE; // for the third


#include <sourcemod>

#define PL_VERSION "1.0"

public Plugin:myinfo =
{
name = "Announce",
author = "7wolf",
description = "Text Output",
version = PL_VERSION,
url = "http://www.nacpat.clan.su/"
};

new Handle:g_announce = INVALID_HANDLE;
new Handle:g_hClientTimers[MAXPLAYERS+1][6];

public OnPluginStart()
{
LoadTranslations ("sm_announce.phrases");
for(new j=0;j<MAXPLAYERS+1;j++) // might need j=1
{
for(new i=0;i<6;i++)
{
g_hClientTimers[j][i] = INVALID_HANDLE;
}
}

g_announce = CreateConVar("sm_announce","1");
}

public OnPluginEnd()
{
CloseHandle(g_announce);
}

public OnClientPutInServer(client)
{
g_hClientTimers[client][0] = CreateTimer (30.0, announceMsg, client);
g_hClientTimers[client][1] = CreateTimer (60.0, announceMsg1, client);
g_hClientTimers[client][2] = CreateTimer (90.0, announceMsg2, client);
g_hClientTimers[client][3] = CreateTimer (120.0, announceMsg3, client);
g_hClientTimers[client][4] = CreateTimer (150.0, announceMsg4, client);
g_hClientTimers[client][5] = CreateTimer (180.0, announceMsg5, client);
}

public OnClientDisconnect(client)
{
if(IsClientInGame(client))
{
for(new i=0;i<6;i++)
{
if(g_hClientTimers[client][i] != INVALID_HANDLE)
{
KillTimer(g_hClientTimers[client][i]);
g_hClientTimers[client][i] = INVALID_HANDLE;
}
}
}
}


public Action:announceMsg (Handle:timer, any:client)
{
g_hClientTimers[client][0] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[^_^]\x01 %T", "Announce", client);
}
return Plugin_Continue;
}
public Action:announceMsg1 (Handle:timer, any:client)
{
g_hClientTimers[client][1] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[o_O]\x01 %T", "Announce1", client);
}
return Plugin_Continue;
}
public Action:announceMsg2 (Handle:timer, any:client)
{
g_hClientTimers[client][2] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[O_o]\x01 %T", "Announce2", client);
}
return Plugin_Continue;
}
public Action:announceMsg3 (Handle:timer, any:client)
{
g_hClientTimers[client][3] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[O_O]\x01 %T", "Announce3", client);
}
return Plugin_Continue;
}
public Action:announceMsg4 (Handle:timer, any:client)
{
g_hClientTimers[client][4] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[o_O]\x01 %T", "Announce4", client);
}
return Plugin_Continue;
}
public Action:announceMsg5 (Handle:timer, any:client)
{
g_hClientTimers[client][5] = INVALID_HANDLE;
if (GetConVarInt (g_announce) == 1 && IsClientInGame (client) && IsClientConnected (client))
{
PrintToChat(client,"\x05[O_o]\x01 %T", "Announce5", client);
}
return Plugin_Continue;
}

kostazs
02-21-2012, 07:04
seems to be working now )) but i still cant understand how you made things go right..
Can you explain what is happening here and why do we need this ?
for(new j=0;j<MAXPLAYERS+1;j++) // might need j=1
{
for(new i=0;i<6;i++)
{
g_hClientTimers[j][i] = INVALID_HANDLE;
}
}
And why do we have to post g_hClientTimers[client][i] = INVALID_HANDLE; every time

I just want to understand this stuff not to make the same error next time.

necavi
02-21-2012, 07:55
Well, that way everything becomes initialized to INVALID_HANDLE, meaning that if(g_hClientTimers[client][i] != INVALID_HANDLE)
{
KillTimer(g_hClientTimers[client][i]);
g_hClientTimers[client][i] = INVALID_HANDLE;
} doesn't accidently trigger, which would cause at the very least an error, possibly a crash, if I remember correctly.

kostazs
02-23-2012, 02:20
thx for helping guys :) i appreciate it