|
| 1 | +package com.aetherteam.nitrogen.event.listeners; |
| 2 | + |
| 3 | +import com.aetherteam.nitrogen.Nitrogen; |
| 4 | +import net.minecraft.client.resources.language.I18n; |
| 5 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 6 | +import net.minecraft.network.chat.Component; |
| 7 | +import net.minecraft.resources.ResourceLocation; |
| 8 | +import net.minecraft.world.entity.player.Player; |
| 9 | +import net.minecraft.world.item.ItemStack; |
| 10 | +import net.minecraftforge.event.entity.player.ItemTooltipEvent; |
| 11 | +import net.minecraftforge.eventbus.api.SubscribeEvent; |
| 12 | +import net.minecraftforge.fml.common.Mod; |
| 13 | + |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +@Mod.EventBusSubscriber(modid = Nitrogen.MODID) |
| 19 | +public class TooltipListeners { |
| 20 | + public static Map<ResourceLocation, TooltipPredicate> PREDICATES = new HashMap<>(); |
| 21 | + |
| 22 | + @SubscribeEvent |
| 23 | + public static void onTooltipCreationLowPriority(ItemTooltipEvent event) { |
| 24 | + Player player = event.getEntity(); |
| 25 | + ItemStack itemStack = event.getItemStack(); |
| 26 | + List<Component> itemTooltips = event.getToolTip(); |
| 27 | + addAbilityTooltips(player, itemStack, itemTooltips); |
| 28 | + } |
| 29 | + |
| 30 | + public static void addAbilityTooltips(Player player, ItemStack stack, List<Component> components) { |
| 31 | + for (int i = 1; i <= 5; i++) { |
| 32 | + String string = stack.getDescriptionId() + "." + Nitrogen.MODID + ".ability.tooltip." + i; |
| 33 | + if (I18n.exists(string)) { |
| 34 | + Component component = Component.translatable(string); |
| 35 | + ResourceLocation location = BuiltInRegistries.ITEM.getKey(stack.getItem()); |
| 36 | + if (PREDICATES.containsKey(location)) { |
| 37 | + component = PREDICATES.get(location).override(player, stack, components, component); |
| 38 | + } |
| 39 | + components.add(i, component); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @FunctionalInterface |
| 45 | + public interface TooltipPredicate { |
| 46 | + Component override(Player player, ItemStack stack, List<Component> components, Component component); |
| 47 | + } |
| 48 | +} |
0 commit comments