2025-11-24 22:52:51 +03:00

118 lines
5.5 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* org.jspecify.annotations.Nullable
*/
package net.minecraft.client.gui.screens;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.CycleButton;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.server.IntegratedServer;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.commands.PublishCommand;
import net.minecraft.util.HttpUtil;
import net.minecraft.world.level.GameType;
import org.jspecify.annotations.Nullable;
public class ShareToLanScreen
extends Screen {
private static final int PORT_LOWER_BOUND = 1024;
private static final int PORT_HIGHER_BOUND = 65535;
private static final Component ALLOW_COMMANDS_LABEL = Component.translatable("selectWorld.allowCommands");
private static final Component GAME_MODE_LABEL = Component.translatable("selectWorld.gameMode");
private static final Component INFO_TEXT = Component.translatable("lanServer.otherPlayers");
private static final Component PORT_INFO_TEXT = Component.translatable("lanServer.port");
private static final Component PORT_UNAVAILABLE = Component.translatable("lanServer.port.unavailable", 1024, 65535);
private static final Component INVALID_PORT = Component.translatable("lanServer.port.invalid", 1024, 65535);
private final Screen lastScreen;
private GameType gameMode = GameType.SURVIVAL;
private boolean commands;
private int port = HttpUtil.getAvailablePort();
private @Nullable EditBox portEdit;
public ShareToLanScreen(Screen lastScreen) {
super(Component.translatable("lanServer.title"));
this.lastScreen = lastScreen;
}
@Override
protected void init() {
IntegratedServer singleplayerServer = this.minecraft.getSingleplayerServer();
this.gameMode = singleplayerServer.getDefaultGameType();
this.commands = singleplayerServer.getWorldData().isAllowCommands();
this.addRenderableWidget(CycleButton.builder(GameType::getShortDisplayName, this.gameMode).withValues((GameType[])new GameType[]{GameType.SURVIVAL, GameType.SPECTATOR, GameType.CREATIVE, GameType.ADVENTURE}).create(this.width / 2 - 155, 100, 150, 20, GAME_MODE_LABEL, (button, value) -> {
this.gameMode = value;
}));
this.addRenderableWidget(CycleButton.onOffBuilder(this.commands).create(this.width / 2 + 5, 100, 150, 20, ALLOW_COMMANDS_LABEL, (button, value) -> {
this.commands = value;
}));
Button startButton = Button.builder(Component.translatable("lanServer.start"), button -> {
this.minecraft.setScreen(null);
MutableComponent message = singleplayerServer.publishServer(this.gameMode, this.commands, this.port) ? PublishCommand.getSuccessMessage(this.port) : Component.translatable("commands.publish.failed");
this.minecraft.gui.getChat().addMessage(message);
this.minecraft.getNarrator().saySystemQueued(message);
this.minecraft.updateTitle();
}).bounds(this.width / 2 - 155, this.height - 28, 150, 20).build();
this.portEdit = new EditBox(this.font, this.width / 2 - 75, 160, 150, 20, Component.translatable("lanServer.port"));
this.portEdit.setResponder(value -> {
Component errorMessage = this.tryParsePort((String)value);
this.portEdit.setHint(Component.literal("" + this.port));
if (errorMessage == null) {
this.portEdit.setTextColor(-2039584);
this.portEdit.setTooltip(null);
startButton.active = true;
} else {
this.portEdit.setTextColor(-2142128);
this.portEdit.setTooltip(Tooltip.create(errorMessage));
startButton.active = false;
}
});
this.portEdit.setHint(Component.literal("" + this.port));
this.addRenderableWidget(this.portEdit);
this.addRenderableWidget(startButton);
this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onClose()).bounds(this.width / 2 + 5, this.height - 28, 150, 20).build());
}
@Override
public void onClose() {
this.minecraft.setScreen(this.lastScreen);
}
private @Nullable Component tryParsePort(String value) {
if (value.isBlank()) {
this.port = HttpUtil.getAvailablePort();
return null;
}
try {
this.port = Integer.parseInt(value);
if (this.port < 1024 || this.port > 65535) {
return INVALID_PORT;
}
if (!HttpUtil.isPortAvailable(this.port)) {
return PORT_UNAVAILABLE;
}
return null;
}
catch (NumberFormatException e) {
this.port = HttpUtil.getAvailablePort();
return INVALID_PORT;
}
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float a) {
super.render(graphics, mouseX, mouseY, a);
graphics.drawCenteredString(this.font, this.title, this.width / 2, 50, -1);
graphics.drawCenteredString(this.font, INFO_TEXT, this.width / 2, 82, -1);
graphics.drawCenteredString(this.font, PORT_INFO_TEXT, this.width / 2, 142, -1);
}
}