Repository navigation
Expand file tree
/
Copy pathexample_client_test.go
More file actions
122 lines (108 loc) · 3.11 KB
/
Copy pathexample_client_test.go
File metadata and controls
122 lines (108 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package pia
import (
"context"
"errors"
"fmt"
"log/slog"
"github.com/df-mc/go-nethernet"
"github.com/df-mc/go-playfab/v2"
"github.com/df-mc/go-xsapi/v2"
"github.com/df-mc/go-xsapi/v2/xal/sisu"
"github.com/lactyy2/pia/signaling"
"github.com/lactyy2/pia/signaling/messaging"
"github.com/sandertv/gophertunnel/minecraft"
"github.com/sandertv/gophertunnel/minecraft/auth"
"github.com/sandertv/gophertunnel/minecraft/protocol/login"
"github.com/sandertv/gophertunnel/minecraft/service"
)
// ExampleClient lists the worlds and joins the first world in the result.
func ExampleClient() {
token, err := auth.RequestLiveToken()
if err != nil {
panic(err)
}
msa := auth.AndroidConfig.TokenSource(context.TODO(), token)
xbl, err := xsapi.NewClient(auth.AndroidConfig.New(msa, nil))
if err != nil {
var acct *sisu.AccountCreationRequiredError
if errors.As(err, &acct) {
fmt.Printf("create an Xbox Live account at %s", acct.SignupURL)
return
}
panic(fmt.Sprintf("error logging in to Xbox Live network services: %s", err))
}
defer xbl.Close()
discovery, err := service.Default(context.TODO())
if err != nil {
panic(fmt.Sprintf("error retrieving default discovery data: %s", err))
}
env := new(service.AuthorizationEnvironment)
if err := discovery.Environment(env); err != nil {
panic(fmt.Sprintf("error resolving environment for %q: %s", env.ServiceName(), err))
}
pf, err := playfab.LoginWithXbox(context.TODO(), env.PlayFabTitleID, xbl, playfab.ClientConfig{
CreateAccount: true,
})
if err != nil {
panic(fmt.Sprintf("error logging in to playfab account: %s", err))
}
defer pf.Close()
src := env.TokenSource(pf, service.TokenConfig{})
client := NewClient(xbl)
worlds, err := client.Worlds(context.TODO())
if err != nil {
panic(fmt.Sprintf("error listing worlds: %s", err))
}
if len(worlds) == 0 {
panic("no open worlds")
}
world := worlds[0]
session, err := world.Join(context.TODO())
if err != nil {
panic(fmt.Sprintf("error joining world: %s", err))
}
defer session.Close()
connection := session.Connection()
var s nethernet.Signaling
switch connection.Type {
case ConnectionTypeSignalingOverJSONRPC:
var d messaging.Dialer
conn, err := d.DialContext(context.TODO(), src)
if err != nil {
panic(fmt.Sprintf("error dialing messaging conn: %s", err))
}
defer conn.Close()
s = conn
case ConnectionTypeSignalingOverWebSocket:
var d signaling.Dialer
conn, err := d.DialContext(context.TODO(), src)
if err != nil {
panic(fmt.Sprintf("error dialing signaling conn: %s", err))
}
defer conn.Close()
s = conn
default:
panic(fmt.Sprintf("invalid connection type: %d", connection.Type))
}
address := connection.Address()
minecraft.RegisterNetwork("nethernet", func(l *slog.Logger) minecraft.Network {
return minecraft.NetherNet{
Signaling: s,
Log: l,
}
})
conn, err := minecraft.Dialer{
XBLClient: xbl,
PlayFabClient: pf,
ClientData: login.ClientData{
Nonce: session.Nonce(),
},
}.Dial("nethernet", address)
if err != nil {
panic(err)
}
defer conn.Close()
if err := conn.DoSpawn(); err != nil {
panic(fmt.Sprintf("spawn: %s", err))
}
}