using Newtonsoft.Json; using System.Text.Json.Serialization; namespace Lib_MCPlayerStats { public class PlayersStats { /// /// Load player Stats from file /// /// File path /// Remove all "minecraft:" /// public static Task LoadPlayerAsync(string file, bool rmc = true) { Player_Stats player = new(); if (!File.Exists(file)) { player.Username = "File not Exist"; return Task.FromResult(player); //If Deserialized object is null return empty stats object with ErrorMessage in Username } Stats_Internal? loaded; if (rmc) { loaded = JsonConvert.DeserializeObject(File.ReadAllText(file).Replace("minecraft:", "")); //Desearialize Stats file to internal object } else { loaded = JsonConvert.DeserializeObject(File.ReadAllText(file)); //Desearialize Stats file to internal object and remove all "minecraft:" } if (loaded == null) { player.Username = "Deserialized object is null"; return Task.FromResult(player); //If Deserialized object is null return empty stats object with ErrorMessage in Username } player.Username = GetNameByUUID(file.Split(@"/").Last().Replace("-", "").Replace(".json", "")); //Get Username by UUID //Move data from loaded stats to object with username player.picked_up = loaded.stats.picked_up; player.mined = loaded.stats.mined; player.crafted = loaded.stats.crafted; player.broken = loaded.stats.broken; player.killed = loaded.stats.killed; player.dropped = loaded.stats.dropped; player.custom = loaded.stats.custom; player.used = loaded.stats.used; player.killed_by = loaded.stats.killed_by; return Task.FromResult(player); //Return object with player Stats } /// /// Load all player stats from files in folder /// /// Folder path /// Remove all "minecraft:" /// public static async Task> LoadPlayersAsync(string folder, bool rmc) { List stats = new(); foreach (string file in Directory.GetFiles(folder)) //Search all .json files for load in selected directory { if (file.EndsWith(".json")) { stats.Add(await LoadPlayerAsync(file, rmc)); } } return stats; } /// /// Get Username from UUID by Mojang Api /// /// /// private static string GetNameByUUID(string UUID) { HttpClient client = new(); MojangApi? data = null; HttpResponseMessage response = client.GetAsync($"https://sessionserver.mojang.com/session/minecraft/profile/{UUID}").Result; if (response.IsSuccessStatusCode) { #pragma warning disable CS8604 data = JsonConvert.DeserializeObject(response.Content.ToString()); #pragma warning restore CS8604 } if (data == null) return UUID; #pragma warning disable CS8603 return data.name; #pragma warning restore CS8603 } } }