package com.yolp900.itsjustacharm.common.tooltips;
import com.yolp900.itsjustacharm.api.IJCConstants;
import com.yolp900.itsjustacharm.reference.LibTexts;
import com.yolp900.itsjustacharm.util.NBTHelper;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
public class TooltipHandler {
@SubscribeEvent
@SideOnly(Side.CLIENT)
public void onRenderTooltips(ItemTooltipEvent event) {
EntityPlayer player = event.getEntityPlayer();
ItemStack stack = event.getItemStack();
List<String> tooltip = event.getToolTip();
if (stack == null) return;
handleITooltipHolders(player, stack, tooltip);
handleBaubleHolding(stack, tooltip);
}
private void handleITooltipHolders(EntityPlayer player, @Nonnull ItemStack stack, List<String> tooltip) {
List<String> tooltips = new ArrayList<>();
if (stack.getItem() instanceof ITooltipHolder) {
tooltips = ((ITooltipHolder) stack.getItem()).getTooltip(stack, player).getLinedFormattedText();
}
if (stack.getItem() instanceof ItemBlock && Block.getBlockFromItem(stack.getItem()) instanceof ITooltipHolder) {
tooltips = ((ITooltipHolder) Block.getBlockFromItem(stack.getItem())).getTooltip(stack, player).getLinedFormattedText();
}
for (String string : tooltips) {
tooltip.add(string);
}
}
private void handleBaubleHolding(@Nonnull ItemStack stack, List<String> tooltip) {
if (NBTHelper.getBoolean(stack, IJCConstants.NBT.BAUBLE_HOLDER)) {
ItemStack ring0 = NBTHelper.getItemStack(stack, IJCConstants.NBT.BAUBLE_ATTACHED(0));
if (ring0 != null) {
tooltip.add(LibTexts.Tooltips.BaubleHolderRingStored.getFormattedText(ring0.getDisplayName()));
if (NBTHelper.getBoolean(stack, IJCConstants.NBT.ADVANCED_BAUBLE_HOLDER)) {
ItemStack ring1 = NBTHelper.getItemStack(stack, IJCConstants.NBT.BAUBLE_ATTACHED(1));
if (ring1 != null) {
tooltip.add(LibTexts.Tooltips.BaubleHolderRingStored.getFormattedText(ring1.getDisplayName()));
} else {
List<String> lines = LibTexts.Tooltips.BaubleHolderAdvancedNoExtraRing.getLinedFormattedText();
for (String line : lines) {
tooltip.add(line);
}
}
}
} else {
List<String> lines = LibTexts.Tooltips.BaubleHolderNoExtraBauble.getLinedFormattedText();
for (String line : lines) {
tooltip.add(line);
}
}
}
}
}