use of net.minecraftforge.eventbus.api.Event 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();
}
use of net.minecraftforge.eventbus.api.Event in project SpongeCommon by SpongePowered.
the class ForgeEventManager method postDualBus.
// Implementation
private boolean postDualBus(final org.spongepowered.api.event.Event spongeEvent, final Collection<? extends Event> forgeEvents, final IEventBusInvokeDispatcher dispatcher) {
try (final NoExceptionClosable ignored = this.preparePost(spongeEvent)) {
final RegisteredListener.Cache listeners = this.getHandlerCache(spongeEvent);
final List<RegisteredListener<?>> beforeModifications = listeners.beforeModifications();
if (!beforeModifications.isEmpty()) {
// First, we fire the Sponge beforeModifications on the Sponge event
this.post(spongeEvent, beforeModifications);
// Then we sync to the Forge events
for (final Event forgeEvent : forgeEvents) {
((ForgeEventBridge_Forge) forgeEvent).bridge$syncFrom(spongeEvent);
}
}
// Then, we fire all our Forge events
for (final Event forgeEvent : forgeEvents) {
this.wrappedEventBus.post(forgeEvent, dispatcher);
// We must sync back the event's changes, if there are any.
// For complex events, this will be a partial sync.
((ForgeEventBridge_Forge) forgeEvent).bridge$syncTo(spongeEvent);
}
// and now we do our standard event listener stuff.
return this.post(spongeEvent, listeners.afterModifications());
}
}
Aggregations