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