From 571ef3aa8f8ab1ca195a6518211b5815bed6ef72 Mon Sep 17 00:00:00 2001 From: Forester Date: Fri, 2 Jan 2026 17:59:34 +0000 Subject: [PATCH 1/2] Dynamic ports --- .../com/minecrafttas/mctcommon/networking/Server.java | 11 +++++++++++ .../java/com/minecrafttas/tasmod/TASmodClient.java | 7 +++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/minecrafttas/mctcommon/networking/Server.java b/src/main/java/com/minecrafttas/mctcommon/networking/Server.java index 9d1d28c5..b6a6feef 100644 --- a/src/main/java/com/minecrafttas/mctcommon/networking/Server.java +++ b/src/main/java/com/minecrafttas/mctcommon/networking/Server.java @@ -30,6 +30,11 @@ public class Server { private final AsynchronousServerSocketChannel serverSocket; private final List clients; + public final int port; + + public Server(PacketID[] packetIDS) throws Exception { + this(0, packetIDS); + } /** * Create and bind socket * @@ -42,6 +47,12 @@ public Server(int port, PacketID[] packetIDs) throws Exception { this.serverSocket = AsynchronousServerSocketChannel.open(); this.serverSocket.bind(new InetSocketAddress(port)); + InetSocketAddress addr = + (InetSocketAddress) this.serverSocket.getLocalAddress(); + this.port = addr.getPort(); + + LOGGER.info(Server, "Server created on port {}", this.port); + // create connection handler this.clients = new ArrayList<>(); this.serverSocket.accept(null, new CompletionHandler() { diff --git a/src/main/java/com/minecrafttas/tasmod/TASmodClient.java b/src/main/java/com/minecrafttas/tasmod/TASmodClient.java index 1ce39bdc..a7046987 100644 --- a/src/main/java/com/minecrafttas/tasmod/TASmodClient.java +++ b/src/main/java/com/minecrafttas/tasmod/TASmodClient.java @@ -156,11 +156,10 @@ public void onInitializeClient() { // Starting local server instance try { - TASmod.server = new Server(TASmod.networkingport - 1, TASmodPackets.values()); + TASmod.server = new Server(TASmodPackets.values()); } catch (Exception e) { LOGGER.error("Unable to launch TASmod server: {}", e.getMessage()); } - } private void createFolders() { @@ -228,7 +227,7 @@ public void onPlayerJoinedClientSide(EntityPlayerSP player) { boolean local; if (server != null) { ip = "localhost"; - port = TASmod.networkingport - 1; + port = TASmod.server.port; local = true; } else { ip = data.serverIP.split(":")[0]; @@ -290,7 +289,7 @@ private void initializeCustomPacketHandler() { Minecraft mc = Minecraft.getMinecraft(); String IP = "localhost"; - int PORT = TASmod.networkingport - 1; + int PORT = TASmod.server.port; // Get the connection on startup from config String configAddress = config.get(TASmodConfig.ServerConnection); From 5e8d52907091fa8507f30c8175f7087384356298 Mon Sep 17 00:00:00 2001 From: Scribble Date: Fri, 2 Jan 2026 21:10:43 +0100 Subject: [PATCH 2/2] [Common] Fix formatting, add documentation --- .../mctcommon/networking/Server.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/minecrafttas/mctcommon/networking/Server.java b/src/main/java/com/minecrafttas/mctcommon/networking/Server.java index b6a6feef..f6b563eb 100644 --- a/src/main/java/com/minecrafttas/mctcommon/networking/Server.java +++ b/src/main/java/com/minecrafttas/mctcommon/networking/Server.java @@ -32,13 +32,21 @@ public class Server { public final int port; - public Server(PacketID[] packetIDS) throws Exception { - this(0, packetIDS); + /** + * Creates and binds a socket on a free port + * + * @param packetIDs Packet ids to use for identifying packets + * @throws Exception + */ + public Server(PacketID[] packetIDs) throws Exception { + this(0, packetIDs); } + /** - * Create and bind socket + * Create and binds a socket on a specified port * - * @param port Port + * @param port The port to use for the server + * @param packetIDs Packet ids to use for identifying packets * @throws Exception Unable to bind */ public Server(int port, PacketID[] packetIDs) throws Exception { @@ -47,8 +55,7 @@ public Server(int port, PacketID[] packetIDs) throws Exception { this.serverSocket = AsynchronousServerSocketChannel.open(); this.serverSocket.bind(new InetSocketAddress(port)); - InetSocketAddress addr = - (InetSocketAddress) this.serverSocket.getLocalAddress(); + InetSocketAddress addr = (InetSocketAddress) this.serverSocket.getLocalAddress(); this.port = addr.getPort(); LOGGER.info(Server, "Server created on port {}", this.port);