Skip to content

Commit

Permalink
Replace pair with record.
Browse files Browse the repository at this point in the history
  • Loading branch information
Provismet committed Aug 17, 2024
1 parent 0d8eed4 commit 74edac5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/provismet/vmcmc/ClientVMC.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static Identifier identifier (String path) {

@Override
public void onInitializeClient () {
Pair<String,Integer> portInfo = Config.readJSON();
PacketSender.initPort(portInfo.getLeft(), portInfo.getRight());
Config.ConnectionInfo portInfo = Config.readJSON();
PacketSender.initPort(portInfo.host(), portInfo.port());
CaptureRegistry.registerStandardEvents();

FabricLoader.getInstance().getEntrypointContainers(MODID, VmcApi.class).forEach(entrypoint -> {
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/com/provismet/vmcmc/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.provismet.vmcmc.vmc.PacketSender;

import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;

public class Config {
private static final String HOST = "host";
Expand All @@ -21,7 +20,7 @@ public class Config {
private static String host = PacketSender.LOCALHOST;
private static int port = PacketSender.DEFAULT_PORT;

public static Pair<String,Integer> readJSON () {
public static ConnectionInfo readJSON () {
try {
FileReader reader = new FileReader(FILEPATH);
JsonReader parser = new JsonReader(reader);
Expand All @@ -47,16 +46,16 @@ public static Pair<String,Integer> readJSON () {
}
}
parser.close();
return new Pair<>(host, port);
return new ConnectionInfo(host, port);
}
catch (FileNotFoundException e) {
ClientVMC.LOGGER.warn("Config not found, creating default config.");
saveJSON();
return new Pair<>(PacketSender.LOCALHOST, PacketSender.DEFAULT_PORT);
return ConnectionInfo.getDefault();
}
catch (Exception e) {
ClientVMC.LOGGER.warn("Config could not be read, using default parameters.", e);
return new Pair<>(PacketSender.LOCALHOST, PacketSender.DEFAULT_PORT);
return ConnectionInfo.getDefault();
}
}

Expand All @@ -71,7 +70,7 @@ public static void saveJSON () {
writer.write(simpleJSON);
writer.close();
}
catch (Exception e) {
catch (Exception ignored) {

}
}
Expand Down Expand Up @@ -106,7 +105,13 @@ public static void setPort (int newPort) {
port = newPort;
}
else {
ClientVMC.LOGGER.error("Attempted to set illegal port: " + newPort);
ClientVMC.LOGGER.error("Attempted to set illegal port: {}", newPort);
}
}

public record ConnectionInfo (String host, int port) {
public static ConnectionInfo getDefault () {
return new ConnectionInfo(PacketSender.LOCALHOST, PacketSender.DEFAULT_PORT);
}
}
}

0 comments on commit 74edac5

Please sign in to comment.