Skip to content

Commit

Permalink
feat: ability tooltip display system
Browse files Browse the repository at this point in the history
  • Loading branch information
bconlon1 committed Sep 29, 2024
1 parent e6961b4 commit e3c6096
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ build
# other
eclipse
run
runs
__pycache__
logs

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aetherteam.nitrogen.data.providers;

import com.aetherteam.nitrogen.Nitrogen;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceKey;
Expand All @@ -23,6 +24,14 @@ public NitrogenLanguageProvider(PackOutput output, String id) {
this.id = id;
}

public void addPerItemAbilityTooltip(Item item, int index, String name) {
this.add(item.getDescriptionId() + "." + Nitrogen.MODID + ".ability.tooltip." + index, name);
}

public void addPerItemAbilityTooltip(Item item, int index, String condition, String name) {
this.add(item.getDescriptionId() + "." + Nitrogen.MODID + ".ability.tooltip." + index + "." + condition, name);
}

public void addDiscDesc(Supplier<? extends Item> key, String name) {
this.add(key.get().getDescriptionId() + ".desc", name);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.aetherteam.nitrogen.event.listeners;

import com.aetherteam.nitrogen.Nitrogen;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.entity.player.ItemTooltipEvent;

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

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

@SubscribeEvent
public static void onTooltipCreationLowPriority(ItemTooltipEvent event) {
Player player = event.getEntity();
ItemStack itemStack = event.getItemStack();
List<Component> itemTooltips = event.getToolTip();
addAbilityTooltips(player, itemStack, itemTooltips);
}

public static void addAbilityTooltips(Player player, ItemStack stack, List<Component> components) {
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 = BuiltInRegistries.ITEM.getKey(stack.getItem());
if (PREDICATES.containsKey(location)) {
component = PREDICATES.get(location).override(player, stack, components, component);
}
components.add(i, component);
}
}
}

@FunctionalInterface
public interface TooltipPredicate {
Component override(Player player, ItemStack stack, List<Component> components, Component component);
}
}

0 comments on commit e3c6096

Please sign in to comment.