Search in sources :

Example 6 with Event

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();
}
Also used : Camera(net.minecraft.client.Camera) Font(net.minecraft.client.gui.Font) Arrays(java.util.Arrays) Connection(net.minecraft.network.Connection) ForgeMod(net.minecraftforge.common.ForgeMod) Dist(net.minecraftforge.api.distmarker.Dist) net.minecraftforge.fml(net.minecraftforge.fml) Pair(org.apache.commons.lang3.tuple.Pair) MultiPlayerGameMode(net.minecraft.client.multiplayer.MultiPlayerGameMode) Map(java.util.Map) AbstractClientPlayer(net.minecraft.client.player.AbstractClientPlayer) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) TooltipComponent(net.minecraft.world.inventory.tooltip.TooltipComponent) WorldPreset(net.minecraft.client.gui.screens.worldselection.WorldPreset) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Window(com.mojang.blaze3d.platform.Window) ForgeVersion(net.minecraftforge.versions.forge.ForgeVersion) SoundInstance(net.minecraft.client.resources.sounds.SoundInstance) Screen(net.minecraft.client.gui.screens.Screen) ResourceManager(net.minecraft.server.packs.resources.ResourceManager) Set(java.util.Set) Language(net.minecraft.locale.Language) NetworkRegistry(net.minecraftforge.network.NetworkRegistry) TextComponent(net.minecraft.network.chat.TextComponent) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) FormattedText(net.minecraft.network.chat.FormattedText) FluidState(net.minecraft.world.level.material.FluidState) ForgeI18n(net.minecraftforge.common.ForgeI18n) ItemStack(net.minecraft.world.item.ItemStack) GameType(net.minecraft.world.level.GameType) ForgeRegistries(net.minecraftforge.registries.ForgeRegistries) BETA(net.minecraftforge.fml.VersionChecker.Status.BETA) Resource(net.minecraft.server.packs.resources.Resource) WorldGenSettings(net.minecraft.world.level.levelgen.WorldGenSettings) com.mojang.blaze3d.vertex(com.mojang.blaze3d.vertex) HumanoidArm(net.minecraft.world.entity.HumanoidArm) BlockState(net.minecraft.world.level.block.state.BlockState) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) Supplier(java.util.function.Supplier) Vector3f(com.mojang.math.Vector3f) ArrayList(java.util.ArrayList) I18n(net.minecraft.client.resources.language.I18n) MouseHandler(net.minecraft.client.MouseHandler) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance) ModelManager(net.minecraft.client.resources.model.ModelManager) TitleScreen(net.minecraft.client.gui.screens.TitleScreen) Marker(org.apache.logging.log4j.Marker) Nullable(javax.annotation.Nullable) TextureAtlasSprite(net.minecraft.client.renderer.texture.TextureAtlasSprite) BlockAndTintGetter(net.minecraft.world.level.BlockAndTintGetter) ServerData(net.minecraft.client.multiplayer.ServerData) BakedModel(net.minecraft.client.resources.model.BakedModel) Transformation(com.mojang.math.Transformation) ItemTransforms(net.minecraft.client.renderer.block.model.ItemTransforms) Component(net.minecraft.network.chat.Component) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) TransformationHelper(net.minecraftforge.common.model.TransformationHelper) IOException(java.io.IOException) NarratorChatListener(net.minecraft.client.gui.chat.NarratorChatListener) File(java.io.File) ItemRenderer(net.minecraft.client.renderer.entity.ItemRenderer) LerpingBossEvent(net.minecraft.client.gui.components.LerpingBossEvent) MinecraftForge(net.minecraftforge.common.MinecraftForge) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) RenderSystem(com.mojang.blaze3d.systems.RenderSystem) ClientTooltipComponent(net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent) InteractionHand(net.minecraft.world.InteractionHand) Material(net.minecraft.client.resources.model.Material) net.minecraftforge.client.event(net.minecraftforge.client.event) ResourceLocation(net.minecraft.resources.ResourceLocation) Either(com.mojang.datafixers.util.Either) LivingEntity(net.minecraft.world.entity.LivingEntity) TextureAtlas(net.minecraft.client.renderer.texture.TextureAtlas) Direction(net.minecraft.core.Direction) Matrix4f(com.mojang.math.Matrix4f) Random(java.util.Random) ServerStatus(net.minecraft.network.protocol.status.ServerStatus) ForgeTextureMetadata(net.minecraftforge.client.textures.ForgeTextureMetadata) GameData(net.minecraftforge.registries.GameData) ChatFormatting(net.minecraft.ChatFormatting) BETA_OUTDATED(net.minecraftforge.fml.VersionChecker.Status.BETA_OUTDATED) FogMode(net.minecraft.client.renderer.FogRenderer.FogMode) Event(net.minecraftforge.eventbus.api.Event) Input(net.minecraft.client.player.Input) ImmutableMap(com.google.common.collect.ImmutableMap) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Matrix3f(com.mojang.math.Matrix3f) Collectors(java.util.stream.Collectors) Player(net.minecraft.world.entity.player.Player) Objects(java.util.Objects) MarkerManager(org.apache.logging.log4j.MarkerManager) List(java.util.List) RecipeManager(net.minecraft.world.item.crafting.RecipeManager) BlockPos(net.minecraft.core.BlockPos) Optional(java.util.Optional) BOSSINFO(net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType.BOSSINFO) Level(net.minecraft.world.level.Level) net.minecraft.client.renderer(net.minecraft.client.renderer) PlayerInfo(net.minecraft.client.multiplayer.PlayerInfo) GuiComponent(net.minecraft.client.gui.GuiComponent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) SoundEngine(net.minecraft.client.sounds.SoundEngine) PlaySoundEvent(net.minecraftforge.client.event.sound.PlaySoundEvent) Function(java.util.function.Function) Stack(java.util.Stack) ItemColors(net.minecraft.client.color.item.ItemColors) BlockRenderDispatcher(net.minecraft.client.renderer.block.BlockRenderDispatcher) ParticleRenderType(net.minecraft.client.particle.ParticleRenderType) Minecraft(net.minecraft.client.Minecraft) JoinMultiplayerScreen(net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen) Fluid(net.minecraft.world.level.material.Fluid) Mod(net.minecraftforge.fml.common.Mod) HumanoidModel(net.minecraft.client.model.HumanoidModel) NativeImage(com.mojang.blaze3d.platform.NativeImage) Nonnull(javax.annotation.Nonnull) EntityHitResult(net.minecraft.world.phys.EntityHitResult) NetworkConstants(net.minecraftforge.network.NetworkConstants) LayerDefinition(net.minecraft.client.model.geom.builders.LayerDefinition) LocalPlayer(net.minecraft.client.player.LocalPlayer) ModelLayerLocation(net.minecraft.client.model.geom.ModelLayerLocation) HitResult(net.minecraft.world.phys.HitResult) Entity(net.minecraft.world.entity.Entity) Model(net.minecraft.client.model.Model) BlockColors(net.minecraft.client.color.block.BlockColors) KeyMapping(net.minecraft.client.KeyMapping) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) ForgeModelBakery(net.minecraftforge.client.model.ForgeModelBakery) FormattedText(net.minecraft.network.chat.FormattedText) ClientTooltipComponent(net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent) Font(net.minecraft.client.gui.Font) Function(java.util.function.Function) Either(com.mojang.datafixers.util.Either) TooltipComponent(net.minecraft.world.inventory.tooltip.TooltipComponent) ClientTooltipComponent(net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent) TooltipComponent(net.minecraft.world.inventory.tooltip.TooltipComponent) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) TextComponent(net.minecraft.network.chat.TextComponent) Component(net.minecraft.network.chat.Component) ClientTooltipComponent(net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent) GuiComponent(net.minecraft.client.gui.GuiComponent)

