1. I'm getting JSON messages when trying to read the data on load.

    Code:
    Failed to initialize plugin 'ServerStats v0.0.1' (JsonSerializationException: Unable to find a constructor to use for type Oxide.Plugins.ServerStats+PlayerStats. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path '76561198298734142.Logins', line 3, position 13.)
    Json Example
    Code:
    {
      "76561198298734142": {
        "Logins": 1,
        "LastLogin": "2017/07/13 08:17:43"
      }
    }

    Code:
    class ServerStats : RustPlugin
        {
            Dictionary<ulong, PlayerStats> playerStats = new Dictionary<ulong, PlayerStats>();
           
            class PlayerStats
            {
                public int Logins;
                public string LastLogin;
               
                public PlayerStats()
                {
                }
                internal PlayerStats(ServerStats serverstats)
                {
                    Logins = 1;
                    LastLogin = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                }
            }
            void Loaded()
            {
                LoadData();
               
                foreach (BasePlayer player in BasePlayer.activePlayerList)
                {
                    if (!playerStats.ContainsKey(player.userID))
                    {
                        playerStats.Add(player.userID, new PlayerStats(this));                    SaveData();
                    }
                   
                }        }
           
            void LoadData()
            {
                playerStats = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<ulong, PlayerStats>>("ServerStats/PlayerStats");
            }
           
            void SaveData()
            {
                Interface.Oxide.DataFileSystem.WriteObject("ServerStats/PlayerStats", playerStats);
            }
           
        }
    I'm learning here and not sure why its not working, anyone able to point me in the right direction?
     
  2. Remove
    Code:
    ServerStats serverstats
    from your constructor. I assume that's why your getting that error.
     
  3. The following is just advice which I have comprised from working heavily with Oxide's data helpers. Firstly, I find it better to store a SteamID as a ulong inside the actual class then you can just make a list.
    Code:
    List<PlayerData> players = new List<PlayerData>();
    Then, you can add a simple method to get them such as the following. Note, if it doesn't exist it will be null.
    Code:
    PlayerData GetData(BasePlayer player) => players.FirstOrDefault(x => x.SteamID == player.userID);
    Alternatively, you could try adding it if it doesn't exist. I suppose your method may function the same but I believe mine is cleaner.

    Edit: Also, DateTime can be serialized.
     
  4. Doesn't matter what parameters are in the constructor, it shouldn't cause a issue for loading data

    Using a Dictionary and key to check if a value exists is faster then using Linq on a List

    @Dubz I don't have any issues loading or saving data using the code you provided above
     
  5. In my opinion using a list makes it cleaner, I don't think it's a noticeable impact.
     
  6. I disagree with your opinion :p

    Case: Checking if a item exists
    Code:
    // Dictionary
    if(dict.ContainsKey(key))
       return true;//List
    if (list.FirstOrDefault(x => x.var == someVar) != null)
       return true;
    
    Case: Finding a value
    Code:
    // Dictionary
    Value value = null;if (dict.ContainsKey(key)) // Second Fastest
    {
       value = dict[key];
       // Perform code
    }
    else
    {
       // Create new value
    }if (dict.TryGetValue(key, out value)) // Fastest
    {
       // Value found, perform code
    }
    else 
    {
       // Create new value
    }// List
    value = list.FirstOrDefault(x => x.var == someVar); // Slowest out of these options
    if (value != null)
    {
       // Perform code
    }
    else
    {
       // Create new value
    }
    
    Performance > Appearance