diff --git a/MinecraftClient/Commands/Tps.cs b/MinecraftClient/Commands/Tps.cs new file mode 100644 index 00000000..0d70f759 --- /dev/null +++ b/MinecraftClient/Commands/Tps.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MinecraftClient.Commands +{ + class Tps : Command + { + public override string CMDName { get { return "tps"; } } + public override string CMDDesc { get { return "Display server current tps (tick per second). May not be accurate"; } } + + public override string Run(McClient handler, string command, Dictionary localVars) + { + var tps = Math.Round(handler.GetServerTPS(), 2); + string color; + if (tps < 10) + color = "§c"; // Red + else if (tps < 15) + color = "§e"; // Yellow + else color = "§a"; // Green + return "Current tps: " + color + tps; + } + } +} diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 0e5fbb35..6ea2d211 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -75,7 +75,14 @@ namespace MinecraftClient // server TPS private long lastAge = 0; private DateTime lastTime; - private Double serverTPS = 0; + private double serverTPS = 0; + private double averageTPS = 20; + private const int maxSamples = 5; + private List tpsSamples = new List(maxSamples); + private double sampleSum = 0; + + // players latency + private Dictionary playersLatency = new Dictionary(); public int GetServerPort() { return port; } public string GetServerHost() { return host; } @@ -84,7 +91,7 @@ namespace MinecraftClient public string GetSessionID() { return sessionid; } public Location GetCurrentLocation() { return location; } public World GetWorld() { return world; } - public Double GetServerTPS() { return serverTPS; } + public Double GetServerTPS() { return averageTPS; } public float GetHealth() { return playerHealth; } public int GetSaturation() { return playerFoodSaturation; } public int GetLevel() { return playerLevel; } @@ -1970,8 +1977,18 @@ namespace MinecraftClient Double tps = tickDiff / (currentTime - lastTime).TotalSeconds; lastAge = WorldAge; lastTime = currentTime; - if (tps <= 20.0 && tps >= 0.0 && serverTPS != tps) + if (tps <= 20 && tps > 0) { + // calculate average tps + if (tpsSamples.Count >= maxSamples) + { + // full + sampleSum -= tpsSamples[0]; + tpsSamples.RemoveAt(0); + } + tpsSamples.Add(tps); + sampleSum += tps; + averageTPS = sampleSum / tpsSamples.Count; serverTPS = tps; DispatchBotEvent(bot => bot.OnServerTpsUpdate(tps)); } @@ -2044,6 +2061,7 @@ namespace MinecraftClient if (onlinePlayers.ContainsKey(uuid)) { playerName = onlinePlayers[uuid]; + playersLatency[playerName] = latency; DispatchBotEvent(bot => bot.OnLatencyUpdate(playerName, uuid, latency)); } } diff --git a/MinecraftClient/MinecraftClient.csproj b/MinecraftClient/MinecraftClient.csproj index a866b7fb..86d98573 100644 --- a/MinecraftClient/MinecraftClient.csproj +++ b/MinecraftClient/MinecraftClient.csproj @@ -110,6 +110,7 @@ +