| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.gui.components.Button;
|
| 8 | import net.minecraft.client.gui.components.CycleButton;
|
| 9 | import net.minecraft.client.gui.components.EditBox;
|
| 10 | import net.minecraft.client.gui.screens.Screen;
|
| 11 | import net.minecraft.core.BlockPos;
|
| 12 | import net.minecraft.network.chat.CommonComponents;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.protocol.game.ServerboundSetTestBlockPacket;
|
| 15 | import net.minecraft.world.level.block.Blocks;
|
| 16 | import net.minecraft.world.level.block.entity.TestBlockEntity;
|
| 17 | import net.minecraft.world.level.block.state.properties.TestBlockMode;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class TestBlockEditScreen extends Screen {
|
| 22 | private static final List<TestBlockMode> MODES = List.of(TestBlockMode.values());
|
| 23 | private static final Component TITLE = Component.translatable(Blocks.TEST_BLOCK.getDescriptionId());
|
| 24 | private static final Component MESSAGE_LABEL = Component.translatable("test_block.message");
|
| 25 | private final BlockPos position;
|
| 26 | private TestBlockMode mode;
|
| 27 | private String message;
|
| 28 | private @Nullable EditBox messageEdit;
|
| 29 |
|
| 30 | public TestBlockEditScreen(final TestBlockEntity block) {
|
| 31 | super(TITLE);
|
| 32 | this.position = block.getBlockPos();
|
| 33 | this.mode = block.getMode();
|
| 34 | this.message = block.getMessage();
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public void init() {
|
| 39 | this.messageEdit = new EditBox(this.font, this.width / 2 - 152, 80, 240, 20, Component.translatable("test_block.message"));
|
| 40 | this.messageEdit.setMaxLength(128);
|
| 41 | this.messageEdit.setValue(this.message);
|
| 42 | this.addRenderableWidget(this.messageEdit);
|
| 43 | this.updateMode(this.mode);
|
| 44 | this.addRenderableWidget(
|
| 45 | CycleButton.builder(TestBlockMode::getDisplayName, this.mode)
|
| 46 | .withValues(MODES)
|
| 47 | .displayOnlyValue()
|
| 48 | .create(this.width / 2 - 4 - 150, 185, 50, 20, TITLE, (button, value) -> this.updateMode(value))
|
| 49 | );
|
| 50 | this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> this.onDone()).bounds(this.width / 2 - 4 - 150, 210, 150, 20).build());
|
| 51 | this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onCancel()).bounds(this.width / 2 + 4, 210, 150, 20).build());
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected void setInitialFocus() {
|
| 56 | if (this.messageEdit != null) {
|
| 57 | this.setInitialFocus(this.messageEdit);
|
| 58 | } else {
|
| 59 | super.setInitialFocus();
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 65 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 66 | graphics.centeredText(this.font, this.title, this.width / 2, 10, -1);
|
| 67 | if (this.mode != TestBlockMode.START) {
|
| 68 | graphics.text(this.font, MESSAGE_LABEL, this.width / 2 - 153, 70, -6250336);
|
| 69 | }
|
| 70 |
|
| 71 | graphics.text(this.font, this.mode.getDetailedMessage(), this.width / 2 - 153, 174, -6250336);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public boolean isInGameUi() {
|
| 76 | return true;
|
| 77 | }
|
| 78 |
|
| 79 | private void onDone() {
|
| 80 | this.message = this.messageEdit.getValue();
|
| 81 | this.minecraft.getConnection().send(new ServerboundSetTestBlockPacket(this.position, this.mode, this.message));
|
| 82 | this.onClose();
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public void onClose() {
|
| 87 | this.onCancel();
|
| 88 | }
|
| 89 |
|
| 90 | private void onCancel() {
|
| 91 | this.minecraft.gui.setScreen(null);
|
| 92 | }
|
| 93 |
|
| 94 | private void updateMode(final TestBlockMode value) {
|
| 95 | this.mode = value;
|
| 96 | this.messageEdit.visible = value != TestBlockMode.START;
|
| 97 | }
|
| 98 | }
|
| 99 |
|