I followed the docs on storing and retrieving data, and it isn't working 100%. The data is stored everytime a player respawns. (It's set to respawn for easy testing)
This leaves me to thing that the playTimeData.Players.Contains(info) isn't being checked properly.
Could someone provide me with an idea as to what is wrong?
Code:using System.Collections.Generic; using Oxide.Core;namespace Oxide.Plugins { [Info("PlayTime", "AnExiledGod", 1.0)] [Description("Logs players play time and allows you to view the players play time with a command.")] public class PlayTime : RustPlugin { class PlayTimeData { public HashSet<PlayerInfo> Players = new HashSet<PlayerInfo>(); public PlayTimeData() { } } class PlayerInfo { public string SteamID; public string Name; public PlayerInfo() { } public PlayerInfo(BasePlayer player) { SteamID = player.userID.ToString(); Name = player.displayName; } } PlayTimeData playTimeData; private void Loaded() { playTimeData = Interface.GetMod().DataFileSystem.ReadObject<PlayTimeData>("PlayTime"); } void OnPlayerRespawned(BasePlayer player) { var info = new PlayerInfo(player); if (playTimeData.Players.Contains(info)) { PrintToChat(player, "Your data has already been added to the file."); } else { PrintToChat(player, "Saving your data to the file."); playTimeData.Players.Add(info); Interface.GetMod().DataFileSystem.WriteObject("PlayTime", playTimeData); } } void OnPlayerDisconnected(BasePlayer player) { Puts("OnPlayerDisconnected works!"); } } }
Solved C# - Storing and Retrieving Data
Discussion in 'Rust Development' started by AnExiledGod, Jun 20, 2015.
-
the PlayTimeData uses a HashSet<PlayerInfo> and contains checks against an object hash, so when you create a new instance of PlayerInfo the Contains method will never return true since the new object has a new hash...you can try it this way:
Code:using System.Collections.Generic; using Oxide.Core;namespace Oxide.Plugins { [Info("PlayTime", "AnExiledGod", 1.0)] [Description("Logs players play time and allows you to view the players play time with a command.")] public class PlayTime : RustPlugin { class PlayTimeData { public Dictionary<string, PlayerInfo> Players = new Dictionary<string, PlayerInfo>(); public PlayTimeData() { } } class PlayerInfo { public string SteamID; public string Name; public PlayerInfo() { } public PlayerInfo(BasePlayer player) { SteamID = player.userID.ToString(); Name = player.displayName; } } PlayTimeData playTimeData; private void Loaded() { playTimeData = Interface.GetMod().DataFileSystem.ReadObject<PlayTimeData>("PlayTime"); } void OnPlayerRespawned(BasePlayer player) { var info = new PlayerInfo(player); if (playTimeData.Players.ContainsKey(info.SteamID)) { PrintToChat(player, "Your data has already been added to the file."); } else { PrintToChat(player, "Saving your data to the file."); playTimeData.Players.Add(info.SteamID, info); Interface.GetMod().DataFileSystem.WriteObject("PlayTime", playTimeData); } } void OnPlayerDisconnected(BasePlayer player) { Puts("OnPlayerDisconnected works!"); } } }
-
[DOUBLEPOST=1434849481,1434831538][/DOUBLEPOST]I know I marked this as solved, but could you help me again?
The data that is stored is like this:
Code:{ "Players": { "76561198051290806": { "SteamID": "76561198051290806", "Name": "AnExiledGod", "LastLoginTime": 1434844747, "Played": 0 } } }
Code:using System.Collections.Generic; using Oxide.Core; using System; using System.Globalization;namespace Oxide.Plugins { [Info("PlayTime", "AnExiledGod", 1.0)] [Description("Logs players play time and allows you to view the players play time with a command.")] public class PlayTime : RustPlugin { class PlayTimeData { public Dictionary<string, PlayerInfo> Players = new Dictionary<string, PlayerInfo>(); public PlayTimeData() { } } class PlayerInfo { public string SteamID; public string Name; public long LastLoginTime; public int Played; public PlayerInfo() { } public PlayerInfo(BasePlayer player) { SteamID = player.userID.ToString(); Name = player.displayName; LastLoginTime = GrabCurrentTimestamp(); Played = 0; } } PlayTimeData playTimeData; private void Loaded() { playTimeData = Interface.GetMod().DataFileSystem.ReadObject<PlayTimeData>("PlayTime"); } void OnPlayerRespawned(BasePlayer player) { var info = new PlayerInfo(player); if (playTimeData.Players.ContainsKey(info.SteamID)) { Puts("Player already has a PlayTime log."); long lastLogin = playTimeData.Players[info.SteamID]["LastLoginTime"]; long totalPlayed = GrabCurrentTimestamp() - lastLogin; Puts("Total Played: " + totalPlayed); } else { Puts("Saving new player to PlayTime log."); playTimeData.Players.Add(info.SteamID, info); Interface.GetMod().DataFileSystem.WriteObject("PlayTime", playTimeData); } } void OnPlayerDisconnected(BasePlayer player) { Puts("OnPlayerDisconnected works!"); } private static long GrabCurrentTimestamp() { long timestamp = 0; long ticks = DateTime.UtcNow.Ticks - DateTime.Parse("01/01/1970 00:00:00").Ticks; ticks /= 10000000; timestamp = ticks; return timestamp; } } }
-
Try:
long lastLogin = playTimeData.Players[info.SteamID].LastLoginTime; -