This repository has been archived by the owner on Jun 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
packet_manager.go
380 lines (312 loc) · 12.7 KB
/
packet_manager.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package gomine
import (
"github.com/golang/geo/r3"
"github.com/google/uuid"
"github.com/irmine/gomine/net/info"
"github.com/irmine/gomine/net/packets"
"github.com/irmine/gomine/net/packets/bedrock"
"github.com/irmine/gomine/net/packets/data"
"github.com/irmine/gomine/net/packets/types"
"github.com/irmine/gomine/net/protocol"
"github.com/irmine/gomine/packs"
"github.com/irmine/gomine/permissions"
"github.com/irmine/worlds/blocks"
"github.com/irmine/worlds/chunks"
data2 "github.com/irmine/worlds/entities/data"
"math"
)
type PacketManager struct {
*protocol.PacketManagerBase
}
func NewPacketManager(server *Server) *PacketManager {
var ids = info.PacketIds
var proto = &PacketManager{protocol.NewPacketManagerBase(info.PacketIds, map[int]func() packets.IPacket{
ids[info.LoginPacket]: func() packets.IPacket { return bedrock.NewLoginPacket() },
ids[info.ClientHandshakePacket]: func() packets.IPacket { return bedrock.NewClientHandshakePacket() },
ids[info.ResourcePackClientResponsePacket]: func() packets.IPacket { return bedrock.NewResourcePackClientResponsePacket() },
ids[info.RequestChunkRadiusPacket]: func() packets.IPacket { return bedrock.NewRequestChunkRadiusPacket() },
ids[info.MovePlayerPacket]: func() packets.IPacket { return bedrock.NewMovePlayerPacket() },
ids[info.CommandRequestPacket]: func() packets.IPacket { return bedrock.NewCommandRequestPacket() },
ids[info.ResourcePackChunkRequestPacket]: func() packets.IPacket { return bedrock.NewResourcePackChunkRequestPacket() },
ids[info.TextPacket]: func() packets.IPacket { return bedrock.NewTextPacket() },
ids[info.PlayerListPacket]: func() packets.IPacket { return bedrock.NewPlayerListPacket() },
ids[info.InteractPacket]: func() packets.IPacket { return bedrock.NewInteractPacket() },
ids[info.SetEntityDataPacket]: func() packets.IPacket { return bedrock.NewSetEntityDataPacket() },
ids[info.PlayerActionPacket]: func() packets.IPacket { return bedrock.NewPlayerActionPacket() },
ids[info.AnimatePacket]: func() packets.IPacket { return bedrock.NewAnimatePacket() },
ids[info.InventoryTransactionPacket]: func() packets.IPacket { return bedrock.NewInventoryTransactionPacket() },
}, map[int][][]protocol.Handler{})}
proto.initHandlers(server)
return proto
}
func (protocol *PacketManager) initHandlers(server *Server) {
protocol.RegisterHandler(info.LoginPacket, NewLoginHandler(server))
protocol.RegisterHandler(info.ClientHandshakePacket, NewClientHandshakeHandler(server))
protocol.RegisterHandler(info.RequestChunkRadiusPacket, NewRequestChunkRadiusHandler(server))
protocol.RegisterHandler(info.ResourcePackClientResponsePacket, NewResourcePackClientResponseHandler(server))
protocol.RegisterHandler(info.MovePlayerPacket, NewMovePlayerHandler(server))
protocol.RegisterHandler(info.CommandRequestPacket, NewCommandRequestHandler(server))
protocol.RegisterHandler(info.ResourcePackChunkRequestPacket, NewResourcePackChunkRequestHandler(server))
protocol.RegisterHandler(info.TextPacket, NewTextHandler(server))
protocol.RegisterHandler(info.InteractPacket, NewInteractHandler(server))
protocol.RegisterHandler(info.PlayerActionPacket, NewPlayerActionHandler(server))
protocol.RegisterHandler(info.AnimatePacket, NewAnimateHandler(server))
protocol.RegisterHandler(info.InventoryTransactionPacket, NewInventoryTransactionHandler(server))
}
func (protocol *PacketManager) GetAddEntity(entity protocol.AddEntityEntry) packets.IPacket {
var pk = bedrock.NewAddEntityPacket()
pk.UniqueId = entity.GetUniqueId()
pk.RuntimeId = entity.GetRuntimeId()
pk.EntityType = entity.GetEntityType()
pk.Position = entity.GetPosition()
pk.Motion = entity.GetMotion()
pk.Rotation = entity.GetRotation()
pk.Attributes = entity.GetAttributeMap()
pk.EntityData = entity.GetEntityData()
return pk
}
func (protocol *PacketManager) GetAddPlayer(uuid uuid.UUID, player protocol.AddPlayerEntry) packets.IPacket {
var pk = bedrock.NewAddPlayerPacket()
pk.UUID = uuid
pk.Username = player.GetName()
pk.EntityRuntimeId = player.GetRuntimeId()
pk.EntityUniqueId = player.GetUniqueId()
pk.Position = player.GetPosition()
pk.Rotation = player.GetRotation()
pk.Motion = player.GetMotion()
return pk
}
func (protocol *PacketManager) GetChunkRadiusUpdated(radius int32) packets.IPacket {
var pk = bedrock.NewChunkRadiusUpdatedPacket()
pk.Radius = radius
return pk
}
func (protocol *PacketManager) GetCraftingData() packets.IPacket {
var pk = bedrock.NewCraftingDataPacket()
return pk
}
func (protocol *PacketManager) GetDisconnect(message string, hideDisconnectScreen bool) packets.IPacket {
var pk = bedrock.NewDisconnectPacket()
pk.HideDisconnectionScreen = hideDisconnectScreen
pk.Message = message
return pk
}
func (protocol *PacketManager) GetFullChunkData(chunk *chunks.Chunk) packets.IPacket {
var pk = bedrock.NewFullChunkDataPacket()
pk.ChunkX, pk.ChunkZ = chunk.X, chunk.Z
pk.ChunkData = chunk.ToBinary()
return pk
}
func (protocol *PacketManager) GetMovePlayer(runtimeId uint64, position r3.Vector, rotation data2.Rotation, mode byte, onGround bool, ridingRuntimeId uint64) packets.IPacket {
var pk = bedrock.NewMovePlayerPacket()
pk.RuntimeId = runtimeId
pk.Position = position
pk.Rotation = rotation
pk.Mode = mode
pk.OnGround = onGround
pk.RidingRuntimeId = ridingRuntimeId
return pk
}
func (protocol *PacketManager) GetPlayerList(listType byte, players map[string]protocol.PlayerListEntry) packets.IPacket {
var pk = bedrock.NewPlayerListPacket()
pk.ListType = listType
var entries = map[string]types.PlayerListEntry{}
for name, player := range players {
entries[name] = types.PlayerListEntry{
UUID: player.GetUUID(),
XUID: player.GetXUID(),
EntityUniqueId: player.GetUniqueId(),
Username: player.GetName(),
DisplayName: player.GetDisplayName(),
Platform: player.GetPlatform(),
SkinId: player.GetSkinId(),
SkinData: player.GetSkinData(),
CapeData: player.GetCapeData(),
GeometryName: player.GetGeometryName(),
GeometryData: player.GetGeometryData(),
}
}
pk.Entries = entries
return pk
}
func (protocol *PacketManager) GetPlayStatus(status int32) packets.IPacket {
var pk = bedrock.NewPlayStatusPacket()
pk.Status = status
return pk
}
func (protocol *PacketManager) GetRemoveEntity(uniqueId int64) packets.IPacket {
var pk = bedrock.NewRemoveEntityPacket()
pk.EntityUniqueId = uniqueId
return pk
}
func (protocol *PacketManager) GetResourcePackChunkData(packUUID string, chunkIndex int32, progress int64, data []byte) packets.IPacket {
var pk = bedrock.NewResourcePackChunkDataPacket()
pk.PackUUID = packUUID
pk.ChunkIndex = chunkIndex
pk.Progress = progress
pk.ChunkData = data
return pk
}
func (protocol *PacketManager) GetResourcePackDataInfo(pack packs.Pack) packets.IPacket {
var pk = bedrock.NewResourcePackDataInfoPacket()
pk.PackUUID = pack.GetUUID()
pk.MaxChunkSize = data.ResourcePackChunkSize
pk.ChunkCount = int32(math.Ceil(float64(pack.GetFileSize()) / float64(data.ResourcePackChunkSize)))
pk.CompressedPackSize = pack.GetFileSize()
pk.Sha256 = pack.GetSha256()
return pk
}
func (protocol *PacketManager) GetResourcePackInfo(mustAccept bool, resourcePacks *packs.Stack, behaviorPacks *packs.Stack) packets.IPacket {
var pk = bedrock.NewResourcePackInfoPacket()
pk.MustAccept = mustAccept
var resourceEntries []types.ResourcePackInfoEntry
var behaviorEntries []types.ResourcePackInfoEntry
for _, pack := range *resourcePacks {
resourceEntries = append(resourceEntries, types.ResourcePackInfoEntry{
UUID: pack.GetUUID(),
Version: pack.GetVersion(),
PackSize: pack.GetFileSize(),
})
}
for _, pack := range *behaviorPacks {
behaviorEntries = append(behaviorEntries, types.ResourcePackInfoEntry{
UUID: pack.GetUUID(),
Version: pack.GetVersion(),
PackSize: pack.GetFileSize(),
})
}
pk.ResourcePacks = resourceEntries
pk.BehaviorPacks = behaviorEntries
return pk
}
func (protocol *PacketManager) GetResourcePackStack(mustAccept bool, resourcePacks *packs.Stack, behaviorPacks *packs.Stack) packets.IPacket {
var pk = bedrock.NewResourcePackStackPacket()
pk.MustAccept = mustAccept
var resourceEntries []types.ResourcePackStackEntry
var behaviorEntries []types.ResourcePackStackEntry
for _, pack := range *resourcePacks {
resourceEntries = append(resourceEntries, types.ResourcePackStackEntry{
UUID: pack.GetUUID(),
Version: pack.GetVersion(),
})
}
for _, pack := range *behaviorPacks {
behaviorEntries = append(behaviorEntries, types.ResourcePackStackEntry{
UUID: pack.GetUUID(),
Version: pack.GetVersion(),
})
}
pk.ResourcePacks = resourceEntries
pk.BehaviorPacks = behaviorEntries
return pk
}
func (protocol *PacketManager) GetServerHandshake(encryptionJwt string) packets.IPacket {
var pk = bedrock.NewServerHandshakePacket()
pk.Jwt = encryptionJwt
return pk
}
func (protocol *PacketManager) GetSetEntityData(runtimeId uint64, data map[uint32][]interface{}) packets.IPacket {
var pk = bedrock.NewSetEntityDataPacket()
pk.RuntimeId = runtimeId
pk.EntityData = data
return pk
}
func (protocol *PacketManager) GetStartGame(player protocol.StartGameEntry, runtimeIdsTable []byte) packets.IPacket {
var pk = bedrock.NewStartGamePacket()
pk.Generator = 1
pk.LevelSeed = 312402
pk.DefaultPermissionLevel = permissions.LevelMember
pk.EntityRuntimeId = player.GetRuntimeId()
pk.EntityUniqueId = player.GetUniqueId()
pk.PlayerGameMode = 1
pk.PlayerPosition = player.GetPosition()
pk.LevelGameMode = 1
pk.LevelSpawnPosition = blocks.NewPosition(0, 7, 0)
pk.CommandsEnabled = true
var gameRules = player.GetDimension().GetLevel().GetGameRules()
var gameRuleEntries = map[string]types.GameRuleEntry{}
for name, gameRule := range gameRules {
gameRuleEntries[string(name)] = types.GameRuleEntry{Name: string(gameRule.GetName()), Value: gameRule.GetValue()}
}
pk.GameRules = gameRuleEntries
pk.LevelName = player.GetDimension().GetLevel().GetName()
pk.CurrentTick = player.GetDimension().GetLevel().GetCurrentTick()
pk.Time = 0
pk.AchievementsDisabled = true
pk.BroadcastToLan = true
pk.RuntimeIdsTable = runtimeIdsTable
pk.PlatformBroadcastIntent = bedrock.GameBroadcastSettingPublic
pk.XBOXBroadcastIntent = bedrock.GameBroadcastSettingPublic
return pk
}
func (protocol *PacketManager) GetText(text types.Text) packets.IPacket {
var pk = bedrock.NewTextPacket()
pk.TextType = text.TextType
pk.Translation = text.IsTranslation
pk.Params = text.TranslationParameters
pk.SourceName = text.SourceName
pk.XUID = text.SourceXUID
pk.Message = text.Message
return pk
}
func (protocol *PacketManager) GetTransfer(address string, port uint16) packets.IPacket {
var pk = bedrock.NewTransferPacket()
pk.Address = address
pk.Port = port
return pk
}
func (protocol *PacketManager) GetUpdateAttributes(runtimeId uint64, attributeMap data2.AttributeMap) packets.IPacket {
var pk = bedrock.NewUpdateAttributesPacket()
pk.RuntimeId = runtimeId
pk.Attributes = attributeMap
return pk
}
func (protocol *PacketManager) GetNetworkChunkPublisherUpdatePacket(position blocks.Position, radius uint32) packets.IPacket {
var pk = bedrock.NewNetworkChunkPublisherUpdatePacket()
pk.Position = position
pk.Radius = radius
return pk
}
func (protocol *PacketManager) GetMoveEntity(runtimeId uint64, position r3.Vector, rot data2.Rotation, flags byte, teleport bool) packets.IPacket {
var pk = bedrock.NewMoveEntityPacket()
pk.RuntimeId = runtimeId
pk.Position = position
pk.Rotation = rot
pk.Flags = flags
if teleport {
pk.Flags |= data.MoveEntityTeleport
}
return pk
}
func (protocol *PacketManager) GetPlayerSkin(uuid2 uuid.UUID, skinId, geometryName, geometryData string, skinData, capeData []byte) packets.IPacket {
var pk = bedrock.NewPlayerSkinPacket()
pk.UUID = uuid2
pk.SkinId = skinId
pk.SkinData = skinData
pk.CapeData = capeData
pk.GeometryName = geometryName
pk.GeometryData = geometryData
return pk
}
func (protocol *PacketManager) GetPlayerAction(runtimeId uint64, action int32, position blocks.Position, face int32) packets.IPacket {
var pk = bedrock.NewPlayerActionPacket()
pk.RuntimeId = runtimeId
pk.Action = action
pk.Position = position
pk.Face = face
return pk
}
func (protocol *PacketManager) GetAnimate(action int32, runtimeId uint64, float float32) packets.IPacket {
var pk = bedrock.NewAnimatePacket()
pk.RuntimeId = runtimeId
pk.Action = action
pk.Float = float
return pk
}
func (protocol *PacketManager) GetUpdateBlock(position blocks.Position, blockRuntimeId, dataLayerId uint32) packets.IPacket {
var pk = bedrock.NewUpdateBlockPacket()
pk.Position = position
pk.BlockRuntimeId = blockRuntimeId
pk.DataLayerId = dataLayerId
return pk
}