Java tutorial
/** * Copyright 2014/2015 JeremGamer * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package io.github.jeremgamer.preview.actions; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import io.github.jeremgamer.preview.LauncherFrame; import io.github.jeremgamer.preview.actions.Identificate.UserProperties; import io.github.jeremgamer.preview.components.CustomCheckBox; import io.github.jeremgamer.preview.components.CustomComboBox; import javax.swing.JComponent; import org.apache.commons.io.IOUtils; import com.google.gson.Gson; public class Launch extends Action { private JComponent parent; private final String JAVA_DIR = System.getProperty("java.home") + "\\bin\\javaw.exe"; private final String VANILLA_GAME_DIRECTORY = System.getProperty("user.home") + "\\AppData\\Roaming\\.minecraft"; private final String VANILLA_PATH = VANILLA_GAME_DIRECTORY + "\\versions\\<version>\\<version>.jar"; private final String VANILLA_NATIVE_DIR = VANILLA_GAME_DIRECTORY + "\\versions\\<version>\\<version>-natives-rocketbuilder"; private final String LIBRARIES = VANILLA_GAME_DIRECTORY + "\\libraries\\net\\sf\\trove4j\\trove4j\\3.0.3\\trove4j-3.0.3.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\com\\ibm\\icu\\icu4j-core-mojang\\51.2\\icu4j-core-mojang-51.2.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\net\\sf\\jopt-simple\\jopt-simple\\4.6\\jopt-simple-4.6.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\com\\paulscode\\codecjorbis\\20101023\\codecjorbis-20101023.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\com\\paulscode\\codecwav\\20101023\\codecwav-20101023.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\com\\paulscode\\libraryjavasound\\20101123\\libraryjavasound-20101123.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\com\\paulscode\\librarylwjglopenal\\20100824\\librarylwjglopenal-20100824.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\com\\paulscode\\soundsystem\\20120107\\soundsystem-20120107.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\io\\netty\\netty-all\\4.0.15.Final\\netty-all-4.0.15.Final.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\com\\google\\guava\\guava\\17.0\\guava-17.0.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\org\\apache\\commons\\commons-lang3\\3.3.2\\commons-lang3-3.3.2.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\commons-io\\commons-io\\2.4\\commons-io-2.4.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\commons-codec\\commons-codec\\1.9\\commons-codec-1.9.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\net\\java\\jinput\\jinput\\2.0.5\\jinput-2.0.5.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\net\\java\\jutils\\jutils\\1.0.0\\jutils-1.0.0.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\com\\google\\code\\gson\\gson\\2.2.4\\gson-2.2.4.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\com\\mojang\\authlib\\1.5.17\\authlib-1.5.17.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\com\\mojang\\realms\\1.5.7\\realms-1.5.7.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\org\\apache\\commons\\commons-compress\\1.8.1\\commons-compress-1.8.1.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\org\\apache\\httpcomponents\\httpclient\\4.3.3\\httpclient-4.3.3.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\commons-logging\\commons-logging\\1.1.3\\commons-logging-1.1.3.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\org\\apache\\httpcomponents\\httpcore\\4.3.2\\httpcore-4.3.2.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\org\\apache\\logging\\log4j\\log4j-api\\2.0-beta9\\log4j-api-2.0-beta9.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\org\\apache\\logging\\log4j\\log4j-core\\2.0-beta9\\log4j-core-2.0-beta9.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\org\\lwjgl\\lwjgl\\lwjgl\\2.9.1\\lwjgl-2.9.1.jar;" + VANILLA_GAME_DIRECTORY + "\\libraries\\org\\lwjgl\\lwjgl\\lwjgl_util\\2.9.1\\lwjgl_util-2.9.1.jar;"; private final String[] NATIVE_JARS = { VANILLA_GAME_DIRECTORY + "\\libraries\\tv\\twitch\\twitch-external-platform\\4.5\\twitch-external-platform-4.5-natives-windows-32.jar", VANILLA_GAME_DIRECTORY + "\\libraries\\org\\lwjgl\\lwjgl\\lwjgl-platform\\2.9.1\\lwjgl-platform-2.9.1-natives-windows.jar", VANILLA_GAME_DIRECTORY + "\\libraries\\tv\\twitch\\twitch-platform\\6.5\\twitch-platform-6.5-natives-windows-32.jar", VANILLA_GAME_DIRECTORY + "\\libraries\\net\\java\\jinput\\jinput-platform\\2.0.5\\jinput-platform-2.0.5-natives-windows.jar" }; private final String MAIN_CLASS = "net.minecraft.client.main.Main"; private String accessToken; private String userID; private UserProperties user; private String name; public Launch(JComponent parent) { this.parent = parent; } public void setToken(String token) { this.accessToken = token; } public void setUserID(String uuid) { this.userID = uuid; } public void setUserProperties(UserProperties user) { this.user = user; } public void setUserName(String name) { this.name = name; } @Override public void perform() { if (LauncherFrame.auth) { for (Action a : getChildrens()) { a.perform(); } if (parent instanceof CustomCheckBox) { if (isChildren()) { if (((CustomCheckBox) parent).isSelected()) { launch(); } } else { launch(); } } else if (parent instanceof CustomComboBox) { if (isChildren()) { if (((CustomComboBox) parent).getSelectedItem().equals(getItemCondition())) { launch(); } } else { launch(); } } else { launch(); } } } private void launch() { try { extractNatives(); } catch (IOException e1) { e1.printStackTrace(); } final Gson gson = new Gson(); List<String> command = new ArrayList<String>(); command.add(JAVA_DIR); command.add("-Djava.library.path=" + VANILLA_NATIVE_DIR.replaceAll("<version>", "1.8.1")); command.add("-cp"); command.add(LIBRARIES + VANILLA_PATH.replaceAll("<version>", "1.8.1")); command.add(MAIN_CLASS); command.add("--username=" + name); command.add("--version=1.8.1"); command.add("--gameDir=" + VANILLA_GAME_DIRECTORY); command.add("--assetsDir=" + VANILLA_GAME_DIRECTORY + "\\assets"); command.add("--assetIndex=" + "1.8.1"); command.add("--uuid=" + userID); command.add("--accessToken=" + accessToken); command.add("--userProperties=" + gson.toJson(user)); command.add("--userType=mojang"); for (String s : command) { System.out.println(s); } try { ProcessBuilder pb = new ProcessBuilder(command.toArray(new String[command.size()])) .directory(new File(VANILLA_GAME_DIRECTORY)); final Process process = pb.start(); StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR"); errorGobbler.start(); } catch (IOException e) { e.printStackTrace(); } } private void extractNatives() throws IOException { for (String s : NATIVE_JARS) { File archive = new File(s); ZipFile zipFile = new ZipFile(archive, Charset.forName("Cp437")); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File entryDestination = new File(VANILLA_NATIVE_DIR.replaceAll("<version>", "1.8.1"), entry.getName()); entryDestination.getParentFile().mkdirs(); if (entry.isDirectory()) entryDestination.mkdirs(); else { InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); in.close(); out.close(); } } zipFile.close(); } } private class StreamGobbler extends Thread { InputStream is; String type; private StreamGobbler(InputStream is, String type) { this.is = is; this.type = type; } @Override public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) System.out.println(type + "> " + line); } catch (IOException ioe) { ioe.printStackTrace(); } } } }