Skip to content

Instantly share code, notes, and snippets.

@Nicbo
Last active May 30, 2022 21:21
Show Gist options
  • Save Nicbo/2db069356d99250a4abd06ce20d7ca73 to your computer and use it in GitHub Desktop.
Save Nicbo/2db069356d99250a4abd06ce20d7ca73 to your computer and use it in GitHub Desktop.
Connect to Minecraft servers while running Forge or MCP inside of your IDE
package net.minecraft.client;
import com.mojang.authlib.Agent;
import com.mojang.authlib.UserAuthentication;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.minecraft.MinecraftSessionService;
import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService;
import net.minecraft.util.Session;
import java.net.Proxy;
import java.util.UUID;
/**
* Place this class in the net.minecraft.client package or wherever you see fit
*
* Make sure you remember to remove the login when you are done developing
* This was made so you can easily join legit minecraft servers while working from your IDE
* It just makes modding minecraft a lot easier as you don't have to build everytime you want to test something
*
* In Minecraft.java replace these two lines:
*
* this.sessionService = (new YggdrasilAuthenticationService(gameConfig.userInfo.proxy, clientToken)).createMinecraftSessionService();
* this.session = gameConfig.userInfo.session;
*
* With this:
*
* MinecraftLogin login = new MinecraftLogin("example@email.com", "password123");
* this.sessionService = login.getSessionService();
* this.session = login.getSession();
*
* Only tested on 1.8.8 MCP
*
* @author Nicbo
*/
public class MinecraftLogin {
private final MinecraftSessionService sessionService;
private final Session session;
public MinecraftLogin(String email, String password) {
YggdrasilAuthenticationService authService = new YggdrasilAuthenticationService(Proxy.NO_PROXY, UUID.randomUUID().toString());
this.sessionService = authService.createMinecraftSessionService();
UserAuthentication auth = authService.createUserAuthentication(Agent.MINECRAFT);
auth.setUsername(email);
auth.setPassword(password);
// Attempts to login
try {
auth.logIn();
} catch (AuthenticationException e) {
System.out.println("Could not login");
e.printStackTrace();
}
// Gets the session from the login
this.session = new Session(
auth.getSelectedProfile().getName(),
auth.getSelectedProfile().getId().toString().replace("-", ""),
auth.getAuthenticatedToken(),
auth.getUserType().getName()
);
}
public MinecraftSessionService getSessionService() {
return sessionService;
}
public Session getSession() {
return session;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment