| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import net.minecraft.server.level.ServerPlayer;
|
| 4 | import net.minecraft.sounds.SoundSource;
|
| 5 | import net.minecraft.util.Prediction;
|
| 6 | import net.minecraft.world.Container;
|
| 7 | import net.minecraft.world.entity.Entity;
|
| 8 | import net.minecraft.world.entity.npc.ClientSideMerchant;
|
| 9 | import net.minecraft.world.entity.player.Inventory;
|
| 10 | import net.minecraft.world.entity.player.Player;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.item.trading.ItemCost;
|
| 13 | import net.minecraft.world.item.trading.Merchant;
|
| 14 | import net.minecraft.world.item.trading.MerchantOffer;
|
| 15 | import net.minecraft.world.item.trading.MerchantOffers;
|
| 16 |
|
| 17 | public class MerchantMenu extends AbstractContainerMenu {
|
| 18 | protected static final int PAYMENT1_SLOT = 0;
|
| 19 | protected static final int PAYMENT2_SLOT = 1;
|
| 20 | protected static final int RESULT_SLOT = 2;
|
| 21 | private static final int INV_SLOT_START = 3;
|
| 22 | private static final int INV_SLOT_END = 30;
|
| 23 | private static final int USE_ROW_SLOT_START = 30;
|
| 24 | private static final int USE_ROW_SLOT_END = 39;
|
| 25 | private static final int SELLSLOT1_X = 136;
|
| 26 | private static final int SELLSLOT2_X = 162;
|
| 27 | private static final int BUYSLOT_X = 220;
|
| 28 | private static final int ROW_Y = 37;
|
| 29 | private final Merchant trader;
|
| 30 | private final MerchantContainer tradeContainer;
|
| 31 | private int merchantLevel;
|
| 32 | private boolean showProgressBar;
|
| 33 | private boolean canRestock;
|
| 34 |
|
| 35 | public MerchantMenu(final int containerId, final Inventory inventory) {
|
| 36 | this(containerId, inventory, new ClientSideMerchant(inventory.player));
|
| 37 | }
|
| 38 |
|
| 39 | public MerchantMenu(final int containerId, final Inventory inventory, final Merchant merchant) {
|
| 40 | super(MenuType.MERCHANT, containerId);
|
| 41 | this.trader = merchant;
|
| 42 | this.tradeContainer = new MerchantContainer(merchant);
|
| 43 | this.addSlot(new Slot(this.tradeContainer, 0, 136, 37));
|
| 44 | this.addSlot(new Slot(this.tradeContainer, 1, 162, 37));
|
| 45 | this.addSlot(new MerchantResultSlot(inventory.player, merchant, this.tradeContainer, 2, 220, 37));
|
| 46 | this.addStandardInventorySlots(inventory, 108, 84);
|
| 47 | }
|
| 48 |
|
| 49 | public void setShowProgressBar(final boolean show) {
|
| 50 | this.showProgressBar = show;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void slotsChanged(final Container container) {
|
| 55 | this.tradeContainer.updateSellItem();
|
| 56 | super.slotsChanged(container);
|
| 57 | }
|
| 58 |
|
| 59 | public void updateSellItem() {
|
| 60 | this.tradeContainer.updateSellItem();
|
| 61 | }
|
| 62 |
|
| 63 | public void setSelectionHint(final int hint) {
|
| 64 | this.tradeContainer.setSelectionHint(hint);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public boolean stillValid(final Player player) {
|
| 69 | return this.trader.stillValid(player);
|
| 70 | }
|
| 71 |
|
| 72 | public int getTraderXp() {
|
| 73 | return this.trader.getVillagerXp();
|
| 74 | }
|
| 75 |
|
| 76 | public int getFutureTraderXp() {
|
| 77 | return this.tradeContainer.getFutureXp();
|
| 78 | }
|
| 79 |
|
| 80 | public void setXp(final int xp) {
|
| 81 | this.trader.overrideXp(xp);
|
| 82 | }
|
| 83 |
|
| 84 | public int getTraderLevel() {
|
| 85 | return this.merchantLevel;
|
| 86 | }
|
| 87 |
|
| 88 | public void setMerchantLevel(final int level) {
|
| 89 | this.merchantLevel = level;
|
| 90 | }
|
| 91 |
|
| 92 | public void setCanRestock(final boolean canRestock) {
|
| 93 | this.canRestock = canRestock;
|
| 94 | }
|
| 95 |
|
| 96 | public boolean canRestock() {
|
| 97 | return this.canRestock;
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public boolean canTakeItemForPickAll(final ItemStack carried, final Slot target) {
|
| 102 | return false;
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 107 | ItemStack clicked = ItemStack.EMPTY;
|
| 108 | Slot slot = this.slots.get(slotIndex);
|
| 109 | if (slot != null && slot.hasItem()) {
|
| 110 | ItemStack stack = slot.getItem();
|
| 111 | clicked = stack.copy();
|
| 112 | if (slotIndex == 2) {
|
| 113 | if (!this.moveItemStackTo(stack, 3, 39, true)) {
|
| 114 | return ItemStack.EMPTY;
|
| 115 | }
|
| 116 |
|
| 117 | slot.onQuickCraft(stack, clicked);
|
| 118 | this.playTradeSound();
|
| 119 | } else if (slotIndex != 0 && slotIndex != 1) {
|
| 120 | if (slotIndex >= 3 && slotIndex < 30) {
|
| 121 | if (!this.moveItemStackTo(stack, 30, 39, false)) {
|
| 122 | return ItemStack.EMPTY;
|
| 123 | }
|
| 124 | } else if (slotIndex >= 30 && slotIndex < 39 && !this.moveItemStackTo(stack, 3, 30, false)) {
|
| 125 | return ItemStack.EMPTY;
|
| 126 | }
|
| 127 | } else if (!this.moveItemStackTo(stack, 3, 39, false)) {
|
| 128 | return ItemStack.EMPTY;
|
| 129 | }
|
| 130 |
|
| 131 | if (stack.isEmpty()) {
|
| 132 | slot.setByPlayer(ItemStack.EMPTY);
|
| 133 | } else {
|
| 134 | slot.setChanged();
|
| 135 | }
|
| 136 |
|
| 137 | if (stack.getCount() == clicked.getCount()) {
|
| 138 | return ItemStack.EMPTY;
|
| 139 | }
|
| 140 |
|
| 141 | slot.onTake(player, stack);
|
| 142 | }
|
| 143 |
|
| 144 | return clicked;
|
| 145 | }
|
| 146 |
|
| 147 | private void playTradeSound() {
|
| 148 | if (!this.trader.isClientSide()) {
|
| 149 | Entity entity = (Entity)this.trader;
|
| 150 | entity.level().playLocalSound(entity.getX(), entity.getY(), entity.getZ(), this.trader.getNotifyTradeSound(), SoundSource.NEUTRAL, 1.0F, 1.0F, false);
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public void removed(final Player player) {
|
| 156 | super.removed(player);
|
| 157 | this.trader.setTradingPlayer(null);
|
| 158 | if (!this.trader.isClientSide()) {
|
| 159 | if (!player.isAlive() || player instanceof ServerPlayer serverPlayer && serverPlayer.hasDisconnected()) {
|
| 160 | ItemStack itemStack = this.tradeContainer.removeItemNoUpdate(0);
|
| 161 | if (!itemStack.isEmpty()) {
|
| 162 | player.drop(itemStack, false, Prediction.SERVER_ONLY);
|
| 163 | }
|
| 164 |
|
| 165 | itemStack = this.tradeContainer.removeItemNoUpdate(1);
|
| 166 | if (!itemStack.isEmpty()) {
|
| 167 | player.drop(itemStack, false, Prediction.SERVER_ONLY);
|
| 168 | }
|
| 169 | } else if (player instanceof ServerPlayer) {
|
| 170 | player.getInventory().placeItemBackInInventory(this.tradeContainer.removeItemNoUpdate(0), Prediction.SERVER_ONLY);
|
| 171 | player.getInventory().placeItemBackInInventory(this.tradeContainer.removeItemNoUpdate(1), Prediction.SERVER_ONLY);
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | public void tryMoveItems(final int newTradeIndex) {
|
| 177 | if (newTradeIndex >= 0 && this.getOffers().size() > newTradeIndex) {
|
| 178 | ItemStack oldCostA = this.tradeContainer.getItem(0);
|
| 179 | if (!oldCostA.isEmpty()) {
|
| 180 | if (!this.moveItemStackTo(oldCostA, 3, 39, true)) {
|
| 181 | return;
|
| 182 | }
|
| 183 |
|
| 184 | this.tradeContainer.setItem(0, oldCostA);
|
| 185 | }
|
| 186 |
|
| 187 | ItemStack oldCostB = this.tradeContainer.getItem(1);
|
| 188 | if (!oldCostB.isEmpty()) {
|
| 189 | if (!this.moveItemStackTo(oldCostB, 3, 39, true)) {
|
| 190 | return;
|
| 191 | }
|
| 192 |
|
| 193 | this.tradeContainer.setItem(1, oldCostB);
|
| 194 | }
|
| 195 |
|
| 196 | if (this.tradeContainer.getItem(0).isEmpty() && this.tradeContainer.getItem(1).isEmpty()) {
|
| 197 | MerchantOffer merchantOffer = this.getOffers().get(newTradeIndex);
|
| 198 | this.moveFromInventoryToPaymentSlot(0, merchantOffer.getItemCostA());
|
| 199 | merchantOffer.getItemCostB().ifPresent(costB -> this.moveFromInventoryToPaymentSlot(1, costB));
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | private void moveFromInventoryToPaymentSlot(final int paymentSlot, final ItemCost cost) {
|
| 205 | for (int i = 3; i < 39; i++) {
|
| 206 | ItemStack inventoryItem = this.slots.get(i).getItem();
|
| 207 | if (!inventoryItem.isEmpty() && cost.test(inventoryItem)) {
|
| 208 | ItemStack currentPaymentItem = this.tradeContainer.getItem(paymentSlot);
|
| 209 | if (currentPaymentItem.isEmpty() || ItemStack.isSameItemSameComponents(inventoryItem, currentPaymentItem)) {
|
| 210 | int maxStackSize = inventoryItem.getMaxStackSize();
|
| 211 | int moveCount = Math.min(maxStackSize - currentPaymentItem.getCount(), inventoryItem.getCount());
|
| 212 | ItemStack newPaymentItem = inventoryItem.copyWithCount(currentPaymentItem.getCount() + moveCount);
|
| 213 | inventoryItem.shrink(moveCount);
|
| 214 | this.tradeContainer.setItem(paymentSlot, newPaymentItem);
|
| 215 | if (newPaymentItem.getCount() >= maxStackSize) {
|
| 216 | break;
|
| 217 | }
|
| 218 | }
|
| 219 | }
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | public void setOffers(final MerchantOffers offers) {
|
| 224 | this.trader.overrideOffers(offers);
|
| 225 | }
|
| 226 |
|
| 227 | public MerchantOffers getOffers() {
|
| 228 | return this.trader.getOffers();
|
| 229 | }
|
| 230 |
|
| 231 | public boolean showProgressBar() {
|
| 232 | return this.showProgressBar;
|
| 233 | }
|
| 234 | }
|
| 235 |
|