|
5 | 5 | import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; |
6 | 6 | import net.minestom.server.instance.block.Block; |
7 | 7 | import net.minestom.server.item.Material; |
| 8 | +import net.swofty.commons.statistics.ItemStatistic; |
8 | 9 |
|
9 | 10 | import java.math.RoundingMode; |
10 | 11 | import java.text.DecimalFormat; |
@@ -275,4 +276,91 @@ public static String ntify(int i) { |
275 | 276 | }; |
276 | 277 | }; |
277 | 278 | } |
| 279 | + |
| 280 | + public static String getFormatedStatistic(ItemStatistic statistic) { |
| 281 | + return statistic.getDisplayColor() + statistic.getSymbol() + " " + statistic.getDisplayName(); |
| 282 | + } |
| 283 | + |
| 284 | + /* |
| 285 | + public static List<String> centerLines(List<String> lines) { |
| 286 | + int maxLength = lines.stream() |
| 287 | + .map(StringUtility::stripColor) |
| 288 | + .mapToInt(String::length) |
| 289 | + .max() |
| 290 | + .orElse(0); |
| 291 | +
|
| 292 | + List<String> centered = new ArrayList<>(); |
| 293 | + for (String line : lines) { |
| 294 | + String stripped = stripColor(line); |
| 295 | + int padding = Math.max(0, (maxLength - stripped.length()) / 2); |
| 296 | + centered.add(" ".repeat(padding) + line); |
| 297 | + } |
| 298 | +
|
| 299 | + return centered; |
| 300 | + } |
| 301 | + */ |
| 302 | + |
| 303 | + //TODO fix this stupid centering |
| 304 | + public static List<String> centerLines(List<String> lines) { |
| 305 | + int CHAT_WIDTH_PIXELS = 320; |
| 306 | + int SPACE_WIDTH = 4; // Minecraft space width |
| 307 | + List<String> centered = new ArrayList<>(); |
| 308 | + |
| 309 | + for (String line : lines) { |
| 310 | + if (line.isEmpty()) { |
| 311 | + centered.add(line); |
| 312 | + continue; |
| 313 | + } |
| 314 | + |
| 315 | + boolean bold = false; |
| 316 | + int pixelWidth = 0; |
| 317 | + int formatCodeCount = 0; |
| 318 | + |
| 319 | + // Calculate visible width and count format codes |
| 320 | + for (int i = 0; i < line.length(); i++) { |
| 321 | + char c = line.charAt(i); |
| 322 | + if (c == '§' && i + 1 < line.length()) { |
| 323 | + char code = line.charAt(i + 1); |
| 324 | + if (code == 'l') bold = true; |
| 325 | + else if (code == 'r') bold = false; |
| 326 | + formatCodeCount += 2; |
| 327 | + i++; // Skip format code |
| 328 | + continue; |
| 329 | + } |
| 330 | + pixelWidth += getCharWidth(c, bold) + 1; // +1 for character spacing |
| 331 | + } |
| 332 | + |
| 333 | + // Subtract the last character's spacing |
| 334 | + if (pixelWidth > 0) pixelWidth--; |
| 335 | + |
| 336 | + // Calculate padding |
| 337 | + int padding = (CHAT_WIDTH_PIXELS - pixelWidth) / 2; |
| 338 | + int spaces = Math.max(0, padding / SPACE_WIDTH); |
| 339 | + |
| 340 | + // Add extra space if needed (Minecraft rendering quirk) |
| 341 | + if (padding % SPACE_WIDTH >= 2) { |
| 342 | + spaces++; |
| 343 | + } |
| 344 | + System.out.printf("Line (%.3f px): %s\n", (float)pixelWidth, line.replace("§", "&")); |
| 345 | + |
| 346 | + centered.add(" ".repeat(spaces) + line); |
| 347 | + } |
| 348 | + |
| 349 | + return centered; |
| 350 | + } |
| 351 | + |
| 352 | + private static int getCharWidth(char c, boolean bold) { |
| 353 | + // Special case for certain characters |
| 354 | + if (c == '➡') return 6; // Arrow character |
| 355 | + if (c == '✯') return 7; // Star character |
| 356 | + |
| 357 | + return switch (c) { |
| 358 | + case ' ', 'I', '[', ']', 't' -> 4; |
| 359 | + case '!', '.', ',', ':', ';', 'i', '|' -> 2; |
| 360 | + case '\'', '`', 'l' -> 3; |
| 361 | + case '*', 'f', 'k', '<', '>' -> 5; |
| 362 | + case '§' -> 0; |
| 363 | + default -> bold ? 6 : 5; |
| 364 | + }; |
| 365 | + } |
278 | 366 | } |
0 commit comments