| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Optional;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.ChatFormatting;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.gui.components.Button;
|
| 10 | import net.minecraft.client.gui.components.EditBox;
|
| 11 | import net.minecraft.client.gui.screens.Screen;
|
| 12 | import net.minecraft.client.input.KeyEvent;
|
| 13 | import net.minecraft.client.renderer.RenderPipelines;
|
| 14 | import net.minecraft.network.chat.CommonComponents;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.protocol.game.ServerboundEditBookPacket;
|
| 17 | import net.minecraft.util.StringUtil;
|
| 18 | import net.minecraft.world.InteractionHand;
|
| 19 | import net.minecraft.world.entity.player.Player;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class BookSignScreen extends Screen {
|
| 23 | private static final Component EDIT_TITLE_LABEL = Component.translatable("book.editTitle");
|
| 24 | private static final Component FINALIZE_WARNING_LABEL = Component.translatable("book.finalizeWarning");
|
| 25 | private static final Component TITLE = Component.translatable("book.sign.title");
|
| 26 | private static final Component TITLE_EDIT_BOX = Component.translatable("book.sign.titlebox");
|
| 27 | private final BookEditScreen bookEditScreen;
|
| 28 | private final Player owner;
|
| 29 | private final List<String> pages;
|
| 30 | private final InteractionHand hand;
|
| 31 | private final Component ownerText;
|
| 32 | private EditBox titleBox;
|
| 33 | private String titleValue = "";
|
| 34 |
|
| 35 | public BookSignScreen(final BookEditScreen bookEditScreen, final Player owner, final InteractionHand hand, final List<String> pages) {
|
| 36 | super(TITLE);
|
| 37 | this.bookEditScreen = bookEditScreen;
|
| 38 | this.owner = owner;
|
| 39 | this.hand = hand;
|
| 40 | this.pages = pages;
|
| 41 | this.ownerText = Component.translatable("book.byAuthor", owner.getName()).withStyle(ChatFormatting.DARK_GRAY);
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected void init() {
|
| 46 | Button finalizeButton = Button.builder(Component.translatable("book.finalizeButton"), button -> {
|
| 47 | this.saveChanges();
|
| 48 | this.minecraft.gui.setScreen(null);
|
| 49 | }).bounds(this.width / 2 - 100, 196, 98, 20).build();
|
| 50 | finalizeButton.active = false;
|
| 51 | this.titleBox = this.addRenderableWidget(new EditBox(this.minecraft.font, (this.width - 114) / 2 - 3, 50, 114, 20, TITLE_EDIT_BOX));
|
| 52 | this.titleBox.setMaxLength(15);
|
| 53 | this.titleBox.setBordered(false);
|
| 54 | this.titleBox.setCentered(true);
|
| 55 | this.titleBox.setTextColor(-16777216);
|
| 56 | this.titleBox.setTextShadow(false);
|
| 57 | this.titleBox.setResponder(value -> finalizeButton.active = !StringUtil.isBlank(value));
|
| 58 | this.titleBox.setValue(this.titleValue);
|
| 59 | this.addRenderableWidget(finalizeButton);
|
| 60 | this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> {
|
| 61 | this.titleValue = this.titleBox.getValue();
|
| 62 | this.minecraft.gui.setScreen(this.bookEditScreen);
|
| 63 | }).bounds(this.width / 2 + 2, 196, 98, 20).build());
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected void setInitialFocus() {
|
| 68 | this.setInitialFocus(this.titleBox);
|
| 69 | }
|
| 70 |
|
| 71 | private void saveChanges() {
|
| 72 | int slot = this.hand == InteractionHand.MAIN_HAND ? this.owner.getInventory().getSelectedSlot() : 40;
|
| 73 | this.minecraft.getConnection().send(new ServerboundEditBookPacket(slot, this.pages, Optional.of(this.titleBox.getValue().trim())));
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public boolean isInGameUi() {
|
| 78 | return true;
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public boolean keyPressed(final KeyEvent event) {
|
| 83 | if (this.titleBox.isFocused() && !this.titleBox.getValue().isEmpty() && event.isConfirmation()) {
|
| 84 | this.saveChanges();
|
| 85 | this.minecraft.gui.setScreen(null);
|
| 86 | return true;
|
| 87 | } else {
|
| 88 | return super.keyPressed(event);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 94 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 95 | int xo = (this.width - 192) / 2;
|
| 96 | int yo = 2;
|
| 97 | int titleHeaderWidth = this.font.width(EDIT_TITLE_LABEL);
|
| 98 | graphics.text(this.font, EDIT_TITLE_LABEL, xo + 36 + (114 - titleHeaderWidth) / 2, 34, -16777216, false);
|
| 99 | int nameWidth = this.font.width(this.ownerText);
|
| 100 | graphics.text(this.font, this.ownerText, xo + 36 + (114 - nameWidth) / 2, 60, -16777216, false);
|
| 101 | graphics.textWithWordWrap(this.font, FINALIZE_WARNING_LABEL, xo + 36, 82, 114, -16777216, false);
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 106 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 107 | graphics.blit(RenderPipelines.GUI_TEXTURED, BookViewScreen.BOOK_LOCATION, (this.width - 192) / 2, 2, 0.0F, 0.0F, 192, 192, 256, 256);
|
| 108 | }
|
| 109 | }
|
| 110 |
|