GuiTooltipMixin.java (3426B)
1 package ambos.vanillafixes.mixin; 2 3 import org.spongepowered.asm.mixin.Mixin; 4 import org.spongepowered.asm.mixin.Shadow; 5 import org.spongepowered.asm.mixin.injection.At; 6 import org.spongepowered.asm.mixin.injection.Inject; 7 import org.spongepowered.asm.mixin.injection.Redirect; 8 import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; 9 10 import ambos.vanillafixes.VanillaFixes; 11 import net.minecraft.client.Minecraft; 12 import net.minecraft.client.gui.Gui; 13 import net.minecraft.client.gui.GuiTooltip; 14 import net.minecraft.core.item.ItemStack; 15 import net.minecraft.core.item.tool.ItemTool; 16 import net.minecraft.core.item.tool.ItemToolSword; 17 import net.minecraft.core.net.command.TextFormatting; 18 import net.minecraft.core.player.inventory.InventoryPlayer; 19 import net.minecraft.core.player.inventory.slot.Slot; 20 21 @Mixin(value = GuiTooltip.class, remap = false) 22 final class GuiTooltipMixin extends Gui { 23 @Shadow 24 Minecraft mc; 25 26 private Slot hoveredSlot; 27 28 @Inject(method = "getTooltipText(Lnet/minecraft/core/item/ItemStack;ZLnet/minecraft/core/player/inventory/slot/Slot;)Ljava/lang/String;", at = @At("HEAD")) 29 private void getSlot(ItemStack itemStack, boolean showDescription, Slot slot, CallbackInfoReturnable<Object> cir) { 30 if (VanillaFixes.IMPROVED_TOOLTIP) { 31 // Stores hovered inventory slot. 32 hoveredSlot = slot; 33 } 34 } 35 36 @Redirect(method = "getTooltipText(Lnet/minecraft/core/item/ItemStack;ZLnet/minecraft/core/player/inventory/slot/Slot;)Ljava/lang/String;", at = @At(value = "INVOKE", target = "Ljava/lang/StringBuilder;append(Ljava/lang/String;)Ljava/lang/StringBuilder;", ordinal = 0)) 37 private StringBuilder addStats(StringBuilder stringBuilder, String str) { 38 stringBuilder.append(str); 39 40 if (!VanillaFixes.IMPROVED_TOOLTIP) { 41 return stringBuilder; 42 } 43 44 if (mc.gameSettings.heldItemCountOverlay.value) { 45 // Gets player inventory. 46 InventoryPlayer inventory = mc.thePlayer.inventory; 47 48 if (inventory.getHeldItemStack() == null && hoveredSlot != null && hoveredSlot.hasStack() && 49 hoveredSlot.getStack().getItemName() != null) { // If the slot isn't empty... 50 if (hoveredSlot.getStack().getItem() instanceof ItemToolSword || 51 hoveredSlot.getStack().getItem() instanceof ItemTool) { // If it's a sword or a tool... 52 53 // Gets durability. 54 int toolDurability = hoveredSlot.getStack().getMaxDamage() - hoveredSlot.getStack() 55 .getItemDamageForDisplay(); 56 57 // Gets damage. 58 int toolDamage = hoveredSlot.getStack().getItem().getDamageVsEntity(null); 59 60 /* Formats durability display */ 61 String durabilityTooltip = "Durability: " + toolDurability + "/" + hoveredSlot 62 .getStack() 63 .getMaxDamage(); 64 stringBuilder.append('\n').append(TextFormatting.LIGHT_GRAY).append(durabilityTooltip); 65 66 /* Formats damage display */ 67 String damageTooltip = "+" + toolDamage + " Attack Damage"; 68 stringBuilder.append('\n').append(TextFormatting.LIGHT_GRAY).append(damageTooltip); 69 } 70 } 71 } 72 73 return stringBuilder; 74 } 75 }