-
Notifications
You must be signed in to change notification settings - Fork 0
/
TooltipEventHandler.java
72 lines (59 loc) · 2.56 KB
/
TooltipEventHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package org.auioc.mcmod.arnicalib.client.event.handler;
import com.mojang.blaze3d.platform.InputConstants;
import org.auioc.mcmod.arnicalib.client.config.ClientConfig;
import org.lwjgl.glfw.GLFW;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
@OnlyIn(Dist.CLIENT)
public class TooltipEventHandler {
private static Minecraft mc = Minecraft.getInstance();
public static void handle(ItemTooltipEvent event) {
if (!ClientConfig.EnableAdvancedTooltip.get()) {
return;
}
ItemStack itemStack = event.getItemStack();
if (itemStack.isEmpty()) {
return;
}
if (itemStack.hasTag()) {
CompoundTag nbt = itemStack.getTag();
Component nbtTooltip = new TextComponent("NBT:").setStyle(Style.EMPTY.withColor(ChatFormatting.DARK_GRAY))
.append(new TextComponent("").setStyle(Style.EMPTY.withColor(ChatFormatting.WHITE)).append(NbtUtils.toPrettyComponent(nbt)));
addLine(event, nbtTooltip);
}
var tags = itemStack.getTags().toList();
if (tags.size() > 0) {
addLine(event, new TextComponent("Tags:").setStyle(Style.EMPTY.withColor(ChatFormatting.DARK_GRAY)));
for (TagKey<Item> tag : tags) {
addLine(event, new TextComponent(" " + tag.location()).setStyle(Style.EMPTY.withColor(ChatFormatting.DARK_GRAY)));
}
}
}
private static void addLine(ItemTooltipEvent event, Component tooltip) {
if (ClientConfig.AdvancedTooltipOnlyOnDebug.get() && !isDebugMode()) {
return;
}
if (ClientConfig.AdvancedTooltipOnlyOnShift.get() && !isShiftKeyDown()) {
return;
}
event.getToolTip().add(tooltip);
}
private static boolean isDebugMode() {
return mc.options.advancedItemTooltips;
}
private static boolean isShiftKeyDown() {
return InputConstants.isKeyDown(mc.getWindow().getWindow(), GLFW.GLFW_KEY_LEFT_SHIFT) ||
InputConstants.isKeyDown(mc.getWindow().getWindow(), GLFW.GLFW_KEY_RIGHT_SHIFT);
}
}