| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.gui.components.Button;
|
| 8 | import net.minecraft.client.input.MouseButtonEvent;
|
| 9 | import net.minecraft.client.renderer.RenderPipelines;
|
| 10 | import net.minecraft.network.chat.CommonComponents;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.network.protocol.game.ServerboundSelectTradePacket;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.util.Mth;
|
| 15 | import net.minecraft.world.entity.npc.villager.VillagerData;
|
| 16 | import net.minecraft.world.entity.player.Inventory;
|
| 17 | import net.minecraft.world.inventory.MerchantMenu;
|
| 18 | import net.minecraft.world.item.ItemStack;
|
| 19 | import net.minecraft.world.item.trading.MerchantOffer;
|
| 20 | import net.minecraft.world.item.trading.MerchantOffers;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class MerchantScreen extends AbstractContainerScreen<MerchantMenu> {
|
| 24 | private static final Identifier OUT_OF_STOCK_SPRITE = Identifier.withDefaultNamespace("container/villager/out_of_stock");
|
| 25 | private static final Identifier EXPERIENCE_BAR_BACKGROUND_SPRITE = Identifier.withDefaultNamespace("container/villager/experience_bar_background");
|
| 26 | private static final Identifier EXPERIENCE_BAR_CURRENT_SPRITE = Identifier.withDefaultNamespace("container/villager/experience_bar_current");
|
| 27 | private static final Identifier EXPERIENCE_BAR_RESULT_SPRITE = Identifier.withDefaultNamespace("container/villager/experience_bar_result");
|
| 28 | private static final Identifier SCROLLER_SPRITE = Identifier.withDefaultNamespace("container/villager/scroller");
|
| 29 | private static final Identifier SCROLLER_DISABLED_SPRITE = Identifier.withDefaultNamespace("container/villager/scroller_disabled");
|
| 30 | private static final Identifier TRADE_ARROW_OUT_OF_STOCK_SPRITE = Identifier.withDefaultNamespace("container/villager/trade_arrow_out_of_stock");
|
| 31 | private static final Identifier TRADE_ARROW_SPRITE = Identifier.withDefaultNamespace("container/villager/trade_arrow");
|
| 32 | private static final Identifier DISCOUNT_STRIKETHRUOGH_SPRITE = Identifier.withDefaultNamespace("container/villager/discount_strikethrough");
|
| 33 | private static final Identifier VILLAGER_LOCATION = Identifier.withDefaultNamespace("textures/gui/container/villager.png");
|
| 34 | private static final int TEXTURE_WIDTH = 512;
|
| 35 | private static final int TEXTURE_HEIGHT = 256;
|
| 36 | private static final int MERCHANT_MENU_PART_X = 99;
|
| 37 | private static final int PROGRESS_BAR_X = 136;
|
| 38 | private static final int PROGRESS_BAR_Y = 16;
|
| 39 | private static final int SELL_ITEM_1_X = 5;
|
| 40 | private static final int SELL_ITEM_2_X = 35;
|
| 41 | private static final int BUY_ITEM_X = 68;
|
| 42 | private static final int LABEL_Y = 6;
|
| 43 | private static final int NUMBER_OF_OFFER_BUTTONS = 7;
|
| 44 | private static final int TRADE_BUTTON_X = 5;
|
| 45 | private static final int TRADE_BUTTON_HEIGHT = 20;
|
| 46 | private static final int TRADE_BUTTON_WIDTH = 88;
|
| 47 | private static final int SCROLLER_HEIGHT = 27;
|
| 48 | private static final int SCROLLER_WIDTH = 6;
|
| 49 | private static final int SCROLL_BAR_HEIGHT = 139;
|
| 50 | private static final int SCROLL_BAR_TOP_POS_Y = 18;
|
| 51 | private static final int SCROLL_BAR_START_X = 94;
|
| 52 | private static final Component TRADES_LABEL = Component.translatable("merchant.trades");
|
| 53 | private static final Component DEPRECATED_TOOLTIP = Component.translatable("merchant.deprecated");
|
| 54 | private int shopItem;
|
| 55 | private final MerchantScreen.TradeOfferButton[] tradeOfferButtons = new MerchantScreen.TradeOfferButton[7];
|
| 56 | private int scrollOff;
|
| 57 | private boolean isDragging;
|
| 58 |
|
| 59 | public MerchantScreen(final MerchantMenu menu, final Inventory inventory, final Component title) {
|
| 60 | super(menu, inventory, title, 276, 166);
|
| 61 | this.inventoryLabelX = 107;
|
| 62 | }
|
| 63 |
|
| 64 | private void postButtonClick() {
|
| 65 | this.menu.setSelectionHint(this.shopItem);
|
| 66 | this.menu.tryMoveItems(this.shopItem);
|
| 67 | this.minecraft.getConnection().send(new ServerboundSelectTradePacket(this.shopItem));
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected void init() {
|
| 72 | super.init();
|
| 73 | int xo = (this.width - this.imageWidth) / 2;
|
| 74 | int yo = (this.height - this.imageHeight) / 2;
|
| 75 | int buttonY = yo + 16 + 2;
|
| 76 |
|
| 77 | for (int i = 0; i < 7; i++) {
|
| 78 | this.tradeOfferButtons[i] = this.addRenderableWidget(new MerchantScreen.TradeOfferButton(xo + 5, buttonY, i, button -> {
|
| 79 | if (button instanceof MerchantScreen.TradeOfferButton tradeOfferButton) {
|
| 80 | this.shopItem = tradeOfferButton.getIndex() + this.scrollOff;
|
| 81 | this.postButtonClick();
|
| 82 | }
|
| 83 | }));
|
| 84 | buttonY += 20;
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected void extractLabels(final GuiGraphicsExtractor graphics, final int xm, final int ym) {
|
| 90 | int traderLevel = this.menu.getTraderLevel();
|
| 91 | if (traderLevel > 0 && traderLevel <= 5 && this.menu.showProgressBar()) {
|
| 92 | Component titleAndLevel = Component.translatable("merchant.title", this.title, Component.translatable("merchant.level." + traderLevel));
|
| 93 | int totalWidth = this.font.width(titleAndLevel);
|
| 94 | int startX = 49 + this.imageWidth / 2 - totalWidth / 2;
|
| 95 | graphics.text(this.font, titleAndLevel, startX, 6, -12566464, false);
|
| 96 | } else {
|
| 97 | graphics.text(this.font, this.title, 49 + this.imageWidth / 2 - this.font.width(this.title) / 2, 6, -12566464, false);
|
| 98 | }
|
| 99 |
|
| 100 | graphics.text(this.font, this.playerInventoryTitle, this.inventoryLabelX, this.inventoryLabelY, -12566464, false);
|
| 101 | int textWidth = this.font.width(TRADES_LABEL);
|
| 102 | graphics.text(this.font, TRADES_LABEL, 5 - textWidth / 2 + 48, 6, -12566464, false);
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 107 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 108 | int xo = (this.width - this.imageWidth) / 2;
|
| 109 | int yo = (this.height - this.imageHeight) / 2;
|
| 110 | graphics.blit(RenderPipelines.GUI_TEXTURED, VILLAGER_LOCATION, xo, yo, 0.0F, 0.0F, this.imageWidth, this.imageHeight, 512, 256);
|
| 111 | MerchantOffers offers = this.menu.getOffers();
|
| 112 | if (!offers.isEmpty()) {
|
| 113 | int itemIndex = this.shopItem;
|
| 114 | if (itemIndex < 0 || itemIndex >= offers.size()) {
|
| 115 | return;
|
| 116 | }
|
| 117 |
|
| 118 | MerchantOffer offer = offers.get(itemIndex);
|
| 119 | if (offer.isOutOfStock()) {
|
| 120 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, OUT_OF_STOCK_SPRITE, this.leftPos + 83 + 99, this.topPos + 35, 28, 21);
|
| 121 | }
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | private void extractProgressBar(final GuiGraphicsExtractor graphics, final int xo, final int yo, final MerchantOffer offer) {
|
| 126 | int traderLevel = this.menu.getTraderLevel();
|
| 127 | int traderXp = this.menu.getTraderXp();
|
| 128 | if (traderLevel < 5) {
|
| 129 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, EXPERIENCE_BAR_BACKGROUND_SPRITE, xo + 136, yo + 16, 102, 5);
|
| 130 | int minXp = VillagerData.getMinXpPerLevel(traderLevel);
|
| 131 | if (traderXp >= minXp && VillagerData.canLevelUp(traderLevel)) {
|
| 132 | int progressLength = 102;
|
| 133 | float multiplier = 102.0F / (VillagerData.getMaxXpPerLevel(traderLevel) - minXp);
|
| 134 | int w = Math.min(Mth.floor(multiplier * (traderXp - minXp)), 102);
|
| 135 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, EXPERIENCE_BAR_CURRENT_SPRITE, 102, 5, 0, 0, xo + 136, yo + 16, w, 5);
|
| 136 | int futureXp = this.menu.getFutureTraderXp();
|
| 137 | if (futureXp > 0) {
|
| 138 | int futureXpWidth = Math.min(Mth.floor(futureXp * multiplier), 102 - w);
|
| 139 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, EXPERIENCE_BAR_RESULT_SPRITE, 102, 5, w, 0, xo + 136 + w, yo + 16, futureXpWidth, 5);
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | private void extractScroller(final GuiGraphicsExtractor graphics, final int xo, final int yo, final int mouseX, final int mouseY, final MerchantOffers offers) {
|
| 146 | int steps = offers.size() + 1 - 7;
|
| 147 | if (steps > 1) {
|
| 148 | int leftOver = 139 - (27 + (steps - 1) * 139 / steps);
|
| 149 | int stepHeight = 1 + leftOver / steps + 139 / steps;
|
| 150 | int maxScrollerOff = 113;
|
| 151 | int scrollerYOff = Math.min(113, this.scrollOff * stepHeight);
|
| 152 | if (this.scrollOff == steps - 1) {
|
| 153 | scrollerYOff = 113;
|
| 154 | }
|
| 155 |
|
| 156 | int scrollerX = xo + 94;
|
| 157 | int scrollerY = yo + 18 + scrollerYOff;
|
| 158 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SCROLLER_SPRITE, scrollerX, scrollerY, 6, 27);
|
| 159 | if (mouseX >= scrollerX && mouseX < xo + 94 + 6 && mouseY >= scrollerY && mouseY <= scrollerY + 27) {
|
| 160 | graphics.requestCursor(this.isDragging ? CursorTypes.RESIZE_NS : CursorTypes.POINTING_HAND);
|
| 161 | }
|
| 162 | } else {
|
| 163 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SCROLLER_DISABLED_SPRITE, xo + 94, yo + 18, 6, 27);
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 169 | super.extractContents(graphics, mouseX, mouseY, a);
|
| 170 | MerchantOffers offers = this.menu.getOffers();
|
| 171 | if (!offers.isEmpty()) {
|
| 172 | int xo = (this.width - this.imageWidth) / 2;
|
| 173 | int yo = (this.height - this.imageHeight) / 2;
|
| 174 | int offerY = yo + 16 + 1;
|
| 175 | int sellItem1X = xo + 5 + 5;
|
| 176 | this.extractScroller(graphics, xo, yo, mouseX, mouseY, offers);
|
| 177 | int currentOfferIndex = 0;
|
| 178 |
|
| 179 | for (MerchantOffer offer : offers) {
|
| 180 | if (!this.canScroll(offers.size()) || currentOfferIndex >= this.scrollOff && currentOfferIndex < 7 + this.scrollOff) {
|
| 181 | ItemStack baseCostA = offer.getBaseCostA();
|
| 182 | ItemStack costA = offer.getCostA();
|
| 183 | ItemStack costB = offer.getCostB();
|
| 184 | ItemStack result = offer.getResult();
|
| 185 | int decorHeight = offerY + 2;
|
| 186 | this.extractAndDecorateCostA(graphics, costA, baseCostA, sellItem1X, decorHeight);
|
| 187 | if (!costB.isEmpty()) {
|
| 188 | graphics.fakeItem(costB, xo + 5 + 35, decorHeight);
|
| 189 | graphics.itemDecorations(this.font, costB, xo + 5 + 35, decorHeight);
|
| 190 | }
|
| 191 |
|
| 192 | this.extractButtonArrows(graphics, offer, xo, decorHeight);
|
| 193 | graphics.fakeItem(result, xo + 5 + 68, decorHeight);
|
| 194 | graphics.itemDecorations(this.font, result, xo + 5 + 68, decorHeight);
|
| 195 | offerY += 20;
|
| 196 | currentOfferIndex++;
|
| 197 | } else {
|
| 198 | currentOfferIndex++;
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | int itemIndex = this.shopItem;
|
| 203 | MerchantOffer selectedOffer = offers.get(itemIndex);
|
| 204 | if (this.menu.showProgressBar()) {
|
| 205 | this.extractProgressBar(graphics, xo, yo, selectedOffer);
|
| 206 | }
|
| 207 |
|
| 208 | if (selectedOffer.isOutOfStock() && this.isHovering(186, 35, 22, 21, mouseX, mouseY) && this.menu.canRestock()) {
|
| 209 | graphics.setTooltipForNextFrame(this.font, DEPRECATED_TOOLTIP, mouseX, mouseY);
|
| 210 | }
|
| 211 |
|
| 212 | for (MerchantScreen.TradeOfferButton button : this.tradeOfferButtons) {
|
| 213 | if (button.isHoveredOrFocused()) {
|
| 214 | button.extractToolTip(graphics, mouseX, mouseY);
|
| 215 | }
|
| 216 |
|
| 217 | button.visible = button.index < this.menu.getOffers().size();
|
| 218 | }
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | private void extractButtonArrows(final GuiGraphicsExtractor graphics, final MerchantOffer offer, final int xo, final int decorHeight) {
|
| 223 | if (offer.isOutOfStock()) {
|
| 224 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TRADE_ARROW_OUT_OF_STOCK_SPRITE, xo + 5 + 35 + 20, decorHeight + 3, 10, 9);
|
| 225 | } else {
|
| 226 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TRADE_ARROW_SPRITE, xo + 5 + 35 + 20, decorHeight + 3, 10, 9);
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | private void extractAndDecorateCostA(
|
| 231 | final GuiGraphicsExtractor graphics, final ItemStack costA, final ItemStack baseCostA, final int sellItem1X, final int decorHeight
|
| 232 | ) {
|
| 233 | graphics.fakeItem(costA, sellItem1X, decorHeight);
|
| 234 | if (baseCostA.getCount() == costA.getCount()) {
|
| 235 | graphics.itemDecorations(this.font, costA, sellItem1X, decorHeight);
|
| 236 | } else {
|
| 237 | graphics.itemDecorations(this.font, baseCostA, sellItem1X, decorHeight, baseCostA.getCount() == 1 ? "1" : null);
|
| 238 | graphics.itemDecorations(this.font, costA, sellItem1X + 14, decorHeight, costA.getCount() == 1 ? "1" : null);
|
| 239 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, DISCOUNT_STRIKETHRUOGH_SPRITE, sellItem1X + 7, decorHeight + 12, 9, 2);
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | private boolean canScroll(final int numberOfOffers) {
|
| 244 | return numberOfOffers > 7;
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY) {
|
| 249 | if (super.mouseScrolled(x, y, scrollX, scrollY)) {
|
| 250 | return true;
|
| 251 | }
|
| 252 |
|
| 253 | int numberOfOffers = this.menu.getOffers().size();
|
| 254 | if (this.canScroll(numberOfOffers)) {
|
| 255 | int maxScrollOff = numberOfOffers - 7;
|
| 256 | this.scrollOff = Mth.clamp((int)(this.scrollOff - scrollY), 0, maxScrollOff);
|
| 257 | }
|
| 258 |
|
| 259 | return true;
|
| 260 | }
|
| 261 |
|
| 262 | @Override
|
| 263 | public boolean mouseDragged(final MouseButtonEvent event, final double dx, final double dy) {
|
| 264 | int numberOfOffers = this.menu.getOffers().size();
|
| 265 | if (this.isDragging) {
|
| 266 | int fullScrollTopPos = this.topPos + 18;
|
| 267 | int fullScrollBottomPos = fullScrollTopPos + 139;
|
| 268 | int maxScrollOff = numberOfOffers - 7;
|
| 269 | float scrolling = ((float)event.y() - fullScrollTopPos - 13.5F) / (fullScrollBottomPos - fullScrollTopPos - 27.0F);
|
| 270 | scrolling = scrolling * maxScrollOff + 0.5F;
|
| 271 | this.scrollOff = Mth.clamp((int)scrolling, 0, maxScrollOff);
|
| 272 | return true;
|
| 273 | } else {
|
| 274 | return super.mouseDragged(event, dx, dy);
|
| 275 | }
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 280 | int xo = (this.width - this.imageWidth) / 2;
|
| 281 | int yo = (this.height - this.imageHeight) / 2;
|
| 282 | if (this.canScroll(this.menu.getOffers().size()) && event.x() > xo + 94 && event.x() < xo + 94 + 6 && event.y() > yo + 18 && event.y() <= yo + 18 + 139 + 1) {
|
| 283 | this.isDragging = true;
|
| 284 | }
|
| 285 |
|
| 286 | return super.mouseClicked(event, doubleClick);
|
| 287 | }
|
| 288 |
|
| 289 | @Override
|
| 290 | public boolean mouseReleased(final MouseButtonEvent event) {
|
| 291 | this.isDragging = false;
|
| 292 | return super.mouseReleased(event);
|
| 293 | }
|
| 294 |
|
| 295 | @Environment(EnvType.CLIENT)
|
| 296 | private class TradeOfferButton extends Button.Plain {
|
| 297 | private final int index;
|
| 298 |
|
| 299 | public TradeOfferButton(final int x, final int y, final int index, final Button.OnPress onPress) {
|
| 300 | super(x, y, 88, 20, CommonComponents.EMPTY, onPress, DEFAULT_NARRATION);
|
| 301 | this.index = index;
|
| 302 | this.visible = false;
|
| 303 | }
|
| 304 |
|
| 305 | public int getIndex() {
|
| 306 | return this.index;
|
| 307 | }
|
| 308 |
|
| 309 | public void extractToolTip(final GuiGraphicsExtractor graphics, final int xm, final int ym) {
|
| 310 | if (this.isHovered && MerchantScreen.this.menu.getOffers().size() > this.index + MerchantScreen.this.scrollOff) {
|
| 311 | if (xm < this.getX() + 20) {
|
| 312 | ItemStack item = MerchantScreen.this.menu.getOffers().get(this.index + MerchantScreen.this.scrollOff).getCostA();
|
| 313 | graphics.setTooltipForNextFrame(MerchantScreen.this.font, item, xm, ym);
|
| 314 | } else if (xm < this.getX() + 50 && xm > this.getX() + 30) {
|
| 315 | ItemStack item = MerchantScreen.this.menu.getOffers().get(this.index + MerchantScreen.this.scrollOff).getCostB();
|
| 316 | if (!item.isEmpty()) {
|
| 317 | graphics.setTooltipForNextFrame(MerchantScreen.this.font, item, xm, ym);
|
| 318 | }
|
| 319 | } else if (xm > this.getX() + 65) {
|
| 320 | ItemStack item = MerchantScreen.this.menu.getOffers().get(this.index + MerchantScreen.this.scrollOff).getResult();
|
| 321 | graphics.setTooltipForNextFrame(MerchantScreen.this.font, item, xm, ym);
|
| 322 | }
|
| 323 | }
|
| 324 | }
|
| 325 | }
|
| 326 | }
|
| 327 |
|