|
|
@@ -58,12 +58,12 @@ public final class LoreDesigner {
|
|
|
* @param color The base Color to use (<-- Not quite sure lol)
|
|
|
* @return A new List of {@link Component}
|
|
|
*/
|
|
|
- public static List<Component> createLore(String text, String widthReference, NamedTextColor color) {
|
|
|
- List<Component> lore = createLore(text, widthReference);
|
|
|
- for(Component component : lore) {
|
|
|
- component.color(color);
|
|
|
- }
|
|
|
- return lore;
|
|
|
+ public static List<Component> createLore(
|
|
|
+ String text,
|
|
|
+ String widthReference,
|
|
|
+ NamedTextColor color
|
|
|
+ ) {
|
|
|
+ return createLoreInternal(text, widthReference, color);
|
|
|
}
|
|
|
/**
|
|
|
* Create a List of {@link Component}s with additional Regex like {@literal <red> or <blue>} and the {@code defaultWidthReference}
|
|
|
@@ -75,6 +75,9 @@ public final class LoreDesigner {
|
|
|
return createLore(text, defaultWidthReference, color);
|
|
|
}
|
|
|
|
|
|
+ public static List<Component> createLore(String text, String widthReference) {
|
|
|
+ return createLoreInternal(text, widthReference, NamedTextColor.GRAY);
|
|
|
+ }
|
|
|
/**
|
|
|
* Create a List of {@link Component}s with additional Regex like {@literal <red> or <blue>} and the {@code defaultWidthReference}
|
|
|
* @param text The Text to display
|
|
|
@@ -89,82 +92,83 @@ public final class LoreDesigner {
|
|
|
* @param widthReference The Reference for how wide it should be
|
|
|
* @return A new List of {@link Component}
|
|
|
*/
|
|
|
- public static List<Component> createLore(String text, String widthReference) {
|
|
|
- int maxLength = widthReference.length();
|
|
|
- if (text == null || text.isBlank() || maxLength <= 0) {
|
|
|
+ private static List<Component> createLoreInternal(
|
|
|
+ String text,
|
|
|
+ String widthReference,
|
|
|
+ NamedTextColor defaultColor
|
|
|
+ ) {
|
|
|
+ if (text == null || text.isBlank()
|
|
|
+ || widthReference == null || widthReference.isEmpty()) {
|
|
|
return List.of();
|
|
|
}
|
|
|
|
|
|
+ int maxLength = widthReference.length();
|
|
|
List<Component> lore = new ArrayList<>();
|
|
|
|
|
|
- StyleState style = new StyleState();
|
|
|
- ComponentBuilder<TextComponent, TextComponent.Builder> lineBuilder = Component.text();
|
|
|
- int currentLength = 0;
|
|
|
+ StyleState style = new StyleState(defaultColor);
|
|
|
+ LineState line = new LineState(style);
|
|
|
|
|
|
String[] lines = text.split("<br>", -1);
|
|
|
|
|
|
for (int i = 0; i < lines.length; i++) {
|
|
|
+ String[] words = lines[i].split(" ");
|
|
|
|
|
|
- String[] wrapParts = lines[i].split(" ");
|
|
|
-
|
|
|
- for (int p = 0; p < wrapParts.length; p++) {
|
|
|
+ for (int p = 0; p < words.length; p++) {
|
|
|
+ ParsedWord parsed = parseWord(words[p], style);
|
|
|
|
|
|
- ParsedWord parsed = parseWord(wrapParts[p], style);
|
|
|
appendWord(
|
|
|
lore,
|
|
|
parsed,
|
|
|
maxLength,
|
|
|
+ p > 0,
|
|
|
style,
|
|
|
- Holder.of(lineBuilder, currentLength)
|
|
|
+ line
|
|
|
);
|
|
|
-
|
|
|
- currentLength = Holder.length;
|
|
|
- lineBuilder = Holder.builder;
|
|
|
-
|
|
|
- // add space between parts
|
|
|
- if (p < wrapParts.length - 1) {
|
|
|
- lineBuilder.append(Component.space());
|
|
|
- currentLength++;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
- currentLength = Holder.length;
|
|
|
- lineBuilder = Holder.builder;
|
|
|
-
|
|
|
- // force line break if <br> occurred
|
|
|
+ // A <br> forces a lore-line break, including for an empty line.
|
|
|
if (i < lines.length - 1) {
|
|
|
- lore.add(lineBuilder.build());
|
|
|
- lineBuilder = Component.text().style(style.toStyle());
|
|
|
- currentLength = 0;
|
|
|
+ lore.add(line.builder.build());
|
|
|
+ line.reset(style);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (currentLength > 0) {
|
|
|
- lore.add(lineBuilder.build());
|
|
|
+ if (line.length > 0) {
|
|
|
+ lore.add(line.builder.build());
|
|
|
}
|
|
|
|
|
|
return lore;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
// -------------------- Append Logic --------------------
|
|
|
|
|
|
private static void appendWord(
|
|
|
List<Component> lore,
|
|
|
ParsedWord parsed,
|
|
|
int maxLength,
|
|
|
+ boolean addSpace,
|
|
|
StyleState style,
|
|
|
- Holder holder
|
|
|
+ LineState line
|
|
|
) {
|
|
|
- int wordLength = parsed.visibleLength();
|
|
|
+ int spaceLength = addSpace && line.length > 0 ? 1 : 0;
|
|
|
+
|
|
|
+ if (line.length > 0
|
|
|
+ && line.length + spaceLength + parsed.visibleLength() > maxLength) {
|
|
|
+ lore.add(line.builder.build());
|
|
|
+ line.reset(style);
|
|
|
+ spaceLength = 0;
|
|
|
+ }
|
|
|
|
|
|
- if (holder.length > 0 && holder.length + 1 + wordLength > maxLength) {
|
|
|
- lore.add(holder.builder.build());
|
|
|
- holder.builder = Component.text().style(style.toStyle());
|
|
|
- holder.length = 0;
|
|
|
+ if (spaceLength > 0) {
|
|
|
+ line.builder.append(Component.text(" ", style.toStyle()));
|
|
|
+ line.length++;
|
|
|
}
|
|
|
|
|
|
- holder.builder.append(parsed.component());
|
|
|
- holder.length += wordLength;
|
|
|
+ if (parsed.visibleLength() > 0) {
|
|
|
+ line.builder.append(parsed.component());
|
|
|
+ line.length += parsed.visibleLength();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// -------------------- Parsing --------------------
|
|
|
@@ -272,9 +276,9 @@ public final class LoreDesigner {
|
|
|
private record ParsedWord(Component component, int visibleLength) {}
|
|
|
|
|
|
private static final class StyleState {
|
|
|
- private final NamedTextColor defaultColor = NamedTextColor.GRAY;
|
|
|
+ private final NamedTextColor defaultColor;
|
|
|
+ private NamedTextColor color;
|
|
|
|
|
|
- private NamedTextColor color = defaultColor;
|
|
|
private final EnumSet<TextDecoration> decorations = EnumSet.noneOf(TextDecoration.class);
|
|
|
|
|
|
void color(NamedTextColor color) {
|
|
|
@@ -287,6 +291,15 @@ public final class LoreDesigner {
|
|
|
decorations.add(decoration);
|
|
|
}
|
|
|
|
|
|
+ StyleState() {
|
|
|
+ this(NamedTextColor.GRAY);
|
|
|
+ }
|
|
|
+ StyleState(NamedTextColor defaultColor) {
|
|
|
+ this.defaultColor = defaultColor == null
|
|
|
+ ? NamedTextColor.GRAY
|
|
|
+ : defaultColor;
|
|
|
+ this.color = this.defaultColor;
|
|
|
+ }
|
|
|
void reset() {
|
|
|
color = defaultColor;
|
|
|
decorations.clear();
|
|
|
@@ -304,14 +317,17 @@ public final class LoreDesigner {
|
|
|
/**
|
|
|
* Mutable holder to avoid excessive object creation
|
|
|
*/
|
|
|
- private static final class Holder {
|
|
|
- static ComponentBuilder builder;
|
|
|
- static int length;
|
|
|
-
|
|
|
- static Holder of(ComponentBuilder b, int l) {
|
|
|
- builder = b;
|
|
|
- length = l;
|
|
|
- return null;
|
|
|
+ private static final class LineState {
|
|
|
+ private ComponentBuilder<TextComponent, TextComponent.Builder> builder;
|
|
|
+ private int length;
|
|
|
+
|
|
|
+ private LineState(StyleState style) {
|
|
|
+ reset(style);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void reset(StyleState style) {
|
|
|
+ builder = Component.text().style(style.toStyle());
|
|
|
+ length = 0;
|
|
|
}
|
|
|
}
|
|
|
}
|