forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld.go
1282 lines (1207 loc) · 38.1 KB
/
world.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package session
import (
"github.com/df-mc/dragonfly/server/entity/effect"
"image/color"
"math/rand/v2"
"strings"
"time"
"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/entity"
"github.com/df-mc/dragonfly/server/internal/nbtconv"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/item/inventory"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/particle"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl32"
"github.com/go-gl/mathgl/mgl64"
"github.com/google/uuid"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)
// NetworkEncodeableEntity is a world.EntityType where the save ID and network
// ID are not the same.
type NetworkEncodeableEntity interface {
// NetworkEncodeEntity returns the network type ID of the entity. This is
// NOT the save ID.
NetworkEncodeEntity() string
}
// OffsetEntity is a world.EntityType that has an additional offset when sent
// over network. This is mostly the case for older entities such as players and
// TNT.
type OffsetEntity interface {
NetworkOffset() float64
}
// entityHidden checks if a world.Entity is being explicitly hidden from the Session.
func (s *Session) entityHidden(e world.Entity) bool {
s.entityMutex.RLock()
_, ok := s.hiddenEntities[e.H().UUID()]
s.entityMutex.RUnlock()
return ok
}
// ViewEntity ...
func (s *Session) ViewEntity(e world.Entity) {
if e.H() == s.ent {
s.ViewEntityState(e)
return
}
if s.entityHidden(e) {
return
}
var runtimeID uint64
_, controllable := e.(Controllable)
s.entityMutex.Lock()
if id, ok := s.entityRuntimeIDs[e.H()]; ok && controllable {
runtimeID = id
} else {
s.currentEntityRuntimeID += 1
runtimeID = s.currentEntityRuntimeID
s.entityRuntimeIDs[e.H()] = runtimeID
s.entities[runtimeID] = e.H()
}
s.entityMutex.Unlock()
yaw, pitch := e.Rotation().Elem()
metadata := s.parseEntityMetadata(e)
id := e.H().Type().EncodeEntity()
switch v := e.(type) {
case Controllable:
_, actualPlayer := sessions.Lookup(v.UUID())
if !actualPlayer {
s.writePacket(&packet.PlayerList{ActionType: packet.PlayerListActionAdd, Entries: []protocol.PlayerListEntry{{
UUID: v.UUID(),
EntityUniqueID: int64(runtimeID),
Username: v.Name(),
Skin: skinToProtocol(v.Skin()),
}}})
}
s.writePacket(&packet.AddPlayer{
EntityMetadata: metadata,
EntityRuntimeID: runtimeID,
GameType: gameTypeFromMode(v.GameMode()),
HeadYaw: float32(yaw),
Pitch: float32(pitch),
Position: vec64To32(e.Position()),
UUID: v.UUID(),
Username: v.Name(),
Yaw: float32(yaw),
AbilityData: protocol.AbilityData{
EntityUniqueID: int64(runtimeID),
Layers: []protocol.AbilityLayer{{
Type: protocol.AbilityLayerTypeBase,
Abilities: protocol.AbilityCount - 1,
}},
},
})
if !actualPlayer {
s.writePacket(&packet.PlayerList{ActionType: packet.PlayerListActionRemove, Entries: []protocol.PlayerListEntry{{
UUID: v.UUID(),
}}})
} else {
s.ViewSkin(e)
}
return
case *entity.Ent:
switch e.H().Type() {
case entity.ItemType:
s.writePacket(&packet.AddItemActor{
EntityUniqueID: int64(runtimeID),
EntityRuntimeID: runtimeID,
Item: instanceFromItem(v.Behaviour().(*entity.ItemBehaviour).Item()),
Position: vec64To32(v.Position()),
Velocity: vec64To32(v.Velocity()),
EntityMetadata: metadata,
})
return
case entity.TextType:
metadata[protocol.EntityDataKeyVariant] = int32(world.BlockRuntimeID(block.Air{}))
case entity.FallingBlockType:
metadata[protocol.EntityDataKeyVariant] = int32(world.BlockRuntimeID(v.Behaviour().(*entity.FallingBlockBehaviour).Block()))
}
}
if v, ok := e.H().Type().(NetworkEncodeableEntity); ok {
id = v.NetworkEncodeEntity()
}
var vel mgl64.Vec3
if v, ok := e.(interface{ Velocity() mgl64.Vec3 }); ok {
vel = v.Velocity()
}
s.writePacket(&packet.AddActor{
EntityUniqueID: int64(runtimeID),
EntityRuntimeID: runtimeID,
EntityType: id,
EntityMetadata: metadata,
Position: vec64To32(e.Position()),
Velocity: vec64To32(vel),
Pitch: float32(pitch),
Yaw: float32(yaw),
HeadYaw: float32(yaw),
})
}
// ViewEntityGameMode ...
func (s *Session) ViewEntityGameMode(e world.Entity) {
if s.entityHidden(e) {
return
}
c, ok := e.(Controllable)
if !ok {
return
}
s.writePacket(&packet.UpdatePlayerGameType{
GameType: gameTypeFromMode(c.GameMode()),
PlayerUniqueID: int64(s.entityRuntimeID(c)),
})
}
// HideEntity ...
func (s *Session) HideEntity(e world.Entity) {
if s.entityRuntimeID(e) == selfEntityRuntimeID {
return
}
s.entityMutex.Lock()
id, ok := s.entityRuntimeIDs[e.H()]
if _, controllable := e.(Controllable); !controllable {
delete(s.entityRuntimeIDs, e.H())
delete(s.entities, id)
}
s.entityMutex.Unlock()
if !ok {
// The entity was already removed some other way. We don't need to send a packet.
return
}
s.writePacket(&packet.RemoveActor{EntityUniqueID: int64(id)})
}
// ViewEntityMovement ...
func (s *Session) ViewEntityMovement(e world.Entity, pos mgl64.Vec3, rot cube.Rotation, onGround bool) {
id := s.entityRuntimeID(e)
if (id == selfEntityRuntimeID && s.moving) || s.entityHidden(e) {
return
}
flags := byte(0)
if onGround {
flags |= packet.MoveFlagOnGround
}
s.writePacket(&packet.MoveActorAbsolute{
EntityRuntimeID: id,
Position: vec64To32(pos.Add(entityOffset(e))),
Rotation: vec64To32(mgl64.Vec3{rot.Pitch(), rot.Yaw(), rot.Yaw()}),
Flags: flags,
})
}
// ViewEntityVelocity ...
func (s *Session) ViewEntityVelocity(e world.Entity, velocity mgl64.Vec3) {
if s.entityHidden(e) {
return
}
s.writePacket(&packet.SetActorMotion{
EntityRuntimeID: s.entityRuntimeID(e),
Velocity: vec64To32(velocity),
})
}
// entityOffset returns the offset that entities have client-side.
func entityOffset(e world.Entity) mgl64.Vec3 {
if offset, ok := e.H().Type().(OffsetEntity); ok {
return mgl64.Vec3{0, offset.NetworkOffset()}
}
return mgl64.Vec3{}
}
// ViewTime ...
func (s *Session) ViewTime(time int) {
s.writePacket(&packet.SetTime{Time: int32(time)})
}
// ViewEntityTeleport ...
func (s *Session) ViewEntityTeleport(e world.Entity, position mgl64.Vec3) {
id := s.entityRuntimeID(e)
if s.entityHidden(e) {
return
}
yaw, pitch := e.Rotation().Elem()
if id == selfEntityRuntimeID {
s.teleportPos.Store(&position)
}
s.writePacket(&packet.SetActorMotion{EntityRuntimeID: id})
if _, ok := e.(Controllable); ok {
s.writePacket(&packet.MovePlayer{
EntityRuntimeID: id,
Position: vec64To32(position.Add(entityOffset(e))),
Pitch: float32(pitch),
Yaw: float32(yaw),
HeadYaw: float32(yaw),
Mode: packet.MoveModeTeleport,
})
return
}
s.writePacket(&packet.MoveActorAbsolute{
EntityRuntimeID: id,
Position: vec64To32(position.Add(entityOffset(e))),
Rotation: vec64To32(mgl64.Vec3{pitch, yaw, yaw}),
Flags: packet.MoveFlagTeleport,
})
}
// ViewEntityItems ...
func (s *Session) ViewEntityItems(e world.Entity) {
runtimeID := s.entityRuntimeID(e)
if runtimeID == selfEntityRuntimeID || s.entityHidden(e) {
// Don't view the items of the entity if the entity is the Controllable entity of the session.
return
}
c, ok := e.(item.Carrier)
if !ok {
return
}
mainHand, offHand := c.HeldItems()
// Show the main hand item.
s.writePacket(&packet.MobEquipment{
EntityRuntimeID: runtimeID,
NewItem: instanceFromItem(mainHand),
})
// Show the off-hand item.
s.writePacket(&packet.MobEquipment{
EntityRuntimeID: runtimeID,
NewItem: instanceFromItem(offHand),
WindowID: protocol.WindowIDOffHand,
})
}
// ViewEntityArmour ...
func (s *Session) ViewEntityArmour(e world.Entity) {
runtimeID := s.entityRuntimeID(e)
if runtimeID == selfEntityRuntimeID || s.entityHidden(e) {
// Don't view the items of the entity if the entity is the Controllable entity of the session.
return
}
armoured, ok := e.(interface {
Armour() *inventory.Armour
})
if !ok {
return
}
inv := armoured.Armour()
// Show the main hand item.
s.writePacket(&packet.MobArmourEquipment{
EntityRuntimeID: runtimeID,
Helmet: instanceFromItem(inv.Helmet()),
Chestplate: instanceFromItem(inv.Chestplate()),
Leggings: instanceFromItem(inv.Leggings()),
Boots: instanceFromItem(inv.Boots()),
})
}
// ViewItemCooldown ...
func (s *Session) ViewItemCooldown(item world.Item, duration time.Duration) {
name, _ := item.EncodeItem()
s.writePacket(&packet.ClientStartItemCooldown{
Category: strings.Split(name, ":")[1],
Duration: int32(duration.Milliseconds() / 50),
})
}
// ViewParticle ...
func (s *Session) ViewParticle(pos mgl64.Vec3, p world.Particle) {
switch pa := p.(type) {
case particle.DragonEggTeleport:
xSign, ySign, zSign := 0, 0, 0
if pa.Diff.X() < 0 {
xSign = 1 << 24
}
if pa.Diff.Y() < 0 {
ySign = 1 << 25
}
if pa.Diff.Z() < 0 {
zSign = 1 << 26
}
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticlesDragonEgg,
Position: vec64To32(pos),
EventData: int32((((((abs(pa.Diff.X()) << 16) | (abs(pa.Diff.Y()) << 8)) | abs(pa.Diff.Z())) | xSign) | ySign) | zSign),
})
case particle.Note:
s.writePacket(&packet.BlockEvent{
EventType: pa.Instrument.Int32(),
EventData: int32(pa.Pitch),
Position: protocol.BlockPos{int32(pos.X()), int32(pos.Y()), int32(pos.Z())},
})
case particle.HugeExplosion:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticlesExplosion,
Position: vec64To32(pos),
})
case particle.BoneMeal:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleCropGrowth,
Position: vec64To32(pos),
})
case particle.BlockForceField:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleDenyBlock,
Position: vec64To32(pos),
})
case particle.BlockBreak:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticlesDestroyBlock,
Position: vec64To32(pos),
EventData: int32(world.BlockRuntimeID(pa.Block)),
})
case particle.PunchBlock:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticlesCrackBlock,
Position: vec64To32(pos),
EventData: int32(world.BlockRuntimeID(pa.Block)) | (int32(pa.Face) << 24),
})
case particle.EndermanTeleport:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticlesTeleport,
Position: vec64To32(pos),
})
case particle.Flame:
if pa.Colour != (color.RGBA{}) {
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 56,
Position: vec64To32(pos),
EventData: nbtconv.Int32FromRGBA(pa.Colour),
})
return
}
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 8,
Position: vec64To32(pos),
})
case particle.Evaporate:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticlesEvaporateWater,
Position: vec64To32(pos),
})
case particle.SnowballPoof:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 15,
Position: vec64To32(pos),
})
case particle.EggSmash:
rid, meta, _ := world.ItemRuntimeID(item.Egg{})
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 14,
EventData: (rid << 16) | int32(meta),
Position: vec64To32(pos),
})
case particle.Splash:
if (pa.Colour == color.RGBA{}) {
pa.Colour, _ = effect.ResultingColour(nil)
}
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticlesPotionSplash,
EventData: (int32(pa.Colour.A) << 24) | (int32(pa.Colour.R) << 16) | (int32(pa.Colour.G) << 8) | int32(pa.Colour.B),
Position: vec64To32(pos),
})
case particle.Effect:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 33,
EventData: (int32(pa.Colour.A) << 24) | (int32(pa.Colour.R) << 16) | (int32(pa.Colour.G) << 8) | int32(pa.Colour.B),
Position: vec64To32(pos),
})
case particle.EntityFlame:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 18,
Position: vec64To32(pos),
})
case particle.Dust:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 32,
Position: vec64To32(pos),
EventData: nbtconv.Int32FromRGBA(pa.Colour),
})
case particle.WaterDrip:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 27,
Position: vec64To32(pos),
})
case particle.LavaDrip:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 28,
Position: vec64To32(pos),
})
case particle.Lava:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 10,
Position: vec64To32(pos),
})
}
}
// tierToSoundEvent converts an item.ArmourTier to a sound event associated with equipping it.
func tierToSoundEvent(tier item.ArmourTier) uint32 {
switch tier.(type) {
case item.ArmourTierLeather:
return packet.SoundEventEquipLeather
case item.ArmourTierGold:
return packet.SoundEventEquipGold
case item.ArmourTierChain:
return packet.SoundEventEquipChain
case item.ArmourTierIron:
return packet.SoundEventEquipIron
case item.ArmourTierDiamond:
return packet.SoundEventEquipDiamond
case item.ArmourTierNetherite:
return packet.SoundEventEquipNetherite
}
return packet.SoundEventEquipGeneric
}
// playSound plays a world.Sound at a position, disabling relative volume if set to true.
func (s *Session) playSound(pos mgl64.Vec3, t world.Sound, disableRelative bool) {
pk := &packet.LevelSoundEvent{
Position: vec64To32(pos),
EntityType: ":",
ExtraData: -1,
DisableRelativeVolume: disableRelative,
}
switch so := t.(type) {
case sound.EquipItem:
switch i := so.Item.(type) {
case item.Helmet:
pk.SoundType = tierToSoundEvent(i.Tier)
case item.Chestplate:
pk.SoundType = tierToSoundEvent(i.Tier)
case item.Leggings:
pk.SoundType = tierToSoundEvent(i.Tier)
case item.Boots:
pk.SoundType = tierToSoundEvent(i.Tier)
case item.Elytra:
pk.SoundType = packet.SoundEventEquipElytra
default:
pk.SoundType = packet.SoundEventEquipGeneric
}
case sound.Note:
pk.SoundType = packet.SoundEventNote
pk.ExtraData = (so.Instrument.Int32() << 8) | int32(so.Pitch)
case sound.DoorCrash:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundZombieDoorCrash,
Position: vec64To32(pos),
})
return
case sound.Explosion:
pk.SoundType = packet.SoundEventExplode
case sound.Thunder:
pk.SoundType, pk.EntityType = packet.SoundEventThunder, "minecraft:lightning_bolt"
case sound.Click:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundClick,
Position: vec64To32(pos),
})
return
case sound.SignWaxed:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventWaxOn,
Position: vec64To32(pos),
})
case sound.WaxedSignFailedInteraction:
pk.SoundType = packet.SoundEventWaxedSignInteractFail
case sound.WaxRemoved:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventWaxOff,
Position: vec64To32(pos),
})
case sound.CopperScraped:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventScrape,
Position: vec64To32(pos),
})
case sound.Pop:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundInfinityArrowPickup,
Position: vec64To32(pos),
})
return
case sound.Teleport:
pk.SoundType = packet.SoundEventTeleport
case sound.ItemAdd:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundAddItem,
Position: vec64To32(pos),
})
return
case sound.ItemFrameRemove:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundItemFrameRemoveItem,
Position: vec64To32(pos),
})
return
case sound.ItemFrameRotate:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundItemFrameRotateItem,
Position: vec64To32(pos),
})
return
case sound.GhastWarning:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundGhastWarning,
Position: vec64To32(pos),
})
return
case sound.GhastShoot:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundGhastFireball,
Position: vec64To32(pos),
})
return
case sound.TNT:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundFuse,
Position: vec64To32(pos),
})
return
case sound.FireworkLaunch:
pk.SoundType = packet.SoundEventLaunch
case sound.FireworkHugeBlast:
pk.SoundType = packet.SoundEventLargeBlast
case sound.FireworkBlast:
pk.SoundType = packet.SoundEventBlast
case sound.FireworkTwinkle:
pk.SoundType = packet.SoundEventTwinkle
case sound.FurnaceCrackle:
pk.SoundType = packet.SoundEventFurnaceUse
case sound.CampfireCrackle:
pk.SoundType = packet.SoundEventCampfireCrackle
case sound.BlastFurnaceCrackle:
pk.SoundType = packet.SoundEventBlastFurnaceUse
case sound.SmokerCrackle:
pk.SoundType = packet.SoundEventSmokerUse
case sound.PotionBrewed:
pk.SoundType = packet.SoundEventPotionBrewed
case sound.UseSpyglass:
pk.SoundType = packet.SoundEventUseSpyglass
case sound.StopUsingSpyglass:
pk.SoundType = packet.SoundEventStopUsingSpyglass
case sound.GoatHorn:
switch so.Horn {
case sound.Ponder():
pk.SoundType = packet.SoundEventGoatCall0
case sound.Sing():
pk.SoundType = packet.SoundEventGoatCall1
case sound.Seek():
pk.SoundType = packet.SoundEventGoatCall2
case sound.Feel():
pk.SoundType = packet.SoundEventGoatCall3
case sound.Admire():
pk.SoundType = packet.SoundEventGoatCall4
case sound.Call():
pk.SoundType = packet.SoundEventGoatCall5
case sound.Yearn():
pk.SoundType = packet.SoundEventGoatCall6
case sound.Dream():
pk.SoundType = packet.SoundEventGoatCall7
}
case sound.FireExtinguish:
pk.SoundType = packet.SoundEventExtinguishFire
case sound.Ignite:
pk.SoundType = packet.SoundEventIgnite
case sound.Burning:
pk.SoundType = packet.SoundEventPlayerHurtOnFire
case sound.Drowning:
pk.SoundType = packet.SoundEventPlayerHurtDrown
case sound.Fall:
pk.EntityType = "minecraft:player"
if so.Distance > 4 {
pk.SoundType = packet.SoundEventFallBig
break
}
pk.SoundType = packet.SoundEventFallSmall
case sound.Burp:
pk.SoundType = packet.SoundEventBurp
case sound.DoorOpen:
pk.SoundType, pk.ExtraData = packet.SoundEventDoorOpen, int32(world.BlockRuntimeID(so.Block))
case sound.DoorClose:
pk.SoundType, pk.ExtraData = packet.SoundEventDoorClose, int32(world.BlockRuntimeID(so.Block))
case sound.TrapdoorOpen:
pk.SoundType, pk.ExtraData = packet.SoundEventTrapdoorOpen, int32(world.BlockRuntimeID(so.Block))
case sound.TrapdoorClose:
pk.SoundType, pk.ExtraData = packet.SoundEventTrapdoorClose, int32(world.BlockRuntimeID(so.Block))
case sound.FenceGateOpen:
pk.SoundType, pk.ExtraData = packet.SoundEventFenceGateOpen, int32(world.BlockRuntimeID(so.Block))
case sound.FenceGateClose:
pk.SoundType, pk.ExtraData = packet.SoundEventFenceGateClose, int32(world.BlockRuntimeID(so.Block))
case sound.Deny:
pk.SoundType = packet.SoundEventDeny
case sound.BlockPlace:
pk.SoundType, pk.ExtraData = packet.SoundEventPlace, int32(world.BlockRuntimeID(so.Block))
case sound.AnvilLand:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundAnvilLand,
Position: vec64To32(pos),
})
return
case sound.AnvilUse:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundAnvilUsed,
Position: vec64To32(pos),
})
return
case sound.AnvilBreak:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundAnvilBroken,
Position: vec64To32(pos),
})
return
case sound.ChestClose:
pk.SoundType = packet.SoundEventChestClosed
case sound.ChestOpen:
pk.SoundType = packet.SoundEventChestOpen
case sound.EnderChestClose:
pk.SoundType = packet.SoundEventEnderChestClosed
case sound.EnderChestOpen:
pk.SoundType = packet.SoundEventEnderChestOpen
case sound.BarrelClose:
pk.SoundType = packet.SoundEventBarrelClose
case sound.BarrelOpen:
pk.SoundType = packet.SoundEventBarrelOpen
case sound.BlockBreaking:
pk.SoundType, pk.ExtraData = packet.SoundEventHit, int32(world.BlockRuntimeID(so.Block))
case sound.ItemBreak:
pk.SoundType = packet.SoundEventBreak
case sound.ItemUseOn:
pk.SoundType, pk.ExtraData = packet.SoundEventItemUseOn, int32(world.BlockRuntimeID(so.Block))
case sound.Fizz:
pk.SoundType = packet.SoundEventFizz
case sound.GlassBreak:
pk.SoundType = packet.SoundEventGlass
case sound.Attack:
pk.SoundType, pk.EntityType = packet.SoundEventAttackStrong, "minecraft:player"
if !so.Damage {
pk.SoundType = packet.SoundEventAttackNoDamage
}
case sound.BucketFill:
if _, water := so.Liquid.(block.Water); water {
pk.SoundType = packet.SoundEventBucketFillWater
break
}
pk.SoundType = packet.SoundEventBucketFillLava
case sound.BucketEmpty:
if _, water := so.Liquid.(block.Water); water {
pk.SoundType = packet.SoundEventBucketEmptyWater
break
}
pk.SoundType = packet.SoundEventBucketEmptyLava
case sound.BowShoot:
pk.SoundType = packet.SoundEventBow
case sound.CrossbowLoad:
switch so.Stage {
case sound.CrossbowLoadingStart:
pk.SoundType = packet.SoundEventCrossbowLoadingStart
if so.QuickCharge {
pk.SoundType = packet.SoundEventCrossbowQuickChargeStart
}
case sound.CrossbowLoadingMiddle:
pk.SoundType = packet.SoundEventCrossbowLoadingMiddle
if so.QuickCharge {
pk.SoundType = packet.SoundEventCrossbowQuickChargeMiddle
}
case sound.CrossbowLoadingEnd:
pk.SoundType = packet.SoundEventCrossbowLoadingEnd
if so.QuickCharge {
pk.SoundType = packet.SoundEventCrossbowQuickChargeEnd
}
default:
panic("invalid crossbow loading stage")
}
case sound.CrossbowShoot:
pk.SoundType = packet.SoundEventCrossbowShoot
case sound.ArrowHit:
pk.SoundType = packet.SoundEventBowHit
case sound.ItemThrow:
pk.SoundType, pk.EntityType = packet.SoundEventThrow, "minecraft:player"
case sound.LevelUp:
pk.SoundType, pk.ExtraData = packet.SoundEventLevelUp, 0x10000000
case sound.Experience:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundExperienceOrbPickup,
Position: vec64To32(pos),
})
return
case sound.MusicDiscPlay:
switch so.DiscType {
case sound.Disc13():
pk.SoundType = packet.SoundEventRecord13
case sound.DiscCat():
pk.SoundType = packet.SoundEventRecordCat
case sound.DiscBlocks():
pk.SoundType = packet.SoundEventRecordBlocks
case sound.DiscChirp():
pk.SoundType = packet.SoundEventRecordChirp
case sound.DiscFar():
pk.SoundType = packet.SoundEventRecordFar
case sound.DiscMall():
pk.SoundType = packet.SoundEventRecordMall
case sound.DiscMellohi():
pk.SoundType = packet.SoundEventRecordMellohi
case sound.DiscStal():
pk.SoundType = packet.SoundEventRecordStal
case sound.DiscStrad():
pk.SoundType = packet.SoundEventRecordStrad
case sound.DiscWard():
pk.SoundType = packet.SoundEventRecordWard
case sound.Disc11():
pk.SoundType = packet.SoundEventRecord11
case sound.DiscWait():
pk.SoundType = packet.SoundEventRecordWait
case sound.DiscOtherside():
pk.SoundType = packet.SoundEventRecordOtherside
case sound.DiscPigstep():
pk.SoundType = packet.SoundEventRecordPigstep
case sound.Disc5():
pk.SoundType = packet.SoundEventRecord5
case sound.DiscRelic():
pk.SoundType = packet.SoundEventRecordRelic
case sound.DiscCreator():
pk.SoundType = packet.SoundEventRecordCreator
case sound.DiscCreatorMusicBox():
pk.SoundType = packet.SoundEventRecordCreatorMusicBox
case sound.DiscPrecipice():
pk.SoundType = packet.SoundEventRecordPrecipice
}
case sound.MusicDiscEnd:
pk.SoundType = packet.SoundEventRecordNull
case sound.FireCharge:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundBlazeFireball,
Position: vec64To32(pos),
})
return
case sound.ComposterEmpty:
pk.SoundType = packet.SoundEventComposterEmpty
case sound.ComposterFill:
pk.SoundType = packet.SoundEventComposterFill
case sound.ComposterFillLayer:
pk.SoundType = packet.SoundEventComposterFillLayer
case sound.ComposterReady:
pk.SoundType = packet.SoundEventComposterReady
case sound.LecternBookPlace:
pk.SoundType = packet.SoundEventLecternBookPlace
case sound.Totem:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventSoundTotemUsed,
Position: vec64To32(pos),
})
}
s.writePacket(pk)
}
// PlaySound plays a world.Sound to the client. The volume is not dependent on the distance to the source if it is a
// sound of the LevelSoundEvent packet.
func (s *Session) PlaySound(t world.Sound, pos mgl64.Vec3) {
if s == Nop {
return
}
s.playSound(pos, t, true)
}
// ViewSound ...
func (s *Session) ViewSound(pos mgl64.Vec3, soundType world.Sound) {
s.playSound(pos, soundType, false)
}
// OpenSign ...
func (s *Session) OpenSign(pos cube.Pos, frontSide bool) {
blockPos := protocol.BlockPos{int32(pos[0]), int32(pos[1]), int32(pos[2])}
s.writePacket(&packet.OpenSign{
Position: blockPos,
FrontSide: frontSide,
})
}
// ViewFurnaceUpdate updates a furnace for the associated session based on previous times.
func (s *Session) ViewFurnaceUpdate(prevCookTime, cookTime, prevRemainingFuelTime, remainingFuelTime, prevMaxFuelTime, maxFuelTime time.Duration) {
if prevCookTime != cookTime {
s.writePacket(&packet.ContainerSetData{
WindowID: byte(s.openedWindowID.Load()),
Key: packet.ContainerDataFurnaceTickCount,
Value: int32(cookTime.Milliseconds() / 50),
})
}
if prevRemainingFuelTime != remainingFuelTime {
s.writePacket(&packet.ContainerSetData{
WindowID: byte(s.openedWindowID.Load()),
Key: packet.ContainerDataFurnaceLitTime,
Value: int32(remainingFuelTime.Milliseconds() / 50),
})
}
if prevMaxFuelTime != maxFuelTime {
s.writePacket(&packet.ContainerSetData{
WindowID: byte(s.openedWindowID.Load()),
Key: packet.ContainerDataFurnaceLitDuration,
Value: int32(maxFuelTime.Milliseconds() / 50),
})
}
}
// ViewBrewingUpdate updates a brewing stand for the associated session based on previous times.
func (s *Session) ViewBrewingUpdate(prevBrewTime, brewTime time.Duration, prevFuelAmount, fuelAmount, prevFuelTotal, fuelTotal int32) {
if prevBrewTime != brewTime {
s.writePacket(&packet.ContainerSetData{
WindowID: byte(s.openedWindowID.Load()),
Key: packet.ContainerDataBrewingStandBrewTime,
Value: int32(brewTime.Milliseconds() / 50),
})
}
if prevFuelAmount != fuelAmount {
s.writePacket(&packet.ContainerSetData{
WindowID: byte(s.openedWindowID.Load()),
Key: packet.ContainerDataBrewingStandFuelAmount,
Value: fuelAmount,
})
}
if prevFuelTotal != fuelTotal {
s.writePacket(&packet.ContainerSetData{
WindowID: byte(s.openedWindowID.Load()),
Key: packet.ContainerDataBrewingStandFuelTotal,
Value: fuelTotal,
})
}
}
// ViewBlockUpdate ...
func (s *Session) ViewBlockUpdate(pos cube.Pos, b world.Block, layer int) {
blockPos := protocol.BlockPos{int32(pos[0]), int32(pos[1]), int32(pos[2])}
s.writePacket(&packet.UpdateBlock{
Position: blockPos,
NewBlockRuntimeID: world.BlockRuntimeID(b),
Flags: packet.BlockUpdateNetwork,
Layer: uint32(layer),
})
if v, ok := b.(world.NBTer); ok {
if nbtData := v.EncodeNBT(); nbtData != nil {
nbtData["x"], nbtData["y"], nbtData["z"] = int32(pos.X()), int32(pos.Y()), int32(pos.Z())
s.writePacket(&packet.BlockActorData{
Position: blockPos,
NBTData: nbtData,
})
}
}
}
// ViewEntityAction ...
func (s *Session) ViewEntityAction(e world.Entity, a world.EntityAction) {
switch act := a.(type) {
case entity.SwingArmAction:
if _, ok := e.(Controllable); ok {
if s.entityRuntimeID(e) == selfEntityRuntimeID && s.swingingArm.Load() {
return
}
s.writePacket(&packet.Animate{
ActionType: packet.AnimateActionSwingArm,
EntityRuntimeID: s.entityRuntimeID(e),
})
return
}
s.writePacket(&packet.ActorEvent{
EntityRuntimeID: s.entityRuntimeID(e),
EventType: packet.ActorEventStartAttacking,
})
case entity.HurtAction:
s.writePacket(&packet.ActorEvent{
EntityRuntimeID: s.entityRuntimeID(e),
EventType: packet.ActorEventHurt,
})
case entity.CriticalHitAction:
s.writePacket(&packet.Animate{
ActionType: packet.AnimateActionCriticalHit,
EntityRuntimeID: s.entityRuntimeID(e),
})
case entity.DeathAction:
s.writePacket(&packet.ActorEvent{
EntityRuntimeID: s.entityRuntimeID(e),
EventType: packet.ActorEventDeath,
})
case entity.PickedUpAction:
s.writePacket(&packet.TakeItemActor{
ItemEntityRuntimeID: s.entityRuntimeID(e),
TakerEntityRuntimeID: s.entityRuntimeID(act.Collector),
})
case entity.ArrowShakeAction:
s.writePacket(&packet.ActorEvent{
EntityRuntimeID: s.entityRuntimeID(e),
EventType: packet.ActorEventShake,
EventData: int32(act.Duration.Milliseconds() / 50),
})
case entity.FireworkExplosionAction:
s.writePacket(&packet.ActorEvent{
EntityRuntimeID: s.entityRuntimeID(e),
EventType: packet.ActorEventFireworksExplode,
})
case entity.EatAction:
if user, ok := e.(item.User); ok {
held, _ := user.HeldItems()
it := held.Item()
if held.Empty() {
// This can happen sometimes if the user switches between items very quickly, so just ignore the action.
return
}
if _, ok := it.(item.Consumable); !ok {
// Not consumable, refer to the comment above.
return
}
rid, meta, _ := world.ItemRuntimeID(it)
s.writePacket(&packet.ActorEvent{
EntityRuntimeID: s.entityRuntimeID(e),
EventType: packet.ActorEventFeed,
// It's a little weird how the runtime ID is still shifted 16 bits to the left here, given the
// runtime ID already includes the meta, but it seems to work.
EventData: (rid << 16) | int32(meta),
})
}
case entity.TotemUseAction:
s.writePacket(&packet.ActorEvent{
EntityRuntimeID: s.entityRuntimeID(e),
EventType: packet.ActorEventTalismanActivate,
})
}
}
// ViewEntityState ...
func (s *Session) ViewEntityState(e world.Entity) {
s.writePacket(&packet.SetActorData{
EntityRuntimeID: s.entityRuntimeID(e),
EntityMetadata: s.parseEntityMetadata(e),
})
}
// ViewEntityAnimation ...