| 1 | package net.minecraft.client.gui.screens.multiplayer;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.gui.components.Button;
|
| 8 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 9 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 10 | import net.minecraft.client.gui.screens.ConfirmScreen;
|
| 11 | import net.minecraft.client.gui.screens.ConnectScreen;
|
| 12 | import net.minecraft.client.gui.screens.DirectJoinServerScreen;
|
| 13 | import net.minecraft.client.gui.screens.ManageServerScreen;
|
| 14 | import net.minecraft.client.gui.screens.Screen;
|
| 15 | import net.minecraft.client.input.KeyEvent;
|
| 16 | import net.minecraft.client.multiplayer.ServerData;
|
| 17 | import net.minecraft.client.multiplayer.ServerList;
|
| 18 | import net.minecraft.client.multiplayer.ServerStatusPinger;
|
| 19 | import net.minecraft.client.multiplayer.resolver.ServerAddress;
|
| 20 | import net.minecraft.client.resources.language.I18n;
|
| 21 | import net.minecraft.client.server.LanServer;
|
| 22 | import net.minecraft.client.server.LanServerDetection;
|
| 23 | import net.minecraft.network.chat.CommonComponents;
|
| 24 | import net.minecraft.network.chat.Component;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 | import org.slf4j.Logger;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | public class JoinMultiplayerScreen extends Screen {
|
| 30 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 31 | private static final int TOP_ROW_BUTTON_WIDTH = 100;
|
| 32 | private static final int LOWER_ROW_BUTTON_WIDTH = 74;
|
| 33 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this, 33, 60);
|
| 34 | private final ServerStatusPinger pinger = new ServerStatusPinger();
|
| 35 | private final Screen lastScreen;
|
| 36 | protected ServerSelectionList serverSelectionList;
|
| 37 | private ServerList servers;
|
| 38 | private Button editButton;
|
| 39 | private Button joinButton;
|
| 40 | private Button deleteButton;
|
| 41 | private ServerData editingServer;
|
| 42 | private LanServerDetection.LanServerList lanServerList;
|
| 43 | private LanServerDetection.@Nullable LanServerDetector lanServerDetector;
|
| 44 |
|
| 45 | public JoinMultiplayerScreen(final Screen lastScreen) {
|
| 46 | super(Component.translatable("multiplayer.title"));
|
| 47 | this.lastScreen = lastScreen;
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected void init() {
|
| 52 | this.layout.addTitleHeader(this.title, this.font);
|
| 53 | this.servers = new ServerList(this.minecraft);
|
| 54 | this.servers.load();
|
| 55 | this.lanServerList = new LanServerDetection.LanServerList();
|
| 56 |
|
| 57 | try {
|
| 58 | this.lanServerDetector = new LanServerDetection.LanServerDetector(this.lanServerList);
|
| 59 | this.lanServerDetector.start();
|
| 60 | } catch (Exception e) {
|
| 61 | LOGGER.warn("Unable to start LAN server detection", e);
|
| 62 | }
|
| 63 |
|
| 64 | this.serverSelectionList = this.layout
|
| 65 | .addToContents(new ServerSelectionList(this, this.minecraft, this.width, this.layout.getContentHeight(), this.layout.getHeaderHeight(), 36));
|
| 66 | this.serverSelectionList.updateOnlineServers(this.servers);
|
| 67 | LinearLayout footer = this.layout.addToFooter(LinearLayout.vertical().spacing(4));
|
| 68 | footer.defaultCellSetting().alignHorizontallyCenter();
|
| 69 | LinearLayout topFooterButtons = footer.addChild(LinearLayout.horizontal().spacing(4));
|
| 70 | LinearLayout bottomFooterButtons = footer.addChild(LinearLayout.horizontal().spacing(4));
|
| 71 | this.joinButton = topFooterButtons.addChild(Button.builder(Component.translatable("selectServer.select"), var1x -> {
|
| 72 | ServerSelectionList.Entry entry = this.serverSelectionList.getSelected();
|
| 73 | if (entry != null) {
|
| 74 | entry.join();
|
| 75 | }
|
| 76 | }).width(100).build());
|
| 77 | topFooterButtons.addChild(Button.builder(Component.translatable("selectServer.direct"), var1x -> {
|
| 78 | this.editingServer = new ServerData(I18n.get("selectServer.defaultName"), "", ServerData.Type.OTHER);
|
| 79 | this.minecraft.gui.setScreen(new DirectJoinServerScreen(this, this::directJoinCallback, this.editingServer));
|
| 80 | }).width(100).build());
|
| 81 | topFooterButtons.addChild(Button.builder(Component.translatable("selectServer.add"), var1x -> {
|
| 82 | this.editingServer = new ServerData("", "", ServerData.Type.OTHER);
|
| 83 | this.minecraft.gui.setScreen(new ManageServerScreen(this, Component.translatable("manageServer.add.title"), this::addServerCallback, this.editingServer));
|
| 84 | }).width(100).build());
|
| 85 | this.editButton = bottomFooterButtons.addChild(Button.builder(Component.translatable("selectServer.edit"), var1x -> {
|
| 86 | ServerSelectionList.Entry entry = this.serverSelectionList.getSelected();
|
| 87 | if (entry instanceof ServerSelectionList.OnlineServerEntry onlineServerEntry) {
|
| 88 | ServerData current = onlineServerEntry.getServerData();
|
| 89 | this.editingServer = new ServerData(current.name, current.ip, ServerData.Type.OTHER);
|
| 90 | this.editingServer.copyFrom(current);
|
| 91 | this.minecraft.gui.setScreen(new ManageServerScreen(this, Component.translatable("manageServer.edit.title"), this::editServerCallback, this.editingServer));
|
| 92 | }
|
| 93 | }).width(74).build());
|
| 94 | this.deleteButton = bottomFooterButtons.addChild(Button.builder(Component.translatable("selectServer.delete"), var1x -> {
|
| 95 | ServerSelectionList.Entry entry = this.serverSelectionList.getSelected();
|
| 96 | if (entry instanceof ServerSelectionList.OnlineServerEntry onlineServerEntry) {
|
| 97 | String serverName = onlineServerEntry.getServerData().name;
|
| 98 | if (serverName != null) {
|
| 99 | Component title = Component.translatable("selectServer.deleteQuestion");
|
| 100 | Component warning = Component.translatable("selectServer.deleteWarning", serverName);
|
| 101 | Component yes = Component.translatable("selectServer.deleteButton");
|
| 102 | Component no = CommonComponents.GUI_CANCEL;
|
| 103 | this.minecraft.gui.setScreen(new ConfirmScreen(this::deleteCallback, title, warning, yes, no));
|
| 104 | }
|
| 105 | }
|
| 106 | }).width(74).build());
|
| 107 | bottomFooterButtons.addChild(Button.builder(Component.translatable("selectServer.refresh"), var1x -> this.refreshServerList()).width(74).build());
|
| 108 | bottomFooterButtons.addChild(Button.builder(CommonComponents.GUI_BACK, var1x -> this.onClose()).width(74).build());
|
| 109 | JoinMultiplayerScreen var4 = this;
|
| 110 | this.layout.visitWidgets(x$0 -> var4.addRenderableWidget(x$0));
|
| 111 | this.repositionElements();
|
| 112 | this.onSelectedChange();
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | protected void repositionElements() {
|
| 117 | this.layout.arrangeElements();
|
| 118 | if (this.serverSelectionList != null) {
|
| 119 | this.serverSelectionList.updateSize(this.width, this.layout);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public void onClose() {
|
| 125 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public void tick() {
|
| 130 | super.tick();
|
| 131 | List<LanServer> lanServers = this.lanServerList.takeDirtyServers();
|
| 132 | if (lanServers != null) {
|
| 133 | this.serverSelectionList.updateNetworkServers(lanServers);
|
| 134 | }
|
| 135 |
|
| 136 | this.pinger.tick();
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public void removed() {
|
| 141 | if (this.lanServerDetector != null) {
|
| 142 | this.lanServerDetector.interrupt();
|
| 143 | this.lanServerDetector = null;
|
| 144 | }
|
| 145 |
|
| 146 | this.pinger.removeAll();
|
| 147 | this.serverSelectionList.removed();
|
| 148 | }
|
| 149 |
|
| 150 | private void refreshServerList() {
|
| 151 | this.minecraft.gui.setScreen(new JoinMultiplayerScreen(this.lastScreen));
|
| 152 | }
|
| 153 |
|
| 154 | private void deleteCallback(final boolean result) {
|
| 155 | ServerSelectionList.Entry entry = this.serverSelectionList.getSelected();
|
| 156 | if (result && entry instanceof ServerSelectionList.OnlineServerEntry onlineServerEntry) {
|
| 157 | this.servers.remove(onlineServerEntry.getServerData());
|
| 158 | this.servers.save();
|
| 159 | this.serverSelectionList.setSelected((ServerSelectionList.Entry)null);
|
| 160 | this.serverSelectionList.updateOnlineServers(this.servers);
|
| 161 | }
|
| 162 |
|
| 163 | this.minecraft.gui.setScreen(this);
|
| 164 | }
|
| 165 |
|
| 166 | private void editServerCallback(final boolean result) {
|
| 167 | ServerSelectionList.Entry entry = this.serverSelectionList.getSelected();
|
| 168 | if (result && entry instanceof ServerSelectionList.OnlineServerEntry onlineServerEntry) {
|
| 169 | ServerData current = onlineServerEntry.getServerData();
|
| 170 | current.name = this.editingServer.name;
|
| 171 | current.ip = this.editingServer.ip;
|
| 172 | current.setState(ServerData.State.INITIAL);
|
| 173 | current.setResourcePackStatus(this.editingServer.getResourcePackStatus());
|
| 174 | this.servers.save();
|
| 175 | this.serverSelectionList.updateOnlineServers(this.servers);
|
| 176 | }
|
| 177 |
|
| 178 | this.minecraft.gui.setScreen(this);
|
| 179 | }
|
| 180 |
|
| 181 | private void addServerCallback(final boolean result) {
|
| 182 | if (result) {
|
| 183 | ServerData serverData = this.servers.unhide(this.editingServer.ip);
|
| 184 | if (serverData != null) {
|
| 185 | serverData.copyNameIconFrom(this.editingServer);
|
| 186 | this.servers.save();
|
| 187 | } else {
|
| 188 | this.servers.add(this.editingServer, false);
|
| 189 | this.servers.save();
|
| 190 | }
|
| 191 |
|
| 192 | this.serverSelectionList.setSelected((ServerSelectionList.Entry)null);
|
| 193 | this.serverSelectionList.updateOnlineServers(this.servers);
|
| 194 | }
|
| 195 |
|
| 196 | this.minecraft.gui.setScreen(this);
|
| 197 | }
|
| 198 |
|
| 199 | private void directJoinCallback(final boolean result) {
|
| 200 | if (result) {
|
| 201 | ServerData serverData = this.servers.get(this.editingServer.ip);
|
| 202 | if (serverData == null) {
|
| 203 | this.servers.add(this.editingServer, true);
|
| 204 | this.servers.save();
|
| 205 | this.join(this.editingServer);
|
| 206 | } else {
|
| 207 | this.join(serverData);
|
| 208 | }
|
| 209 | } else {
|
| 210 | this.minecraft.gui.setScreen(this);
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | public boolean keyPressed(final KeyEvent event) {
|
| 216 | if (super.keyPressed(event)) {
|
| 217 | return true;
|
| 218 | } else if (event.shortcutKey() == 1073741886) {
|
| 219 | this.refreshServerList();
|
| 220 | return true;
|
| 221 | } else {
|
| 222 | return false;
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | public void join(final ServerData data) {
|
| 227 | ConnectScreen.startConnecting(this, this.minecraft, ServerAddress.parseString(data.ip), data, false, null);
|
| 228 | }
|
| 229 |
|
| 230 | protected void onSelectedChange() {
|
| 231 | this.joinButton.active = false;
|
| 232 | this.editButton.active = false;
|
| 233 | this.deleteButton.active = false;
|
| 234 | ServerSelectionList.Entry entry = this.serverSelectionList.getSelected();
|
| 235 | if (entry != null && !(entry instanceof ServerSelectionList.LANHeader)) {
|
| 236 | this.joinButton.active = true;
|
| 237 | if (entry instanceof ServerSelectionList.OnlineServerEntry) {
|
| 238 | this.editButton.active = true;
|
| 239 | this.deleteButton.active = true;
|
| 240 | }
|
| 241 | }
|
| 242 | }
|
| 243 |
|
| 244 | public ServerStatusPinger getPinger() {
|
| 245 | return this.pinger;
|
| 246 | }
|
| 247 |
|
| 248 | public ServerList getServers() {
|
| 249 | return this.servers;
|
| 250 | }
|
| 251 | }
|
| 252 |
|