| 1 | package net.minecraft.client.quickplay;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.RealmsMainScreen;
|
| 5 | import com.mojang.realmsclient.client.RealmsClient;
|
| 6 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 7 | import com.mojang.realmsclient.dto.RealmsServerList;
|
| 8 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 9 | import com.mojang.realmsclient.gui.screens.RealmsLongRunningMcoTaskScreen;
|
| 10 | import com.mojang.realmsclient.util.task.GetServerDetailsTask;
|
| 11 | import java.util.List;
|
| 12 | import java.util.concurrent.ExecutionException;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.client.Minecraft;
|
| 16 | import net.minecraft.client.gui.screens.ConnectScreen;
|
| 17 | import net.minecraft.client.gui.screens.DisconnectedScreen;
|
| 18 | import net.minecraft.client.gui.screens.Screen;
|
| 19 | import net.minecraft.client.gui.screens.TitleScreen;
|
| 20 | import net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen;
|
| 21 | import net.minecraft.client.gui.screens.worldselection.SelectWorldScreen;
|
| 22 | import net.minecraft.client.main.GameConfig;
|
| 23 | import net.minecraft.client.multiplayer.ServerData;
|
| 24 | import net.minecraft.client.multiplayer.ServerList;
|
| 25 | import net.minecraft.client.multiplayer.resolver.ServerAddress;
|
| 26 | import net.minecraft.client.resources.language.I18n;
|
| 27 | import net.minecraft.network.chat.Component;
|
| 28 | import net.minecraft.util.StringUtil;
|
| 29 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 30 | import net.minecraft.world.level.storage.LevelSummary;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 | import org.slf4j.Logger;
|
| 33 |
|
| 34 | @Environment(EnvType.CLIENT)
|
| 35 | public class QuickPlay {
|
| 36 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 37 | public static final Component ERROR_TITLE = Component.translatable("quickplay.error.title");
|
| 38 | private static final Component INVALID_IDENTIFIER = Component.translatable("quickplay.error.invalid_identifier");
|
| 39 | private static final Component REALM_CONNECT = Component.translatable("quickplay.error.realm_connect");
|
| 40 | private static final Component REALM_PERMISSION = Component.translatable("quickplay.error.realm_permission");
|
| 41 | private static final Component TO_TITLE = Component.translatable("gui.toTitle");
|
| 42 | private static final Component TO_WORLD_LIST = Component.translatable("gui.toWorld");
|
| 43 | private static final Component TO_REALMS_LIST = Component.translatable("gui.toRealms");
|
| 44 |
|
| 45 | public static void connect(final Minecraft minecraft, final GameConfig.QuickPlayVariant quickPlayVariant, final RealmsClient realmsClient) {
|
| 46 | if (!quickPlayVariant.isEnabled()) {
|
| 47 | LOGGER.error("Quick play disabled");
|
| 48 | minecraft.gui.setScreen(new TitleScreen());
|
| 49 | } else {
|
| 50 | switch (quickPlayVariant) {
|
| 51 | case GameConfig.QuickPlayMultiplayerData multiplayerData:
|
| 52 | joinMultiplayerWorld(minecraft, multiplayerData.serverAddress());
|
| 53 | break;
|
| 54 | case GameConfig.QuickPlayRealmsData realmsData:
|
| 55 | joinRealmsWorld(minecraft, realmsClient, realmsData.realmId());
|
| 56 | break;
|
| 57 | case GameConfig.QuickPlaySinglePlayerData singlePlayerData:
|
| 58 | String worldId = singlePlayerData.worldId();
|
| 59 | if (StringUtil.isBlank(worldId)) {
|
| 60 | worldId = getLatestSingleplayerWorld(minecraft.getLevelSource());
|
| 61 | }
|
| 62 |
|
| 63 | joinSingleplayerWorld(minecraft, worldId);
|
| 64 | break;
|
| 65 | case GameConfig.QuickPlayDisabled disabled:
|
| 66 | LOGGER.error("Quick play disabled");
|
| 67 | minecraft.gui.setScreen(new TitleScreen());
|
| 68 | break;
|
| 69 | default:
|
| 70 | throw new MatchException(null, null);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | private static @Nullable String getLatestSingleplayerWorld(final LevelStorageSource levelSource) {
|
| 76 | try {
|
| 77 | List<LevelSummary> levels = levelSource.loadLevelSummaries(levelSource.findLevelCandidates()).get();
|
| 78 | if (levels.isEmpty()) {
|
| 79 | LOGGER.warn("no latest singleplayer world found");
|
| 80 | return null;
|
| 81 | } else {
|
| 82 | return levels.getFirst().getLevelId();
|
| 83 | }
|
| 84 | } catch (InterruptedException | ExecutionException e) {
|
| 85 | LOGGER.error("failed to load singleplayer world summaries", e);
|
| 86 | return null;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | private static void joinSingleplayerWorld(final Minecraft minecraft, final @Nullable String identifier) {
|
| 91 | if (!StringUtil.isBlank(identifier) && minecraft.getLevelSource().levelExists(identifier)) {
|
| 92 | minecraft.createWorldOpenFlows().openWorld(identifier, () -> minecraft.gui.setScreen(new TitleScreen()));
|
| 93 | } else {
|
| 94 | Screen parent = new SelectWorldScreen(new TitleScreen());
|
| 95 | minecraft.gui.setScreen(new DisconnectedScreen(parent, ERROR_TITLE, INVALID_IDENTIFIER, TO_WORLD_LIST));
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | private static void joinMultiplayerWorld(final Minecraft minecraft, final String serverAddressString) {
|
| 100 | ServerList servers = new ServerList(minecraft);
|
| 101 | servers.load();
|
| 102 | ServerData serverData = servers.get(serverAddressString);
|
| 103 | if (serverData == null) {
|
| 104 | serverData = new ServerData(I18n.get("selectServer.defaultName"), serverAddressString, ServerData.Type.OTHER);
|
| 105 | servers.add(serverData, true);
|
| 106 | servers.save();
|
| 107 | }
|
| 108 |
|
| 109 | ServerAddress serverAddress = ServerAddress.parseString(serverAddressString);
|
| 110 | ConnectScreen.startConnecting(new JoinMultiplayerScreen(new TitleScreen()), minecraft, serverAddress, serverData, true, null);
|
| 111 | }
|
| 112 |
|
| 113 | private static void joinRealmsWorld(final Minecraft minecraft, final RealmsClient realmsClient, final String identifier) {
|
| 114 | long realmId;
|
| 115 | RealmsServerList realmsServerList;
|
| 116 | try {
|
| 117 | realmId = Long.parseLong(identifier);
|
| 118 | realmsServerList = realmsClient.listRealms();
|
| 119 | } catch (NumberFormatException e) {
|
| 120 | Screen parent = new RealmsMainScreen(new TitleScreen());
|
| 121 | minecraft.gui.setScreen(new DisconnectedScreen(parent, ERROR_TITLE, INVALID_IDENTIFIER, TO_REALMS_LIST));
|
| 122 | return;
|
| 123 | } catch (RealmsServiceException e) {
|
| 124 | Screen parent = new TitleScreen();
|
| 125 | minecraft.gui.setScreen(new DisconnectedScreen(parent, ERROR_TITLE, REALM_CONNECT, TO_TITLE));
|
| 126 | return;
|
| 127 | }
|
| 128 |
|
| 129 | RealmsServer server = realmsServerList.servers().stream().filter(realmsServer -> realmsServer.id == realmId).findFirst().orElse(null);
|
| 130 | if (server == null) {
|
| 131 | Screen parent = new RealmsMainScreen(new TitleScreen());
|
| 132 | minecraft.gui.setScreen(new DisconnectedScreen(parent, ERROR_TITLE, REALM_PERMISSION, TO_REALMS_LIST));
|
| 133 | } else {
|
| 134 | TitleScreen titleScreen = new TitleScreen();
|
| 135 | minecraft.gui.setScreen(new RealmsLongRunningMcoTaskScreen(titleScreen, new GetServerDetailsTask(titleScreen, server)));
|
| 136 | }
|
| 137 | }
|
| 138 | }
|
| 139 |
|