Example 7 with Event

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());
    }
}
Also used : Event(net.minecraftforge.eventbus.api.Event) GenericEvent(net.minecraftforge.eventbus.api.GenericEvent) RegisteredListener(org.spongepowered.common.event.manager.RegisteredListener) ForgeEventBridge_Forge(org.spongepowered.forge.launch.bridge.event.ForgeEventBridge_Forge)

Aggregations

Event (net.minecraftforge.eventbus.api.Event)7 LerpingBossEvent (net.minecraft.client.gui.components.LerpingBossEvent)6 PlaySoundEvent (net.minecraftforge.client.event.sound.PlaySoundEvent)6 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)6 Window (com.mojang.blaze3d.platform.Window)3 ImmutableMap (com.google.common.collect.ImmutableMap)1 NativeImage (com.mojang.blaze3d.platform.NativeImage)1 RenderSystem (com.mojang.blaze3d.systems.RenderSystem)1 com.mojang.blaze3d.vertex (com.mojang.blaze3d.vertex)1 Either (com.mojang.datafixers.util.Either)1 Matrix3f (com.mojang.math.Matrix3f)1 Matrix4f (com.mojang.math.Matrix4f)1 Transformation (com.mojang.math.Transformation)1 Vector3f (com.mojang.math.Vector3f)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1