package net.minecraft; import javax.imageio.ImageIO; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.net.URLEncoder; public class LauncherFrame extends Frame { public static final int VERSION = 12; private static final long serialVersionUID = 1L; public LauncherFrame() { super("Minecraft Launcher"); setBackground(Color.BLACK); this.loginForm = new LoginForm(this); setLayout(new BorderLayout()); add(this.loginForm, "Center"); this.loginForm.setPreferredSize(new Dimension(854, 480)); pack(); setLocationRelativeTo(null); try { setIconImage(ImageIO.read(LauncherFrame.class.getResource("favicon.png"))); } catch (IOException e1) { e1.printStackTrace(); } addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { (new Thread() { public void run() { try { Thread.sleep(30000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("FORCING EXIT!"); System.exit(0); } }).start(); if (LauncherFrame.this.launcher != null) { LauncherFrame.this.launcher.stop(); LauncherFrame.this.launcher.destroy(); } System.exit(0); } }); } private Launcher launcher; private LoginForm loginForm; public void playCached(String userName) { try { if (userName == null || userName.length() <= 0) { userName = "Player"; } this.launcher = new Launcher(); this.launcher.customParameters.put("userName", userName); this.launcher.init(); removeAll(); add(this.launcher, "Center"); validate(); this.launcher.start(); this.loginForm = null; setTitle("Minecraft"); } catch (Exception e) { e.printStackTrace(); showError(e.toString()); } } public void login(String userName, String password) { try { String parameters = "user=" + URLEncoder.encode(userName, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8") + "&version=" + '\f'; String result = Util.excutePost("http://www.minecraft.net/game/getversion.jsp", parameters); if (result == null) { showError("Can't connect to minecraft.net"); this.loginForm.setNoNetwork(); return; } if (!result.contains(":")) { if (result.trim().equals("Bad login")) { showError("Login failed"); } else if (result.trim().equals("Old version")) { this.loginForm.setOutdated(); showError("Outdated launcher"); } else { showError(result); } this.loginForm.setNoNetwork(); return; } String[] values = result.split(":"); System.out.println("Username is '" + values[2] + "'"); this.launcher = new Launcher(); this.launcher.customParameters.put("userName", values[2].trim()); this.launcher.customParameters.put("latestVersion", values[0].trim()); this.launcher.customParameters.put("downloadTicket", values[1].trim()); this.launcher.customParameters.put("sessionId", values[3].trim()); this.launcher.init(); removeAll(); add(this.launcher, "Center"); validate(); this.launcher.start(); this.loginForm.loginOk(); this.loginForm = null; setTitle("Minecraft"); } catch (Exception e) { e.printStackTrace(); showError(e.toString()); this.loginForm.setNoNetwork(); } } private void showError(String error) { removeAll(); add(this.loginForm); this.loginForm.setError(error); validate(); } public boolean canPlayOffline(String userName) { Launcher launcher = new Launcher(); launcher.init(userName, null, null, null); return launcher.canPlayOffline(); } public static void main(String[] args) { LauncherFrame launcherFrame = new LauncherFrame(); launcherFrame.setVisible(true); } }