Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 2.4 kB view raw
1package com.legacyminecraft.poseidon.commands; 2 3import com.legacyminecraft.poseidon.Poseidon; 4import org.bukkit.command.Command; 5import org.bukkit.command.CommandSender; 6import org.bukkit.command.defaults.VanillaCommand; 7 8import java.util.LinkedList; 9import java.util.LinkedHashMap; 10import java.util.Map; 11 12public class TPSCommand extends Command { 13 14 private final LinkedHashMap<String, Integer> intervals = new LinkedHashMap<>(); 15 16 public TPSCommand(String name) { 17 super(name); 18 this.description = "Shows the server's TPS for various intervals"; 19 this.usageMessage = "/tps"; 20 this.setPermission("poseidon.command.tps"); 21 22 // Define the intervals for TPS calculation 23 intervals.put("5s", 5); 24 intervals.put("30s", 30); 25 intervals.put("1m", 60); 26 intervals.put("5m", 300); 27 intervals.put("10m", 600); 28 intervals.put("15m", 900); 29 } 30 31 @Override 32 public boolean execute(CommandSender sender, String currentAlias, String[] args) { 33 if (!testPermission(sender)) return true; 34 35 LinkedList<Double> tpsRecords = Poseidon.getTpsRecords(); 36 StringBuilder message = new StringBuilder("§bServer TPS: "); 37 38 // Calculate and format TPS for each interval dynamically 39 for (Map.Entry<String, Integer> entry : intervals.entrySet()) { 40 double averageTps = calculateAverage(tpsRecords, entry.getValue()); 41 message.append(formatTps(averageTps)).append(" (").append(entry.getKey()).append("), "); 42 } 43 44 // Remove the trailing comma and space 45 if (message.length() > 0) { 46 message.setLength(message.length() - 2); 47 } 48 49 sender.sendMessage(message.toString()); 50 return true; 51 } 52 53 private double calculateAverage(LinkedList<Double> records, int seconds) { 54 int size = Math.min(records.size(), seconds); 55 if (size == 0) return 20.0; 56 57 double total = 0; 58 for (int i = 0; i < size; i++) { 59 total += records.get(i); 60 } 61 return total / size; 62 } 63 64 private String formatTps(double tps) { 65 String colorCode; 66 if (tps >= 19) { 67 colorCode = "§a"; 68 } else if (tps >= 15) { 69 colorCode = "§e"; 70 } else { 71 colorCode = "§c"; 72 } 73 return colorCode + String.format("%.2f", tps); 74 } 75}