1. 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:

    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
    What I do with this hook is incredibly simplistic, I add the PlayerSession to a Hash list. Any ideas?

    Hook code:
    Code:
            private void OnPlayerConnected(PlayerSession session)
            {            //Add the player to the list
                RegisterPlayer(session);
            }
    Register player code:

    Code:
    private Hash<PlayerSession, PlayerInfo> player = new Hash<PlayerSession, PlayerInfo>();private void RegisterPlayer(PlayerSession session)
            {
                player.Add(session, new PlayerInfo(session));
            }
    PlayerInfo class:

    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;
                }
            }
     
    Last edited by a moderator: Jan 12, 2016
  2. 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.
     
  3. Wulf

    Wulf Community Admin

    It isn't, it would only be created in OnPlayerSpawn.
     
  4. I'm used to OnPlayerInit from Rust. I made the silly assumption that the entity would be made by the time OnPlayerConnected was invoked. I'll report back soon.
    [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[]
     
  5. Wulf

    Wulf Community Admin

    Hurtworld is a bit odd right now with the hook ordering, as there are a few references to players instead of just one.

    Looking at our hooks, OnPlayerInit should actually be right after the player's entity is linked to their session.

    b48bc81d8fc3a52cb6c07a0e73ccdc20.png
     
  6. I appreciate the follow up Wulf. I made my own work around, if I can avoid relying on hooks I do, saves me time in the future when devs move things around!

    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;
                }