Tnze / go-mc

Collection of Go libraries for Minecraft
https://go-mc.github.io/tutorial/
MIT License
853 stars 114 forks source link

Online Mode Supported? #26

Closed SugarD-x closed 5 years ago

SugarD-x commented 5 years ago

Is online mode supported out of the box, or does it have to be manually added? I was able to get offline mode working just fine, but online mode keeps throwing me authentication errors using a legitimate Minecraft account. Specifically: bot: encryption fail: login fail: auth fail: {"error":"IllegalArgumentException","errorMessage":"Access Token can not be null or empty."} exit status 1

I've gone through and set up the account information everywhere I can find, but I'm wondering if I could be missing a file or setting somewhere. I'm still a bit new to this, so I apologize if this is a stupid question. For information, this is for connecting to a vanilla 1.14.4 server on localhost with online-mode set to true.

Edit: On the server's end, it doesn't show me connecting, but it does show me disconnecting. No errors come up there.

Tnze commented 5 years ago

Are you code with bot package or just running a example in cmd? There is an example of online mode at https://github.com/Tnze/go-mc/blob/7d31acbb483689e47aeb0251c2a1d08b2f2c03ca/bot/example_test.go#L47

Make sure you have set c.AsTk.

SugarD-x commented 5 years ago

I'm running the daze example from cmd. That and luncher were the only two that would run for me without throwing an error about not being a main package. Is there a way to specifically run the example without creating a new package?

My apologies, I'm brand new to go-lang, and I'm still trying to wrap my head around how this project works. I'm attempting to get the basics down and work out a simple online mode connection before making anything with it so I don't create a bigger mess.

Edit: Is there a way to set c.AsTk, or does it just need the authentication from the username/email and password to get it? I have those set, but I don't see a place to specify c.AsTk itself.

Tnze commented 5 years ago

To set c.AsTk, just write c.AsTk = auth.AccessToken() into the daze.go

You get accessToken by yggdrasil.Authenticate() as an easy way.

daze.go(online)

package main

import (
    "bytes"
    "log"

    "github.com/Tnze/go-mc/bot"
    "github.com/Tnze/go-mc/chat"
    _ "github.com/Tnze/go-mc/data/lang/en-us"
    pk "github.com/Tnze/go-mc/net/packet"
    "github.com/Tnze/go-mc/yggdrasil"
)

func main() {
    c := bot.NewClient()

    //Login Mojang account to get AccessToken
    auth, err := yggdrasil.Authenticate("Your E-mail", "Your Password")
    if err != nil {
        panic(err)
    }

    c.Auth.UUID, c.Name = auth.SelectedProfile()
    c.AsTk = auth.AccessToken()

    //Login
    err := c.JoinServer("localhost", 25565)
    if err != nil {
        log.Fatal(err)
    }
    log.Println("Login success")

    //Register event handlers
    c.Events.GameStart = onGameStart
    c.Events.ChatMsg = onChatMsg
    c.Events.Disconnect = onDisconnect
    c.Events.PluginMessage = onPluginMessage

    //JoinGame
    err = c.HandleGame()
    if err != nil {
        log.Fatal(err)
    }
}

func onGameStart() error {
    log.Println("Game start")
    return nil //if err isn't nil, HandleGame() will return it.
}

func onChatMsg(c chat.Message, pos byte) error {
    log.Println("Chat:", c.ClearString()) // output chat message without any format code (like color or bold)
    return nil
}

func onDisconnect(c chat.Message) error {
    log.Println("Disconnect:", c)
    return nil
}

func onPluginMessage(channel string, data []byte) error {
    switch channel {
    case "minecraft:brand":
        var brand pk.String
        if err := brand.Decode(bytes.NewReader(data)); err != nil {
            return err
        }
        log.Println("Server brand is:", brand)

    default:
        log.Println("PluginMessage", channel, data)
    }
    return nil
}
Tnze commented 5 years ago

The example can't directly run. You must write your code,

SugarD-x commented 5 years ago

Thank you!!! Apparently I was on the right track and didn't realize it. I had tried messing with daze.go previously to do something similar to your example, but I had trouble getting it to work. What I didn't realize until now is that I needed to import github.com/Tnze/go-mc/yggdrasil and change the server login section's err name so it didn't match the one used in the access token login, else it would throw errors when running. You are awesome!!! Thank you again!!!

Tnze commented 5 years ago

You're welcome.