Search in sources :

Example 1 with IPAddress

use of mathax.client.utils.network.serverfinder.IPAddress in project Client by MatHax.

the class ServerFinderScreen method saveToFile.

private void saveToFile() {
    int newIPs = 0;
    Path filePath = MatHax.GAME_FOLDER.toPath().resolve("servers.txt");
    Set<IPAddress> hashedIPs = new HashSet<>();
    if (Files.exists(filePath)) {
        try {
            List<String> ips = Files.readAllLines(filePath);
            for (String ip : ips) {
                IPAddress parsedIP = IPAddress.fromText(ip);
                if (parsedIP != null)
                    hashedIPs.add(parsedIP);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    ServerList servers = multiplayerScreen.getServerList();
    for (int i = 0; i < servers.size(); i++) {
        ServerInfo info = servers.get(i);
        IPAddress addr = IPAddress.fromText(info.address);
        if (addr != null && hashedIPs.add(addr))
            newIPs++;
    }
    StringBuilder fileOutput = new StringBuilder();
    for (IPAddress ip : hashedIPs) {
        String stringIP = ip.toString();
        if (stringIP != null)
            fileOutput.append(stringIP).append("\n");
    }
    try {
        Files.writeString(filePath, fileOutput.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
    MatHax.LOG.info("Saved " + newIPs + " new IP" + (newIPs == 1 ? "" : "s"));
}
Also used : Path(java.nio.file.Path) MServerInfo(mathax.client.utils.network.serverfinder.MServerInfo) ServerInfo(net.minecraft.client.network.ServerInfo) ServerList(net.minecraft.client.option.ServerList) IOException(java.io.IOException) IPAddress(mathax.client.utils.network.serverfinder.IPAddress)

Aggregations

IOException (java.io.IOException)1 Path (java.nio.file.Path)1 IPAddress (mathax.client.utils.network.serverfinder.IPAddress)1 MServerInfo (mathax.client.utils.network.serverfinder.MServerInfo)1 ServerInfo (net.minecraft.client.network.ServerInfo)1 ServerList (net.minecraft.client.option.ServerList)1