70 lines
2.4 KiB
Java
70 lines
2.4 KiB
Java
package me.viper.teamplugin.util;
|
|
|
|
import java.time.Instant;
|
|
import java.time.ZoneId;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.Locale;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class Utils {
|
|
|
|
// Matches &#RRGGBB hex color codes
|
|
private static final Pattern HEX_PATTERN = Pattern.compile("&#([A-Fa-f0-9]{6})");
|
|
|
|
public static String formatIsoNow() {
|
|
return DateTimeFormatter.ISO_INSTANT
|
|
.withZone(ZoneId.systemDefault())
|
|
.format(Instant.now());
|
|
}
|
|
|
|
/**
|
|
* Formats an ISO timestamp into dd/MM/yyyy format.
|
|
*/
|
|
public static String prettifyIso(String iso) {
|
|
if (iso == null || iso.isEmpty()) return "—";
|
|
try {
|
|
Instant inst = Instant.parse(iso);
|
|
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy")
|
|
.withLocale(Locale.getDefault())
|
|
.withZone(ZoneId.systemDefault());
|
|
return fmt.format(inst);
|
|
} catch (Exception e) {
|
|
return iso; // return as-is if parsing fails
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Translates & color codes and &#RRGGBB hex codes into Minecraft format codes.
|
|
* Hex usage in config: &#FF5500 → §x§F§F§5§5§0§0
|
|
*/
|
|
public static String color(String s) {
|
|
if (s == null) return "";
|
|
// Process hex colors first: &#RRGGBB → §x§R§R§G§G§B§B
|
|
Matcher m = HEX_PATTERN.matcher(s);
|
|
StringBuffer sb = new StringBuffer();
|
|
while (m.find()) {
|
|
StringBuilder repl = new StringBuilder("§x");
|
|
for (char c : m.group(1).toCharArray()) {
|
|
repl.append('§').append(c);
|
|
}
|
|
m.appendReplacement(sb, repl.toString());
|
|
}
|
|
m.appendTail(sb);
|
|
// Then translate remaining & codes
|
|
return sb.toString().replace("&", "§");
|
|
}
|
|
|
|
/**
|
|
* Replaces placeholder pairs in a template string.
|
|
* e.g. replace(template, "%player%", "Steve", "%rank%", "Admin")
|
|
*/
|
|
public static String replace(String template, String... pairs) {
|
|
if (template == null) return "";
|
|
String out = template;
|
|
for (int i = 0; i + 1 < pairs.length; i += 2) {
|
|
out = out.replace(pairs[i], pairs[i + 1]);
|
|
}
|
|
return out;
|
|
}
|
|
} |