Hello,
I try to add a GameSession object to each player's logout that contains the login date, logout date and game time. Unfortunately after several tries I can't record a game session. I get an error I can't solve.
Here is the code that causes me problemsCode:Failed to call hook 'OnPlayerDisconnected' on plugin 'RaidSettings v0.1.0' (NullReferenceException: Object reference not set to an instance of an object) at Oxide.Plugins.RaidSettings.AddLastDisconnect (BasePlayer player) [0x0005c] in <7325edb6b67246efb2860f586d0e30b4>:0 at Oxide.Plugins.RaidSettings.OnPlayerDisconnected (BasePlayer player) [0x00000] in <7325edb6b67246efb2860f586d0e30b4>:0 at Oxide.Plugins.RaidSettings.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x0012e] in <7325edb6b67246efb2860f586d0e30b4>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00079] in <c42b530987394b2da63b2e4218904439>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x000d8] in <0f58cfbbba844942876db6036f28ac86>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00060] in <0f58cfbbba844942876db6036f28ac86>:0
The code that makes me see this error is this oneCode:private void OnPlayerDisconnected(BasePlayer player) { AddLastDisconnect(player); SaveData(); } private void AddLastDisconnect(BasePlayer player) { var dateTime = DateTime.Now; LastDisconnect lastDisconnectPlayer; if (!_lastDisconnect.TryGetValue(player.userID, out lastDisconnectPlayer)) { lastDisconnectPlayer = new LastDisconnect(player, dateTime); lastDisconnectPlayer.GameSessions.Add(new LastDisconnect.GameSession(player, dateTime)); _lastDisconnect.Add(player.userID, lastDisconnectPlayer); } else { lastDisconnectPlayer.GameSessions.Add(new LastDisconnect.GameSession(player, dateTime)); lastDisconnectPlayer.lastDisconnect = DateTime.Now; } }class LastDisconnect { public readonly ulong Userid; public long LastDisconnectLong; public List<GameSession> GameSessions; [JsonIgnore] public DateTime lastDisconnect { private get { return DateTime.FromBinary(LastDisconnectLong); } set { LastDisconnectLong = value.ToBinary(); } } [JsonConstructor] public LastDisconnect(ulong userid, long lastDisconnectLong) { Userid = userid; LastDisconnectLong = lastDisconnectLong; GameSessions = new List<GameSession>(); } public LastDisconnect(BasePlayer player, DateTime lastDisconnect) { Userid = player.userID; this.lastDisconnect = lastDisconnect; GameSessions = new List<GameSession>(); } [JsonIgnore] private BasePlayer Player => BasePlayer.FindByID(Userid); public bool IsConnected() { var player = Player; return player != null && player.IsConnected; } public bool IsOffline() { return HasMinutes(CooldownMinutes); } [JsonIgnore] private double Days { get { var ts = DateTime.Now - lastDisconnect; return ts.TotalDays; } } private bool HasDays(int days) { return Days >= days; } [JsonIgnore] private double Minutes { get { var ts = DateTime.Now - lastDisconnect; return ts.TotalMinutes; } } private bool HasMinutes(int minutes) { return Minutes >= minutes; } [JsonIgnore] private double Hours { get { var ts = DateTime.Now - lastDisconnect; return ts.TotalHours; } } private bool HasHours(int hours) { return Hours >= hours; } public class GameSession { public long TimePlayed; public long ConnectionLong; public long DeconnectionLong; [JsonIgnore] public TimeSpan timePlayed { private get { return TimeSpan.FromTicks(TimePlayed); } set { TimePlayed = value.Ticks; } } [JsonIgnore] public DateTime connection { private get { return DateTime.FromBinary(ConnectionLong); } set { ConnectionLong = value.ToBinary(); } } [JsonIgnore] public DateTime deconnection { private get { return DateTime.FromBinary(DeconnectionLong); } set { DeconnectionLong = value.ToBinary(); } } [JsonConstructor] public GameSession(long timePlayed, long connectionLong, long deconnectionLong) { TimePlayed = timePlayed; ConnectionLong = connectionLong; DeconnectionLong = deconnectionLong; } public GameSession(BasePlayer player, DateTime dateTime) { this.timePlayed = TimeSpan.FromSeconds(player.Connection.GetSecondsConnected()); this.connection = player.Connection.connectionTime; this.deconnection = dateTime; } } }
Thanks for your helpCode:lastDisconnectPlayer.GameSessions.Add(new LastDisconnect.GameSession(player, dateTime));
Add game session list to player data does not work
Discussion in 'Rust Development' started by Sorrow, Jul 16, 2018.