Skip to content

Commit f26d190

Browse files
committed
The very start of centered text support
1 parent 8c02355 commit f26d190

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
<groupId>me.rayzr522</groupId>
66
<artifactId>jsonmessage</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.2.0</version>
88
<name>JSONMessage</name>
99
<description>A modern implementation of Fanciful</description>
1010

1111
<properties>
1212
<java.version>1.8</java.version>
13-
<bukkit.version>LATEST</bukkit.version>
13+
<bukkit.version>1.8.8-R0.1-SNAPSHOT</bukkit.version>
1414
</properties>
1515

1616
<repositories>

src/main/java/me/rayzr522/jsonmessage/JSONMessage.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121
import java.util.Objects;
22+
import java.util.Vector;
2223

2324
/**
2425
* This is a complete JSON message builder class. To create a new JSONMessage do
@@ -28,7 +29,6 @@
2829
*/
2930
@SuppressWarnings({"WeakerAccess", "unused"})
3031
public class JSONMessage {
31-
3232
private static final BiMap<ChatColor, String> stylesToNames;
3333

3434
static {
@@ -56,7 +56,9 @@ public class JSONMessage {
5656
stylesToNames = builder.build();
5757
}
5858

59+
5960
private final List<MessagePart> parts = new ArrayList<>();
61+
private int centeringStartIndex = -1;
6062

6163
/**
6264
* Creates a new {@link JSONMessage} object
@@ -343,6 +345,54 @@ public JSONMessage newline() {
343345
return then("\n");
344346
}
345347

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+
346396
///////////////////////////
347397
// BEGIN UTILITY CLASSES //
348398
///////////////////////////

0 commit comments

Comments
 (0)