Player.Joined and Player.Disconnected events

Player.Joined and Player.Disconnected events

Postby dzienny » 22 May 2013, 20:18

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.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Player.Joined and Player.Disconnected events

Postby ismellike » 24 May 2013, 03:35

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)
        {
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Player.Joined and Player.Disconnected events

Postby lucasds12 » 24 May 2013, 03:46

It works for me.

[code][/code Horray!]
There is only one thing I do in life, that's contributing here.
lucasds12
 
Posts: 334
Joined: 17 Apr 2013, 16:17
Location: In the deep caves.

Re: Player.Joined and Player.Disconnected events

Postby ismellike » 24 May 2013, 03:51

What did you use?
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Player.Joined and Player.Disconnected events

Postby lucasds12 » 24 May 2013, 03:55

I didn't know what I was doing.
I was just doing crap and it worked.
I'll check. xXx
There is only one thing I do in life, that's contributing here.
lucasds12
 
Posts: 334
Joined: 17 Apr 2013, 16:17
Location: In the deep caves.

Re: Player.Joined and Player.Disconnected events

Postby lucasds12 » 24 May 2013, 03:57

I just did random things.
And it worked.



So Yeah!
There is only one thing I do in life, that's contributing here.
lucasds12
 
Posts: 334
Joined: 17 Apr 2013, 16:17
Location: In the deep caves.

Re: Player.Joined and Player.Disconnected events

Postby dzienny » 24 May 2013, 09:24

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.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27


Return to Knowledge Base

Who is online

Users browsing this forum: No registered users and 1 guest

cron