Skip to content

Commit

Permalink
refactor: loop to check for item supplier instead
Browse files Browse the repository at this point in the history
  • Loading branch information
bconlon1 committed Sep 30, 2024
1 parent 72c7224 commit 40809b4
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import com.aetherteam.nitrogen.Nitrogen;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

@Mod.EventBusSubscriber(modid = Nitrogen.MODID)
public class TooltipListeners {
public static Map<ResourceLocation, TooltipPredicate> PREDICATES = new HashMap<>();
public static Map<Supplier<Item>, TooltipPredicate> PREDICATES = new HashMap<>();

@SubscribeEvent
public static void onTooltipCreationLowPriority(ItemTooltipEvent event) {
Expand All @@ -28,13 +28,19 @@ public static void onTooltipCreationLowPriority(ItemTooltipEvent event) {
}

public static void addAbilityTooltips(Player player, ItemStack stack, List<Component> components) {
TooltipPredicate predicate = null;
for (Map.Entry<Supplier<Item>, TooltipPredicate> entry : PREDICATES.entrySet()) {
if (entry.getKey().get() == stack.getItem()) {
predicate = entry.getValue();
break;
}
}
for (int i = 1; i <= 5; i++) {
String string = stack.getDescriptionId() + "." + Nitrogen.MODID + ".ability.tooltip." + i;
if (I18n.exists(string)) {
Component component = Component.translatable(string);
ResourceLocation location = ForgeRegistries.ITEMS.getKey(stack.getItem());
if (PREDICATES.containsKey(location)) {
component = PREDICATES.get(location).override(player, stack, components, component);
if (predicate != null) {
component = predicate.override(player, stack, components, component);
}
components.add(i, component);
}
Expand Down

0 comments on commit 40809b4

Please sign in to comment.