-
-
Save kotran/5b1aec102481f055118d to your computer and use it in GitHub Desktop.
[1.8.x] Connect to legit Minecraft server with forge/mcp Minecraft inside eclipse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A java file to use in eclipse/MCP in order to authenticate with Minecraft's servers | |
* This allows for modders to play online (legit servers) straight from eclipse -- streamlining the modding process | |
* Works for MC 1.8.X | |
* | |
* To use: | |
* 1) Place this .java file somewhere in your project | |
* 2) in Minecraft.java | |
* replace: | |
this.sessionService = (new YggdrasilAuthenticationService(gameConfig.userInfo.proxy, UUID.randomUUID().toString())).createMinecraftSessionService(); | |
this.session = gameConfig.userInfo.session; | |
* with: | |
clientToken = UUID.randomUUID().toString(); | |
this.sessionService = (new YggdrasilAuthenticationService(gameConfig.userInfo.proxy, clientToken)).createMinecraftSessionService(); | |
try { | |
this.session = MinecraftLogin.login("username","password"); | |
} catch (Exception e) { | |
System.out.println("Login Failed"); | |
e.printStackTrace(); | |
} | |
* | |
*/ | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.StringReader; | |
import java.net.URL; | |
import java.nio.charset.Charset; | |
import javax.net.ssl.HttpsURLConnection; | |
import org.apache.commons.io.IOUtils; | |
import com.google.gson.stream.JsonReader; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.util.Session; | |
public class MinecraftLogin { | |
static String accessToken; | |
static String clientToken; | |
static String profileID; | |
static String response; | |
static String username; | |
public static Session login(String usernameIn, String password) throws Exception{ | |
String payload = String.format("{\"agent\": {\"name\": \"Minecraft\",\"version\": 1},\"username\": \"" + usernameIn + "\",\"password\": \"" + password + "\",\"clientToken\": \"" + Minecraft.getMinecraft().clientToken + "\"}"); | |
URL authenticationURL = new URL("https://authserver.mojang.com/authenticate"); | |
HttpsURLConnection connection = (HttpsURLConnection)authenticationURL.openConnection(); | |
byte payloadAsBytes[] = payload.getBytes(Charset.forName("UTF-8")); | |
connection.setConnectTimeout(15000); | |
connection.setReadTimeout(15000); | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("charset", "UTF-8"); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
connection.setRequestProperty("Content-Length", Integer.toString(payloadAsBytes.length)); | |
connection.setDoInput(true); | |
connection.setDoOutput(true); | |
DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); | |
wr.write(payloadAsBytes); | |
wr.flush(); | |
wr.close(); | |
InputStream inputStream = null; | |
try { | |
response = IOUtils.toString(connection.getInputStream()); | |
} | |
catch(IOException e) { | |
System.out.println("Test"); | |
System.out.println(inputStream); | |
e.printStackTrace(); | |
} | |
System.out.println(response); | |
parseJSON(response); | |
System.out.println(usernameIn); | |
System.out.println(username); | |
System.out.println(profileID); | |
System.out.println(accessToken); | |
return new Session(username,profileID,accessToken,"mojang"); | |
} | |
public static void parseJSON(String toParse) throws IOException{ | |
JsonReader reader = new JsonReader(new StringReader(toParse)); | |
reader.beginObject(); | |
while(reader.hasNext()) { | |
String name = reader.nextName(); | |
if(name.equals("accessToken")) { | |
accessToken = reader.nextString(); | |
} | |
else if(name.equals("clientToken")){ | |
System.out.println(reader.nextString() + "V" + Minecraft.getMinecraft().clientToken); | |
} | |
else if (name.equals("selectedProfile")) { | |
reader.beginObject(); | |
while(reader.hasNext()){ | |
if(reader.nextName().equals("id")) { | |
profileID = reader.nextString(); | |
} | |
if(reader.nextName().equals("name")) { | |
username = reader.nextString(); | |
} | |
} | |
reader.endObject(); | |
} | |
else if(name.equals("availableProfiles")) { | |
reader.beginArray(); | |
while(reader.hasNext()) { | |
reader.skipValue(); | |
} | |
reader.endArray(); | |
} | |
} | |
reader.endObject(); | |
reader.close(); | |
} | |
} |
1.12.2?
doesnt it work for 1.12.2 ?
can someone make a yt tut about this i dont really understand u dont need to talk just wondering how to do it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've put it on eclipse but there are some errors. Can you fix them? tnks also can you make a 1.12 version of this for mods?