From a623bbca3fc74c4145418cbd9dc58c6c79fdb7ad Mon Sep 17 00:00:00 2001 From: high Date: Fri, 1 Jul 2011 16:07:05 -0400 Subject: [PATCH] Added null checking to player --- TShockAPI/TSPlayer.cs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index e06974c8..2756486f 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -43,13 +43,15 @@ namespace TShockAPI public TSPlayer LastWhisper; public int LoginAttempts { get; set; } + Player FakePlayer = null; + public bool RealPlayer { - get { return Index >= 0 && Index < Main.maxNetPlayers; } + get { return Index >= 0 && Index < Main.maxNetPlayers && Main.player[Index] != null; } } public bool ConnectionAlive { - get { return RealPlayer ? Netplay.serverSock[Index].active && !Netplay.serverSock[Index].kill : false; } + get { return RealPlayer ? Netplay.serverSock[Index] != null && Netplay.serverSock[Index].active && !Netplay.serverSock[Index].kill : false; } } public string IP { @@ -61,14 +63,20 @@ namespace TShockAPI /// /// Terraria Player /// - public Player TPlayer { get; protected set; } + public Player TPlayer + { + get + { + return FakePlayer ?? Main.player[Index]; + } + } public string Name { get { return TPlayer.name; } } public bool Active { - get { return TPlayer.active; } + get { return TPlayer != null && TPlayer.active; } } public int Team { @@ -104,9 +112,9 @@ namespace TShockAPI bool flag = false; if (RealPlayer) { - for (int i = 0; i < 40; i++) + for (int i = 0; i < TPlayer.inventory.Length; i++) { - if (!TPlayer.inventory[i].active) + if (TPlayer.inventory[i] == null || !TPlayer.inventory[i].active) { flag = true; break; @@ -121,7 +129,6 @@ namespace TShockAPI { TilesDestroyed = new Dictionary(); Index = index; - TPlayer = Main.player[index]; Group = new Group("null"); } @@ -129,7 +136,7 @@ namespace TShockAPI { TilesDestroyed = new Dictionary(); Index = -1; - TPlayer = new Player { name = playerName, whoAmi = -1 }; + FakePlayer = new Player { name = playerName, whoAmi = -1 }; Group = new Group("null"); }