|
| 1 | +package com.euphony.better_client.client.renderer; |
| 2 | + |
| 3 | +import com.euphony.better_client.config.BetterClientConfig; |
| 4 | +import com.euphony.better_client.config.option.TradingHudPos; |
| 5 | +import com.euphony.better_client.utils.MerchantInfo; |
| 6 | +import net.minecraft.client.DeltaTracker; |
| 7 | +import net.minecraft.client.Minecraft; |
| 8 | +import net.minecraft.client.gui.Font; |
| 9 | +import net.minecraft.client.gui.GuiGraphics; |
| 10 | +import net.minecraft.resources.ResourceLocation; |
| 11 | +import net.minecraft.util.CommonColors; |
| 12 | +import net.minecraft.world.entity.player.Player; |
| 13 | +import net.minecraft.world.item.ItemStack; |
| 14 | +import net.minecraft.world.item.trading.MerchantOffer; |
| 15 | +import net.minecraft.world.item.trading.MerchantOffers; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +/** |
| 20 | + * Trading HUD renderer — draws up to 7 merchant offers on-screen while a tradable |
| 21 | + * villager / wandering trader is under the crosshair. |
| 22 | + */ |
| 23 | +public final class TradingHudRenderer { |
| 24 | + |
| 25 | + private static final int MAX_VISIBLE_OFFERS = 7; |
| 26 | + private static final int HUD_WIDTH = 90; |
| 27 | + private static final int OFFER_HEIGHT = 20; |
| 28 | + private static final int EDGE_MARGIN = 5; |
| 29 | + private static final int REAL_COST_EXTRA_SPACE = 15; |
| 30 | + |
| 31 | + private static final ResourceLocation TRADE_ARROW_OUT_OF_STOCK_SPRITE = |
| 32 | + ResourceLocation.withDefaultNamespace("container/villager/trade_arrow_out_of_stock"); |
| 33 | + private static final ResourceLocation TRADE_ARROW_SPRITE = |
| 34 | + ResourceLocation.withDefaultNamespace("container/villager/trade_arrow"); |
| 35 | + private static final ResourceLocation DISCOUNT_STRIKETHROUGH_SPRITE = |
| 36 | + ResourceLocation.withDefaultNamespace("container/villager/discount_strikethrough"); |
| 37 | + |
| 38 | + private TradingHudRenderer() {} |
| 39 | + |
| 40 | + public static void renderHud(GuiGraphics graphics, DeltaTracker deltaTracker) { |
| 41 | + BetterClientConfig config = BetterClientConfig.HANDLER.instance(); |
| 42 | + if (!config.enableTradingHud) return; |
| 43 | + |
| 44 | + MerchantInfo merchantInfo = MerchantInfo.INSTANCE; |
| 45 | + if (merchantInfo.getLastEntityId().isEmpty()) return; |
| 46 | + |
| 47 | + MerchantOffers offers = merchantInfo.getOffers(); |
| 48 | + if (offers.isEmpty()) return; |
| 49 | + |
| 50 | + Minecraft minecraft = Minecraft.getInstance(); |
| 51 | + Player player = minecraft.player; |
| 52 | + if (player == null) return; |
| 53 | + |
| 54 | + Font font = minecraft.font; |
| 55 | + List<String> enchantmentTexts = merchantInfo.getOfferEnchantmentTexts(); |
| 56 | + |
| 57 | + int screenWidth = minecraft.getWindow().getGuiScaledWidth(); |
| 58 | + int screenHeight = minecraft.getWindow().getGuiScaledHeight(); |
| 59 | + |
| 60 | + int visibleOffers = Math.min(offers.size(), MAX_VISIBLE_OFFERS); |
| 61 | + int hudHeight = visibleOffers * OFFER_HEIGHT; |
| 62 | + |
| 63 | + int i; |
| 64 | + int k; |
| 65 | + TradingHudPos pos = config.tradingHudPos; |
| 66 | + switch (pos) { |
| 67 | + case TOP_LEFT -> { |
| 68 | + i = 0; |
| 69 | + k = EDGE_MARGIN; |
| 70 | + } |
| 71 | + case TOP_RIGHT -> { |
| 72 | + i = screenWidth - HUD_WIDTH; |
| 73 | + k = EDGE_MARGIN; |
| 74 | + } |
| 75 | + case BOTTOM_LEFT -> { |
| 76 | + i = 0; |
| 77 | + k = screenHeight - hudHeight - EDGE_MARGIN; |
| 78 | + } |
| 79 | + case BOTTOM_RIGHT -> { |
| 80 | + i = screenWidth - HUD_WIDTH; |
| 81 | + k = screenHeight - hudHeight - EDGE_MARGIN; |
| 82 | + } |
| 83 | + default -> { |
| 84 | + i = config.tradingHudXOffset; |
| 85 | + k = EDGE_MARGIN + config.tradingHudYOffset; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + int costAStartX = i + EDGE_MARGIN + EDGE_MARGIN; |
| 90 | + int extraSpace = config.renderRealCostDirectly ? 0 : REAL_COST_EXTRA_SPACE; |
| 91 | + |
| 92 | + int rendered = 0; |
| 93 | + for (MerchantOffer offer : offers) { |
| 94 | + if (rendered >= MAX_VISIBLE_OFFERS) { |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + ItemStack baseCostA = offer.getBaseCostA(); |
| 99 | + ItemStack costA = offer.getCostA(); |
| 100 | + ItemStack costB = offer.getCostB(); |
| 101 | + ItemStack result = offer.getResult(); |
| 102 | + |
| 103 | + int rowY = k + 2; |
| 104 | + |
| 105 | + renderAndDecorateCostA(graphics, font, costA, baseCostA, costAStartX, rowY, config.renderRealCostDirectly); |
| 106 | + |
| 107 | + if (!costB.isEmpty()) { |
| 108 | + int costBX = i + EDGE_MARGIN + 25 + extraSpace; |
| 109 | + graphics.renderFakeItem(costB, costBX, rowY); |
| 110 | + graphics.renderItemDecorations(font, costB, costBX, rowY); |
| 111 | + } |
| 112 | + |
| 113 | + renderTradeArrow(graphics, offer, i, rowY); |
| 114 | + |
| 115 | + int resultX = i + EDGE_MARGIN + 58 + extraSpace; |
| 116 | + graphics.renderFakeItem(result, resultX, rowY); |
| 117 | + graphics.renderItemDecorations(font, result, resultX, rowY); |
| 118 | + |
| 119 | + if (rendered < enchantmentTexts.size()) { |
| 120 | + String enchantmentText = enchantmentTexts.get(rendered); |
| 121 | + if (!enchantmentText.isEmpty()) { |
| 122 | + graphics.drawString(font, enchantmentText, i + 85 + extraSpace, rowY + 3, CommonColors.WHITE); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + k += OFFER_HEIGHT; |
| 127 | + rendered++; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static void renderTradeArrow(GuiGraphics graphics, MerchantOffer offer, int posX, int posY) { |
| 132 | + int extraSpace = BetterClientConfig.HANDLER.instance().renderRealCostDirectly ? 0 : REAL_COST_EXTRA_SPACE; |
| 133 | + ResourceLocation sprite = offer.isOutOfStock() ? TRADE_ARROW_OUT_OF_STOCK_SPRITE : TRADE_ARROW_SPRITE; |
| 134 | + graphics.blitSprite(sprite, posX + EDGE_MARGIN + 25 + 20 + extraSpace, posY + 3, 10, 9); |
| 135 | + } |
| 136 | + |
| 137 | + private static void renderAndDecorateCostA( |
| 138 | + GuiGraphics graphics, |
| 139 | + Font font, |
| 140 | + ItemStack realCost, |
| 141 | + ItemStack baseCost, |
| 142 | + int x, |
| 143 | + int y, |
| 144 | + boolean renderRealCostDirectly) { |
| 145 | + graphics.renderFakeItem(realCost, x, y); |
| 146 | + if (baseCost.getCount() == realCost.getCount() || renderRealCostDirectly) { |
| 147 | + graphics.renderItemDecorations(font, realCost, x, y); |
| 148 | + } else { |
| 149 | + graphics.renderItemDecorations(font, baseCost, x, y, baseCost.getCount() == 1 ? "1" : null); |
| 150 | + graphics.renderItemDecorations(font, realCost, x + 14, y, realCost.getCount() == 1 ? "1" : null); |
| 151 | + graphics.blitSprite(DISCOUNT_STRIKETHROUGH_SPRITE, x + 7, y + 12, 9, 2); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments