ServerAddress .java

net.minecraft.client.multiplayer.ServerAddress (class, 116 lines): members: fromString, ipAddress, getIP, getPort, serverPort. strings: "_minecraft._tcp.".

net.minecraft.client.multiplayerclass116 linesclient3.3 KB

API Surface (3)

String getIP()
int getPort()
ServerAddress fromString(String p_78860_0_)

Fields & Constants (2)

ipAddress
serverPort

String literals (6)

"com.sun.jndi.dns.DnsContextFactory"
"java.naming.factory.initial"
"java.naming.provider.url"
"dns:"
"com.sun.jndi.dns.timeout.retries"
"_minecraft._tcp."

Full Source

ServerAddress.java116 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.client.multiplayer;
2
3
import java.net.IDN;
4
import java.util.Hashtable;
5
import javax.naming.directory.Attributes;
6
import javax.naming.directory.DirContext;
7
import javax.naming.directory.InitialDirContext;
8
9
public class ServerAddress
10
{
11
    private final String ipAddress;
12
    private final int serverPort;
13
14
    private ServerAddress(String address, int port)
15
    {
16
        this.ipAddress = address;
17
        this.serverPort = port;
18
    }
19
20
    public String getIP()
21
    {
22
        return IDN.toASCII(this.ipAddress);
23
    }
24
25
    public int getPort()
26
    {
27
        return this.serverPort;
28
    }
29
30
    public static ServerAddress fromString(String p_78860_0_)
31
    {
32
        if (p_78860_0_ == null)
33
        {
34
            return null;
35
        }
36
        else
37
        {
38
            String[] astring = p_78860_0_.split(":");
39
40
            if (p_78860_0_.startsWith("["))
41
            {
42
                int i = p_78860_0_.indexOf("]");
43
44
                if (i > 0)
45
                {
46
                    String s = p_78860_0_.substring(1, i);
47
                    String s1 = p_78860_0_.substring(i + 1).trim();
48
49
                    if (s1.startsWith(":") && s1.length() > 0)
50
                    {
51
                        s1 = s1.substring(1);
52
                        astring = new String[] {s, s1};
53
                    }
54
                    else
55
                    {
56
                        astring = new String[] {s};
57
                    }
58
                }
59
            }
60
61
            if (astring.length > 2)
62
            {
63
                astring = new String[] {p_78860_0_};
64
            }
65
66
            String s2 = astring[0];
67
            int j = astring.length > 1 ? parseIntWithDefault(astring[1], 25565) : 25565;
68
69
            if (j == 25565)
70
            {
71
                String[] astring1 = getServerAddress(s2);
72
                s2 = astring1[0];
73
                j = parseIntWithDefault(astring1[1], 25565);
74
            }
75
76
            return new ServerAddress(s2, j);
77
        }
78
    }
79
80
    /**
81
     * Returns a server's address and port for the specified hostname, looking up the SRV record if possible
82
     */
83
    private static String[] getServerAddress(String p_78863_0_)
84
    {
85
        try
86
        {
87
            String s = "com.sun.jndi.dns.DnsContextFactory";
88
            Class.forName("com.sun.jndi.dns.DnsContextFactory");
89
            Hashtable hashtable = new Hashtable();
90
            hashtable.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
91
            hashtable.put("java.naming.provider.url", "dns:");
92
            hashtable.put("com.sun.jndi.dns.timeout.retries", "1");
93
            DirContext dircontext = new InitialDirContext(hashtable);
94
            Attributes attributes = dircontext.getAttributes("_minecraft._tcp." + p_78863_0_, new String[] {"SRV"});
95
            String[] astring = attributes.get("srv").get().toString().split(" ", 4);
96
            return new String[] {astring[3], astring[2]};
97
        }
98
        catch (Throwable var6)
99
        {
100
            return new String[] {p_78863_0_, Integer.toString(25565)};
101
        }
102
    }
103
104
    private static int parseIntWithDefault(String p_78862_0_, int p_78862_1_)
105
    {
106
        try
107
        {
108
            return Integer.parseInt(p_78862_0_.trim());
109
        }
110
        catch (Exception var3)
111
        {
112
            return p_78862_1_;
113
        }
114
    }
115
}
116

Deobfuscated MCP 1.8.9 source of ServerAddress from Marcelektro/MavenMCP-1.8.9 (net/minecraft/client/multiplayer/ServerAddress.java), stored verbatim.