| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import net.minecraft.core.NonNullList;
|
| 4 | import net.minecraft.world.Container;
|
| 5 | import net.minecraft.world.ContainerHelper;
|
| 6 | import net.minecraft.world.entity.player.Player;
|
| 7 | import net.minecraft.world.item.ItemStack;
|
| 8 | import net.minecraft.world.item.trading.Merchant;
|
| 9 | import net.minecraft.world.item.trading.MerchantOffer;
|
| 10 | import net.minecraft.world.item.trading.MerchantOffers;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class MerchantContainer implements Container {
|
| 14 | private final Merchant merchant;
|
| 15 | private final NonNullList<ItemStack> itemStacks = NonNullList.withSize(3, ItemStack.EMPTY);
|
| 16 | private @Nullable MerchantOffer activeOffer;
|
| 17 | private int selectionHint;
|
| 18 | private int futureXp;
|
| 19 |
|
| 20 | public MerchantContainer(final Merchant villager) {
|
| 21 | this.merchant = villager;
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public int getContainerSize() {
|
| 26 | return this.itemStacks.size();
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public boolean isEmpty() {
|
| 31 | for (ItemStack itemStack : this.itemStacks) {
|
| 32 | if (!itemStack.isEmpty()) {
|
| 33 | return false;
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | return true;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public ItemStack getItem(final int slot) {
|
| 42 | return this.itemStacks.get(slot);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public ItemStack removeItem(final int slot, final int count) {
|
| 47 | ItemStack itemStack = this.itemStacks.get(slot);
|
| 48 | if (slot == 2 && !itemStack.isEmpty()) {
|
| 49 | return ContainerHelper.removeItem(this.itemStacks, slot, itemStack.getCount());
|
| 50 | }
|
| 51 |
|
| 52 | ItemStack result = ContainerHelper.removeItem(this.itemStacks, slot, count);
|
| 53 | if (!result.isEmpty() && this.isPaymentSlot(slot)) {
|
| 54 | this.updateSellItem();
|
| 55 | }
|
| 56 |
|
| 57 | return result;
|
| 58 | }
|
| 59 |
|
| 60 | private boolean isPaymentSlot(final int slot) {
|
| 61 | return slot == 0 || slot == 1;
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public ItemStack removeItemNoUpdate(final int slot) {
|
| 66 | return ContainerHelper.takeItem(this.itemStacks, slot);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void setItem(final int slot, final ItemStack itemStack) {
|
| 71 | this.itemStacks.set(slot, itemStack);
|
| 72 | itemStack.limitSize(this.getMaxStackSize(itemStack));
|
| 73 | if (this.isPaymentSlot(slot)) {
|
| 74 | this.updateSellItem();
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public boolean stillValid(final Player player) {
|
| 80 | return this.merchant.getTradingPlayer() == player;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void setChanged() {
|
| 85 | this.updateSellItem();
|
| 86 | }
|
| 87 |
|
| 88 | public void updateSellItem() {
|
| 89 | this.activeOffer = null;
|
| 90 | ItemStack buyA;
|
| 91 | ItemStack buyB;
|
| 92 | if (this.itemStacks.get(0).isEmpty()) {
|
| 93 | buyA = this.itemStacks.get(1);
|
| 94 | buyB = ItemStack.EMPTY;
|
| 95 | } else {
|
| 96 | buyA = this.itemStacks.get(0);
|
| 97 | buyB = this.itemStacks.get(1);
|
| 98 | }
|
| 99 |
|
| 100 | if (buyA.isEmpty()) {
|
| 101 | this.setItem(2, ItemStack.EMPTY);
|
| 102 | this.futureXp = 0;
|
| 103 | } else {
|
| 104 | MerchantOffers offers = this.merchant.getOffers();
|
| 105 | if (!offers.isEmpty()) {
|
| 106 | MerchantOffer offer = offers.getRecipeFor(buyA, buyB, this.selectionHint);
|
| 107 | if (offer == null || offer.isOutOfStock()) {
|
| 108 | this.activeOffer = offer;
|
| 109 | offer = offers.getRecipeFor(buyB, buyA, this.selectionHint);
|
| 110 | }
|
| 111 |
|
| 112 | if (offer != null && !offer.isOutOfStock()) {
|
| 113 | this.activeOffer = offer;
|
| 114 | this.setItem(2, offer.assemble());
|
| 115 | this.futureXp = offer.getXp();
|
| 116 | } else {
|
| 117 | this.setItem(2, ItemStack.EMPTY);
|
| 118 | this.futureXp = 0;
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | this.merchant.notifyTradeUpdated(this.getItem(2));
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | public @Nullable MerchantOffer getActiveOffer() {
|
| 127 | return this.activeOffer;
|
| 128 | }
|
| 129 |
|
| 130 | public void setSelectionHint(final int selectionHint) {
|
| 131 | this.selectionHint = selectionHint;
|
| 132 | this.updateSellItem();
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public void clearContent() {
|
| 137 | this.itemStacks.clear();
|
| 138 | }
|
| 139 |
|
| 140 | public int getFutureXp() {
|
| 141 | return this.futureXp;
|
| 142 | }
|
| 143 | }
|
| 144 |
|