001package net.minecraft.client.mco;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.io.IOException;
006import java.io.UnsupportedEncodingException;
007import java.net.URLEncoder;
008import net.minecraft.util.Session;
009
010@SideOnly(Side.CLIENT)
011public class McoClient
012{
013    private static McoOption field_98178_a = McoOption.func_98154_b();
014    private final String field_96390_a;
015    private final String field_100007_c;
016    private static String field_96388_b = "https://mcoapi.minecraft.net/";
017
018    public McoClient(Session par1Session)
019    {
020        this.field_96390_a = par1Session.sessionId;
021        this.field_100007_c = par1Session.username;
022    }
023
024    public ValueObjectList func_96382_a() throws ExceptionMcoService
025    {
026        String s = this.func_96377_a(Request.func_96358_a(field_96388_b + "worlds"));
027        return ValueObjectList.func_98161_a(s);
028    }
029
030    public McoServer func_98176_a(long par1) throws ExceptionMcoService, IOException
031    {
032        String s = this.func_96377_a(Request.func_96358_a(field_96388_b + "worlds" + "/$ID".replace("$ID", String.valueOf(par1))));
033        return McoServer.func_98165_c(s);
034    }
035
036    public McoServerAddress func_96374_a(long par1) throws ExceptionMcoService, IOException
037    {
038        String s = field_96388_b + "worlds" + "/$ID/join".replace("$ID", "" + par1);
039        String s1 = this.func_96377_a(Request.func_96358_a(s));
040        return McoServerAddress.func_98162_a(s1);
041    }
042
043    public void func_96386_a(String par1Str, String par2Str, String par3Str) throws IOException, ExceptionMcoService, UnsupportedEncodingException
044    {
045        String s3 = field_96388_b + "worlds" + "/$NAME/$LOCATION_ID".replace("$NAME", this.func_96380_a(par1Str)).replace("$LOCATION_ID", par3Str);
046
047        if (par2Str != null && !par2Str.trim().equals(""))
048        {
049            s3 = s3 + "?motd=" + this.func_96380_a(par2Str);
050        }
051
052        this.func_96377_a(Request.func_96361_b(s3, ""));
053    }
054
055    public Boolean func_96375_b() throws ExceptionMcoService, IOException
056    {
057        String s = field_96388_b + "mco" + "/available";
058        String s1 = this.func_96377_a(Request.func_96358_a(s));
059        return Boolean.valueOf(s1);
060    }
061
062    public int func_96379_c() throws ExceptionMcoService
063    {
064        String s = field_96388_b + "payments" + "/unused";
065        String s1 = this.func_96377_a(Request.func_96358_a(s));
066        return Integer.valueOf(s1).intValue();
067    }
068
069    public void func_96381_a(long par1, String par3Str) throws ExceptionMcoService
070    {
071        String s1 = field_96388_b + "worlds" + "/$WORLD_ID/invites/$USER_NAME".replace("$WORLD_ID", String.valueOf(par1)).replace("$USER_NAME", par3Str);
072        this.func_96377_a(Request.func_96355_b(s1));
073    }
074
075    public McoServer func_96387_b(long par1, String par3Str) throws ExceptionMcoService, IOException
076    {
077        String s1 = field_96388_b + "worlds" + "/$WORLD_ID/invites/$USER_NAME".replace("$WORLD_ID", String.valueOf(par1)).replace("$USER_NAME", par3Str);
078        String s2 = this.func_96377_a(Request.func_96361_b(s1, ""));
079        return McoServer.func_98165_c(s2);
080    }
081
082    public void func_96384_a(long par1, String par3Str, String par4Str) throws ExceptionMcoService, UnsupportedEncodingException
083    {
084        String s2 = field_96388_b + "worlds" + "/$WORLD_ID/$NAME".replace("$WORLD_ID", String.valueOf(par1)).replace("$NAME", this.func_96380_a(par3Str));
085
086        if (par4Str != null && !par4Str.trim().equals(""))
087        {
088            s2 = s2 + "?motd=" + this.func_96380_a(par4Str);
089        }
090
091        this.func_96377_a(Request.func_96363_c(s2, ""));
092    }
093
094    public Boolean func_96383_b(long par1) throws ExceptionMcoService, IOException
095    {
096        String s = field_96388_b + "worlds" + "/$WORLD_ID/open".replace("$WORLD_ID", String.valueOf(par1));
097        String s1 = this.func_96377_a(Request.func_96363_c(s, ""));
098        return Boolean.valueOf(s1);
099    }
100
101    public Boolean func_96378_c(long par1) throws ExceptionMcoService, IOException
102    {
103        String s = field_96388_b + "worlds" + "/$WORLD_ID/close".replace("$WORLD_ID", String.valueOf(par1));
104        String s1 = this.func_96377_a(Request.func_96363_c(s, ""));
105        return Boolean.valueOf(s1);
106    }
107
108    public Boolean func_96376_d(long par1) throws ExceptionMcoService, IOException
109    {
110        String s = field_96388_b + "worlds" + "/$WORLD_ID/reset".replace("$WORLD_ID", String.valueOf(par1));
111        String s1 = this.func_96377_a(Request.func_96353_a(s, "", 30000, 80000));
112        return Boolean.valueOf(s1);
113    }
114
115    public Locations func_96385_d() throws ExceptionMcoService, IOException
116    {
117        String s = this.func_96377_a(Request.func_96358_a(field_96388_b + "worlds" + "/locations"));
118        return Locations.func_98174_a(s);
119    }
120
121    public ValueObjectSubscription func_98177_f(long par1) throws ExceptionMcoService, IOException
122    {
123        String s = this.func_96377_a(Request.func_96358_a(field_96388_b + "subscriptions" + "/$WORLD_ID".replace("$WORLD_ID", String.valueOf(par1))));
124        return ValueObjectSubscription.func_98169_a(s);
125    }
126
127    private String func_96380_a(String par1Str) throws UnsupportedEncodingException
128    {
129        return URLEncoder.encode(par1Str, "UTF-8");
130    }
131
132    private String func_96377_a(Request par1Request) throws ExceptionMcoService
133    {
134        par1Request.func_100006_a("sid", this.field_96390_a);
135        par1Request.func_100006_a("user", this.field_100007_c);
136
137        if (field_98178_a instanceof McoOptionSome)
138        {
139            McoPair mcopair = (McoPair)field_98178_a.func_98155_a();
140            par1Request.func_100006_a((String)mcopair.func_100005_a(), (String)mcopair.func_100004_b());
141        }
142
143        try
144        {
145            int i = par1Request.func_96362_a();
146
147            if (i == 503)
148            {
149                throw new ExceptionRetryCall(10);
150            }
151            else if (i >= 200 && i < 300)
152            {
153                McoOption mcooption = par1Request.func_98175_b();
154
155                if (mcooption instanceof McoOptionSome)
156                {
157                    field_98178_a = mcooption;
158                }
159
160                return par1Request.func_96364_c();
161            }
162            else
163            {
164                throw new ExceptionMcoService(par1Request.func_96362_a(), par1Request.func_96364_c());
165            }
166        }
167        catch (ExceptionMcoHttp exceptionmcohttp)
168        {
169            throw new ExceptionMcoService(500, "Server not available!");
170        }
171    }
172}