| 1 | /*
|
| 2 | * This file is part of the Combatant Client distribution.
|
| 3 | * Copyright (c) 2026 pivosos2007.
|
| 4 | *
|
| 5 | * Licensed under the GNU General Public License v3.0.
|
| 6 | */
|
| 7 |
|
| 8 | package combatant.client.mixins;
|
| 9 |
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.world.entity.player.Player;
|
| 12 | import net.minecraft.world.item.Item;
|
| 13 | import net.minecraft.world.item.ItemStack;
|
| 14 | import net.minecraft.world.item.TooltipFlag;
|
| 15 | import org.jetbrains.annotations.Nullable;
|
| 16 | import org.spongepowered.asm.mixin.Mixin;
|
| 17 | import org.spongepowered.asm.mixin.Unique;
|
| 18 | import org.spongepowered.asm.mixin.injection.At;
|
| 19 | import org.spongepowered.asm.mixin.injection.Inject;
|
| 20 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
| 21 | import combatant.client.features.gui.hud.nondraggable.impl.BetterTooltips;
|
| 22 | import combatant.client.util.item.IllegalItemUtil;
|
| 23 | import combatant.client.util.item.RarityColorUtil;
|
| 24 | import combatant.client.util.item.TopEnchantUtil;
|
| 25 |
|
| 26 | import java.util.ArrayList;
|
| 27 | import java.util.HashSet;
|
| 28 | import java.util.List;
|
| 29 | import java.util.Set;
|
| 30 |
|
| 31 | @Mixin(ItemStack.class)
|
| 32 | public class ItemStackMixin {
|
| 33 |
|
| 34 | @Unique
|
| 35 | private static Component recolor(Component text, int argb) {
|
| 36 | int rgb = argb & 0x00FFFFFF;
|
| 37 | return text.copy().setStyle(text.getStyle().withColor(rgb));
|
| 38 | }
|
| 39 |
|
| 40 | @Unique
|
| 41 | private static int argbFrom(float[] rgba) {
|
| 42 | if (rgba == null || rgba.length < 3) return 0xFFFFFFFF;
|
| 43 | float r = rgba[0], g = rgba[1], b = rgba[2];
|
| 44 | float a = rgba.length > 3 ? rgba[3] : 1f;
|
| 45 | int ai = (int) (a * 255) & 0xFF;
|
| 46 | int ri = (int) (r * 255) & 0xFF;
|
| 47 | int gi = (int) (g * 255) & 0xFF;
|
| 48 | int bi = (int) (b * 255) & 0xFF;
|
| 49 | return (ai << 24) | (ri << 16) | (gi << 8) | bi;
|
| 50 | }
|
| 51 |
|
| 52 | @Inject(method = "getTooltipLines", at = @At("RETURN"), cancellable = true)
|
| 53 | private void combatant$colorizeTooltip(Item.TooltipContext tooltipContext, @Nullable Player player, TooltipFlag type, CallbackInfoReturnable<List<Component>> cir) {
|
| 54 | ItemStack self = (ItemStack) (Object) this;
|
| 55 | // Item tooltip scheduling often reaches GuiGraphicsExtractor through the generic
|
| 56 | // lines+optional-image overload. Capture the producing stack independently from text
|
| 57 | // colorization so the central tooltip hook can still select the item renderer.
|
| 58 | BetterTooltips.setLastTooltipStack(self);
|
| 59 |
|
| 60 | BetterTooltips tooltips = BetterTooltips.get();
|
| 61 | if (tooltips == null || !tooltips.isItemInfoColorizeEnabled()) return;
|
| 62 |
|
| 63 | List<Component> original = cir.getReturnValue();
|
| 64 | if (original == null || original.isEmpty()) return;
|
| 65 |
|
| 66 | List<Component> modified = new ArrayList<>(original);
|
| 67 |
|
| 68 | int illegalColor = IllegalItemUtil.illegalColor();
|
| 69 | int topColor = TopEnchantUtil.topColor();
|
| 70 | int rarityColor = argbFrom(RarityColorUtil.INSTANCE.getRarityColor(self));
|
| 71 |
|
| 72 | Set<String> illegalLabels = new HashSet<>();
|
| 73 | Set<String> topLabels = new HashSet<>();
|
| 74 | IllegalItemUtil.collectEnchants(self, (label, overMax) -> {
|
| 75 | if (overMax) illegalLabels.add(label.trim());
|
| 76 | });
|
| 77 | TopEnchantUtil.collectTopEnchants(self, (label, isTop) -> {
|
| 78 | if (isTop) topLabels.add(label.trim());
|
| 79 | });
|
| 80 |
|
| 81 | boolean hasIllegal = !illegalLabels.isEmpty();
|
| 82 | boolean hasTop = !topLabels.isEmpty();
|
| 83 |
|
| 84 | if (!modified.isEmpty()) {
|
| 85 | int nameColor = hasIllegal ? illegalColor : (hasTop ? topColor : rarityColor);
|
| 86 | modified.set(0, recolor(modified.get(0), nameColor));
|
| 87 | }
|
| 88 |
|
| 89 | for (int i = 1; i < modified.size(); i++) {
|
| 90 | Component t = modified.get(i);
|
| 91 | String trimmed = t.getString().trim();
|
| 92 | if (illegalLabels.contains(trimmed)) {
|
| 93 | modified.set(i, recolor(t, illegalColor));
|
| 94 | continue;
|
| 95 | }
|
| 96 | if (topLabels.contains(trimmed)) {
|
| 97 | modified.set(i, recolor(t, topColor));
|
| 98 | continue;
|
| 99 | }
|
| 100 | if (hasIllegal && t.getStyle().getColor() == null) {
|
| 101 | modified.set(i, recolor(t, illegalColor));
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | cir.setReturnValue(modified);
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|