Page 1 of 1

Player.Joined and Player.Disconnected events

PostPosted: 22 May 2013, 20:18
by dzienny
In the version 10.1 of MCDzienny two new events were added:
Code: Select all
Player.Joined += (object sender, PlayerEventArgs e) => {};
Player.Disconnected += (object sender, PlayerEventArgs e) => {};

, where object sender is always null and PlayerEventArgs e contains Player object that represents a player that has just joined, or just left.

Event Joined is triggered after a player fully joins the server that includes reading a database and a map loading.
Event Disconnected is triggered after a player was disconnected and their info was saved to a database.

Simple examples:
Code: Select all
Player.Joined += (object sender, PlayerEventArgs e) =>
{
   Player.SendMessage(e.Player, "Here's a message that is showed to a player that joins a server.");
};
Player.Disconnected += (object sender, PlayerEventArgs e) =>
{
   // If a player disconnects, log their time online.
   Server.s.Log("A player named " + e.Player.name + " has just left after playing " +
      (int)DateTime.Now.Subtract(e.Player.timeLogged).TotalMinutes + " minutes.", false);
};


To implement these events listeners follow the same pattern that is used in a chat filter example.

Re: Player.Joined and Player.Disconnected events

PostPosted: 24 May 2013, 03:35
by ismellike
I was trying to use this but it didn't work.

I decided to try the one listed on vs:
Code: Select all
Player.Joined+=new EventHandler<PlayerEventArgs>(Player_Joined);


It compiled but nothing happened when someone joined.

I was using

Code: Select all
        public void Player_Joined(object sender, PlayerEventArgs e)
        {

Re: Player.Joined and Player.Disconnected events

PostPosted: 24 May 2013, 03:46
by lucasds12
It works for me.

[code][/code Horray!]

Re: Player.Joined and Player.Disconnected events

PostPosted: 24 May 2013, 03:51
by ismellike
What did you use?

Re: Player.Joined and Player.Disconnected events

PostPosted: 24 May 2013, 03:55
by lucasds12
I didn't know what I was doing.
I was just doing crap and it worked.
I'll check. xXx

Re: Player.Joined and Player.Disconnected events

PostPosted: 24 May 2013, 03:57
by lucasds12
I just did random things.
And it worked.



So Yeah!

Re: Player.Joined and Player.Disconnected events

PostPosted: 24 May 2013, 09:24
by dzienny
ismellike wrote:I was trying to use this but it didn't work.

I decided to try the one listed on vs:
Code: Select all
Player.Joined+=new EventHandler<PlayerEventArgs>(Player_Joined);


It compiled but nothing happened when someone joined.

I was using

Code: Select all
        public void Player_Joined(object sender, PlayerEventArgs e)
        {


I've just edited the first post to provide an example how to use it. I also changed () to {} as it's the correct way to do it. Anyway the method Player.Joined += new EventHandler<PlayerEventArgs>(Player_Joined); or just Player.Joined += Player_Joined; is superior to the one in the given example, because it allows you to later remove the event listener by using Player.Joined -= Player_Joined;. So, it's good you are using this method, and it has to work. Just make sure you follow the same pattern of implementing the event listener as is used in a ChatFilter example.