vb.netaccess-tokenminecraftlauncher

VB.Net Custom Minecraft Launcher


I want to create my own Minecraft Launcher for me and my friends in VB.Net. I have this code which gives me access token.

Private ACCESS_TOKEN As String
Public Function GetAccessToken() As String
    Return ACCESS_TOKEN
End Function
Public Sub ObtainAccessToken()
    Dim username As String = TextBox1.Text
    Dim password As String = TextBox2.Text
    Dim UUID As String = Guid.NewGuid.ToString()
    Dim httpWebRequest = DirectCast(WebRequest.Create("https://authserver.mojang.com/authenticate"), HttpWebRequest)
    httpWebRequest.ContentType = "application/json"
    httpWebRequest.Method = "POST"

    Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
        Dim json As String = (Convert.ToString((Convert.ToString("{""agent"":{""name"":""Minecraft"",""version"":1},""username"":""") & username) + """,""password"":""") & password) + """,""clientToken"":" & ControlChars.Quote & UUID & ControlChars.Quote & "}"

        streamWriter.Write(json)
        streamWriter.Flush()
        streamWriter.Close()

        Dim httpResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
        Using streamReader = New StreamReader(httpResponse.GetResponseStream())
            Dim result = streamReader.ReadToEnd()
            ACCESS_TOKEN = result
        End Using
    End Using
End Sub

BUT What can I do with this access token ? How can I start minecraft directly with java arguments ? I want to start minecraft.jar file.


Solution

  • These were my beginnings of programming :) I started with Visual Basic now I'm doing C++. This is for anyone who is wondering how to make Minecraft Launcher.

    First you have to download "Game Files" everything is well documented HERE. Then you can start thinking about how to start the game.

    You will need access and client token I improved the code a bit:

    Imports System.IO
    Imports System.Net
    Imports System.Web.Script.Serialization ' Add Reference System.Web.Extensions
    
    Module Program
    
        Public Function Authenticate(ByRef username As String, ByRef password As String) As String
    
            Dim UUID As String = Guid.NewGuid.ToString()
            Dim Request As HttpWebRequest = DirectCast(WebRequest.Create("https://authserver.mojang.com/authenticate"), HttpWebRequest)
            Request.ContentType = "application/json"
            Request.Method = "POST"
    
            Using Writer = New StreamWriter(Request.GetRequestStream())
    
                Dim Json As String = ("{ 'agent': {'name': 'Minecraft', 'version': 1}, 'username': '" + username + "', 'password': '" + password + "', 'clientToken': '" + UUID + "', 'requestUser': true }").Replace("'", Chr(34))
    
                Writer.Write(Json)
                Writer.Flush()
                Writer.Close()
    
                Dim Response = DirectCast(Request.GetResponse(), HttpWebResponse)
                Using Reader = New StreamReader(Response.GetResponseStream())
                    Return Reader.ReadToEnd()
                End Using
    
            End Using
    
        End Function
    
        Sub Main()
    
            Dim Response As String = Authenticate("username", "password")
            Dim Serializer As JavaScriptSerializer = New JavaScriptSerializer()
    
            Dim Dictionary As Dictionary(Of String, Object) = Serializer.Deserialize(Of Dictionary(Of String, Object))(Response)
    
            Dim AccessToken As String = Dictionary("accessToken")
            Dim ClientToken As String = Dictionary("clientToken")
    
            Console.WriteLine(AccessToken)
            Console.WriteLine(ClientToken)
            Console.Read()
    
        End Sub
    
    End Module
    
    

    In this code you are sending HTTP POST request to the mojang auth server and then parsing the response as json with JavaScriptSerializer. The access token and client token are stored in AccessToken and ClientToken variables. Mojang Authentication is documented HERE. Then I would recommend you to create Process from System.Diagnostics and start it like so:

    Public Sub LaunchMinecraft(ByRef AccessToken As String, ByRef ClientToken As String)
    
        Dim Xmx As String = "4G"
        Dim Xms As String = "2G"
        Dim Title As String = "My Minecraft Instance"
        Dim Version As String = "1.7.10"
        Dim AssetIndex As String = "..." ' AssetIndex obtained from mojang server
        Dim Username As String = "..." ' Minecraft username obtained from mojang server
        Dim Libraries As String = "..." ' Paths to all libraries separated by ;
    
        Dim JavaProcess As Process = New Process()
        JavaProcess.StartInfo.UseShellExecute = False
        JavaProcess.StartInfo.CreateNoWindow = False
        JavaProcess.StartInfo.FileName = "..." ' Path to javaw.exe or java.exe
        JavaProcess.StartInfo.Arguments = "-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump" +
                                " -Xmx" + Xmx +
                                " -Xms" + Xms +
                                " -Djava.library.path=.//bin/natives" +
                                " -Dminecraft.client.jar=.//bin/modpack.jar" +
                                " -Dminecraft.applet.TargetDirectory=.//" +
                                " -cp .//bin/modpack.jar;" + Libraries + ";.//bin/minecraft.jar" +
                                " -XX:+UnlockExperimentalVMOptions" +
                                " -XX:+UseG1GC -XX:G1NewSizePercent=20" +
                                " -XX:G1ReservePercent=20" +
                                " -XX:MaxGCPauseMillis=50" +
                                " -XX:G1HeapRegionSize=16M" +
                                " net.minecraft.launchwrapper.Launch" +
                                " --gameDir .//" +
                                " --username " + Username +
                                " --assetsDir ..//..//..//cache/assets" +
                                " --assetIndex " + AssetIndex +
                                " --version " + Version +
                                " --uuid " + ClientToken +
                                " --accessToken " + AccessToken +
                                " --userProperties {}" +
                                " --userType mojang" +
                                " --tweakClass cpw.mods.fml.common.launcher.FMLTweaker" +
                                " --title " + Title
        JavaProcess.Start()
    
    End Sub
    

    This code wasn't tested and also has relative paths. It's just as an example. Note that this is for modded minecraft. Hope it helps!