Event for players disconnecting?

Event for players disconnecting?

Postby Conor » 27 Feb 2013, 01:42

Hey,

I previously thought that when players disconnect, their Player object becomes null. However I have recently realized this is not the case and it puts flaws in many of my past custom commands and code. For example the lottery command.

I'm currently working on a pretty large command which operates a mini gun game, however when players leave the server, they are not removed from the game which is a dilemma. If I have access to an event when a player leaves the server I can remove them from all necessary items related to the gun game to ensure it plays efficiently.

Please get back to me asap :)

Thanks.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Event for players disconnecting?

Postby Conor » 27 Feb 2013, 12:23

Actually, even if there is a bool which is changed when players disconnect, that would be fine, I'm not sure if one already exists?
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Event for players disconnecting?

Postby ismellike » 28 Feb 2013, 00:08

I have no idea if these will work, but there is

p.disconnected=true/false;

p.fullylogged=true/false;

p.loggedIn=true/false;

got these from mcdzienny_.dll
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Event for players disconnecting?

Postby Conor » 28 Feb 2013, 02:33

ismellike wrote:I have no idea if these will work, but there is

p.disconnected=true/false;

p.fullylogged=true/false;

p.loggedIn=true/false;

got these from mcdzienny_.dll


Thanks, I'll have to run some tests but the 'disconnected' bool should really be what I'm looking for, if not then Dzienny uses some misleading variable names xD

Thanks ismellike for looking through the variables.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Event for players disconnecting?

Postby dzienny » 03 Mar 2013, 01:55

Conor wrote:I previously thought that when players disconnect, their Player object becomes null. However I have recently realized this is not the case and it puts flaws in many of my past custom commands and code. For example the lottery command.


Yes, you are right. Player objects are never set to null. It would actually be a very bad practice to set it to null. I can remember too many NullReferenceExceptions that it caused in the past. Here's an example of what can go wrong:
Code: Select all
...
if (p != null) // p is not null here.
{
   int x = 2; // p was set to null by some other thread.
   int money = p.money; // Boom! NullReferenceException is thrown.
   ...
}
...

It's the magic of concurrency, and it may happen even on a single core proccessor, not to mention multi core ones. Of course, there are many ways of how to deal with similar cases. But, luckily you don't have to.

ismellike wrote:p.disconnected=true/false;

Congratulations, you found it :) This variable is set to true when a player disconnects.

Conor wrote:... if not then Dzienny uses some misleading variable names xD

I really try not to ;)
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Event for players disconnecting?

Postby Conor » 03 Mar 2013, 13:57

Thank you for the help Dzienny :)
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 7 guests

cron