Unicopia/src/main/java/com/minelittlepony/unicopia/client/TextHelper.java

30 lines
1.1 KiB
Java
Raw Normal View History

2021-12-24 17:35:12 +01:00
package com.minelittlepony.unicopia.client;
import java.util.Optional;
2024-02-12 20:36:43 +01:00
import java.util.stream.Collectors;
2021-12-24 17:35:12 +01:00
import java.util.stream.Stream;
2024-02-12 20:36:43 +01:00
import java.util.stream.StreamSupport;
2021-12-24 17:35:12 +01:00
import net.minecraft.client.MinecraftClient;
2022-06-25 00:19:55 +02:00
import net.minecraft.text.*;
2021-12-24 17:35:12 +01:00
2024-02-12 20:36:43 +01:00
public interface TextHelper {
2021-12-24 17:35:12 +01:00
static Stream<Text> wrap(Text text, int maxWidth) {
return MinecraftClient.getInstance().textRenderer.getTextHandler().wrapLines(text, maxWidth, Style.EMPTY).stream().map(line -> {
2022-06-25 00:19:55 +02:00
MutableText compiled = Text.literal("");
2021-12-24 17:35:12 +01:00
line.visit((s, t) -> {
2022-06-25 00:19:55 +02:00
compiled.append(Text.literal(t).setStyle(s));
2021-12-24 17:35:12 +01:00
return Optional.empty();
}, text.getStyle());
return compiled;
});
}
2024-02-12 20:36:43 +01:00
static Text join(Text delimiter, Iterable<? extends MutableText> elements) {
MutableText initial = Text.empty();
return StreamSupport.stream(elements.spliterator(), false).collect(Collectors.reducing(initial, (a, b) -> {
return a == initial ? b : a.append(delimiter).append(b);
}));
}
2021-12-24 17:35:12 +01:00
}