| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
|
| 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.EditBox;
|
| 9 | import net.minecraft.client.input.KeyEvent;
|
| 10 | import net.minecraft.client.multiplayer.ServerData;
|
| 11 | import net.minecraft.client.multiplayer.resolver.ServerAddress;
|
| 12 | import net.minecraft.network.chat.CommonComponents;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class DirectJoinServerScreen extends Screen {
|
| 17 | private static final Component ENTER_IP_LABEL = Component.translatable("manageServer.enterIp");
|
| 18 | private Button selectButton;
|
| 19 | private final ServerData serverData;
|
| 20 | private EditBox ipEdit;
|
| 21 | private final BooleanConsumer callback;
|
| 22 | private final Screen lastScreen;
|
| 23 |
|
| 24 | public DirectJoinServerScreen(final Screen lastScreen, final BooleanConsumer callback, final ServerData serverData) {
|
| 25 | super(Component.translatable("selectServer.direct"));
|
| 26 | this.lastScreen = lastScreen;
|
| 27 | this.serverData = serverData;
|
| 28 | this.callback = callback;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public boolean keyPressed(final KeyEvent event) {
|
| 33 | if (this.selectButton.active && this.getFocused() == this.ipEdit && event.isConfirmation()) {
|
| 34 | this.onSelect();
|
| 35 | return true;
|
| 36 | } else {
|
| 37 | return super.keyPressed(event);
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected void init() {
|
| 43 | this.ipEdit = new EditBox(this.font, this.width / 2 - 100, 116, 200, 20, ENTER_IP_LABEL);
|
| 44 | this.ipEdit.setMaxLength(128);
|
| 45 | this.ipEdit.setValue(this.minecraft.options.lastMpIp);
|
| 46 | this.ipEdit.setResponder(value -> this.updateSelectButtonStatus());
|
| 47 | this.addWidget(this.ipEdit);
|
| 48 | this.selectButton = this.addRenderableWidget(
|
| 49 | Button.builder(Component.translatable("selectServer.select"), button -> this.onSelect())
|
| 50 | .bounds(this.width / 2 - 100, this.height / 4 + 96 + 12, 200, 20)
|
| 51 | .build()
|
| 52 | );
|
| 53 | this.addRenderableWidget(
|
| 54 | Button.builder(CommonComponents.GUI_CANCEL, button -> this.callback.accept(false)).bounds(this.width / 2 - 100, this.height / 4 + 120 + 12, 200, 20).build()
|
| 55 | );
|
| 56 | this.updateSelectButtonStatus();
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected void setInitialFocus() {
|
| 61 | this.setInitialFocus(this.ipEdit);
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public void resize(final int width, final int height) {
|
| 66 | String oldEdit = this.ipEdit.getValue();
|
| 67 | this.init(width, height);
|
| 68 | this.ipEdit.setValue(oldEdit);
|
| 69 | }
|
| 70 |
|
| 71 | private void onSelect() {
|
| 72 | this.serverData.ip = this.ipEdit.getValue();
|
| 73 | this.callback.accept(true);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void onClose() {
|
| 78 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public void removed() {
|
| 83 | this.minecraft.options.lastMpIp = this.ipEdit.getValue();
|
| 84 | this.minecraft.options.save();
|
| 85 | }
|
| 86 |
|
| 87 | private void updateSelectButtonStatus() {
|
| 88 | this.selectButton.active = ServerAddress.isValidAddress(this.ipEdit.getValue());
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 93 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 94 | graphics.centeredText(this.font, this.title, this.width / 2, 20, -1);
|
| 95 | graphics.text(this.font, ENTER_IP_LABEL, this.width / 2 - 100 + 1, 100, -6250336);
|
| 96 | this.ipEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 97 | }
|
| 98 | }
|
| 99 |
|