forked from yanggx98/Immersive-Tooltip
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better durability tooltip + make badges work in survival
i'm tiredddddd........
- Loading branch information
1 parent
4390236
commit 96987ea
Showing
7 changed files
with
1,802 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/dev/ultimatchamp/enhancedtooltips/component/DurabilityTooltipComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package dev.ultimatchamp.enhancedtooltips.component; | ||
|
||
import dev.ultimatchamp.enhancedtooltips.config.EnhancedTooltipsConfig; | ||
import dev.ultimatchamp.enhancedtooltips.util.BadgesUtils; | ||
import net.minecraft.client.font.TextRenderer; | ||
import net.minecraft.client.gui.DrawContext; | ||
import net.minecraft.client.gui.tooltip.TooltipComponent; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.text.Text; | ||
|
||
public class DurabilityTooltipComponent implements TooltipComponent { | ||
private static final int SPACING = 4; | ||
private static final int WIDTH = 80; | ||
private final ItemStack stack; | ||
|
||
public DurabilityTooltipComponent(ItemStack stack) { | ||
this.stack = stack; | ||
} | ||
|
||
@Override | ||
public int getHeight(/*? if >1.21.1 {*/TextRenderer textRenderer/*?}*/) { | ||
if (!stack.isDamageable() || EnhancedTooltipsConfig.load().durabilityTooltip.equals(EnhancedTooltipsConfig.DurabilityTooltipMode.OFF)) return 0; | ||
return 14; | ||
} | ||
|
||
@Override | ||
public int getWidth(TextRenderer textRenderer) { | ||
if (!stack.isDamageable() || EnhancedTooltipsConfig.load().durabilityTooltip.equals(EnhancedTooltipsConfig.DurabilityTooltipMode.OFF)) return 0; | ||
return textRenderer.getWidth(Text.translatable("enhancedtooltips.tooltip.durability")) + SPACING + WIDTH + 1; | ||
} | ||
|
||
@Override | ||
//? if >1.21.1 { | ||
public void drawItems(TextRenderer textRenderer, int x, int y, int width, int height, DrawContext context) { | ||
//?} else { | ||
/*public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) { | ||
*///?} | ||
if (!stack.isDamageable() || EnhancedTooltipsConfig.load().durabilityTooltip.equals(EnhancedTooltipsConfig.DurabilityTooltipMode.OFF)) return; | ||
|
||
y += 2; | ||
|
||
int textHeight = textRenderer.fontHeight; | ||
int textY = y - textRenderer.fontHeight + SPACING * 2 + 2; | ||
|
||
context.drawText( | ||
textRenderer, | ||
Text.translatable("enhancedtooltips.tooltip.durability"), | ||
x, | ||
textY, | ||
0xffffffff, | ||
true | ||
); | ||
|
||
x += textRenderer.getWidth(Text.translatable("enhancedtooltips.tooltip.durability")) + SPACING; | ||
|
||
var damaged = stack.getMaxDamage() - stack.getDamage(); | ||
|
||
context.fill( | ||
x, | ||
textY - SPACING / 2, | ||
x + (damaged * WIDTH) / stack.getMaxDamage(), | ||
textY + textHeight, | ||
0xff000000 | stack.getItemBarColor() | ||
); | ||
|
||
Text durabilityText = Text.empty(); | ||
if (EnhancedTooltipsConfig.load().durabilityTooltip == EnhancedTooltipsConfig.DurabilityTooltipMode.VALUE) { | ||
durabilityText = Text.literal(" " + damaged + " / " + stack.getMaxDamage()); | ||
} else if (EnhancedTooltipsConfig.load().durabilityTooltip == EnhancedTooltipsConfig.DurabilityTooltipMode.PERCENTAGE) { | ||
durabilityText = Text.literal(" " + damaged * 100 / stack.getMaxDamage() + "%"); | ||
} | ||
|
||
int textX = x + ((WIDTH - textRenderer.getWidth(durabilityText)) / 2); | ||
|
||
context.drawText( | ||
textRenderer, | ||
durabilityText, | ||
textX, | ||
textY, | ||
0xffffffff, | ||
true | ||
); | ||
|
||
BadgesUtils.drawFrame( | ||
context, | ||
x, | ||
textY - SPACING / 2, | ||
WIDTH, | ||
textHeight + SPACING, | ||
400, | ||
BadgesUtils.darkenColor(0xff000000 | stack.getItemBarColor(), 0.8f) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/dev/ultimatchamp/enhancedtooltips/util/BadgesUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dev.ultimatchamp.enhancedtooltips.util; | ||
|
||
import net.minecraft.client.gui.DrawContext; | ||
|
||
public class BadgesUtils { | ||
public static int darkenColor(int color, float factor) { | ||
int alpha = (color >> 24) & 0xFF; | ||
int red = (color >> 16) & 0xFF; | ||
int green = (color >> 8) & 0xFF; | ||
int blue = color & 0xFF; | ||
|
||
red = Math.max(0, (int)(red * factor)); | ||
green = Math.max(0, (int)(green * factor)); | ||
blue = Math.max(0, (int)(blue * factor)); | ||
|
||
return (alpha << 24) | (red << 16) | (green << 8) | blue; | ||
} | ||
|
||
public static void drawFrame(DrawContext context, int x, int y, int width, int height, int z, int color) { | ||
renderVerticalLine(context, x, y, height - 2, z, color); | ||
renderVerticalLine(context, x + width - 1, y, height - 2, z, color); | ||
renderHorizontalLine(context, x + 1, y - 1, width - 2, z, color); | ||
renderHorizontalLine(context, x + 1, y - 1 + height - 1, width - 2, z, color); | ||
} | ||
|
||
private static void renderVerticalLine(DrawContext context, int x, int y, int height, int z, int color) { | ||
context.fill(x, y, x + 1, y + height, z, color); | ||
} | ||
|
||
private static void renderHorizontalLine(DrawContext context, int x, int y, int width, int z, int color) { | ||
context.fill(x, y, x + width, y + 1, z, color); | ||
} | ||
} |
Oops, something went wrong.