Skip to content

Commit

Permalink
feat: option to disable the durability bar
Browse files Browse the repository at this point in the history
* you can set various combos of durability texts and bars, as they have now been separated into 2 new options
* durability bar is now disabled by default
* some enhancements to the value-based durability text
* slight adjustments to the color of durability bar and item badges
* adjusted the priority of item badges
* the border for common rarity items is now the same as vanilla
* config items are now grouped
* some code formatting
  • Loading branch information
UltimatChamp committed Jan 30, 2025
1 parent 792daee commit dd626d9
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public void onInitializeClient() {
list.add(1, new EffectsTooltipComponent(stack));

int color = TooltipHelper.borderColorProvider.getItemBorderColor(stack);
if (stack.getItem() instanceof ArmorItem) {
list.add(new ModelViewerComponent(stack, 0xff000000 | color));
} else if (stack.getItem() instanceof EntityBucketItem) {
list.add(new ModelViewerComponent(stack, 0xff000000 | color));
} else if (stack.getItem() instanceof SpawnEggItem) {
if (stack.getItem() instanceof ArmorItem || stack.getItem() instanceof EntityBucketItem || stack.getItem() instanceof SpawnEggItem) {
list.add(new ModelViewerComponent(stack, 0xff000000 | color));
} else {
list.add(new ColorBorderComponent(0xff000000 | color));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,56 @@
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Style;
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;
private final EnhancedTooltipsConfig config;

public DurabilityTooltipComponent(ItemStack stack) {
this.stack = stack;
this.config = EnhancedTooltipsConfig.load();
}

public boolean isDurabilityDisabled() {
return !stack.isDamageable() || (config.durabilityTooltip.equals(EnhancedTooltipsConfig.DurabilityTooltipMode.OFF) && !config.durabilityBar);
}

private Text getDurabilityText() {
int damaged = stack.getMaxDamage() - stack.getDamage();
return switch (config.durabilityTooltip) {
case VALUE -> config.durabilityBar
? Text.literal(" " + damaged + " / " + stack.getMaxDamage())
: Text.literal(" ")
.append(Text.literal(String.valueOf(damaged)).setStyle(Style.EMPTY.withColor(stack.getItemBarColor())))
.append(Text.literal(" / ").setStyle(Style.EMPTY.withColor(-8355712)))
.append(Text.literal(String.valueOf(stack.getMaxDamage())).setStyle(Style.EMPTY.withColor(0xFF00FF00)));
case PERCENTAGE -> {
Text percentageText = Text.literal(" " + (damaged * 100 / stack.getMaxDamage()) + "%");
yield config.durabilityBar ? percentageText : percentageText.getWithStyle(Style.EMPTY.withColor(stack.getItemBarColor())).get(0);
}
default -> Text.empty();
};
}

@Override
public int getHeight(/*? if >1.21.1 {*/TextRenderer textRenderer/*?}*/) {
if (!stack.isDamageable() || EnhancedTooltipsConfig.load().durabilityTooltip.equals(EnhancedTooltipsConfig.DurabilityTooltipMode.OFF)) return 0;
return 14;
if (isDurabilityDisabled()) return 0;
return config.durabilityBar ? 14 : 9;
}

@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;
if (isDurabilityDisabled()) return 0;

int durabilityTextWidth = textRenderer.getWidth(Text.translatable("enhancedtooltips.tooltip.durability"));
if (config.durabilityBar) return durabilityTextWidth + SPACING + WIDTH + 1;

Text durability = getDurabilityText();
return durabilityTextWidth + textRenderer.getWidth(durability);
}

@Override
Expand All @@ -35,60 +64,41 @@ public void drawItems(TextRenderer textRenderer, int x, int y, int width, int he
//?} 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;
if (isDurabilityDisabled()) return;

if (config.durabilityBar) y += 2;
int textHeight = textRenderer.fontHeight;
int textY = y - textRenderer.fontHeight + SPACING * 2 + 2;
int textY = config.durabilityBar ? y - textHeight + SPACING * 2 + 2 : y;

context.drawText(
textRenderer,
Text.translatable("enhancedtooltips.tooltip.durability"),
x,
textY,
0xffffffff,
true
);
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 damaged = stack.getMaxDamage() - stack.getDamage();

if (config.durabilityBar)
context.fill(
x,
textY - SPACING / 2,
x + (damaged * WIDTH) / stack.getMaxDamage(),
textY + textHeight,
BadgesUtils.darkenColor(0xff000000 | stack.getItemBarColor(), 0.9f)
);

Text durabilityText = getDurabilityText();
if (!durabilityText.equals(Text.empty())) {
int textX = config.durabilityBar ? x + ((WIDTH - textRenderer.getWidth(durabilityText)) / 2) : x - SPACING;
context.drawText(textRenderer, durabilityText, textX, textY, 0xFFFFFFFF, true);
}

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)
);
if (config.durabilityBar)
BadgesUtils.drawFrame(
context,
x,
textY - SPACING / 2,
WIDTH,
textHeight + SPACING,
400,
BadgesUtils.darkenColor(0xff000000 | stack.getItemBarColor(), 0.8f)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@

public class EffectsTooltipComponent implements TooltipComponent {
private final ItemStack stack;
private final EnhancedTooltipsConfig config;

public EffectsTooltipComponent(ItemStack stack) {
this.stack = stack;
this.config = EnhancedTooltipsConfig.load();
}

//? if >1.21.1 {
Expand Down Expand Up @@ -103,10 +105,10 @@ public int getHeight(/*? if >1.21.1 {*/TextRenderer textRenderer/*?}*/) {
//?} else {
/*if (foodComponent != null) {
*///?}
if (EnhancedTooltipsConfig.load().hungerTooltip) height += 9 + 1;
if (EnhancedTooltipsConfig.load().saturationTooltip) height += 9 + 1;
if (config.hungerTooltip) height += 9 + 1;
if (config.saturationTooltip) height += 9 + 1;

if (EnhancedTooltipsConfig.load().effectsTooltip == EnhancedTooltipsConfig.EffectsTooltipMode.OFF) return height;
if (config.effectsTooltip == EnhancedTooltipsConfig.EffectsTooltipMode.OFF) return height;

//? if >1.21.1 {
for (ConsumeEffect entry : consumableComponent.onConsumeEffects()) {
Expand Down Expand Up @@ -140,14 +142,14 @@ public int getWidth(TextRenderer textRenderer) {
int hunger = getHunger();

int hungerLine = 0;
if (EnhancedTooltipsConfig.load().hungerTooltip) hungerLine = textRenderer.getWidth(Text.translatable(EnhancedTooltips.identifier("tooltip.hunger").toTranslationKey())) + 1 + ((textRenderer.fontHeight - 2) * hunger);
if (config.hungerTooltip) hungerLine = textRenderer.getWidth(Text.translatable(EnhancedTooltips.identifier("tooltip.hunger").toTranslationKey())) + 1 + ((textRenderer.fontHeight - 2) * hunger);

int saturationLine = 0;
if (EnhancedTooltipsConfig.load().saturationTooltip) saturationLine = textRenderer.getWidth(Text.translatable(EnhancedTooltips.identifier("tooltip.saturation").toTranslationKey(), "100%"));
if (config.saturationTooltip) saturationLine = textRenderer.getWidth(Text.translatable(EnhancedTooltips.identifier("tooltip.saturation").toTranslationKey(), "100%"));

foodWidth = Math.max(hungerLine, saturationLine);

if (EnhancedTooltipsConfig.load().effectsTooltip == EnhancedTooltipsConfig.EffectsTooltipMode.OFF) return foodWidth + 4;
if (config.effectsTooltip == EnhancedTooltipsConfig.EffectsTooltipMode.OFF) return foodWidth + 4;

if (foodComponent == null) return 0;

Expand Down Expand Up @@ -198,7 +200,7 @@ public void drawItems(TextRenderer textRenderer, int x, int y, int width, int he

var lineY = y;

if (EnhancedTooltipsConfig.load().hungerTooltip) {
if (config.hungerTooltip) {
context.drawText(textRenderer, hungerText, x, lineY, 0xffffffff, true);

Identifier fullHunger = Identifier.of("minecraft", "hud/food_full");
Expand Down Expand Up @@ -233,9 +235,9 @@ public void drawItems(TextRenderer textRenderer, int x, int y, int width, int he
lineY += textRenderer.fontHeight + 1;
}

if (EnhancedTooltipsConfig.load().saturationTooltip) context.drawText(textRenderer, saturationText, x, lineY, 0xff00ffff, true); else lineY -= textRenderer.fontHeight + 1;
if (config.saturationTooltip) context.drawText(textRenderer, saturationText, x, lineY, 0xff00ffff, true); else lineY -= textRenderer.fontHeight + 1;

if (EnhancedTooltipsConfig.load().effectsTooltip == EnhancedTooltipsConfig.EffectsTooltipMode.OFF) return;
if (config.effectsTooltip == EnhancedTooltipsConfig.EffectsTooltipMode.OFF) return;

//? if >1.21.1 {
for (ConsumeEffect entry : consumableComponent.onConsumeEffects()) {
Expand Down Expand Up @@ -267,7 +269,7 @@ public void drawItems(TextRenderer textRenderer, int x, int y, int width, int he

lineY += textRenderer.fontHeight + 1;

if (EnhancedTooltipsConfig.load().effectsTooltip == EnhancedTooltipsConfig.EffectsTooltipMode.WITH_ICONS) {
if (config.effectsTooltip == EnhancedTooltipsConfig.EffectsTooltipMode.WITH_ICONS) {
//? if >1.21.1 {
context.drawSpriteStretched(RenderLayer::getGuiTextured, effectTexture, x - 2, lineY, textRenderer.fontHeight, textRenderer.fontHeight);
//?} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ public class HeaderTooltipComponent implements TooltipComponent {
private final ItemStack stack;
private final OrderedText nameText;
private final OrderedText rarityName;
private final EnhancedTooltipsConfig config;

public HeaderTooltipComponent(ItemStack stack) {
this.stack = stack;
this.nameText = TooltipHelper.getDisplayName(stack).asOrderedText();
this.rarityName = TooltipHelper.getRarityName(stack).asOrderedText();
this.config = EnhancedTooltipsConfig.load();
}

@Override
Expand All @@ -40,7 +42,7 @@ public int getWidth(TextRenderer textRenderer) {
int badgeWidth = 0;

//? if >1.20.6 {
if (EnhancedTooltipsConfig.load().itemBadges) {
if (config.itemBadges) {
badgeWidth = textRenderer.getWidth(Text.translatable("gamerule.category.misc")) + SPACING * 2;

for (Map.Entry<List<Item>, Pair<String, Integer>> entry : ItemGroupsUtils.getItemGroups().entrySet()) {
Expand All @@ -65,7 +67,7 @@ public void drawText(TextRenderer textRenderer, int x, int y, Matrix4f matrix, V
float startDrawY = y + 1;
textRenderer.draw(this.nameText, startDrawX, startDrawY, -1, true, matrix, vertexConsumers, TextRenderer.TextLayerType.NORMAL, 0, 0xF000F0);

if (EnhancedTooltipsConfig.load().rarityTooltip) {
if (config.rarityTooltip) {
startDrawY += textRenderer.fontHeight + SPACING;
textRenderer.draw(this.rarityName, startDrawX, startDrawY, -1, true, matrix, vertexConsumers, TextRenderer.TextLayerType.NORMAL, 0, 0xF000F0);
}
Expand All @@ -83,7 +85,7 @@ public void drawItems(TextRenderer textRenderer, int x, int y, int width, int he
context.drawItem(this.stack, startDrawX, startDrawY);

//? if >1.20.6 {
if (!EnhancedTooltipsConfig.load().itemBadges) return;
if (!config.itemBadges) return;

String translation = "gamerule.category.misc";
int fillColor = -6250336;
Expand Down Expand Up @@ -113,7 +115,7 @@ private void drawBadge(TextRenderer textRenderer, Text text, int x, int y, DrawC
textY - SPACING / 2,
textX + textWidth + SPACING,
textY + textHeight,
fillColor
BadgesUtils.darkenColor(fillColor, 0.9f)
);

context.drawText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ public class ModelViewerComponent extends ColorBorderComponent {
private static final int SHADOW_LIGHT_COLOR = 15728880;

private final ItemStack stack;
private final EnhancedTooltipsConfig config;

public ModelViewerComponent(ItemStack stack, int color) {
super(color);
this.stack = stack;
this.config = EnhancedTooltipsConfig.load();
}

//? if >1.21.1 {
Expand All @@ -53,13 +55,13 @@ public void render(DrawContext context, int x, int y, int width, int height, int
currentRotation = (currentRotation + ROTATION_INCREMENT) % 360;

if (stack.getItem() instanceof ArmorItem) {
if (!EnhancedTooltipsConfig.load().armorTooltip) return;
if (!config.armorTooltip) return;
renderArmorStand(context, x, y, z);
} else if (stack.getItem() instanceof EntityBucketItem bucketItem) {
if (!EnhancedTooltipsConfig.load().bucketTooltip) return;
if (!config.bucketTooltip) return;
renderBucketEntity(context, x, y, z, bucketItem);
} else if (stack.getItem() instanceof SpawnEggItem spawnEggItem) {
if (!EnhancedTooltipsConfig.load().spawnEggTooltip) return;
if (!config.spawnEggTooltip) return;
renderSpawnEggEntity(context, x, y, z, spawnEggItem);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ private void renderVerticalLine(DrawContext context, int x, int y, int height, i
}

protected void renderVerticalLine(DrawContext context, int x, int y, int height, int z, int startColor, int endColor) {
if (startColor == -1 || startColor == 0xffffff) startColor = endColor;
context.fillGradient(x, y, x + 1, y + height, z, startColor, endColor);
}

protected void renderHorizontalLine(DrawContext context, int x, int y, int width, int z, int color) {
if (color == -1 || color == 0xffffff) color = 1347420415;
context.fill(x, y, x + width, y + 1, z, color);
}

Expand Down
Loading

0 comments on commit dd626d9

Please sign in to comment.