Hi all, I've been working on an AntiCheat plugin for a couple of days now, it's been running great, out of the blue the OnPlayerConnected hook stopped working, and in some cases is crashing the server entirely.
At this point I know it's something incredibly dumb, I'm pulling my hair out and need a second look, and perhaps some more coffee..
Here's the error:
What I do with this hook is incredibly simplistic, I add the PlayerSession to a Hash list. Any ideas?Code:[Oxide] 12:51 PM [Error] Failed to call hook 'OnPlayerConnected' on plugin 'Ant Cheat v0.1.0' (NullReferenceException: Object reference not set to an instance f an object) [Oxide] 12:51 PM [Debug] at Oxide.Plugins.AntiCheat+PlayerInfo..ctor (.Player ession session) [0x00000] in <filename unknown>:0 at Oxide.Plugins.AntiCheat.RegisterPlayer (.PlayerSession session) [0x00000] n <filename unknown>:0 at Oxide.Plugins.AntiCheat.OnPlayerConnected (.PlayerSession session) [0x0000 ] in <filename unknown>:0 at Oxide.Plugins.AntiCheat.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (System.Reflection.MethodInfo meth d, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[ args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hookname, System.Object[ args) [0x00000] in <filename unknown>:0
Hook code:
Register player code:Code:private void OnPlayerConnected(PlayerSession session) { //Add the player to the list RegisterPlayer(session); }
PlayerInfo class:Code:private Hash<PlayerSession, PlayerInfo> player = new Hash<PlayerSession, PlayerInfo>();private void RegisterPlayer(PlayerSession session) { player.Add(session, new PlayerInfo(session)); }
Code:public class PlayerInfo { public PlayerSession session; public bool kick; public string reason; public Vector3 location; public long lastCheck; public PlayerInfo(PlayerSession session) { this.session = session; //Load the location location = session.WorldPlayerEntity.transform.position; } public void setKick(bool kick, string reason) { this.kick = kick; this.reason = reason; } }
Solved OnPlayerConnected failed
Discussion in 'Hurtworld Development' started by Incisus, Jan 12, 2016.
-
Depending if the user has been on the server recently or not it is possible that the WorldPlayerEntity is not yet set at the time that OnPlayerConnected runs which will cause an error on saving the location when you are creating the new PlayerInfo object.
-
Wulf Community Admin
-
[DOUBLEPOST=1452626473][/DOUBLEPOST]Reporting back, same outcome with OnPlayerSpawn. Is it possible the entity is created elsewhere?
Code:[Oxide] 2:18 PM [Error] Failed to call hook 'OnPlayerSpawn' on plugin 'AntiCheat v0.1.0' (NullReferenceException: Object reference not set to an instance of an object) [Oxide] 2:18 PM [Debug] at Oxide.Plugins.AntiCheat+PlayerInfo..ctor (.PlayerSe ssion session) [0x00000] in <filename unknown>:0 at Oxide.Plugins.AntiCheat.RegisterPlayer (.PlayerSession session) [0x00000] i n <filename unknown>:0 at Oxide.Plugins.AntiCheat.OnPlayerSpawn (.PlayerSession session) [0x00000] in <filename unknown>:0 at Oxide.Plugins.AntiCheat.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (System.Reflection.MethodInfo metho d, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hookname, System.Object[]
-
Wulf Community Admin
Looking at our hooks, OnPlayerInit should actually be right after the player's entity is linked to their session.
-
Code://Make sure the player entity is fully loaded, and that it's not an admin if (curPlayer.session.WorldPlayerEntity != null && !curPlayer.session.IsAdmin) { }
Code://Init player location if (curPlayer.location.x == 0 && curPlayer.location.y == 0 && curPlayer.location.z == 0) { curPlayer.location = curLocation; }