| 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.CycleButton;
|
| 9 | import net.minecraft.client.gui.components.EditBox;
|
| 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 ManageServerScreen extends Screen {
|
| 17 | private static final Component NAME_LABEL = Component.translatable("manageServer.enterName");
|
| 18 | private static final Component IP_LABEL = Component.translatable("manageServer.enterIp");
|
| 19 | private static final Component DEFAULT_SERVER_NAME = Component.translatable("selectServer.defaultName");
|
| 20 | private Button addButton;
|
| 21 | private final BooleanConsumer callback;
|
| 22 | private final ServerData serverData;
|
| 23 | private EditBox ipEdit;
|
| 24 | private EditBox nameEdit;
|
| 25 | private final Screen lastScreen;
|
| 26 |
|
| 27 | public ManageServerScreen(final Screen lastScreen, final Component title, final BooleanConsumer callback, final ServerData serverData) {
|
| 28 | super(title);
|
| 29 | this.lastScreen = lastScreen;
|
| 30 | this.callback = callback;
|
| 31 | this.serverData = serverData;
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | protected void init() {
|
| 36 | this.nameEdit = new EditBox(this.font, this.width / 2 - 100, 66, 200, 20, NAME_LABEL);
|
| 37 | this.nameEdit.setValue(this.serverData.name);
|
| 38 | this.nameEdit.setHint(DEFAULT_SERVER_NAME);
|
| 39 | this.nameEdit.setResponder(v -> this.updateAddButtonStatus());
|
| 40 | this.addWidget(this.nameEdit);
|
| 41 | this.ipEdit = new EditBox(this.font, this.width / 2 - 100, 106, 200, 20, IP_LABEL);
|
| 42 | this.ipEdit.setMaxLength(128);
|
| 43 | this.ipEdit.setValue(this.serverData.ip);
|
| 44 | this.ipEdit.setResponder(v -> this.updateAddButtonStatus());
|
| 45 | this.addWidget(this.ipEdit);
|
| 46 | this.addRenderableWidget(
|
| 47 | CycleButton.builder(ServerData.ServerPackStatus::getName, this.serverData.getResourcePackStatus())
|
| 48 | .withValues(ServerData.ServerPackStatus.values())
|
| 49 | .create(
|
| 50 | this.width / 2 - 100,
|
| 51 | this.height / 4 + 72,
|
| 52 | 200,
|
| 53 | 20,
|
| 54 | Component.translatable("manageServer.resourcePack"),
|
| 55 | (button, value) -> this.serverData.setResourcePackStatus(value)
|
| 56 | )
|
| 57 | );
|
| 58 | this.addButton = this.addRenderableWidget(
|
| 59 | Button.builder(CommonComponents.GUI_DONE, button -> this.onAdd()).bounds(this.width / 2 - 100, this.height / 4 + 96 + 18, 200, 20).build()
|
| 60 | );
|
| 61 | this.addRenderableWidget(
|
| 62 | Button.builder(CommonComponents.GUI_CANCEL, button -> this.callback.accept(false)).bounds(this.width / 2 - 100, this.height / 4 + 120 + 18, 200, 20).build()
|
| 63 | );
|
| 64 | this.updateAddButtonStatus();
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | protected void setInitialFocus() {
|
| 69 | this.setInitialFocus(this.nameEdit);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public void resize(final int width, final int height) {
|
| 74 | String oldIpEdit = this.ipEdit.getValue();
|
| 75 | String oldNameEdit = this.nameEdit.getValue();
|
| 76 | this.init(width, height);
|
| 77 | this.ipEdit.setValue(oldIpEdit);
|
| 78 | this.nameEdit.setValue(oldNameEdit);
|
| 79 | }
|
| 80 |
|
| 81 | private void onAdd() {
|
| 82 | String name = this.nameEdit.getValue();
|
| 83 | this.serverData.name = name.isEmpty() ? DEFAULT_SERVER_NAME.getString() : name;
|
| 84 | this.serverData.ip = this.ipEdit.getValue();
|
| 85 | this.callback.accept(true);
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public void onClose() {
|
| 90 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 91 | }
|
| 92 |
|
| 93 | private void updateAddButtonStatus() {
|
| 94 | this.addButton.active = ServerAddress.isValidAddress(this.ipEdit.getValue());
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 99 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 100 | graphics.centeredText(this.font, this.title, this.width / 2, 17, -1);
|
| 101 | graphics.text(this.font, NAME_LABEL, this.width / 2 - 100 + 1, 53, -6250336);
|
| 102 | graphics.text(this.font, IP_LABEL, this.width / 2 - 100 + 1, 94, -6250336);
|
| 103 | this.nameEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 104 | this.ipEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 105 | }
|
| 106 | }
|
| 107 |
|