PlayerStats API Integration Guide
Setup
- Add a reference to
RestoreMonarchy.PlayerStats
in your project
- Import the API namespace:
using RestoreMonarchy.PlayerStats.APIs;
Available Methods
Get Player Rankings
// Get single player ranking (PvP or Zombie kills)
PlayerStatsAPI.GetPlayerRanking(steamId, pvp, (ranking) => {
// Handle the ranking data
Logger.Log($"Player Rank: {ranking.Rank}");
});
// Get top players ranking
PlayerStatsAPI.GetPlayerRankings(10, pvp, (rankings) => {
foreach (var rank in rankings) {
Logger.Log($"#{rank.Rank} - {rank.PlayerName}");
}
});
Get Player Stats
// Async method for offline players
PlayerStatsAPI.GetPlayerStats(steamId, (stats) => {
Logger.Log($"Kills: {stats.Kills}, Deaths: {stats.Deaths}");
});
// Sync method for online players
try {
PlayerStatsData stats = PlayerStatsAPI.GetPlayerStats(player);
Logger.Log($"Kills: {stats.Kills}, Deaths: {stats.Deaths}");
} catch (Exception ex) {
Logger.Log($"Failed to get player stats: {ex.Message}");
}
Exception Handling
The API may throw these exceptions:
Exception: "PlayerStats plugin is not loaded!"
- Ensure the PlayerStats plugin is loaded before making API calls
Exception: "PlayerStats component is not attached to the player!"
- When using GetPlayerStats(Player) on a player without the component
Best Practices
- Always check if the plugin is loaded before making API calls
- Use try-catch blocks when calling synchronous methods
- For offline players, prefer the async methods to avoid blocking the main thread
- Use the synchronous GetPlayerStats(Player) only for currently online players
Remember: Async methods use callbacks to return data, while the synchronous method returns data directly but only works for online players.