| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.ListIterator;
|
| 6 | import java.util.Optional;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.Minecraft;
|
| 10 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 11 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 12 | import net.minecraft.client.gui.TextAlignment;
|
| 13 | import net.minecraft.client.gui.components.Button;
|
| 14 | import net.minecraft.client.gui.components.MultiLineEditBox;
|
| 15 | import net.minecraft.client.gui.screens.Screen;
|
| 16 | import net.minecraft.client.input.KeyEvent;
|
| 17 | import net.minecraft.client.renderer.RenderPipelines;
|
| 18 | import net.minecraft.core.component.DataComponents;
|
| 19 | import net.minecraft.network.chat.CommonComponents;
|
| 20 | import net.minecraft.network.chat.Component;
|
| 21 | import net.minecraft.network.protocol.game.ServerboundEditBookPacket;
|
| 22 | import net.minecraft.server.network.Filterable;
|
| 23 | import net.minecraft.world.InteractionHand;
|
| 24 | import net.minecraft.world.entity.player.Player;
|
| 25 | import net.minecraft.world.item.ItemStack;
|
| 26 | import net.minecraft.world.item.component.WritableBookContent;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | public class BookEditScreen extends Screen {
|
| 30 | public static final int TEXT_WIDTH = 114;
|
| 31 | public static final int TEXT_HEIGHT = 126;
|
| 32 | public static final int IMAGE_WIDTH = 192;
|
| 33 | public static final int IMAGE_HEIGHT = 192;
|
| 34 | public static final int BACKGROUND_TEXTURE_WIDTH = 256;
|
| 35 | public static final int BACKGROUND_TEXTURE_HEIGHT = 256;
|
| 36 | private static final int MENU_BUTTON_MARGIN = 4;
|
| 37 | private static final int MENU_BUTTON_SIZE = 98;
|
| 38 | private static final int PAGE_BUTTON_Y = 157;
|
| 39 | private static final int PAGE_BACK_BUTTON_X = 43;
|
| 40 | private static final int PAGE_FORWARD_BUTTON_X = 116;
|
| 41 | private static final int PAGE_INDICATOR_TEXT_Y_OFFSET = 16;
|
| 42 | private static final int PAGE_INDICATOR_X_OFFSET = 148;
|
| 43 | private static final Component TITLE = Component.translatable("book.edit.title");
|
| 44 | private static final Component SIGN_BOOK_LABEL = Component.translatable("book.signButton");
|
| 45 | private final Player owner;
|
| 46 | private final ItemStack book;
|
| 47 | private final BookSignScreen signScreen;
|
| 48 | private int currentPage;
|
| 49 | private final List<String> pages = Lists.newArrayList();
|
| 50 | private PageButton forwardButton;
|
| 51 | private PageButton backButton;
|
| 52 | private final InteractionHand hand;
|
| 53 | private Component numberOfPages = CommonComponents.EMPTY;
|
| 54 | private MultiLineEditBox page;
|
| 55 |
|
| 56 | public BookEditScreen(final Player owner, final ItemStack book, final InteractionHand hand, final WritableBookContent content) {
|
| 57 | super(TITLE);
|
| 58 | this.owner = owner;
|
| 59 | this.book = book;
|
| 60 | this.hand = hand;
|
| 61 | content.getPages(Minecraft.getInstance().isTextFilteringEnabled()).forEach(this.pages::add);
|
| 62 | if (this.pages.isEmpty()) {
|
| 63 | this.pages.add("");
|
| 64 | }
|
| 65 |
|
| 66 | this.signScreen = new BookSignScreen(this, owner, hand, this.pages);
|
| 67 | }
|
| 68 |
|
| 69 | private int getNumPages() {
|
| 70 | return this.pages.size();
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected void init() {
|
| 75 | int left = this.backgroundLeft();
|
| 76 | int top = this.backgroundTop();
|
| 77 | int padding = 8;
|
| 78 | this.page = MultiLineEditBox.builder()
|
| 79 | .setShowDecorations(false)
|
| 80 | .setTextColor(-16777216)
|
| 81 | .setCursorColor(-16777216)
|
| 82 | .setShowBackground(false)
|
| 83 | .setTextShadow(false)
|
| 84 | .setX((this.width - 114) / 2 - 8)
|
| 85 | .setY(28)
|
| 86 | .build(this.font, 122, 134, CommonComponents.EMPTY);
|
| 87 | this.page.setCharacterLimit(1024);
|
| 88 | this.page.setLineLimit(126 / 9);
|
| 89 | this.page.setValueListener(value -> this.pages.set(this.currentPage, value));
|
| 90 | this.addRenderableWidget(this.page);
|
| 91 | this.updatePageContent();
|
| 92 | this.numberOfPages = this.getPageNumberMessage();
|
| 93 | this.backButton = this.addRenderableWidget(new PageButton(left + 43, top + 157, false, button -> this.pageBack(), true));
|
| 94 | this.forwardButton = this.addRenderableWidget(new PageButton(left + 116, top + 157, true, button -> this.pageForward(), true));
|
| 95 | this.addRenderableWidget(
|
| 96 | Button.builder(SIGN_BOOK_LABEL, button -> this.minecraft.gui.setScreen(this.signScreen))
|
| 97 | .pos(this.width / 2 - 98 - 2, this.menuControlsTop())
|
| 98 | .width(98)
|
| 99 | .build()
|
| 100 | );
|
| 101 | this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> {
|
| 102 | this.minecraft.gui.setScreen(null);
|
| 103 | this.saveChanges();
|
| 104 | }).pos(this.width / 2 + 2, this.menuControlsTop()).width(98).build());
|
| 105 | this.updateButtonVisibility();
|
| 106 | }
|
| 107 |
|
| 108 | private int backgroundLeft() {
|
| 109 | return (this.width - 192) / 2;
|
| 110 | }
|
| 111 |
|
| 112 | private int backgroundTop() {
|
| 113 | return 2;
|
| 114 | }
|
| 115 |
|
| 116 | private int menuControlsTop() {
|
| 117 | return this.backgroundTop() + 192 + 2;
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | protected void setInitialFocus() {
|
| 122 | this.setInitialFocus(this.page);
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public Component getNarrationMessage() {
|
| 127 | return CommonComponents.joinForNarration(super.getNarrationMessage(), this.getPageNumberMessage());
|
| 128 | }
|
| 129 |
|
| 130 | private Component getPageNumberMessage() {
|
| 131 | return Component.translatable("book.pageIndicator", this.currentPage + 1, this.getNumPages()).withColor(-16777216).withoutShadow();
|
| 132 | }
|
| 133 |
|
| 134 | private void pageBack() {
|
| 135 | if (this.currentPage > 0) {
|
| 136 | this.currentPage--;
|
| 137 | this.updatePageContent();
|
| 138 | }
|
| 139 |
|
| 140 | this.updateButtonVisibility();
|
| 141 | }
|
| 142 |
|
| 143 | private void pageForward() {
|
| 144 | if (this.currentPage < this.getNumPages() - 1) {
|
| 145 | this.currentPage++;
|
| 146 | } else {
|
| 147 | this.appendPageToBook();
|
| 148 | if (this.currentPage < this.getNumPages() - 1) {
|
| 149 | this.currentPage++;
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | this.updatePageContent();
|
| 154 | this.updateButtonVisibility();
|
| 155 | }
|
| 156 |
|
| 157 | private void updatePageContent() {
|
| 158 | this.page.setValue(this.pages.get(this.currentPage), true);
|
| 159 | this.numberOfPages = this.getPageNumberMessage();
|
| 160 | }
|
| 161 |
|
| 162 | private void updateButtonVisibility() {
|
| 163 | this.backButton.visible = this.currentPage > 0;
|
| 164 | }
|
| 165 |
|
| 166 | private void eraseEmptyTrailingPages() {
|
| 167 | ListIterator<String> pagesIt = this.pages.listIterator(this.pages.size());
|
| 168 |
|
| 169 | while (pagesIt.hasPrevious() && pagesIt.previous().isEmpty()) {
|
| 170 | pagesIt.remove();
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | private void saveChanges() {
|
| 175 | this.eraseEmptyTrailingPages();
|
| 176 | this.updateLocalCopy();
|
| 177 | int slot = this.hand == InteractionHand.MAIN_HAND ? this.owner.getInventory().getSelectedSlot() : 40;
|
| 178 | this.minecraft.getConnection().send(new ServerboundEditBookPacket(slot, this.pages, Optional.empty()));
|
| 179 | }
|
| 180 |
|
| 181 | private void updateLocalCopy() {
|
| 182 | this.book.set(DataComponents.WRITABLE_BOOK_CONTENT, new WritableBookContent(this.pages.stream().map(Filterable::passThrough).toList()));
|
| 183 | }
|
| 184 |
|
| 185 | private void appendPageToBook() {
|
| 186 | if (this.getNumPages() < 100) {
|
| 187 | this.pages.add("");
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public boolean isInGameUi() {
|
| 193 | return true;
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | public boolean keyPressed(final KeyEvent event) {
|
| 198 | switch (event.shortcutKey()) {
|
| 199 | case 1073741899:
|
| 200 | this.backButton.onPress(event);
|
| 201 | return true;
|
| 202 | case 1073741902:
|
| 203 | this.forwardButton.onPress(event);
|
| 204 | return true;
|
| 205 | default:
|
| 206 | return super.keyPressed(event);
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 212 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 213 | this.visitText(graphics.textRenderer());
|
| 214 | }
|
| 215 |
|
| 216 | private void visitText(final ActiveTextCollector collector) {
|
| 217 | int left = this.backgroundLeft();
|
| 218 | int top = this.backgroundTop();
|
| 219 | collector.accept(TextAlignment.RIGHT, left + 148, top + 16, this.numberOfPages);
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 224 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 225 | graphics.blit(RenderPipelines.GUI_TEXTURED, BookViewScreen.BOOK_LOCATION, this.backgroundLeft(), this.backgroundTop(), 0.0F, 0.0F, 192, 192, 256, 256);
|
| 226 | }
|
| 227 | }
|
| 228 |
|