252 lines
11 KiB
Java
252 lines
11 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.logging.LogUtils
|
|
* org.jspecify.annotations.Nullable
|
|
* org.slf4j.Logger
|
|
*/
|
|
package net.minecraft.client.gui.screens.multiplayer;
|
|
|
|
import com.mojang.logging.LogUtils;
|
|
import java.util.List;
|
|
import net.minecraft.client.gui.components.AbstractWidget;
|
|
import net.minecraft.client.gui.components.Button;
|
|
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
|
import net.minecraft.client.gui.layouts.LinearLayout;
|
|
import net.minecraft.client.gui.screens.ConfirmScreen;
|
|
import net.minecraft.client.gui.screens.ConnectScreen;
|
|
import net.minecraft.client.gui.screens.DirectJoinServerScreen;
|
|
import net.minecraft.client.gui.screens.ManageServerScreen;
|
|
import net.minecraft.client.gui.screens.Screen;
|
|
import net.minecraft.client.gui.screens.multiplayer.ServerSelectionList;
|
|
import net.minecraft.client.input.KeyEvent;
|
|
import net.minecraft.client.multiplayer.ServerData;
|
|
import net.minecraft.client.multiplayer.ServerList;
|
|
import net.minecraft.client.multiplayer.ServerStatusPinger;
|
|
import net.minecraft.client.multiplayer.resolver.ServerAddress;
|
|
import net.minecraft.client.resources.language.I18n;
|
|
import net.minecraft.client.server.LanServer;
|
|
import net.minecraft.client.server.LanServerDetection;
|
|
import net.minecraft.network.chat.CommonComponents;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.network.chat.MutableComponent;
|
|
import org.jspecify.annotations.Nullable;
|
|
import org.slf4j.Logger;
|
|
|
|
public class JoinMultiplayerScreen
|
|
extends Screen {
|
|
private static final Logger LOGGER = LogUtils.getLogger();
|
|
private static final int TOP_ROW_BUTTON_WIDTH = 100;
|
|
private static final int LOWER_ROW_BUTTON_WIDTH = 74;
|
|
private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this, 33, 60);
|
|
private final ServerStatusPinger pinger = new ServerStatusPinger();
|
|
private final Screen lastScreen;
|
|
protected ServerSelectionList serverSelectionList;
|
|
private ServerList servers;
|
|
private Button editButton;
|
|
private Button selectButton;
|
|
private Button deleteButton;
|
|
private ServerData editingServer;
|
|
private LanServerDetection.LanServerList lanServerList;
|
|
private @Nullable LanServerDetection.LanServerDetector lanServerDetector;
|
|
|
|
public JoinMultiplayerScreen(Screen lastScreen) {
|
|
super(Component.translatable("multiplayer.title"));
|
|
this.lastScreen = lastScreen;
|
|
}
|
|
|
|
@Override
|
|
protected void init() {
|
|
this.layout.addTitleHeader(this.title, this.font);
|
|
this.servers = new ServerList(this.minecraft);
|
|
this.servers.load();
|
|
this.lanServerList = new LanServerDetection.LanServerList();
|
|
try {
|
|
this.lanServerDetector = new LanServerDetection.LanServerDetector(this.lanServerList);
|
|
this.lanServerDetector.start();
|
|
}
|
|
catch (Exception e) {
|
|
LOGGER.warn("Unable to start LAN server detection: {}", (Object)e.getMessage());
|
|
}
|
|
this.serverSelectionList = this.layout.addToContents(new ServerSelectionList(this, this.minecraft, this.width, this.layout.getContentHeight(), this.layout.getHeaderHeight(), 36));
|
|
this.serverSelectionList.updateOnlineServers(this.servers);
|
|
LinearLayout footer = this.layout.addToFooter(LinearLayout.vertical().spacing(4));
|
|
footer.defaultCellSetting().alignHorizontallyCenter();
|
|
LinearLayout topFooterButtons = footer.addChild(LinearLayout.horizontal().spacing(4));
|
|
LinearLayout bottomFooterButtons = footer.addChild(LinearLayout.horizontal().spacing(4));
|
|
this.selectButton = topFooterButtons.addChild(Button.builder(Component.translatable("selectServer.select"), button -> {
|
|
ServerSelectionList.Entry entry = (ServerSelectionList.Entry)this.serverSelectionList.getSelected();
|
|
if (entry != null) {
|
|
entry.join();
|
|
}
|
|
}).width(100).build());
|
|
topFooterButtons.addChild(Button.builder(Component.translatable("selectServer.direct"), button -> {
|
|
this.editingServer = new ServerData(I18n.get("selectServer.defaultName", new Object[0]), "", ServerData.Type.OTHER);
|
|
this.minecraft.setScreen(new DirectJoinServerScreen(this, this::directJoinCallback, this.editingServer));
|
|
}).width(100).build());
|
|
topFooterButtons.addChild(Button.builder(Component.translatable("selectServer.add"), button -> {
|
|
this.editingServer = new ServerData("", "", ServerData.Type.OTHER);
|
|
this.minecraft.setScreen(new ManageServerScreen(this, Component.translatable("manageServer.add.title"), this::addServerCallback, this.editingServer));
|
|
}).width(100).build());
|
|
this.editButton = bottomFooterButtons.addChild(Button.builder(Component.translatable("selectServer.edit"), button -> {
|
|
ServerSelectionList.Entry entry = (ServerSelectionList.Entry)this.serverSelectionList.getSelected();
|
|
if (entry instanceof ServerSelectionList.OnlineServerEntry) {
|
|
ServerData current = ((ServerSelectionList.OnlineServerEntry)entry).getServerData();
|
|
this.editingServer = new ServerData(current.name, current.ip, ServerData.Type.OTHER);
|
|
this.editingServer.copyFrom(current);
|
|
this.minecraft.setScreen(new ManageServerScreen(this, Component.translatable("manageServer.edit.title"), this::editServerCallback, this.editingServer));
|
|
}
|
|
}).width(74).build());
|
|
this.deleteButton = bottomFooterButtons.addChild(Button.builder(Component.translatable("selectServer.delete"), button -> {
|
|
String serverName;
|
|
ServerSelectionList.Entry entry = (ServerSelectionList.Entry)this.serverSelectionList.getSelected();
|
|
if (entry instanceof ServerSelectionList.OnlineServerEntry && (serverName = ((ServerSelectionList.OnlineServerEntry)entry).getServerData().name) != null) {
|
|
MutableComponent title = Component.translatable("selectServer.deleteQuestion");
|
|
MutableComponent warning = Component.translatable("selectServer.deleteWarning", serverName);
|
|
MutableComponent yes = Component.translatable("selectServer.deleteButton");
|
|
Component no = CommonComponents.GUI_CANCEL;
|
|
this.minecraft.setScreen(new ConfirmScreen(this::deleteCallback, title, warning, yes, no));
|
|
}
|
|
}).width(74).build());
|
|
bottomFooterButtons.addChild(Button.builder(Component.translatable("selectServer.refresh"), button -> this.refreshServerList()).width(74).build());
|
|
bottomFooterButtons.addChild(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).width(74).build());
|
|
this.layout.visitWidgets(x$0 -> {
|
|
AbstractWidget cfr_ignored_0 = (AbstractWidget)this.addRenderableWidget(x$0);
|
|
});
|
|
this.repositionElements();
|
|
this.onSelectedChange();
|
|
}
|
|
|
|
@Override
|
|
protected void repositionElements() {
|
|
this.layout.arrangeElements();
|
|
if (this.serverSelectionList != null) {
|
|
this.serverSelectionList.updateSize(this.width, this.layout);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onClose() {
|
|
this.minecraft.setScreen(this.lastScreen);
|
|
}
|
|
|
|
@Override
|
|
public void tick() {
|
|
super.tick();
|
|
List<LanServer> lanServers = this.lanServerList.takeDirtyServers();
|
|
if (lanServers != null) {
|
|
this.serverSelectionList.updateNetworkServers(lanServers);
|
|
}
|
|
this.pinger.tick();
|
|
}
|
|
|
|
@Override
|
|
public void removed() {
|
|
if (this.lanServerDetector != null) {
|
|
this.lanServerDetector.interrupt();
|
|
this.lanServerDetector = null;
|
|
}
|
|
this.pinger.removeAll();
|
|
this.serverSelectionList.removed();
|
|
}
|
|
|
|
private void refreshServerList() {
|
|
this.minecraft.setScreen(new JoinMultiplayerScreen(this.lastScreen));
|
|
}
|
|
|
|
private void deleteCallback(boolean result) {
|
|
ServerSelectionList.Entry entry = (ServerSelectionList.Entry)this.serverSelectionList.getSelected();
|
|
if (result && entry instanceof ServerSelectionList.OnlineServerEntry) {
|
|
this.servers.remove(((ServerSelectionList.OnlineServerEntry)entry).getServerData());
|
|
this.servers.save();
|
|
this.serverSelectionList.setSelected((ServerSelectionList.Entry)null);
|
|
this.serverSelectionList.updateOnlineServers(this.servers);
|
|
}
|
|
this.minecraft.setScreen(this);
|
|
}
|
|
|
|
private void editServerCallback(boolean result) {
|
|
ServerSelectionList.Entry entry = (ServerSelectionList.Entry)this.serverSelectionList.getSelected();
|
|
if (result && entry instanceof ServerSelectionList.OnlineServerEntry) {
|
|
ServerData current = ((ServerSelectionList.OnlineServerEntry)entry).getServerData();
|
|
current.name = this.editingServer.name;
|
|
current.ip = this.editingServer.ip;
|
|
current.copyFrom(this.editingServer);
|
|
this.servers.save();
|
|
this.serverSelectionList.updateOnlineServers(this.servers);
|
|
}
|
|
this.minecraft.setScreen(this);
|
|
}
|
|
|
|
private void addServerCallback(boolean result) {
|
|
if (result) {
|
|
ServerData serverData = this.servers.unhide(this.editingServer.ip);
|
|
if (serverData != null) {
|
|
serverData.copyNameIconFrom(this.editingServer);
|
|
this.servers.save();
|
|
} else {
|
|
this.servers.add(this.editingServer, false);
|
|
this.servers.save();
|
|
}
|
|
this.serverSelectionList.setSelected((ServerSelectionList.Entry)null);
|
|
this.serverSelectionList.updateOnlineServers(this.servers);
|
|
}
|
|
this.minecraft.setScreen(this);
|
|
}
|
|
|
|
private void directJoinCallback(boolean result) {
|
|
if (result) {
|
|
ServerData serverData = this.servers.get(this.editingServer.ip);
|
|
if (serverData == null) {
|
|
this.servers.add(this.editingServer, true);
|
|
this.servers.save();
|
|
this.join(this.editingServer);
|
|
} else {
|
|
this.join(serverData);
|
|
}
|
|
} else {
|
|
this.minecraft.setScreen(this);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean keyPressed(KeyEvent event) {
|
|
if (super.keyPressed(event)) {
|
|
return true;
|
|
}
|
|
if (event.key() == 294) {
|
|
this.refreshServerList();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void join(ServerData data) {
|
|
ConnectScreen.startConnecting(this, this.minecraft, ServerAddress.parseString(data.ip), data, false, null);
|
|
}
|
|
|
|
protected void onSelectedChange() {
|
|
this.selectButton.active = false;
|
|
this.editButton.active = false;
|
|
this.deleteButton.active = false;
|
|
ServerSelectionList.Entry entry = (ServerSelectionList.Entry)this.serverSelectionList.getSelected();
|
|
if (entry != null && !(entry instanceof ServerSelectionList.LANHeader)) {
|
|
this.selectButton.active = true;
|
|
if (entry instanceof ServerSelectionList.OnlineServerEntry) {
|
|
this.editButton.active = true;
|
|
this.deleteButton.active = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
public ServerStatusPinger getPinger() {
|
|
return this.pinger;
|
|
}
|
|
|
|
public ServerList getServers() {
|
|
return this.servers;
|
|
}
|
|
}
|
|
|