97 lines
3.9 KiB
Java
97 lines
3.9 KiB
Java
package de.serverpulse.spigot.utils;
|
||
|
||
import net.md_5.bungee.api.ChatColor;
|
||
import org.bukkit.command.CommandSender;
|
||
|
||
import java.text.DecimalFormat;
|
||
import java.time.LocalDateTime;
|
||
import java.time.format.DateTimeFormatter;
|
||
|
||
public class MsgUtil {
|
||
|
||
private static final DecimalFormat DF2 = new DecimalFormat("##.##");
|
||
private static final DateTimeFormatter DT = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
|
||
|
||
// '─' (U+2500) ist in Minecraft's Standard-Schrift ca. 6px breit.
|
||
// Der Chat ist ~320px breit → ~53 Zeichen.
|
||
// Mit Prefix "[ServerPulse] " (~14 Zeichen) bleiben ~39 Zeichen übrig.
|
||
// In der Praxis wirkt die Zeile mit den Farbformatierungen etwas breiter.
|
||
// 34 ergibt hier eine saubere Breite ohne Umbruch.
|
||
private static final int LINE_LEN = 34;
|
||
private static final char LINE_CHAR = '─';
|
||
|
||
private MsgUtil() {}
|
||
|
||
// ── Farben ───────────────────────────────────────────────────────────────
|
||
|
||
public static String colorize(String s) {
|
||
return s == null ? "" : ChatColor.translateAlternateColorCodes('&', s);
|
||
}
|
||
|
||
public static void send(CommandSender sender, String msg) {
|
||
sender.sendMessage(colorize(msg));
|
||
}
|
||
|
||
// ── Zahlenformatierung ───────────────────────────────────────────────────
|
||
|
||
public static String formatTps(double tps) {
|
||
return colorize((tps >= 18 ? "&a" : tps >= 15 ? "&e" : "&c")
|
||
+ DF2.format(Math.min(20.0, tps)));
|
||
}
|
||
|
||
public static String formatMspt(double mspt) {
|
||
return colorize((mspt <= 40 ? "&a" : mspt <= 50 ? "&e" : "&c")
|
||
+ DF2.format(mspt) + "ms");
|
||
}
|
||
|
||
public static String formatRam(long used, long max) {
|
||
double pct = (double) used / max * 100;
|
||
return colorize((pct <= 75 ? "&a" : pct <= 90 ? "&e" : "&c")
|
||
+ used + "MB / " + max + "MB (" + (int) pct + "%)");
|
||
}
|
||
|
||
public static String formatCount(int val, int warn, int crit) {
|
||
return colorize((val >= crit ? "&c" : val >= warn ? "&e" : "&a") + val);
|
||
}
|
||
|
||
// ── Fortschrittsbalken ───────────────────────────────────────────────────
|
||
|
||
public static String bar(double val, double max, int len) {
|
||
int filled = (int) Math.min(Math.round((val / max) * len), len);
|
||
double pct = val / max * 100;
|
||
String col = pct <= 50 ? "&a" : pct <= 75 ? "&e" : "&c";
|
||
return colorize(col + "█".repeat(filled) + "&8" + "░".repeat(Math.max(0, len - filled)));
|
||
}
|
||
|
||
// ── Zeitstempel ──────────────────────────────────────────────────────────
|
||
|
||
public static String timestamp() {
|
||
return LocalDateTime.now().format(DT);
|
||
}
|
||
|
||
// ── Trennlinien ──────────────────────────────────────────────────────────
|
||
|
||
/**
|
||
* Einfache Trennlinie – passt zur Chat-Breite mit Prefix.
|
||
*/
|
||
public static String separator() {
|
||
return colorize("&8" + String.valueOf(LINE_CHAR).repeat(LINE_LEN));
|
||
}
|
||
|
||
/**
|
||
* Zentrierter, symmetrischer Header.
|
||
* Beispiel: ───── ServerPulse Status ─────
|
||
*/
|
||
public static String header(String title) {
|
||
int available = Math.max(6, LINE_LEN - title.length() - 2); // -2 für Leerzeichen
|
||
int side = Math.max(3, available / 2);
|
||
|
||
String l = String.valueOf(LINE_CHAR);
|
||
return colorize(
|
||
"&8" + l.repeat(side) +
|
||
" &b&l" + title +
|
||
" &8" + l.repeat(side)
|
||
);
|
||
}
|
||
}
|