1.21.8 data
Some checks failed
CodeQL / Analyze (go) (push) Has been cancelled
Go / Test (1.22) (push) Has been cancelled
Go / Test (^1.22) (push) Has been cancelled

This commit is contained in:
2025-08-22 06:17:33 +08:00
parent 133e3fab4a
commit 0958972953
173 changed files with 13782 additions and 406 deletions

79
bot/example_test.go Normal file
View File

@ -0,0 +1,79 @@
package bot
import (
"encoding/hex"
"log"
"github.com/Tnze/go-mc/offline"
"github.com/Tnze/go-mc/yggdrasil"
)
func ExamplePingAndList() {
resp, delay, err := PingAndList("localhost:25565")
if err != nil {
log.Fatalf("ping and list server fail: %v", err)
}
log.Println("Status:", string(resp))
log.Println("Delay:", delay)
}
func ExampleClient_JoinServer_offline() {
c := NewClient()
c.Auth.Name = "Tnze" // set its name before login.
id := offline.NameToUUID(c.Auth.Name) // optional, get uuid of offline mode game
c.Auth.UUID = hex.EncodeToString(id[:])
// Login
err := c.JoinServer("127.0.0.1")
if err != nil {
log.Fatal(err)
}
log.Println("Login success")
// Register event handlers
// c.Events.AddListener(...)
// JoinGame
err = c.HandleGame()
if err != nil {
log.Fatal(err)
}
}
func ExampleClient_JoinServer_online() {
c := NewClient()
// Login Mojang account to get AccessToken
// To use Microsoft Account, see issue #106
// https://github.com/Tnze/go-mc/issues/106
auth, err := yggdrasil.Authenticate("Your E-mail", "Your Password")
if err != nil {
panic(err)
}
// As long as you set these three fields correctly,
// the client can connect to the online-mode server
c.Auth.UUID, c.Auth.Name = auth.SelectedProfile()
c.Auth.AsTk = auth.AccessToken()
// Connect server
err = c.JoinServer("127.0.0.1")
if err != nil {
log.Fatal(err)
}
log.Println("Login success")
// Register event handlers
// c.Events.GameStart = onGameStartFunc
// c.Events.ChatMsg = onChatMsgFunc
// c.Events.Disconnect = onDisconnectFunc
// ...
// Join the game
err = c.HandleGame()
if err != nil {
log.Fatal(err)
}
}