| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import net.minecraft.util.Prediction;
|
| 4 | import net.minecraft.world.Container;
|
| 5 | import net.minecraft.world.SimpleContainer;
|
| 6 | import net.minecraft.world.entity.player.Player;
|
| 7 | import net.minecraft.world.item.ItemStack;
|
| 8 |
|
| 9 | public class LecternMenu extends AbstractContainerMenu {
|
| 10 | private static final int DATA_COUNT = 1;
|
| 11 | private static final int SLOT_COUNT = 1;
|
| 12 | public static final int BUTTON_PREV_PAGE = 1;
|
| 13 | public static final int BUTTON_NEXT_PAGE = 2;
|
| 14 | public static final int BUTTON_TAKE_BOOK = 3;
|
| 15 | public static final int BUTTON_PAGE_JUMP_RANGE_START = 100;
|
| 16 | private final Container lectern;
|
| 17 | private final ContainerData lecternData;
|
| 18 |
|
| 19 | public LecternMenu(final int containerId) {
|
| 20 | this(containerId, new SimpleContainer(1), new SimpleContainerData(1));
|
| 21 | }
|
| 22 |
|
| 23 | public LecternMenu(final int containerId, final Container lectern, final ContainerData lecternData) {
|
| 24 | super(MenuType.LECTERN, containerId);
|
| 25 | checkContainerSize(lectern, 1);
|
| 26 | checkContainerDataCount(lecternData, 1);
|
| 27 | this.lectern = lectern;
|
| 28 | this.lecternData = lecternData;
|
| 29 | this.addSlot(new Slot(lectern, 0, 0, 0) {
|
| 30 | @Override
|
| 31 | public void setChanged() {
|
| 32 | super.setChanged();
|
| 33 | LecternMenu.this.slotsChanged(this.container);
|
| 34 | }
|
| 35 | });
|
| 36 | this.addDataSlots(lecternData);
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public boolean clickMenuButton(final Player player, final int buttonId) {
|
| 41 | if (buttonId >= 100) {
|
| 42 | int pageToSet = buttonId - 100;
|
| 43 | this.setData(0, pageToSet);
|
| 44 | return true;
|
| 45 | }
|
| 46 |
|
| 47 | switch (buttonId) {
|
| 48 | case 1: {
|
| 49 | int currentPage = this.lecternData.get(0);
|
| 50 | this.setData(0, currentPage - 1);
|
| 51 | return true;
|
| 52 | }
|
| 53 | case 2: {
|
| 54 | int currentPage = this.lecternData.get(0);
|
| 55 | this.setData(0, currentPage + 1);
|
| 56 | return true;
|
| 57 | }
|
| 58 | case 3:
|
| 59 | if (!player.mayBuild()) {
|
| 60 | return false;
|
| 61 | }
|
| 62 |
|
| 63 | ItemStack book = this.lectern.removeItemNoUpdate(0);
|
| 64 | this.lectern.setChanged();
|
| 65 | if (!player.getInventory().add(book)) {
|
| 66 | player.drop(book, false, Prediction.PREDICTED);
|
| 67 | }
|
| 68 |
|
| 69 | return true;
|
| 70 | default:
|
| 71 | return false;
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 77 | return ItemStack.EMPTY;
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public void setData(final int id, final int value) {
|
| 82 | super.setData(id, value);
|
| 83 | this.broadcastChanges();
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public boolean stillValid(final Player player) {
|
| 88 | return this.lectern.stillValid(player);
|
| 89 | }
|
| 90 |
|
| 91 | public ItemStack getBook() {
|
| 92 | return this.lectern.getItem(0);
|
| 93 | }
|
| 94 |
|
| 95 | public int getPage() {
|
| 96 | return this.lecternData.get(0);
|
| 97 | }
|
| 98 | }
|
| 99 |
|