From 780a311141492e3347f9f053aa75f1a68fd4b76f Mon Sep 17 00:00:00 2001 From: Scribble Date: Wed, 15 Nov 2023 20:00:48 +0100 Subject: [PATCH] [Networking] Added config option for automatically connecting to a custom server on startup --- .../minecrafttas/common/Configuration.java | 3 +- .../com/minecrafttas/tasmod/TASmodClient.java | 35 ++++++++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/minecrafttas/common/Configuration.java b/src/main/java/com/minecrafttas/common/Configuration.java index 17c6420a..6fd879d9 100644 --- a/src/main/java/com/minecrafttas/common/Configuration.java +++ b/src/main/java/com/minecrafttas/common/Configuration.java @@ -118,7 +118,8 @@ public void delete(ConfigOptions configOption) { } public static enum ConfigOptions{ - FileToOpen("fileToOpen", ""); + FileToOpen("fileToOpen", ""), + ServerConnection("serverConnection", ""); private String configKey; private String defaultValue; diff --git a/src/main/java/com/minecrafttas/tasmod/TASmodClient.java b/src/main/java/com/minecrafttas/tasmod/TASmodClient.java index a63e6d7f..9ee3c417 100644 --- a/src/main/java/com/minecrafttas/tasmod/TASmodClient.java +++ b/src/main/java/com/minecrafttas/tasmod/TASmodClient.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; +import org.apache.logging.log4j.Level; import org.lwjgl.input.Keyboard; import com.minecrafttas.common.Configuration; @@ -258,27 +259,43 @@ public void onPlayerJoinedClientSide(EntityPlayerSP player) { @Override public GuiScreen onOpenGui(GuiScreen gui) { - if(gui instanceof GuiMainMenu) { - if(client == null) { + if (gui instanceof GuiMainMenu) { + if (client == null) { Minecraft mc = Minecraft.getMinecraft(); + + String IP = "localhost"; + int PORT = TASmod.networkingport - 1; + + // Get the connection on startup from config + String configAddress = config.get(ConfigOptions.ServerConnection); + if(configAddress != null && !configAddress.isEmpty()) { + String[] ipSplit = configAddress.split(":"); + IP = ipSplit[0]; + try { + PORT = Integer.parseInt(ipSplit[1]); + } catch (Exception e) { + LOGGER.catching(Level.ERROR, e); + IP = "localhost"; + PORT = TASmod.networkingport - 1; + } + } + try { // connect to server and authenticate - client = new Client("localhost", TASmod.networkingport-1, TASmodPackets.values(), mc.getSession().getUsername(), true); + client = new Client(IP, PORT, TASmodPackets.values(), mc.getSession().getUsername(), true); } catch (Exception e) { LOGGER.error("Unable to connect TASmod client: {}", e); } ticksyncClient.setEnabled(true); } - } - else if(gui instanceof GuiControls) { + } else if (gui instanceof GuiControls) { TASmodClient.virtual.getContainer().setTASState(TASstate.NONE); // Set the TASState to nothing to avoid collisions - if(TASmodClient.tickratechanger.ticksPerSecond==0) { + if (TASmodClient.tickratechanger.ticksPerSecond == 0) { TASmodClient.tickratechanger.pauseClientGame(false); // Unpause the game waszero = true; } - } - else if(!(gui instanceof GuiControls)) { - if(waszero) { + } else if (!(gui instanceof GuiControls)) { + if (waszero) { waszero = false; TASmodClient.tickratechanger.pauseClientGame(true); }