| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import net.minecraft.world.Container;
|
| 4 | import net.minecraft.world.SimpleContainer;
|
| 5 | import net.minecraft.world.entity.player.Inventory;
|
| 6 | import net.minecraft.world.entity.player.Player;
|
| 7 | import net.minecraft.world.item.ItemStack;
|
| 8 |
|
| 9 | public class ChestMenu extends AbstractContainerMenu {
|
| 10 | private final Container container;
|
| 11 | private final int containerRows;
|
| 12 |
|
| 13 | private ChestMenu(final MenuType<?> menuType, final int containerId, final Inventory inventory, final int rows) {
|
| 14 | this(menuType, containerId, inventory, new SimpleContainer(9 * rows), rows);
|
| 15 | }
|
| 16 |
|
| 17 | public static ChestMenu oneRow(final int containerId, final Inventory inventory) {
|
| 18 | return new ChestMenu(MenuType.GENERIC_9x1, containerId, inventory, 1);
|
| 19 | }
|
| 20 |
|
| 21 | public static ChestMenu twoRows(final int containerId, final Inventory inventory) {
|
| 22 | return new ChestMenu(MenuType.GENERIC_9x2, containerId, inventory, 2);
|
| 23 | }
|
| 24 |
|
| 25 | public static ChestMenu threeRows(final int containerId, final Inventory inventory) {
|
| 26 | return new ChestMenu(MenuType.GENERIC_9x3, containerId, inventory, 3);
|
| 27 | }
|
| 28 |
|
| 29 | public static ChestMenu fourRows(final int containerId, final Inventory inventory) {
|
| 30 | return new ChestMenu(MenuType.GENERIC_9x4, containerId, inventory, 4);
|
| 31 | }
|
| 32 |
|
| 33 | public static ChestMenu fiveRows(final int containerId, final Inventory inventory) {
|
| 34 | return new ChestMenu(MenuType.GENERIC_9x5, containerId, inventory, 5);
|
| 35 | }
|
| 36 |
|
| 37 | public static ChestMenu sixRows(final int containerId, final Inventory inventory) {
|
| 38 | return new ChestMenu(MenuType.GENERIC_9x6, containerId, inventory, 6);
|
| 39 | }
|
| 40 |
|
| 41 | public static ChestMenu threeRows(final int containerId, final Inventory inventory, final Container container) {
|
| 42 | return new ChestMenu(MenuType.GENERIC_9x3, containerId, inventory, container, 3);
|
| 43 | }
|
| 44 |
|
| 45 | public static ChestMenu sixRows(final int containerId, final Inventory inventory, final Container container) {
|
| 46 | return new ChestMenu(MenuType.GENERIC_9x6, containerId, inventory, container, 6);
|
| 47 | }
|
| 48 |
|
| 49 | public ChestMenu(final MenuType<?> menuType, final int containerId, final Inventory inventory, final Container container, final int rows) {
|
| 50 | super(menuType, containerId);
|
| 51 | checkContainerSize(container, rows * 9);
|
| 52 | this.container = container;
|
| 53 | this.containerRows = rows;
|
| 54 | container.startOpen(inventory.player);
|
| 55 | int chestGridTop = 18;
|
| 56 | this.addChestGrid(container, 8, 18);
|
| 57 | int inventoryTop = 18 + this.containerRows * 18 + 13;
|
| 58 | this.addStandardInventorySlots(inventory, 8, inventoryTop);
|
| 59 | }
|
| 60 |
|
| 61 | private void addChestGrid(final Container container, final int left, final int top) {
|
| 62 | for (int y = 0; y < this.containerRows; y++) {
|
| 63 | for (int x = 0; x < 9; x++) {
|
| 64 | this.addSlot(new Slot(container, x + y * 9, left + x * 18, top + y * 18));
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public boolean stillValid(final Player player) {
|
| 71 | return this.container.stillValid(player);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 76 | ItemStack clicked = ItemStack.EMPTY;
|
| 77 | Slot slot = this.slots.get(slotIndex);
|
| 78 | if (slot != null && slot.hasItem()) {
|
| 79 | ItemStack stack = slot.getItem();
|
| 80 | clicked = stack.copy();
|
| 81 | if (slotIndex < this.containerRows * 9) {
|
| 82 | if (!this.moveItemStackTo(stack, this.containerRows * 9, this.slots.size(), true)) {
|
| 83 | return ItemStack.EMPTY;
|
| 84 | }
|
| 85 | } else if (!this.moveItemStackTo(stack, 0, this.containerRows * 9, false)) {
|
| 86 | return ItemStack.EMPTY;
|
| 87 | }
|
| 88 |
|
| 89 | if (stack.isEmpty()) {
|
| 90 | slot.setByPlayer(ItemStack.EMPTY);
|
| 91 | } else {
|
| 92 | slot.setChanged();
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | return clicked;
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public void removed(final Player player) {
|
| 101 | super.removed(player);
|
| 102 | this.container.stopOpen(player);
|
| 103 | }
|
| 104 |
|
| 105 | public Container getContainer() {
|
| 106 | return this.container;
|
| 107 | }
|
| 108 |
|
| 109 | public int getRowCount() {
|
| 110 | return this.containerRows;
|
| 111 | }
|
| 112 | }
|
| 113 |
|