forked from Tnze/go-mc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (163 loc) · 4.74 KB
/
main.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// This example is a minimal minecraft 1.17.1 server allowing vanilla clients or go-mc/bot to connect.
// Has the same functionality as "simpleServer1.15.2", but with 1.17.1 protocol.
//
// It is used to test DimensionCodec stuffs during go-mc development.
package main
import (
_ "embed"
"log"
"github.com/google/uuid"
"github.com/SkyVillageMC/go-mc/nbt"
"github.com/SkyVillageMC/go-mc/net"
pk "github.com/SkyVillageMC/go-mc/net/packet"
"github.com/SkyVillageMC/go-mc/offline"
)
const (
ProtocolVersion = 756
MaxPlayer = 200
)
// Packet IDs
const (
PlayerPositionAndLookClientbound = 0x38
JoinGame = 0x26
)
func main() {
l, err := net.ListenMC(":25565")
if err != nil {
log.Fatalf("Listen error: %v", err)
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatalf("Accept error: %v", err)
}
go acceptConn(conn)
}
}
func acceptConn(conn net.Conn) {
defer conn.Close()
// handshake
protocol, intention, err := handshake(conn)
if err != nil {
log.Printf("Handshake error: %v", err)
return
}
switch intention {
default: // unknown error
log.Printf("Unknown handshake intention: %v", intention)
case 1: // for status
acceptListPing(conn)
case 2: // for login
handlePlaying(conn, protocol)
}
}
func handlePlaying(conn net.Conn, protocol int32) {
// login, get player info
info, err := acceptLogin(conn)
if err != nil {
log.Print("Login failed")
return
}
// Write LoginSuccess packet
if err = loginSuccess(conn, info.Name, info.UUID); err != nil {
log.Print("Login failed on success")
return
}
if err := joinGame(conn); err != nil {
log.Print("Login failed on joinGame")
return
}
if err := conn.WritePacket(pk.Marshal(PlayerPositionAndLookClientbound,
// https://wiki.vg/index.php?title=Protocol&oldid=16067#Player_Position_And_Look_.28clientbound.29
pk.Double(0), pk.Double(0), pk.Double(0), // XYZ
pk.Float(0), pk.Float(0), // Yaw Pitch
pk.Byte(0), // flag
pk.VarInt(0), // TP ID
pk.Boolean(false), // Dismount vehicle
)); err != nil {
log.Printf("Login failed on sending PlayerPositionAndLookClientbound: %v", err)
return
}
// Just for block this goroutine. Keep the connection
for {
var p pk.Packet
if err := conn.ReadPacket(&p); err != nil {
log.Printf("ReadPacket error: %v", err)
break
}
// KeepAlive packet is not handled, so client might
// exit because of "time out".
}
}
type PlayerInfo struct {
Name string
UUID uuid.UUID
OPLevel int
}
// acceptLogin check player's account
func acceptLogin(conn net.Conn) (info PlayerInfo, err error) {
// login start
var p pk.Packet
err = conn.ReadPacket(&p)
if err != nil {
return
}
err = p.Scan((*pk.String)(&info.Name)) // decode username as pk.String
if err != nil {
return
}
// auth
const OnlineMode = false
if OnlineMode {
log.Panic("Not Implement")
} else {
// offline-mode UUID
info.UUID = offline.NameToUUID(info.Name)
}
return
}
// handshake receive and parse Handshake packet
func handshake(conn net.Conn) (protocol, intention int32, err error) {
var (
p pk.Packet
Protocol, Intention pk.VarInt
ServerAddress pk.String // ignored
ServerPort pk.UnsignedShort // ignored
)
// receive handshake packet
if err = conn.ReadPacket(&p); err != nil {
return
}
err = p.Scan(&Protocol, &ServerAddress, &ServerPort, &Intention)
return int32(Protocol), int32(Intention), err
}
// loginSuccess send LoginSuccess packet to client
func loginSuccess(conn net.Conn, name string, uuid uuid.UUID) error {
return conn.WritePacket(pk.Marshal(0x02,
pk.UUID(uuid),
pk.String(name),
))
}
//go:embed DimensionCodec.snbt
var dimensionCodecSNBT string
//go:embed Dimension.snbt
var dimensionSNBT string
func joinGame(conn net.Conn) error {
return conn.WritePacket(pk.Marshal(JoinGame,
pk.Int(0), // EntityID
pk.Boolean(false), // Is hardcore
pk.UnsignedByte(1), // Gamemode
pk.Byte(1), // Previous Gamemode
pk.Array([]pk.Identifier{"world"}), // World Names
pk.NBT(nbt.StringifiedMessage(dimensionCodecSNBT)), // Dimension codec
pk.NBT(nbt.StringifiedMessage(dimensionSNBT)), // Dimension
pk.Identifier("world"), // World Name
pk.Long(0), // Hashed Seed
pk.VarInt(MaxPlayer), // Max Players
pk.VarInt(15), // View Distance
pk.Boolean(false), // Reduced Debug Info
pk.Boolean(true), // Enable respawn screen
pk.Boolean(false), // Is Debug
pk.Boolean(true), // Is Flat
))
}