| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.GameNarrator;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.gui.components.Button;
|
| 8 | import net.minecraft.client.gui.components.CommandSuggestions;
|
| 9 | import net.minecraft.client.gui.components.CycleButton;
|
| 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.input.MouseButtonEvent;
|
| 14 | import net.minecraft.network.chat.CommonComponents;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.chat.MutableComponent;
|
| 17 | import net.minecraft.world.level.BaseCommandBlock;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public abstract class AbstractCommandBlockEditScreen extends Screen {
|
| 21 | private static final Component SET_COMMAND_LABEL = Component.translatable("advMode.setCommand");
|
| 22 | private static final Component COMMAND_LABEL = Component.translatable("advMode.command");
|
| 23 | private static final Component PREVIOUS_OUTPUT_LABEL = Component.translatable("advMode.previousOutput");
|
| 24 | protected EditBox commandEdit;
|
| 25 | protected EditBox previousEdit;
|
| 26 | protected Button doneButton;
|
| 27 | protected Button cancelButton;
|
| 28 | protected CycleButton<Boolean> outputButton;
|
| 29 | private CommandSuggestions commandSuggestions;
|
| 30 |
|
| 31 | public AbstractCommandBlockEditScreen() {
|
| 32 | super(GameNarrator.NO_TITLE);
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public void tick() {
|
| 37 | if (!this.getCommandBlock().isValid()) {
|
| 38 | this.onClose();
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | protected abstract BaseCommandBlock getCommandBlock();
|
| 43 |
|
| 44 | protected abstract int getPreviousY();
|
| 45 |
|
| 46 | @Override
|
| 47 | protected void init() {
|
| 48 | boolean trackOutput = this.getCommandBlock().isTrackOutput();
|
| 49 | this.commandEdit = new EditBox(this.font, this.width / 2 - 150, 50, 300, 20, Component.translatable("advMode.command")) {
|
| 50 | @Override
|
| 51 | protected MutableComponent createNarrationMessage() {
|
| 52 | return super.createNarrationMessage().append(AbstractCommandBlockEditScreen.this.commandSuggestions.getNarrationMessage());
|
| 53 | }
|
| 54 | };
|
| 55 | this.commandEdit.setMaxLength(32500);
|
| 56 | this.commandEdit.setResponder(this::onEdited);
|
| 57 | this.addWidget(this.commandEdit);
|
| 58 | this.previousEdit = new EditBox(this.font, this.width / 2 - 150, this.getPreviousY(), 276, 20, Component.translatable("advMode.previousOutput"));
|
| 59 | this.previousEdit.setMaxLength(32500);
|
| 60 | this.previousEdit.setEditable(false);
|
| 61 | this.previousEdit.setValue("-");
|
| 62 | this.addWidget(this.previousEdit);
|
| 63 | this.outputButton = this.addRenderableWidget(
|
| 64 | CycleButton.booleanBuilder(Component.literal("O"), Component.literal("X"), trackOutput)
|
| 65 | .displayOnlyValue()
|
| 66 | .create(this.width / 2 + 150 - 20, this.getPreviousY(), 20, 20, Component.translatable("advMode.trackOutput"), (button, value) -> {
|
| 67 | BaseCommandBlock commandBlock = this.getCommandBlock();
|
| 68 | commandBlock.setTrackOutput(value);
|
| 69 | this.updatePreviousOutput(value);
|
| 70 | })
|
| 71 | );
|
| 72 | this.addExtraControls();
|
| 73 | this.doneButton = this.addRenderableWidget(
|
| 74 | Button.builder(CommonComponents.GUI_DONE, button -> this.onDone()).bounds(this.width / 2 - 4 - 150, this.height / 4 + 120 + 12, 150, 20).build()
|
| 75 | );
|
| 76 | this.cancelButton = this.addRenderableWidget(
|
| 77 | Button.builder(CommonComponents.GUI_CANCEL, button -> this.onClose()).bounds(this.width / 2 + 4, this.height / 4 + 120 + 12, 150, 20).build()
|
| 78 | );
|
| 79 | this.commandSuggestions = new CommandSuggestions(this.minecraft, this, this.commandEdit, this.font, true, true, 0, 7, false, Integer.MIN_VALUE);
|
| 80 | this.commandSuggestions.setAllowSuggestions(true);
|
| 81 | this.commandSuggestions.updateCommandInfo();
|
| 82 | this.updatePreviousOutput(trackOutput);
|
| 83 | }
|
| 84 |
|
| 85 | protected void addExtraControls() {
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected void setInitialFocus() {
|
| 90 | this.setInitialFocus(this.commandEdit);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected Component getUsageNarration() {
|
| 95 | return this.commandSuggestions.isVisible() ? this.commandSuggestions.getUsageNarration() : super.getUsageNarration();
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void resize(final int width, final int height) {
|
| 100 | String oldText = this.commandEdit.getValue();
|
| 101 | this.init(width, height);
|
| 102 | this.commandEdit.setValue(oldText);
|
| 103 | this.commandSuggestions.updateCommandInfo();
|
| 104 | }
|
| 105 |
|
| 106 | protected void updatePreviousOutput(final boolean isTracking) {
|
| 107 | this.previousEdit.setValue(isTracking ? this.getCommandBlock().getLastOutput().getString() : "-");
|
| 108 | }
|
| 109 |
|
| 110 | protected void onDone() {
|
| 111 | this.populateAndSendPacket();
|
| 112 | BaseCommandBlock commandBlock = this.getCommandBlock();
|
| 113 | if (!commandBlock.isTrackOutput()) {
|
| 114 | commandBlock.setLastOutput(null);
|
| 115 | }
|
| 116 |
|
| 117 | this.minecraft.gui.setScreen(null);
|
| 118 | }
|
| 119 |
|
| 120 | protected abstract void populateAndSendPacket();
|
| 121 |
|
| 122 | private void onEdited(final String value) {
|
| 123 | this.commandSuggestions.updateCommandInfo();
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public boolean isInGameUi() {
|
| 128 | return true;
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public boolean keyPressed(final KeyEvent event) {
|
| 133 | if (this.commandSuggestions.keyPressed(event)) {
|
| 134 | return true;
|
| 135 | } else if (super.keyPressed(event)) {
|
| 136 | return true;
|
| 137 | } else if (event.isConfirmation()) {
|
| 138 | this.onDone();
|
| 139 | return true;
|
| 140 | } else {
|
| 141 | return false;
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY) {
|
| 147 | return this.commandSuggestions.mouseScrolled(scrollY) ? true : super.mouseScrolled(x, y, scrollX, scrollY);
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 152 | return this.commandSuggestions.mouseClicked(event) ? true : super.mouseClicked(event, doubleClick);
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 157 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 158 | graphics.centeredText(this.font, SET_COMMAND_LABEL, this.width / 2, 20, -1);
|
| 159 | graphics.text(this.font, COMMAND_LABEL, this.width / 2 - 150 + 1, 40, -6250336);
|
| 160 | this.commandEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 161 | int y = 75;
|
| 162 | if (!this.previousEdit.getValue().isEmpty()) {
|
| 163 | y += 5 * 9 + 1 + this.getPreviousY() - 135;
|
| 164 | graphics.text(this.font, PREVIOUS_OUTPUT_LABEL, this.width / 2 - 150 + 1, y + 4, -6250336);
|
| 165 | this.previousEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 166 | }
|
| 167 |
|
| 168 | this.commandSuggestions.extractRenderState(graphics, mouseX, mouseY);
|
| 169 | }
|
| 170 | }
|
| 171 |
|