mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Add TPS command and improve the calculation of TPS (#1210)
* Add tps command * Improve tps calculation and tps command * Remove debug log line * Improve if-else statement
This commit is contained in:
parent
4fb47f20c7
commit
236e077e44
3 changed files with 47 additions and 3 deletions
25
MinecraftClient/Commands/Tps.cs
Normal file
25
MinecraftClient/Commands/Tps.cs
Normal file
|
|
@ -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<string, object> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<double> tpsSamples = new List<double>(maxSamples);
|
||||
private double sampleSum = 0;
|
||||
|
||||
// players latency
|
||||
private Dictionary<string, int> playersLatency = new Dictionary<string, int>();
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -110,6 +110,7 @@
|
|||
<Compile Include="Commands\Set.cs" />
|
||||
<Compile Include="Commands\Health.cs" />
|
||||
<Compile Include="Commands\Sneak.cs" />
|
||||
<Compile Include="Commands\Tps.cs" />
|
||||
<Compile Include="Commands\Useblock.cs" />
|
||||
<Compile Include="Commands\UseItem.cs" />
|
||||
<Compile Include="INIFile.cs" />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue