Skip to content

Instantly share code, notes, and snippets.

@zzh8829
Created August 30, 2013 16:56
Show Gist options
  • Select an option

  • Save zzh8829/6391998 to your computer and use it in GitHub Desktop.

Select an option

Save zzh8829/6391998 to your computer and use it in GitHub Desktop.
[1.6] Connect to legit Minecraft server with forge/mcp Minecraft inside eclipse
package net.minecraft.client;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import net.minecraft.util.Session;
import org.apache.commons.io.IOUtils;
import com.google.gson.stream.JsonReader;
/*
Usage:
Add this class to package net.minecraft.client
At constructor of Minecraft.java , approximately line 300,
replace this.session = par1Session with
this.session = new MinecraftLogin("usrname","password").getSession();
Then click run, you can now login to anti-pirate server with mcp minecraft.
*/
class MinecraftLogin {
String username;
String password;
String accessToken;
String profileId;
public MinecraftLogin(String us,String pa) {
username = us;
password = pa;
}
public String getUsername()
{
int i=0;
while(i<username.length() && username.charAt(i)!='@')i++;
return username.substring(0, i);
}
public String getSessionToken()
{
try {
login();
} catch (Exception e) {
}
return String.format("token:%s:%s", accessToken, profileId);
}
public Session getSession()
{
return new Session(getUsername(),getSessionToken());
}
private void login() throws Exception
{
String json = String.format("{\"password\": \"%s\", \"agent\": {\"name\": \"Minecraft\", \"version\": 1}, \"username\": \"%s\"}",
password,username);
URL url = new URL("https://authserver.mojang.com/authenticate");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
byte paramAsBytes[] = json.getBytes(Charset.forName("UTF-8"));
connection.setConnectTimeout(15000);
connection.setReadTimeout(15000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", (new StringBuilder()).append("application/json").append("; charset=utf-8").toString());
connection.setRequestProperty("Content-Length", (new StringBuilder()).append("").append(paramAsBytes.length).toString());
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.write(paramAsBytes);
writer.flush();
writer.close();
InputStream stream = null;
try {
stream = connection.getInputStream();
}
catch(IOException e) {
//throw e;
}
json = IOUtils.toString(stream);
JsonReader reader = new JsonReader(new StringReader(json));
reader.beginObject();
while(reader.hasNext()) {
String name = reader.nextName();
if(name.equals("accessToken")) {
accessToken = reader.nextString();
} else if (name.equals("selectedProfile")) {
reader.beginObject();
while(reader.hasNext()) {
String name2 = reader.nextName();
if(name2.equals("id")) {
profileId = reader.nextString();
}
}
reader.endObject();
} else if(name.equals("clientToken")){
reader.nextString();
} else if(name.equals("availableProfiles")) {
reader.beginArray();
while(reader.hasNext()) {
reader.beginObject();
while(reader.hasNext()) {
reader.nextName();
reader.nextString();
}
reader.endObject();
}
reader.endArray();
}
}
reader.endObject();
reader.close();
}
}
@ndornseif
Copy link

Please update it for the 1.8!
return new Session(getUsername(),getSessionToken());
now needs 4 args

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment