Skip to content

Commit

Permalink
feat: better durability tooltip + make badges work in survival
Browse files Browse the repository at this point in the history
i'm tiredddddd........
  • Loading branch information
UltimatChamp committed Jan 28, 2025
1 parent 4390236 commit 96987ea
Show file tree
Hide file tree
Showing 7 changed files with 1,802 additions and 101 deletions.
5 changes: 0 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ repositories {
}

loom {
runConfigs.all {
ideConfigGenerated true
runDir "../../run"
}

accessWidenerPath = file("../../src/main/resources/enhancedtooltips.accesswidener")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package dev.ultimatchamp.enhancedtooltips;

import dev.ultimatchamp.enhancedtooltips.component.ColorBorderComponent;
import dev.ultimatchamp.enhancedtooltips.component.EffectsTooltipComponent;
import dev.ultimatchamp.enhancedtooltips.component.HeaderTooltipComponent;
import dev.ultimatchamp.enhancedtooltips.component.ModelViewerComponent;
import dev.ultimatchamp.enhancedtooltips.config.EnhancedTooltipsConfig;
import dev.ultimatchamp.enhancedtooltips.component.*;
import dev.ultimatchamp.enhancedtooltips.kaleido.render.tooltip.TooltipModule;
import dev.ultimatchamp.enhancedtooltips.kaleido.render.tooltip.api.TooltipComparatorProvider;
import dev.ultimatchamp.enhancedtooltips.kaleido.render.tooltip.api.TooltipComponentAPI;
import dev.ultimatchamp.enhancedtooltips.kaleido.render.tooltip.api.TooltipDrawerProvider;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.EntityBucketItem;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -33,48 +27,25 @@ public void onInitializeClient() {
new TooltipModule().load();

TooltipComparatorProvider.setComparator(Comparator.comparingInt(EnhancedTooltips::getSerialNumber));
//? if >1.20.4 {
ItemTooltipCallback.EVENT.register((stack, tooltipContext, tooltipType, lines) -> {
if (stack.isDamageable() && !tooltipType.isAdvanced()) {
//?} else {
/*ItemTooltipCallback.EVENT.register((stack, tooltipContext, lines) -> {
if (stack.isDamageable() && !tooltipContext.isAdvanced()) {
*///?}
var damaged = stack.getMaxDamage() - stack.getDamage();
Text durabilityText = Text.empty();

if (EnhancedTooltipsConfig.load().durabilityTooltip == EnhancedTooltipsConfig.DurabilityTooltipMode.VALUE) {
durabilityText = Text.translatable("enhancedtooltips.tooltip.durability")
.append(Text.literal(" " + damaged + " / " + stack.getMaxDamage())
.setStyle(Style.EMPTY.withColor(stack.getItemBarColor()))
);
} else if (EnhancedTooltipsConfig.load().durabilityTooltip == EnhancedTooltipsConfig.DurabilityTooltipMode.PERCENTAGE) {
durabilityText = Text.translatable("enhancedtooltips.tooltip.durability")
.append(Text.literal(" " + damaged * 100 / stack.getMaxDamage() + "%")
.setStyle(Style.EMPTY.withColor(stack.getItemBarColor()))
);
}

if (!durabilityText.equals(Text.empty())) lines.add(durabilityText);
}
});

TooltipComponentAPI.EVENT.register((list, itemStack) -> {
TooltipComponentAPI.EVENT.register((list, stack) -> {
list.remove(0);
list.add(0, new HeaderTooltipComponent(itemStack));
list.add(0, new HeaderTooltipComponent(stack));

list.add(1, new EffectsTooltipComponent(itemStack));
list.add(1, new EffectsTooltipComponent(stack));

int color = TooltipHelper.borderColorProvider.getItemBorderColor(itemStack);
if (itemStack.getItem() instanceof ArmorItem) {
list.add(new ModelViewerComponent(itemStack, 0xff000000 | color));
} else if (itemStack.getItem() instanceof EntityBucketItem) {
list.add(new ModelViewerComponent(itemStack, 0xff000000 | color));
} else if (itemStack.getItem() instanceof SpawnEggItem) {
list.add(new ModelViewerComponent(itemStack, 0xff000000 | color));
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) {
list.add(new ModelViewerComponent(stack, 0xff000000 | color));
} else {
list.add(new ColorBorderComponent(0xff000000 | color));
}

if (!MinecraftClient.getInstance().options.advancedItemTooltips) list.add(new DurabilityTooltipComponent(stack));
});

TooltipDrawerProvider.setTooltipDrawerProvider(new EnhancedTooltipsDrawer());
Expand Down
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)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

import dev.ultimatchamp.enhancedtooltips.TooltipHelper;
import dev.ultimatchamp.enhancedtooltips.config.EnhancedTooltipsConfig;
import dev.ultimatchamp.enhancedtooltips.util.BadgesUtils;
import dev.ultimatchamp.enhancedtooltips.util.ItemGroupsUtils;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.item.*;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import net.minecraft.util.Pair;
import org.joml.Matrix4f;

import java.util.List;
import java.util.Map;

public class HeaderTooltipComponent implements TooltipComponent {
private static final int TEXTURE_SIZE = 20;
private static final int ITEM_MODEL_SIZE = 16;
Expand All @@ -34,6 +40,7 @@ public int getHeight(/*? if >1.21.1 {*/TextRenderer textRenderer/*?}*/) {
public int getWidth(TextRenderer textRenderer) {
int badgeWidth = 0;

//? if >1.20.6 {
if (EnhancedTooltipsConfig.load().itemBadges) {
for (var tab : ItemGroups.getGroups()) {
if (tab.equals(ItemGroups.getSearchGroup())) continue;
Expand All @@ -51,6 +58,7 @@ public int getWidth(TextRenderer textRenderer) {
if (identified) break;
}
}
//?}

return Math.max(textRenderer.getWidth(this.nameText) + badgeWidth, textRenderer.getWidth(this.rarityName)) + SPACING + TEXTURE_SIZE;
}
Expand Down Expand Up @@ -82,49 +90,26 @@ 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;

for (var tab : ItemGroups.getGroups()) {
if (tab.equals(ItemGroups.getSearchGroup())) continue;

var identified = false;

for (var displayStack : tab.getDisplayStacks()) {
if (stack.isOf(displayStack.getItem())) {
if (tab.contains(Items.NETHERITE_PICKAXE.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xff9b59b6, 0xff8e44ad); // Tools & Utilities
} else if (tab.contains(Items.NETHERITE_SWORD.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xfff94144, 0xffd72638); // Combat
} else if (tab.contains(Items.REPEATER.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xffff6b6b, 0xffe63939); // Redstone Blocks
} else if (tab.contains(Items.OAK_SIGN.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xff2a9d8f, 0xff207567); // Functional Blocks
} else if (tab.contains(Items.COOKED_BEEF.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xff61b748, 0xff7ad85b); // Food and Drinks
} else if (tab.contains(Items.GOLD_INGOT.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xffff6347, 0xffffa07a); // Ingredients
} else if (tab.contains(Items.ALLAY_SPAWN_EGG.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xffa29bfe, 0xff817dc4); // Spawn Eggs
} else if (tab.contains(Items.COMMAND_BLOCK.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xff9c89b8, 0xff7a6395); // Operator Utilities
} else if (tab.contains(Items.GRASS_BLOCK.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xff66bb6a, 0xffa5d6a7); // Natural Blocks
} else if (tab.contains(Items.BLUE_WOOL.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xff42a5f5, 0xff90caf9); // Colored Blocks
} else if (tab.contains(Items.IRON_BARS.getDefaultStack())) {
drawBadge(textRenderer, tab.getDisplayName(), x, y, context, 0xfff2c94c, 0xffe0a800); // Building Blocks
}
String translation = "gamerule.category.misc";
int fillColor = -6250336;

identified = true;
break;
}
for (Map.Entry<List<Item>, Pair<String, Integer>> entry : ItemGroupsUtils.getItemGroups().entrySet()) {
if (entry.getKey().contains(stack.getItem())) {
translation = entry.getValue().getLeft();
fillColor = entry.getValue().getRight();
break;
}

if (identified) break;
}

drawBadge(textRenderer, Text.translatable(translation), x, y, context, fillColor);
//?}
}

private void drawBadge(TextRenderer textRenderer, Text text, int x, int y, DrawContext context, int fillColor, int borderColor) {
//? if >1.20.6 {
private void drawBadge(TextRenderer textRenderer, Text text, int x, int y, DrawContext context, int fillColor) {
int textWidth = textRenderer.getWidth(text);
int textHeight = textRenderer.fontHeight;

Expand All @@ -148,29 +133,15 @@ private void drawBadge(TextRenderer textRenderer, Text text, int x, int y, DrawC
true
);

drawFrame(
BadgesUtils.drawFrame(
context,
textX - SPACING,
textY - SPACING / 2,
textWidth + SPACING * 2,
textHeight + SPACING,
400,
borderColor
BadgesUtils.darkenColor(fillColor, 0.8f)
);
}

private 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);
}
//?}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import java.nio.file.Path;

public class EnhancedTooltipsConfig {
//? if >1.20.6 {
@Comment("(default: true)")
public boolean itemBadges = true;
//?}

@Comment("(default: true)")
public boolean hungerTooltip = true;
Expand Down
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);
}
}
Loading

0 comments on commit 96987ea

Please sign in to comment.