mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-20 20:22:28 +00:00
Tooltip event rewrite (#8038)
Redesigns the tooltip events, and adds a feature where mods can register factories for ClientTooltipComponents, which are used for custom rendering in tooltips. The old events are maintained for binary compatibility.
This commit is contained in:
1 parent
95be0d87d9
commit
6a25c99097
10 files changed
+574
-50
No files matched your search
@@ -0,0 +1,147 @@
|
||||
package net.minecraftforge.debug.client;
|
||||
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import com.mojang.datafixers.util.Either;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.GuiComponent;
|
||||
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
|
||||
import net.minecraft.client.renderer.entity.ItemRenderer;
|
||||
import net.minecraft.client.renderer.texture.TextureManager;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.TextComponent;
|
||||
import net.minecraft.world.inventory.tooltip.TooltipComponent;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.item.TooltipFlag;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraftforge.client.MinecraftForgeClient;
|
||||
import net.minecraftforge.client.event.RenderTooltipEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import net.minecraftforge.fml.loading.FMLEnvironment;
|
||||
import net.minecraftforge.fmllegacy.RegistryObject;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Mod(CustomTooltipTest.ID)
|
||||
public class CustomTooltipTest
|
||||
{
|
||||
|
||||
static final boolean ENABLED = true;
|
||||
static final String ID = "custom_tooltip_test";
|
||||
|
||||
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, ID);
|
||||
static final RegistryObject<Item> CUSTOM_ITEM = ITEMS.register("test_item", () -> new CustomItemWithTooltip(new Item.Properties()));
|
||||
|
||||
public CustomTooltipTest()
|
||||
{
|
||||
if (ENABLED) {
|
||||
if (FMLEnvironment.dist.isClient())
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.register(ClientEventHandler.class);
|
||||
FMLJavaModLoadingContext.get().getModEventBus().register(ClientModBusEventHandler.class);
|
||||
}
|
||||
ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
|
||||
}
|
||||
}
|
||||
|
||||
static record CustomTooltip(int color) implements TooltipComponent
|
||||
{
|
||||
}
|
||||
|
||||
record CustomClientTooltip(CustomTooltip tooltip) implements ClientTooltipComponent
|
||||
{
|
||||
|
||||
@Override
|
||||
public int getHeight()
|
||||
{
|
||||
return 10;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth(Font font)
|
||||
{
|
||||
return 10;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderImage(Font font, int x, int y, PoseStack poseStack, ItemRenderer itemRenderer_, int zIndex, TextureManager textureManager)
|
||||
{
|
||||
GuiComponent.fill(poseStack, x, y, x + 10, y+ 10, tooltip.color);
|
||||
}
|
||||
}
|
||||
|
||||
static class CustomItemWithTooltip extends Item
|
||||
{
|
||||
public CustomItemWithTooltip(Properties properties)
|
||||
{
|
||||
super(properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> components, TooltipFlag flag)
|
||||
{
|
||||
super.appendHoverText(stack, level, components, flag);
|
||||
components.add(new TextComponent("This is a very very very very very very long hover text that should really really be split across multiple lines.").withStyle(ChatFormatting.YELLOW));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<TooltipComponent> getTooltipImage(ItemStack stack)
|
||||
{
|
||||
return Optional.of(new CustomTooltip(0xFFFF0000));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class ClientModBusEventHandler
|
||||
{
|
||||
|
||||
@SubscribeEvent
|
||||
public static void clientSetup(FMLClientSetupEvent event)
|
||||
{
|
||||
MinecraftForgeClient.registerTooltipComponentFactory(CustomTooltip.class, CustomClientTooltip::new);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class ClientEventHandler
|
||||
{
|
||||
|
||||
@SubscribeEvent
|
||||
public static void gatherTooltips(RenderTooltipEvent.GatherComponents event)
|
||||
{
|
||||
if (event.getStack().getItem() == Items.STICK)
|
||||
{
|
||||
event.getTooltipElements().add(Either.right(new CustomTooltip(0xFF0000FF)));
|
||||
}
|
||||
if (event.getStack().getItem() == Items.CLOCK)
|
||||
{
|
||||
event.setMaxWidth(30);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void preTooltip(RenderTooltipEvent.Color event)
|
||||
{
|
||||
if (event.getStack().getItem() == Items.APPLE)
|
||||
{
|
||||
event.setBackgroundStart(0xFF0000FF);
|
||||
event.setBackgroundEnd(0xFFFFFF00);
|
||||
event.setBorderStart(0xFFFF0000);
|
||||
event.setBorderEnd(0xFFFF0011);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user