| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 6 | import net.minecraft.tags.ItemTags;
|
| 7 | import net.minecraft.util.Prediction;
|
| 8 | import net.minecraft.world.Container;
|
| 9 | import net.minecraft.world.SimpleContainer;
|
| 10 | import net.minecraft.world.effect.MobEffect;
|
| 11 | import net.minecraft.world.entity.player.Player;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.block.Blocks;
|
| 15 | import net.minecraft.world.level.block.entity.BeaconBlockEntity;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public class BeaconMenu extends AbstractContainerMenu {
|
| 19 | private static final int PAYMENT_SLOT = 0;
|
| 20 | private static final int SLOT_COUNT = 1;
|
| 21 | private static final int DATA_COUNT = 3;
|
| 22 | private static final int INV_SLOT_START = 1;
|
| 23 | private static final int INV_SLOT_END = 28;
|
| 24 | private static final int USE_ROW_SLOT_START = 28;
|
| 25 | private static final int USE_ROW_SLOT_END = 37;
|
| 26 | private static final int NO_EFFECT = 0;
|
| 27 | private final Container beacon = new SimpleContainer(1) {
|
| 28 | @Override
|
| 29 | public boolean canPlaceItem(final int slot, final ItemStack itemStack) {
|
| 30 | return itemStack.is(ItemTags.BEACON_PAYMENT_ITEMS);
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public int getMaxStackSize() {
|
| 35 | return 1;
|
| 36 | }
|
| 37 | };
|
| 38 | private final BeaconMenu.PaymentSlot paymentSlot;
|
| 39 | private final ContainerLevelAccess access;
|
| 40 | private final ContainerData beaconData;
|
| 41 |
|
| 42 | public BeaconMenu(final int containerId, final Container inventory) {
|
| 43 | this(containerId, inventory, new SimpleContainerData(3), ContainerLevelAccess.NULL);
|
| 44 | }
|
| 45 |
|
| 46 | public BeaconMenu(final int containerId, final Container inventory, final ContainerData beaconData, final ContainerLevelAccess access) {
|
| 47 | super(MenuType.BEACON, containerId);
|
| 48 | checkContainerDataCount(beaconData, 3);
|
| 49 | this.beaconData = beaconData;
|
| 50 | this.access = access;
|
| 51 | this.paymentSlot = new BeaconMenu.PaymentSlot(this.beacon, 0, 136, 110);
|
| 52 | this.addSlot(this.paymentSlot);
|
| 53 | this.addDataSlots(beaconData);
|
| 54 | this.addStandardInventorySlots(inventory, 36, 137);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public void removed(final Player player) {
|
| 59 | super.removed(player);
|
| 60 | if (!player.level().isClientSide()) {
|
| 61 | ItemStack itemStack = this.paymentSlot.remove(this.paymentSlot.getMaxStackSize());
|
| 62 | if (!itemStack.isEmpty()) {
|
| 63 | player.drop(itemStack, false, Prediction.SERVER_ONLY);
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public boolean stillValid(final Player player) {
|
| 70 | return stillValid(this.access, player, Blocks.BEACON);
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public void setData(final int id, final int value) {
|
| 75 | super.setData(id, value);
|
| 76 | this.broadcastChanges();
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 81 | ItemStack clicked = ItemStack.EMPTY;
|
| 82 | Slot slot = this.slots.get(slotIndex);
|
| 83 | if (slot != null && slot.hasItem()) {
|
| 84 | ItemStack stack = slot.getItem();
|
| 85 | clicked = stack.copy();
|
| 86 | if (slotIndex == 0) {
|
| 87 | if (!this.moveItemStackTo(stack, 1, 37, true)) {
|
| 88 | return ItemStack.EMPTY;
|
| 89 | }
|
| 90 |
|
| 91 | slot.onQuickCraft(stack, clicked);
|
| 92 | } else if (!this.paymentSlot.hasItem() && this.paymentSlot.mayPlace(stack) && stack.getCount() == 1) {
|
| 93 | if (!this.moveItemStackTo(stack, 0, 1, false)) {
|
| 94 | return ItemStack.EMPTY;
|
| 95 | }
|
| 96 | } else if (slotIndex >= 1 && slotIndex < 28) {
|
| 97 | if (!this.moveItemStackTo(stack, 28, 37, false)) {
|
| 98 | return ItemStack.EMPTY;
|
| 99 | }
|
| 100 | } else if (slotIndex >= 28 && slotIndex < 37) {
|
| 101 | if (!this.moveItemStackTo(stack, 1, 28, false)) {
|
| 102 | return ItemStack.EMPTY;
|
| 103 | }
|
| 104 | } else if (!this.moveItemStackTo(stack, 1, 37, false)) {
|
| 105 | return ItemStack.EMPTY;
|
| 106 | }
|
| 107 |
|
| 108 | if (stack.isEmpty()) {
|
| 109 | slot.setByPlayer(ItemStack.EMPTY);
|
| 110 | } else {
|
| 111 | slot.setChanged();
|
| 112 | }
|
| 113 |
|
| 114 | if (stack.getCount() == clicked.getCount()) {
|
| 115 | return ItemStack.EMPTY;
|
| 116 | }
|
| 117 |
|
| 118 | slot.onTake(player, stack);
|
| 119 | }
|
| 120 |
|
| 121 | return clicked;
|
| 122 | }
|
| 123 |
|
| 124 | public int getLevels() {
|
| 125 | return this.beaconData.get(0);
|
| 126 | }
|
| 127 |
|
| 128 | public static int encodeEffect(final @Nullable Holder<MobEffect> mobEffect) {
|
| 129 | return mobEffect == null ? 0 : BuiltInRegistries.MOB_EFFECT.asHolderIdMap().getId(mobEffect) + 1;
|
| 130 | }
|
| 131 |
|
| 132 | public static @Nullable Holder<MobEffect> decodeEffect(final int id) {
|
| 133 | return id == 0 ? null : BuiltInRegistries.MOB_EFFECT.asHolderIdMap().byId(id - 1);
|
| 134 | }
|
| 135 |
|
| 136 | public @Nullable Holder<MobEffect> getPrimaryEffect() {
|
| 137 | return decodeEffect(this.beaconData.get(1));
|
| 138 | }
|
| 139 |
|
| 140 | public @Nullable Holder<MobEffect> getSecondaryEffect() {
|
| 141 | return decodeEffect(this.beaconData.get(2));
|
| 142 | }
|
| 143 |
|
| 144 | public boolean updateEffects(final Optional<Holder<MobEffect>> primary, final Optional<Holder<MobEffect>> secondary) {
|
| 145 | if (this.paymentSlot.hasItem()) {
|
| 146 | int levels = this.getLevels();
|
| 147 | Holder<MobEffect> primaryEffect = primary.orElse(null);
|
| 148 | Holder<MobEffect> secondaryEffect = secondary.orElse(null);
|
| 149 | if (!BeaconBlockEntity.validateEffects(primaryEffect, secondaryEffect, levels)) {
|
| 150 | return false;
|
| 151 | }
|
| 152 |
|
| 153 | this.beaconData.set(1, encodeEffect(primaryEffect));
|
| 154 | this.beaconData.set(2, encodeEffect(secondaryEffect));
|
| 155 | this.paymentSlot.remove(1);
|
| 156 | this.access.execute(Level::blockEntityChanged);
|
| 157 | return true;
|
| 158 | } else {
|
| 159 | return false;
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | public boolean hasPayment() {
|
| 164 | return !this.beacon.getItem(0).isEmpty();
|
| 165 | }
|
| 166 |
|
| 167 | private static class PaymentSlot extends Slot {
|
| 168 | public PaymentSlot(final Container container, final int slot, final int x, final int y) {
|
| 169 | super(container, slot, x, y);
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public boolean mayPlace(final ItemStack itemStack) {
|
| 174 | return itemStack.is(ItemTags.BEACON_PAYMENT_ITEMS);
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public int getMaxStackSize() {
|
| 179 | return 1;
|
| 180 | }
|
| 181 | }
|
| 182 | }
|
| 183 |
|