| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import net.minecraft.core.component.DataComponents;
|
| 4 | import net.minecraft.sounds.SoundEvents;
|
| 5 | import net.minecraft.sounds.SoundSource;
|
| 6 | import net.minecraft.tags.ItemTags;
|
| 7 | import net.minecraft.world.Container;
|
| 8 | import net.minecraft.world.SimpleContainer;
|
| 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.Items;
|
| 13 | import net.minecraft.world.item.MapItem;
|
| 14 | import net.minecraft.world.item.component.MapPostProcessing;
|
| 15 | import net.minecraft.world.level.block.Blocks;
|
| 16 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 17 |
|
| 18 | public class CartographyTableMenu extends AbstractContainerMenu {
|
| 19 | public static final int MAP_SLOT = 0;
|
| 20 | public static final int ADDITIONAL_SLOT = 1;
|
| 21 | public static final int RESULT_SLOT = 2;
|
| 22 | private static final int INV_SLOT_START = 3;
|
| 23 | private static final int INV_SLOT_END = 30;
|
| 24 | private static final int USE_ROW_SLOT_START = 30;
|
| 25 | private static final int USE_ROW_SLOT_END = 39;
|
| 26 | private final ContainerLevelAccess access;
|
| 27 | private long lastSoundTime;
|
| 28 | public final Container container = new SimpleContainer(2) {
|
| 29 | @Override
|
| 30 | public void setChanged() {
|
| 31 | CartographyTableMenu.this.slotsChanged(this);
|
| 32 | super.setChanged();
|
| 33 | }
|
| 34 | };
|
| 35 | private final ResultContainer resultContainer = new ResultContainer() {
|
| 36 | @Override
|
| 37 | public void setChanged() {
|
| 38 | CartographyTableMenu.this.slotsChanged(this);
|
| 39 | super.setChanged();
|
| 40 | }
|
| 41 | };
|
| 42 |
|
| 43 | public CartographyTableMenu(final int containerId, final Inventory inventory) {
|
| 44 | this(containerId, inventory, ContainerLevelAccess.NULL);
|
| 45 | }
|
| 46 |
|
| 47 | public CartographyTableMenu(final int containerId, final Inventory inventory, final ContainerLevelAccess access) {
|
| 48 | super(MenuType.CARTOGRAPHY_TABLE, containerId);
|
| 49 | this.access = access;
|
| 50 | this.addSlot(new Slot(this.container, 0, 15, 15) {
|
| 51 | @Override
|
| 52 | public boolean mayPlace(final ItemStack itemStack) {
|
| 53 | return itemStack.has(DataComponents.MAP_ID);
|
| 54 | }
|
| 55 | });
|
| 56 | this.addSlot(new Slot(this.container, 1, 15, 52) {
|
| 57 | @Override
|
| 58 | public boolean mayPlace(final ItemStack itemStack) {
|
| 59 | return itemStack.is(Items.PAPER) || itemStack.is(Items.MAP) || itemStack.is(Items.GLASS_PANE);
|
| 60 | }
|
| 61 | });
|
| 62 | this.addSlot(new Slot(this.resultContainer, 2, 145, 39) {
|
| 63 | @Override
|
| 64 | public boolean mayPlace(final ItemStack itemStack) {
|
| 65 | return false;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public void onTake(final Player player, final ItemStack carried) {
|
| 70 | CartographyTableMenu.this.slots.get(0).remove(1);
|
| 71 | CartographyTableMenu.this.slots.get(1).remove(1);
|
| 72 | carried.getItem().onCraftedBy(carried, player);
|
| 73 | access.execute((level, pos) -> {
|
| 74 | long gameTime = level.getGameTime();
|
| 75 | if (CartographyTableMenu.this.lastSoundTime != gameTime) {
|
| 76 | level.playSound(null, pos, SoundEvents.UI_CARTOGRAPHY_TABLE_TAKE_RESULT, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 77 | CartographyTableMenu.this.lastSoundTime = gameTime;
|
| 78 | }
|
| 79 | });
|
| 80 | super.onTake(player, carried);
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public ItemStack safeClone(final Player player) {
|
| 85 | ItemStack result = super.safeClone(player);
|
| 86 | result.getItem().onCraftedBy(result, player);
|
| 87 | return result;
|
| 88 | }
|
| 89 | });
|
| 90 | this.addStandardInventorySlots(inventory, 8, 84);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public boolean stillValid(final Player player) {
|
| 95 | return stillValid(this.access, player, Blocks.CARTOGRAPHY_TABLE);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void slotsChanged(final Container container) {
|
| 100 | ItemStack mapStack = this.container.getItem(0);
|
| 101 | ItemStack additionalStack = this.container.getItem(1);
|
| 102 | ItemStack resultStack = this.resultContainer.getItem(2);
|
| 103 | if (resultStack.isEmpty() || !mapStack.isEmpty() && !additionalStack.isEmpty()) {
|
| 104 | if (!mapStack.isEmpty() && !additionalStack.isEmpty()) {
|
| 105 | this.setupResultSlot(mapStack, additionalStack, resultStack);
|
| 106 | }
|
| 107 | } else {
|
| 108 | this.resultContainer.removeItemNoUpdate(2);
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | private void setupResultSlot(final ItemStack mapStack, final ItemStack additionalStack, final ItemStack resultStack) {
|
| 113 | this.access.execute((level, pos) -> {
|
| 114 | MapItemSavedData mapData = MapItem.getSavedData(mapStack, level);
|
| 115 | if (mapData != null) {
|
| 116 | ItemStack result;
|
| 117 | if (additionalStack.is(Items.PAPER) && mapStack.is(ItemTags.EXTENDABLE_MAPS) && !mapData.locked && mapData.scale < 4) {
|
| 118 | result = mapStack.copyWithCount(1);
|
| 119 | result.set(DataComponents.MAP_POST_PROCESSING, MapPostProcessing.SCALE);
|
| 120 | this.broadcastChanges();
|
| 121 | } else if (additionalStack.is(Items.GLASS_PANE) && !mapData.locked) {
|
| 122 | result = mapStack.copyWithCount(1);
|
| 123 | result.set(DataComponents.MAP_POST_PROCESSING, MapPostProcessing.LOCK);
|
| 124 | this.broadcastChanges();
|
| 125 | } else {
|
| 126 | if (!additionalStack.is(Items.MAP)) {
|
| 127 | this.resultContainer.removeItemNoUpdate(2);
|
| 128 | this.broadcastChanges();
|
| 129 | return;
|
| 130 | }
|
| 131 |
|
| 132 | result = mapStack.copyWithCount(2);
|
| 133 | this.broadcastChanges();
|
| 134 | }
|
| 135 |
|
| 136 | if (!ItemStack.matches(result, resultStack)) {
|
| 137 | this.resultContainer.setItem(2, result);
|
| 138 | this.broadcastChanges();
|
| 139 | }
|
| 140 | }
|
| 141 | });
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public boolean canTakeItemForPickAll(final ItemStack carried, final Slot target) {
|
| 146 | return target.container != this.resultContainer && super.canTakeItemForPickAll(carried, target);
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 151 | ItemStack clicked = ItemStack.EMPTY;
|
| 152 | Slot slot = this.slots.get(slotIndex);
|
| 153 | if (slot != null && slot.hasItem()) {
|
| 154 | ItemStack stack = slot.getItem();
|
| 155 | clicked = stack.copy();
|
| 156 | if (slotIndex == 2) {
|
| 157 | stack.getItem().onCraftedBy(stack, player);
|
| 158 | if (!this.moveItemStackTo(stack, 3, 39, true)) {
|
| 159 | return ItemStack.EMPTY;
|
| 160 | }
|
| 161 |
|
| 162 | slot.onQuickCraft(stack, clicked);
|
| 163 | } else if (slotIndex != 1 && slotIndex != 0) {
|
| 164 | if (stack.has(DataComponents.MAP_ID)) {
|
| 165 | if (!this.moveItemStackTo(stack, 0, 1, false)) {
|
| 166 | return ItemStack.EMPTY;
|
| 167 | }
|
| 168 | } else if (!stack.is(Items.PAPER) && !stack.is(Items.MAP) && !stack.is(Items.GLASS_PANE)) {
|
| 169 | if (slotIndex >= 3 && slotIndex < 30) {
|
| 170 | if (!this.moveItemStackTo(stack, 30, 39, false)) {
|
| 171 | return ItemStack.EMPTY;
|
| 172 | }
|
| 173 | } else if (slotIndex >= 30 && slotIndex < 39 && !this.moveItemStackTo(stack, 3, 30, false)) {
|
| 174 | return ItemStack.EMPTY;
|
| 175 | }
|
| 176 | } else if (!this.moveItemStackTo(stack, 1, 2, false)) {
|
| 177 | return ItemStack.EMPTY;
|
| 178 | }
|
| 179 | } else if (!this.moveItemStackTo(stack, 3, 39, false)) {
|
| 180 | return ItemStack.EMPTY;
|
| 181 | }
|
| 182 |
|
| 183 | if (stack.isEmpty()) {
|
| 184 | slot.setByPlayer(ItemStack.EMPTY);
|
| 185 | }
|
| 186 |
|
| 187 | slot.setChanged();
|
| 188 | if (stack.getCount() == clicked.getCount()) {
|
| 189 | return ItemStack.EMPTY;
|
| 190 | }
|
| 191 |
|
| 192 | slot.onTake(player, stack);
|
| 193 | this.broadcastChanges();
|
| 194 | }
|
| 195 |
|
| 196 | return clicked;
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public void removed(final Player player) {
|
| 201 | super.removed(player);
|
| 202 | this.resultContainer.removeItemNoUpdate(2);
|
| 203 | this.access.execute((level, pos) -> this.clearContainer(player, this.container));
|
| 204 | }
|
| 205 | }
|
| 206 |
|