File size: 2,731 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package net.minecraft.client.multiplayer.resolver;

import com.google.common.net.HostAndPort;
import com.mojang.logging.LogUtils;
import java.net.IDN;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.slf4j.Logger;

@OnlyIn(Dist.CLIENT)
public final class ServerAddress {
    private static final Logger LOGGER = LogUtils.getLogger();
    private final HostAndPort hostAndPort;
    private static final ServerAddress INVALID = new ServerAddress(HostAndPort.fromParts("server.invalid", 25565));

    public ServerAddress(String p_171861_, int p_171862_) {
        this(HostAndPort.fromParts(p_171861_, p_171862_));
    }

    private ServerAddress(HostAndPort p_171859_) {
        this.hostAndPort = p_171859_;
    }

    public String getHost() {
        try {
            return IDN.toASCII(this.hostAndPort.getHost());
        } catch (IllegalArgumentException illegalargumentexception) {
            return "";
        }
    }

    public int getPort() {
        return this.hostAndPort.getPort();
    }

    public static ServerAddress parseString(String p_171865_) {
        if (p_171865_ == null) {
            return INVALID;
        } else {
            try {
                HostAndPort hostandport = HostAndPort.fromString(p_171865_).withDefaultPort(25565);
                return hostandport.getHost().isEmpty() ? INVALID : new ServerAddress(hostandport);
            } catch (IllegalArgumentException illegalargumentexception) {
                LOGGER.info("Failed to parse URL {}", p_171865_, illegalargumentexception);
                return INVALID;
            }
        }
    }

    public static boolean isValidAddress(String p_171868_) {
        try {
            HostAndPort hostandport = HostAndPort.fromString(p_171868_);
            String s = hostandport.getHost();
            if (!s.isEmpty()) {
                IDN.toASCII(s);
                return true;
            }
        } catch (IllegalArgumentException illegalargumentexception) {
        }

        return false;
    }

    static int parsePort(String p_171870_) {
        try {
            return Integer.parseInt(p_171870_.trim());
        } catch (Exception exception) {
            return 25565;
        }
    }

    @Override
    public String toString() {
        return this.hostAndPort.toString();
    }

    @Override
    public boolean equals(Object p_171872_) {
        if (this == p_171872_) {
            return true;
        } else {
            return p_171872_ instanceof ServerAddress ? this.hostAndPort.equals(((ServerAddress)p_171872_).hostAndPort) : false;
        }
    }

    @Override
    public int hashCode() {
        return this.hostAndPort.hashCode();
    }
}