use of net.minecraft.network.chat.FormattedText in project MinecraftForge by MinecraftForge.
the class TextComponentMessageFormatHandler method handle.
public static int handle(final TranslatableComponent parent, final Consumer<FormattedText> addChild, final Object[] formatArgs, final String format) {
try {
final String formattedString = ForgeI18n.parseFormat(format, formatArgs);
// See MinecraftForge/MinecraftForge#7396
if (format.indexOf('\'') != -1) {
final boolean onlyMissingQuotes = format.chars().filter(ch -> formattedString.indexOf((char) ch) == -1).allMatch(ch -> ch == '\'');
if (onlyMissingQuotes) {
return 0;
}
}
TextComponent component = new TextComponent(formattedString);
component.getStyle().applyTo(parent.getStyle());
addChild.accept(component);
return format.length();
} catch (IllegalArgumentException ex) {
return 0;
}
}
use of net.minecraft.network.chat.FormattedText in project MinecraftForge by MinecraftForge.
the class ForgeHooksClient method gatherTooltipComponents.
public static List<ClientTooltipComponent> gatherTooltipComponents(ItemStack stack, List<? extends FormattedText> textElements, Optional<TooltipComponent> itemComponent, int mouseX, int screenWidth, int screenHeight, @Nullable Font forcedFont, Font fallbackFont) {
Font font = getTooltipFont(forcedFont, stack, fallbackFont);
List<Either<FormattedText, TooltipComponent>> elements = textElements.stream().map((Function<FormattedText, Either<FormattedText, TooltipComponent>>) Either::left).collect(Collectors.toCollection(ArrayList::new));
itemComponent.ifPresent(c -> elements.add(1, Either.right(c)));
var event = new RenderTooltipEvent.GatherComponents(stack, screenWidth, screenHeight, elements, -1);
MinecraftForge.EVENT_BUS.post(event);
if (event.isCanceled())
return List.of();
// text wrapping
int tooltipTextWidth = event.getTooltipElements().stream().mapToInt(either -> either.map(font::width, component -> 0)).max().orElse(0);
boolean needsWrap = false;
int tooltipX = mouseX + 12;
if (tooltipX + tooltipTextWidth + 4 > screenWidth) {
tooltipX = mouseX - 16 - tooltipTextWidth;
if (// if the tooltip doesn't fit on the screen
tooltipX < 4) {
if (mouseX > screenWidth / 2)
tooltipTextWidth = mouseX - 12 - 8;
else
tooltipTextWidth = screenWidth - 16 - mouseX;
needsWrap = true;
}
}
if (event.getMaxWidth() > 0 && tooltipTextWidth > event.getMaxWidth()) {
tooltipTextWidth = event.getMaxWidth();
needsWrap = true;
}
int tooltipTextWidthF = tooltipTextWidth;
if (needsWrap) {
return event.getTooltipElements().stream().flatMap(either -> either.map(text -> font.split(text, tooltipTextWidthF).stream().map(ClientTooltipComponent::create), component -> Stream.of(ClientTooltipComponent.create(component)))).toList();
}
return event.getTooltipElements().stream().map(either -> either.map(text -> ClientTooltipComponent.create(text instanceof Component ? ((Component) text).getVisualOrderText() : Language.getInstance().getVisualOrder(text)), ClientTooltipComponent::create)).toList();
}
Aggregations