Let’s create a client that connects to a Minecraft server, spawns in the world, and reads packets.
1
Create the Client File
Create a new file called client.go:
client.go
package mainimport ( "fmt" "github.com/sandertv/gophertunnel/minecraft" "github.com/sandertv/gophertunnel/minecraft/auth" "github.com/sandertv/gophertunnel/minecraft/protocol/packet")func main() { // Create a minecraft.Dialer with an auth.TokenSource to authenticate dialer := minecraft.Dialer{ TokenSource: auth.TokenSource, } // Dial a connection to the target server address := "mco.mineplex.com:19132" conn, err := dialer.Dial("raknet", address) if err != nil { panic(err) } defer conn.Close() // Make the client spawn in the world if err := conn.DoSpawn(); err != nil { panic(err) } fmt.Println("Connected and spawned successfully!") // Read packets in a loop for { pk, err := conn.ReadPacket() if err != nil { break } // Type assert packets to access their data switch p := pk.(type) { case *packet.Emote: fmt.Printf("Emote packet received: %v\n", p.EmoteID) case *packet.MovePlayer: fmt.Printf("Player %v moved to %v\n", p.EntityRuntimeID, p.Position) case *packet.Text: fmt.Printf("Chat message: %v\n", p.Message) } }}
2
Run the Client
go run client.go
The first time you run this, you’ll be prompted to authenticate with Microsoft/Xbox Live. Follow the authentication flow in your browser.
The auth.TokenSource handles Xbox Live authentication automatically. Tokens are cached for subsequent runs.