| 1 | package net.minecraft.client.gui.screens.multiplayer;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
| 5 | import com.mojang.blaze3d.platform.NativeImage;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import java.net.UnknownHostException;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.Arrays;
|
| 10 | import java.util.List;
|
| 11 | import java.util.concurrent.ScheduledThreadPoolExecutor;
|
| 12 | import java.util.concurrent.ThreadPoolExecutor;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.ChatFormatting;
|
| 16 | import net.minecraft.DefaultUncaughtExceptionHandler;
|
| 17 | import net.minecraft.SharedConstants;
|
| 18 | import net.minecraft.client.Minecraft;
|
| 19 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 20 | import net.minecraft.client.gui.components.LoadingDotsWidget;
|
| 21 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 22 | import net.minecraft.client.gui.components.SelectableEntry;
|
| 23 | import net.minecraft.client.gui.screens.FaviconTexture;
|
| 24 | import net.minecraft.client.input.KeyEvent;
|
| 25 | import net.minecraft.client.input.MouseButtonEvent;
|
| 26 | import net.minecraft.client.multiplayer.ServerData;
|
| 27 | import net.minecraft.client.multiplayer.ServerList;
|
| 28 | import net.minecraft.client.renderer.RenderPipelines;
|
| 29 | import net.minecraft.client.server.LanServer;
|
| 30 | import net.minecraft.network.chat.CommonComponents;
|
| 31 | import net.minecraft.network.chat.Component;
|
| 32 | import net.minecraft.network.chat.ComponentUtils;
|
| 33 | import net.minecraft.network.chat.MutableComponent;
|
| 34 | import net.minecraft.resources.Identifier;
|
| 35 | import net.minecraft.server.network.EventLoopGroupHolder;
|
| 36 | import net.minecraft.util.FormattedCharSequence;
|
| 37 | import net.minecraft.util.Util;
|
| 38 | import org.jspecify.annotations.Nullable;
|
| 39 | import org.slf4j.Logger;
|
| 40 |
|
| 41 | @Environment(EnvType.CLIENT)
|
| 42 | public class ServerSelectionList extends ObjectSelectionList<ServerSelectionList.Entry> {
|
| 43 | private static final Identifier INCOMPATIBLE_SPRITE = Identifier.withDefaultNamespace("server_list/incompatible");
|
| 44 | private static final Identifier UNREACHABLE_SPRITE = Identifier.withDefaultNamespace("server_list/unreachable");
|
| 45 | private static final Identifier PING_1_SPRITE = Identifier.withDefaultNamespace("server_list/ping_1");
|
| 46 | private static final Identifier PING_2_SPRITE = Identifier.withDefaultNamespace("server_list/ping_2");
|
| 47 | private static final Identifier PING_3_SPRITE = Identifier.withDefaultNamespace("server_list/ping_3");
|
| 48 | private static final Identifier PING_4_SPRITE = Identifier.withDefaultNamespace("server_list/ping_4");
|
| 49 | private static final Identifier PING_5_SPRITE = Identifier.withDefaultNamespace("server_list/ping_5");
|
| 50 | private static final Identifier PINGING_1_SPRITE = Identifier.withDefaultNamespace("server_list/pinging_1");
|
| 51 | private static final Identifier PINGING_2_SPRITE = Identifier.withDefaultNamespace("server_list/pinging_2");
|
| 52 | private static final Identifier PINGING_3_SPRITE = Identifier.withDefaultNamespace("server_list/pinging_3");
|
| 53 | private static final Identifier PINGING_4_SPRITE = Identifier.withDefaultNamespace("server_list/pinging_4");
|
| 54 | private static final Identifier PINGING_5_SPRITE = Identifier.withDefaultNamespace("server_list/pinging_5");
|
| 55 | private static final Identifier JOIN_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("server_list/join_highlighted");
|
| 56 | private static final Identifier JOIN_SPRITE = Identifier.withDefaultNamespace("server_list/join");
|
| 57 | private static final Identifier MOVE_UP_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("server_list/move_up_highlighted");
|
| 58 | private static final Identifier MOVE_UP_SPRITE = Identifier.withDefaultNamespace("server_list/move_up");
|
| 59 | private static final Identifier MOVE_DOWN_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("server_list/move_down_highlighted");
|
| 60 | private static final Identifier MOVE_DOWN_SPRITE = Identifier.withDefaultNamespace("server_list/move_down");
|
| 61 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 62 | private static final ThreadPoolExecutor THREAD_POOL = new ScheduledThreadPoolExecutor(
|
| 63 | 5,
|
| 64 | new ThreadFactoryBuilder()
|
| 65 | .setNameFormat("Server Pinger #%d")
|
| 66 | .setDaemon(true)
|
| 67 | .setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(LOGGER))
|
| 68 | .build()
|
| 69 | );
|
| 70 | private static final Component SCANNING_LABEL = Component.translatable("lanServer.scanning");
|
| 71 | private static final Component CANT_RESOLVE_TEXT = Component.translatable("multiplayer.status.cannot_resolve").withColor(-65536);
|
| 72 | private static final Component CANT_CONNECT_TEXT = Component.translatable("multiplayer.status.cannot_connect").withColor(-65536);
|
| 73 | private static final Component INCOMPATIBLE_STATUS = Component.translatable("multiplayer.status.incompatible");
|
| 74 | private static final Component NO_CONNECTION_STATUS = Component.translatable("multiplayer.status.no_connection");
|
| 75 | private static final Component PINGING_STATUS = Component.translatable("multiplayer.status.pinging");
|
| 76 | private static final Component ONLINE_STATUS = Component.translatable("multiplayer.status.online");
|
| 77 | private final JoinMultiplayerScreen screen;
|
| 78 | private final List<ServerSelectionList.OnlineServerEntry> onlineServers = Lists.newArrayList();
|
| 79 | private final ServerSelectionList.Entry lanHeader = new ServerSelectionList.LANHeader();
|
| 80 | private final List<ServerSelectionList.NetworkServerEntry> networkServers = Lists.newArrayList();
|
| 81 |
|
| 82 | public ServerSelectionList(final JoinMultiplayerScreen screen, final Minecraft minecraft, final int width, final int height, final int y, final int itemHeight) {
|
| 83 | super(minecraft, width, height, y, itemHeight);
|
| 84 | this.screen = screen;
|
| 85 | }
|
| 86 |
|
| 87 | private void refreshEntries() {
|
| 88 | ServerSelectionList.Entry previouslySelected = this.getSelected();
|
| 89 | List<ServerSelectionList.Entry> entriesToAdd = new ArrayList<>(this.onlineServers);
|
| 90 | entriesToAdd.add(this.lanHeader);
|
| 91 | entriesToAdd.addAll(this.networkServers);
|
| 92 | this.replaceEntries(entriesToAdd);
|
| 93 | if (previouslySelected != null) {
|
| 94 | for (ServerSelectionList.Entry entry : entriesToAdd) {
|
| 95 | if (entry.matches(previouslySelected)) {
|
| 96 | this.setSelected(entry);
|
| 97 | break;
|
| 98 | }
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public void setSelected(final ServerSelectionList.@Nullable Entry selected) {
|
| 104 | super.setSelected(selected);
|
| 105 | this.screen.onSelectedChange();
|
| 106 | }
|
| 107 |
|
| 108 | public void updateOnlineServers(final ServerList servers) {
|
| 109 | this.onlineServers.clear();
|
| 110 |
|
| 111 | for (int i = 0; i < servers.size(); i++) {
|
| 112 | this.onlineServers.add(new ServerSelectionList.OnlineServerEntry(this.screen, servers.get(i)));
|
| 113 | }
|
| 114 |
|
| 115 | this.refreshEntries();
|
| 116 | }
|
| 117 |
|
| 118 | public void updateNetworkServers(final List<LanServer> servers) {
|
| 119 | int newServerCount = servers.size() - this.networkServers.size();
|
| 120 | this.networkServers.clear();
|
| 121 |
|
| 122 | for (LanServer server : servers) {
|
| 123 | this.networkServers.add(new ServerSelectionList.NetworkServerEntry(this.screen, server));
|
| 124 | }
|
| 125 |
|
| 126 | this.refreshEntries();
|
| 127 |
|
| 128 | for (int i = this.networkServers.size() - newServerCount; i < this.networkServers.size(); i++) {
|
| 129 | ServerSelectionList.NetworkServerEntry newServer = this.networkServers.get(i);
|
| 130 | int entryIndex = i - this.networkServers.size() + this.children().size();
|
| 131 | int rowTop = this.getRowTop(entryIndex);
|
| 132 | int rowBottom = this.getRowBottom(entryIndex);
|
| 133 | if (rowBottom >= this.getY() && rowTop <= this.getBottom()) {
|
| 134 | this.minecraft.getNarrator().saySystemQueued(Component.translatable("multiplayer.lan.server_found", newServer.getServerNarration()));
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public int getRowWidth() {
|
| 141 | return 305;
|
| 142 | }
|
| 143 |
|
| 144 | public void removed() {
|
| 145 | }
|
| 146 |
|
| 147 | @Environment(EnvType.CLIENT)
|
| 148 | public abstract static class Entry extends ObjectSelectionList.Entry<ServerSelectionList.Entry> implements AutoCloseable {
|
| 149 | @Override
|
| 150 | public void close() {
|
| 151 | }
|
| 152 |
|
| 153 | protected abstract boolean matches(final ServerSelectionList.Entry other);
|
| 154 |
|
| 155 | public abstract void join();
|
| 156 | }
|
| 157 |
|
| 158 | @Environment(EnvType.CLIENT)
|
| 159 | public static class LANHeader extends ServerSelectionList.Entry {
|
| 160 | private final Minecraft minecraft = Minecraft.getInstance();
|
| 161 | private final LoadingDotsWidget loadingDotsWidget = new LoadingDotsWidget(this.minecraft.font, ServerSelectionList.SCANNING_LABEL);
|
| 162 |
|
| 163 | @Override
|
| 164 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 165 | this.loadingDotsWidget.setPosition(this.getContentXMiddle() - this.minecraft.font.width(ServerSelectionList.SCANNING_LABEL) / 2, this.getContentY());
|
| 166 | this.loadingDotsWidget.extractRenderState(graphics, mouseX, mouseY, a);
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | public Component getNarration() {
|
| 171 | return ServerSelectionList.SCANNING_LABEL;
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | protected boolean matches(final ServerSelectionList.Entry other) {
|
| 176 | return other instanceof ServerSelectionList.LANHeader;
|
| 177 | }
|
| 178 |
|
| 179 | @Override
|
| 180 | public void join() {
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | @Environment(EnvType.CLIENT)
|
| 185 | public static class NetworkServerEntry extends ServerSelectionList.Entry {
|
| 186 | private static final int ICON_WIDTH = 32;
|
| 187 | private static final Component LAN_SERVER_HEADER = Component.translatable("lanServer.title");
|
| 188 | private static final Component HIDDEN_ADDRESS_TEXT = Component.translatable("selectServer.hiddenAddress");
|
| 189 | private final JoinMultiplayerScreen screen;
|
| 190 | protected final Minecraft minecraft;
|
| 191 | protected final LanServer serverData;
|
| 192 |
|
| 193 | protected NetworkServerEntry(final JoinMultiplayerScreen screen, final LanServer serverData) {
|
| 194 | this.screen = screen;
|
| 195 | this.serverData = serverData;
|
| 196 | this.minecraft = Minecraft.getInstance();
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 201 | graphics.text(this.minecraft.font, LAN_SERVER_HEADER, this.getContentX() + 32 + 3, this.getContentY() + 1, -1);
|
| 202 | graphics.text(this.minecraft.font, this.serverData.getMotd(), this.getContentX() + 32 + 3, this.getContentY() + 12, -8355712);
|
| 203 | if (this.minecraft.options.hideServerAddress) {
|
| 204 | graphics.text(this.minecraft.font, HIDDEN_ADDRESS_TEXT, this.getContentX() + 32 + 3, this.getContentY() + 12 + 11, -8355712);
|
| 205 | } else {
|
| 206 | graphics.text(this.minecraft.font, this.serverData.getAddress(), this.getContentX() + 32 + 3, this.getContentY() + 12 + 11, -8355712);
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 212 | if (doubleClick) {
|
| 213 | this.join();
|
| 214 | }
|
| 215 |
|
| 216 | return super.mouseClicked(event, doubleClick);
|
| 217 | }
|
| 218 |
|
| 219 | @Override
|
| 220 | public boolean keyPressed(final KeyEvent event) {
|
| 221 | if (event.isSelection()) {
|
| 222 | this.join();
|
| 223 | return true;
|
| 224 | } else {
|
| 225 | return super.keyPressed(event);
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | @Override
|
| 230 | public void join() {
|
| 231 | this.screen.join(new ServerData(this.serverData.getMotd(), this.serverData.getAddress(), ServerData.Type.LAN));
|
| 232 | }
|
| 233 |
|
| 234 | @Override
|
| 235 | public Component getNarration() {
|
| 236 | return Component.translatable("narrator.select", this.getServerNarration());
|
| 237 | }
|
| 238 |
|
| 239 | public Component getServerNarration() {
|
| 240 | return Component.empty().append(LAN_SERVER_HEADER).append(CommonComponents.SPACE).append(this.serverData.getMotd());
|
| 241 | }
|
| 242 |
|
| 243 | @Override
|
| 244 | protected boolean matches(final ServerSelectionList.Entry other) {
|
| 245 | return other instanceof ServerSelectionList.NetworkServerEntry networkServerEntry && networkServerEntry.serverData == this.serverData;
|
| 246 | }
|
| 247 | }
|
| 248 |
|
| 249 | @Environment(EnvType.CLIENT)
|
| 250 | public class OnlineServerEntry extends ServerSelectionList.Entry implements SelectableEntry {
|
| 251 | private static final int ICON_SIZE = 32;
|
| 252 | private static final int SPACING = 5;
|
| 253 | private static final int STATUS_ICON_WIDTH = 10;
|
| 254 | private static final int STATUS_ICON_HEIGHT = 8;
|
| 255 | private final JoinMultiplayerScreen screen;
|
| 256 | private final Minecraft minecraft;
|
| 257 | private final ServerData serverData;
|
| 258 | private final FaviconTexture icon;
|
| 259 | private byte @Nullable [] lastIconBytes;
|
| 260 | private @Nullable List<Component> onlinePlayersTooltip;
|
| 261 | private @Nullable Identifier statusIcon;
|
| 262 | private @Nullable Component statusIconTooltip;
|
| 263 |
|
| 264 | protected OnlineServerEntry(final JoinMultiplayerScreen screen, final ServerData serverData) {
|
| 265 | this.screen = screen;
|
| 266 | this.serverData = serverData;
|
| 267 | this.minecraft = Minecraft.getInstance();
|
| 268 | this.icon = FaviconTexture.forServer(this.minecraft.getTextureManager(), serverData.ip);
|
| 269 | this.refreshStatus();
|
| 270 | }
|
| 271 |
|
| 272 | @Override
|
| 273 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 274 | if (this.serverData.state() == ServerData.State.INITIAL) {
|
| 275 | this.serverData.setState(ServerData.State.PINGING);
|
| 276 | this.serverData.motd = CommonComponents.EMPTY;
|
| 277 | this.serverData.status = CommonComponents.EMPTY;
|
| 278 | ServerSelectionList.THREAD_POOL
|
| 279 | .submit(
|
| 280 | () -> {
|
| 281 | try {
|
| 282 | this.screen
|
| 283 | .getPinger()
|
| 284 | .pingServer(
|
| 285 | this.serverData,
|
| 286 | () -> this.minecraft.execute(this::updateServerList),
|
| 287 | () -> {
|
| 288 | this.serverData
|
| 289 | .setState(
|
| 290 | this.serverData.protocol == SharedConstants.getCurrentVersion().protocolVersion() ? ServerData.State.SUCCESSFUL : ServerData.State.INCOMPATIBLE
|
| 291 | );
|
| 292 | this.minecraft.execute(this::refreshStatus);
|
| 293 | },
|
| 294 | EventLoopGroupHolder.remote(this.minecraft.options.useNativeTransport())
|
| 295 | );
|
| 296 | } catch (UnknownHostException ignored) {
|
| 297 | this.serverData.setState(ServerData.State.UNREACHABLE);
|
| 298 | this.serverData.motd = ServerSelectionList.CANT_RESOLVE_TEXT;
|
| 299 | this.minecraft.execute(this::refreshStatus);
|
| 300 | } catch (Exception ignored) {
|
| 301 | this.serverData.setState(ServerData.State.UNREACHABLE);
|
| 302 | this.serverData.motd = ServerSelectionList.CANT_CONNECT_TEXT;
|
| 303 | this.minecraft.execute(this::refreshStatus);
|
| 304 | }
|
| 305 | }
|
| 306 | );
|
| 307 | }
|
| 308 |
|
| 309 | graphics.text(this.minecraft.font, this.serverData.name, this.getContentX() + 32 + 3, this.getContentY() + 1, -1);
|
| 310 | List<FormattedCharSequence> lines = this.minecraft.font.split(this.serverData.motd, this.getContentWidth() - 32 - 2);
|
| 311 |
|
| 312 | for (int i = 0; i < Math.min(lines.size(), 2); i++) {
|
| 313 | graphics.text(this.minecraft.font, lines.get(i), this.getContentX() + 32 + 3, this.getContentY() + 12 + 9 * i, -8355712);
|
| 314 | }
|
| 315 |
|
| 316 | this.extractIcon(graphics, this.getContentX(), this.getContentY(), this.icon.textureLocation());
|
| 317 | int index = ServerSelectionList.this.children().indexOf(this);
|
| 318 | if (this.serverData.state() == ServerData.State.PINGING) {
|
| 319 | int iconIndex = (int)(Util.getMillis() / 100L + index * 2 & 7L);
|
| 320 | if (iconIndex > 4) {
|
| 321 | iconIndex = 8 - iconIndex;
|
| 322 | }
|
| 323 | this.statusIcon = switch (iconIndex) {
|
| 324 | case 1 -> ServerSelectionList.PINGING_2_SPRITE;
|
| 325 | case 2 -> ServerSelectionList.PINGING_3_SPRITE;
|
| 326 | case 3 -> ServerSelectionList.PINGING_4_SPRITE;
|
| 327 | case 4 -> ServerSelectionList.PINGING_5_SPRITE;
|
| 328 | default -> ServerSelectionList.PINGING_1_SPRITE;
|
| 329 | };
|
| 330 | }
|
| 331 |
|
| 332 | int statusIconX = this.getContentRight() - 10 - 5;
|
| 333 | if (this.statusIcon != null) {
|
| 334 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, this.statusIcon, statusIconX, this.getContentY(), 10, 8);
|
| 335 | }
|
| 336 |
|
| 337 | byte[] currentIconBytes = this.serverData.getIconBytes();
|
| 338 | if (!Arrays.equals(currentIconBytes, this.lastIconBytes)) {
|
| 339 | if (this.uploadServerIcon(currentIconBytes)) {
|
| 340 | this.lastIconBytes = currentIconBytes;
|
| 341 | } else {
|
| 342 | this.serverData.setIconBytes(null);
|
| 343 | this.updateServerList();
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | Component status = this.serverData.state() == ServerData.State.INCOMPATIBLE
|
| 348 | ? this.serverData.version.copy().withStyle(ChatFormatting.RED)
|
| 349 | : this.serverData.status;
|
| 350 | int statusWidth = this.minecraft.font.width(status);
|
| 351 | int statusX = statusIconX - statusWidth - 5;
|
| 352 | graphics.text(this.minecraft.font, status, statusX, this.getContentY() + 1, -8355712);
|
| 353 | if (this.statusIconTooltip != null
|
| 354 | && mouseX >= statusIconX
|
| 355 | && mouseX <= statusIconX + 10
|
| 356 | && mouseY >= this.getContentY()
|
| 357 | && mouseY <= this.getContentY() + 8) {
|
| 358 | graphics.setTooltipForNextFrame(this.statusIconTooltip, mouseX, mouseY);
|
| 359 | } else if (this.onlinePlayersTooltip != null
|
| 360 | && mouseX >= statusX
|
| 361 | && mouseX <= statusX + statusWidth
|
| 362 | && mouseY >= this.getContentY()
|
| 363 | && mouseY <= this.getContentY() - 1 + 9) {
|
| 364 | graphics.setTooltipForNextFrame(Lists.transform(this.onlinePlayersTooltip, Component::getVisualOrderText), mouseX, mouseY);
|
| 365 | }
|
| 366 |
|
| 367 | if (hovered) {
|
| 368 | graphics.fill(this.getContentX(), this.getContentY(), this.getContentX() + 32, this.getContentY() + 32, -1601138544);
|
| 369 | int relX = mouseX - this.getContentX();
|
| 370 | int relY = mouseY - this.getContentY();
|
| 371 | if (this.mouseOverRightHalf(relX, relY, 32)) {
|
| 372 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ServerSelectionList.JOIN_HIGHLIGHTED_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 373 | ServerSelectionList.this.handleCursor(graphics);
|
| 374 | } else {
|
| 375 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ServerSelectionList.JOIN_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 376 | }
|
| 377 |
|
| 378 | if (index > 0) {
|
| 379 | if (this.mouseOverTopLeftQuarter(relX, relY, 32)) {
|
| 380 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ServerSelectionList.MOVE_UP_HIGHLIGHTED_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 381 | ServerSelectionList.this.handleCursor(graphics);
|
| 382 | } else {
|
| 383 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ServerSelectionList.MOVE_UP_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 384 | }
|
| 385 | }
|
| 386 |
|
| 387 | if (index < this.screen.getServers().size() - 1) {
|
| 388 | if (this.mouseOverBottomLeftQuarter(relX, relY, 32)) {
|
| 389 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ServerSelectionList.MOVE_DOWN_HIGHLIGHTED_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 390 | ServerSelectionList.this.handleCursor(graphics);
|
| 391 | } else {
|
| 392 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ServerSelectionList.MOVE_DOWN_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 393 | }
|
| 394 | }
|
| 395 | }
|
| 396 | }
|
| 397 |
|
| 398 | private void refreshStatus() {
|
| 399 | this.onlinePlayersTooltip = null;
|
| 400 | switch (this.serverData.state()) {
|
| 401 | case INITIAL:
|
| 402 | case PINGING:
|
| 403 | this.statusIcon = ServerSelectionList.PING_1_SPRITE;
|
| 404 | this.statusIconTooltip = ServerSelectionList.PINGING_STATUS;
|
| 405 | break;
|
| 406 | case INCOMPATIBLE:
|
| 407 | this.statusIcon = ServerSelectionList.INCOMPATIBLE_SPRITE;
|
| 408 | this.statusIconTooltip = ServerSelectionList.INCOMPATIBLE_STATUS;
|
| 409 | this.onlinePlayersTooltip = this.serverData.playerList;
|
| 410 | break;
|
| 411 | case UNREACHABLE:
|
| 412 | this.statusIcon = ServerSelectionList.UNREACHABLE_SPRITE;
|
| 413 | this.statusIconTooltip = ServerSelectionList.NO_CONNECTION_STATUS;
|
| 414 | break;
|
| 415 | case SUCCESSFUL:
|
| 416 | if (this.serverData.ping < 150L) {
|
| 417 | this.statusIcon = ServerSelectionList.PING_5_SPRITE;
|
| 418 | } else if (this.serverData.ping < 300L) {
|
| 419 | this.statusIcon = ServerSelectionList.PING_4_SPRITE;
|
| 420 | } else if (this.serverData.ping < 600L) {
|
| 421 | this.statusIcon = ServerSelectionList.PING_3_SPRITE;
|
| 422 | } else if (this.serverData.ping < 1000L) {
|
| 423 | this.statusIcon = ServerSelectionList.PING_2_SPRITE;
|
| 424 | } else {
|
| 425 | this.statusIcon = ServerSelectionList.PING_1_SPRITE;
|
| 426 | }
|
| 427 |
|
| 428 | this.statusIconTooltip = Component.translatable("multiplayer.status.ping", this.serverData.ping);
|
| 429 | this.onlinePlayersTooltip = this.serverData.playerList;
|
| 430 | }
|
| 431 | }
|
| 432 |
|
| 433 | public void updateServerList() {
|
| 434 | this.screen.getServers().save();
|
| 435 | }
|
| 436 |
|
| 437 | protected void extractIcon(final GuiGraphicsExtractor graphics, final int rowLeft, final int rowTop, final Identifier location) {
|
| 438 | graphics.blit(RenderPipelines.GUI_TEXTURED, location, rowLeft, rowTop, 0.0F, 0.0F, 32, 32, 32, 32);
|
| 439 | }
|
| 440 |
|
| 441 | private boolean uploadServerIcon(final byte @Nullable [] serverIconBytes) {
|
| 442 | if (serverIconBytes == null) {
|
| 443 | this.icon.clear();
|
| 444 | } else {
|
| 445 | try {
|
| 446 | this.icon.upload(NativeImage.read(serverIconBytes));
|
| 447 | } catch (Throwable t) {
|
| 448 | ServerSelectionList.LOGGER.error("Invalid icon for server {} ({})", this.serverData.name, this.serverData.ip, t);
|
| 449 | return false;
|
| 450 | }
|
| 451 | }
|
| 452 |
|
| 453 | return true;
|
| 454 | }
|
| 455 |
|
| 456 | @Override
|
| 457 | public boolean keyPressed(final KeyEvent event) {
|
| 458 | if (event.isSelection()) {
|
| 459 | this.join();
|
| 460 | return true;
|
| 461 | }
|
| 462 |
|
| 463 | if (event.hasShiftDown()) {
|
| 464 | ServerSelectionList list = this.screen.serverSelectionList;
|
| 465 | int currentIndex = list.children().indexOf(this);
|
| 466 | if (currentIndex == -1) {
|
| 467 | return true;
|
| 468 | }
|
| 469 |
|
| 470 | if (event.isDown() && currentIndex < this.screen.getServers().size() - 1 || event.isUp() && currentIndex > 0) {
|
| 471 | this.swap(currentIndex, event.isDown() ? currentIndex + 1 : currentIndex - 1);
|
| 472 | return true;
|
| 473 | }
|
| 474 | }
|
| 475 |
|
| 476 | return super.keyPressed(event);
|
| 477 | }
|
| 478 |
|
| 479 | @Override
|
| 480 | public void join() {
|
| 481 | this.screen.join(this.serverData);
|
| 482 | }
|
| 483 |
|
| 484 | private void swap(final int currentIndex, final int newIndex) {
|
| 485 | this.screen.getServers().swap(currentIndex, newIndex);
|
| 486 | this.screen.serverSelectionList.swap(currentIndex, newIndex);
|
| 487 | }
|
| 488 |
|
| 489 | @Override
|
| 490 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 491 | int relX = (int)event.x() - this.getContentX();
|
| 492 | int relY = (int)event.y() - this.getContentY();
|
| 493 | if (this.mouseOverRightHalf(relX, relY, 32)) {
|
| 494 | this.join();
|
| 495 | return true;
|
| 496 | }
|
| 497 |
|
| 498 | int currentIndex = this.screen.serverSelectionList.children().indexOf(this);
|
| 499 | if (currentIndex > 0 && this.mouseOverTopLeftQuarter(relX, relY, 32)) {
|
| 500 | this.swap(currentIndex, currentIndex - 1);
|
| 501 | return true;
|
| 502 | }
|
| 503 |
|
| 504 | if (currentIndex < this.screen.getServers().size() - 1 && this.mouseOverBottomLeftQuarter(relX, relY, 32)) {
|
| 505 | this.swap(currentIndex, currentIndex + 1);
|
| 506 | return true;
|
| 507 | }
|
| 508 |
|
| 509 | if (doubleClick) {
|
| 510 | this.join();
|
| 511 | }
|
| 512 |
|
| 513 | return super.mouseClicked(event, doubleClick);
|
| 514 | }
|
| 515 |
|
| 516 | public ServerData getServerData() {
|
| 517 | return this.serverData;
|
| 518 | }
|
| 519 |
|
| 520 | @Override
|
| 521 | public Component getNarration() {
|
| 522 | MutableComponent narrationComponent = Component.empty();
|
| 523 | narrationComponent.append(Component.translatable("narrator.select", this.serverData.name));
|
| 524 | narrationComponent.append(CommonComponents.NARRATION_SEPARATOR);
|
| 525 | switch (this.serverData.state()) {
|
| 526 | case PINGING:
|
| 527 | narrationComponent.append(ServerSelectionList.PINGING_STATUS);
|
| 528 | break;
|
| 529 | case INCOMPATIBLE:
|
| 530 | narrationComponent.append(ServerSelectionList.INCOMPATIBLE_STATUS);
|
| 531 | narrationComponent.append(CommonComponents.NARRATION_SEPARATOR);
|
| 532 | narrationComponent.append(Component.translatable("multiplayer.status.version.narration", this.serverData.version));
|
| 533 | narrationComponent.append(CommonComponents.NARRATION_SEPARATOR);
|
| 534 | narrationComponent.append(Component.translatable("multiplayer.status.motd.narration", this.serverData.motd));
|
| 535 | break;
|
| 536 | case UNREACHABLE:
|
| 537 | narrationComponent.append(ServerSelectionList.NO_CONNECTION_STATUS);
|
| 538 | break;
|
| 539 | default:
|
| 540 | narrationComponent.append(ServerSelectionList.ONLINE_STATUS);
|
| 541 | narrationComponent.append(CommonComponents.NARRATION_SEPARATOR);
|
| 542 | narrationComponent.append(Component.translatable("multiplayer.status.ping.narration", this.serverData.ping));
|
| 543 | narrationComponent.append(CommonComponents.NARRATION_SEPARATOR);
|
| 544 | narrationComponent.append(Component.translatable("multiplayer.status.motd.narration", this.serverData.motd));
|
| 545 | if (this.serverData.players != null) {
|
| 546 | narrationComponent.append(CommonComponents.NARRATION_SEPARATOR);
|
| 547 | narrationComponent.append(
|
| 548 | Component.translatable("multiplayer.status.player_count.narration", this.serverData.players.online(), this.serverData.players.max())
|
| 549 | );
|
| 550 | narrationComponent.append(CommonComponents.NARRATION_SEPARATOR);
|
| 551 | narrationComponent.append(ComponentUtils.formatList(this.serverData.playerList, Component.literal(", ")));
|
| 552 | }
|
| 553 | }
|
| 554 |
|
| 555 | return narrationComponent;
|
| 556 | }
|
| 557 |
|
| 558 | @Override
|
| 559 | public void close() {
|
| 560 | this.icon.close();
|
| 561 | }
|
| 562 |
|
| 563 | @Override
|
| 564 | protected boolean matches(final ServerSelectionList.Entry other) {
|
| 565 | return other instanceof ServerSelectionList.OnlineServerEntry onlineServerEntry && onlineServerEntry.serverData == this.serverData;
|
| 566 | }
|
| 567 | }
|
| 568 | }
|
| 569 |
|