|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.Objects;
|
| 22 | +import java.util.Vector; |
22 | 23 |
|
23 | 24 | /**
|
24 | 25 | * This is a complete JSON message builder class. To create a new JSONMessage do
|
|
28 | 29 | */
|
29 | 30 | @SuppressWarnings({"WeakerAccess", "unused"})
|
30 | 31 | public class JSONMessage {
|
31 |
| - |
32 | 32 | private static final BiMap<ChatColor, String> stylesToNames;
|
33 | 33 |
|
34 | 34 | static {
|
@@ -56,7 +56,9 @@ public class JSONMessage {
|
56 | 56 | stylesToNames = builder.build();
|
57 | 57 | }
|
58 | 58 |
|
| 59 | + |
59 | 60 | private final List<MessagePart> parts = new ArrayList<>();
|
| 61 | + private int centeringStartIndex = -1; |
60 | 62 |
|
61 | 63 | /**
|
62 | 64 | * Creates a new {@link JSONMessage} object
|
@@ -343,6 +345,54 @@ public JSONMessage newline() {
|
343 | 345 | return then("\n");
|
344 | 346 | }
|
345 | 347 |
|
| 348 | + /** |
| 349 | + * Sets the starting point to begin centering JSONMessages. |
| 350 | + * |
| 351 | + * @return This {@link JSONMessage} instance |
| 352 | + */ |
| 353 | + public JSONMessage beginCenter() { |
| 354 | + // Start with the NEXT message part. |
| 355 | + centeringStartIndex = parts.size(); |
| 356 | + return this; |
| 357 | + } |
| 358 | + |
| 359 | + /** |
| 360 | + * Ends the centering of the JSONMessage text. |
| 361 | + * |
| 362 | + * @return This {@link JSONMessage} instance |
| 363 | + */ |
| 364 | + public JSONMessage endCenter() { |
| 365 | + int current = centeringStartIndex; |
| 366 | + |
| 367 | + while (current < parts.size()) { |
| 368 | + Vector<MessagePart> currentLine = new Vector<>(); |
| 369 | + int totalLineLength = 0; |
| 370 | + |
| 371 | + for (; ; current++) { |
| 372 | + MessagePart part = current < parts.size() ? parts.get(current) : null; |
| 373 | + String raw = part == null ? null : ChatColor.stripColor(part.toLegacy()); |
| 374 | + |
| 375 | + if (current >= parts.size() || totalLineLength + raw.length() >= 53) { |
| 376 | + int padding = Math.max(0, (53 - totalLineLength) / 2); |
| 377 | + currentLine.firstElement().setText(Strings.repeat(" ", padding) + currentLine.firstElement().getText()); |
| 378 | + currentLine.lastElement().setText(currentLine.lastElement().getText() + "\n"); |
| 379 | + currentLine.clear(); |
| 380 | + break; |
| 381 | + } |
| 382 | + |
| 383 | + totalLineLength += raw.length(); |
| 384 | + currentLine.add(part); |
| 385 | + } |
| 386 | + } |
| 387 | + |
| 388 | + MessagePart last = parts.get(parts.size() - 1); |
| 389 | + last.setText(last.getText().substring(0, last.getText().length() - 1)); |
| 390 | + |
| 391 | + centeringStartIndex = -1; |
| 392 | + |
| 393 | + return this; |
| 394 | + } |
| 395 | + |
346 | 396 | ///////////////////////////
|
347 | 397 | // BEGIN UTILITY CLASSES //
|
348 | 398 | ///////////////////////////
|
|
0 commit comments