Skip to content

Commit

Permalink
[Networking] Added config option for automatically connecting to a cu…
Browse files Browse the repository at this point in the history
…stom server on startup
  • Loading branch information
ScribbleTAS committed Nov 16, 2023
1 parent cdb2804 commit 780a311
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/minecrafttas/common/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/com/minecrafttas/tasmod/TASmodClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 780a311

Please sign in to comment.