-
Notifications
You must be signed in to change notification settings - Fork 1
/
Protocol.hs
1294 lines (1136 loc) · 32.4 KB
/
Protocol.hs
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
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Protocol where
import Codec.Compression.Zlib
import Control.Applicative
import Control.Monad
import Data.Binary.Get
import Data.Binary.Put
import Data.Bits
import Data.ByteString.Lazy (ByteString)
import Data.Foldable
import Data.Int
import Data.Word
import Debug.Trace
import Network.Socket (ServiceName)
import qualified Codec.Compression.Zlib.Internal as ZI
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Internal as LI
import JavaBinary
defaultMinecraftPort :: ServiceName
defaultMinecraftPort = "25565"
type MessageTag = Int8
type ChunkLoc = (Int32, Int32)
type BlockLoc = (Int8, Int8, Int8)
newtype EntityId = EID Int32
deriving (Eq, Ord, Show,Read,JavaBinary)
newtype SlotId = SID Int16
deriving (Eq, Ord, Show,Read,JavaBinary)
newtype WindowId = WID Int8
deriving (Eq, Ord, Show,Read,JavaBinary)
newtype TransactionId = TID Int16
deriving (Eq, Ord, Show,Read,JavaBinary)
newtype ItemId = IID Int16
deriving (Eq, Ord, Show,Read,JavaBinary)
newtype ProgressBarId = PID Int16
deriving (Eq, Ord, Show,Read,JavaBinary)
newtype GraphicId = GID Int32
deriving (Eq, Ord, Show,Read,JavaBinary)
data Message
= KeepAliv -- Keep alives are sent every 1200 ticks
| LoginRequest Int32 -- Protocol Version
String -- Username
String -- Password
Int64 -- Map Seed
Int8 -- Dimension
| Handshake String -- Session ID
| Chat String -- Message
| TimeUpdate Int64 -- The world time in minutes
| EntityEquipment EntityId
SlotId
ItemId -- Item ID
Int16 -- Damage?
| SpawnPosition Int32 -- X
Int32 -- Y
Int32 -- Z
| UseEntity EntityId -- User
EntityId -- Target
Bool -- Left-click?
| UpdateHealth Int16 -- Health: 0--20
| Respawn
| Player Bool -- On Ground
| PlayerPosition Double -- X
Double -- Y
Double -- Stance
Double -- Z
Bool -- On ground
| PlayerLook Float -- Yaw
Float -- Pitch
Bool -- On Ground
| PlayerPositionLook
Double -- X
Double -- Y
Double -- Stance
Double -- Z
Float -- Yaw
Float -- Pitch
Bool -- On Ground
| PlayerDigging DiggingStatus
Int32 -- X
Int8 -- Y
Int32 -- Z
Face
| PlayerBlockPlacement
Int32 -- X
Int8 -- Y
Int32 -- Z
Face
(Maybe (ItemId, Int8, Int16)) -- Optional block, count, and use
| HoldingChange SlotId
| Animation EntityId -- Player ID
Animate
| EntityAction EntityId -- Player ID
Action
| NamedEntitySpawn EntityId -- Player ID
String -- Player Name
Int32 -- X
Int32 -- Y
Int32 -- Z
Int8 -- Rotation
Int8 -- Pitch
ItemId -- Current Item
| PickupSpawn EntityId -- Player ID
ItemId
Int8 -- Count
Int16 -- Damage?
Int32 -- X
Int32 -- Y
Int32 -- Z
Int8 -- Rotation
Int8 -- Pitch
Int8 -- Roll
| CollectItem EntityId -- Collected
EntityId -- Collector
| AddObject EntityId
Int8 -- Type ID
Int32 -- X
Int32 -- Y
Int32 -- Z
| MobSpawn EntityId
MobId -- Mob ID
Int32 -- X
Int32 -- Y
Int32 -- Z
Int8 -- Yaw
Int8 -- Pitch
Metadata
| Painting EntityId
String -- Name of painting
Int32 -- X
Int32 -- Y
Int32 -- Z
GraphicId
| EntityVelocity EntityId
Int16 -- X Velocity
Int16 -- Y Velocity
Int16 -- Z Velocity
| DestroyEntity EntityId
| Entity EntityId
| EntityRelativeMove EntityId
Int8 -- Change in X
Int8 -- Change in Y
Int8 -- Change in Z
| EntityLook EntityId
Int8 -- Yaw
Int8 -- Pitch
| EntityLookMove EntityId
Int8 -- dX
Int8 -- dY
Int8 -- dZ
Int8 -- Yaw
Int8 -- Pitch
| EntityTeleport EntityId
Int32 -- X
Int32 -- Y
Int32 -- Z
Int8 -- Yaw
Int8 -- Pitch
| EntityStatus EntityId
EntityStatus
| AttachEntity EntityId -- Entity ID
EntityId -- Vehicle ID
| EntityMetadata EntityId Metadata
| Prechunk ChunkLoc PrechunkStatus
| Mapchunk Int32 -- X
Int16 -- Y
Int32 -- Z
Int8 -- X length
Int8 -- Y length
Int8 -- Z length
[BlockId]
ByteString
ByteString
ByteString
| MultiblockChange ChunkLoc
[(BlockLoc, BlockId, Int8)] -- Coordinate, Block type, Meta
| BlockChange Int32 -- Block X
Int8 -- Block Y
Int32 -- Block Z
BlockId
Int8 -- Block metadata
| PlayNote Int32 -- Block X
Int16 -- Block Y
Int32 -- Block Z
InstrumentType
Int8 -- Pitch
| Explosion Double -- X
Double -- Y
Double -- Z
Float -- Radius?
[(Int8,Int8,Int8)] -- Relative X,Y,Z of affected blocks
| OpenWindow WindowId
InventoryType
String -- Title
Int8 -- Number of slots
| CloseWindow WindowId
| WindowClick WindowId
SlotId
Bool -- Right-click
TransactionId
(Maybe (ItemId, Int8, Int16)) -- Optional Item, Count, Uses
| SetSlot WindowId
SlotId
(Maybe (ItemId, Int8, Int16)) -- Item, Count and Use
| WindowItems WindowId
[Maybe (ItemId, Int8, Int16)] -- List of slots (Item ID, Count, Uses)
| UpdateProgressBar WindowId
ProgressBarId
Int16 -- Value
| Transaction WindowId
TransactionId
Bool -- Success
| UpdateSign Int32 -- X
Int16 -- Y
Int32 -- Z
String -- Text on line 1
String -- Text on line 2
String -- Text on line 3
String -- Text on line 4
| Disconnect String -- Reason
deriving (Show, Read)
-- | 'AutoGet' provides a short-cut to writing 'getJ' implementations
-- for simple message constructors.
class AutoGet a where
autoGet :: a -> Get Message
instance AutoGet Message where
autoGet m = return m
instance (JavaBinary x, AutoGet y) => AutoGet (x -> y) where
autoGet f = do x <- getJ
autoGet (f x)
instance JavaBinary Message where
getJ = getMessage
putJ = putMessage
data PrechunkStatus
= LoadChunk
| UnloadChunk
deriving (Read, Show, Eq)
instance JavaBinary PrechunkStatus where
getJ = do
tag <- getJ :: Get Int8
return $! case tag of
0 -> UnloadChunk
_ -> LoadChunk
putJ UnloadChunk = putJ (0 :: Int8)
putJ LoadChunk = putJ (1 :: Int8)
data EntityStatus
= Damaged
| Died
| OtherStatus Int8
deriving (Read, Show, Eq)
instance JavaBinary EntityStatus where
getJ = do
tag <- getJ
return $! case tag of
2 -> Damaged
3 -> Died
_ -> trace ("Unknown status " ++ show tag) (OtherStatus tag)
putJ Damaged = putJ (2 :: Int8)
putJ Died = putJ (3 :: Int8)
putJ (OtherStatus tag) = putJ tag
data InstrumentType
= Harp
| DoubleBass
| SnareDrum
| Sticks
| BassDrum
| OtherInstrument Int8
deriving (Show, Read)
instance JavaBinary InstrumentType where
getJ = do
tag <- getJ
return $! case tag of
0 -> Harp
1 -> DoubleBass
2 -> SnareDrum
3 -> Sticks
4 -> BassDrum
_ -> trace ("Unknown instrument " ++ show tag) (OtherInstrument tag)
putJ Harp = putJ (0 :: Int8)
putJ DoubleBass = putJ (1 :: Int8)
putJ SnareDrum = putJ (2 :: Int8)
putJ Sticks = putJ (3 :: Int8)
putJ BassDrum = putJ (4 :: Int8)
putJ (OtherInstrument tag) = putJ tag
data InventoryType
= BasicInventory
| WorkbenchInventory
| FurnaceInventory
| DispenserInventory
| UnknownInventory Int8
deriving (Show, Read)
instance JavaBinary InventoryType where
getJ = do
tag <- getJ
return $! case tag :: Int8 of
0 -> BasicInventory
1 -> WorkbenchInventory
2 -> FurnaceInventory
3 -> DispenserInventory
_ -> trace ("Unknown inventory " ++ show tag) (UnknownInventory tag)
putJ BasicInventory = putJ (0 :: Int8)
putJ WorkbenchInventory = putJ (1 :: Int8)
putJ FurnaceInventory = putJ (2 :: Int8)
putJ DispenserInventory = putJ (3 :: Int8)
putJ (UnknownInventory tag) = putJ tag
data MobId
= Creeper
| Skeleton
| Spider
| GiantSpider
| Zombie
| Slime
| Ghast
| ZombiePigman
| Pig
| Sheep
| Cow
| Hen
| Squid
| OtherMob Int8
deriving (Show, Read, Eq)
instance JavaBinary MobId where
getJ = do
tag <- getJ
return $! case tag of
50 -> Creeper
51 -> Skeleton
52 -> Spider
53 -> GiantSpider
54 -> Zombie
55 -> Slime
56 -> Ghast
57 -> ZombiePigman
90 -> Pig
91 -> Sheep
92 -> Cow
93 -> Hen
94 -> Squid
_ -> trace ("Unknown mob " ++ show tag) (OtherMob tag)
putJ Creeper = putJ (50 :: Int8)
putJ Skeleton = putJ (51 :: Int8)
putJ Spider = putJ (52 :: Int8)
putJ GiantSpider = putJ (53 :: Int8)
putJ Zombie = putJ (54 :: Int8)
putJ Slime = putJ (55 :: Int8)
putJ Ghast = putJ (56 :: Int8)
putJ ZombiePigman = putJ (57 :: Int8)
putJ Pig = putJ (90 :: Int8)
putJ Sheep = putJ (91 :: Int8)
putJ Cow = putJ (92 :: Int8)
putJ Hen = putJ (93 :: Int8)
putJ Squid = putJ (94 :: Int8)
putJ (OtherMob tag) = putJ (tag :: Int8)
data BlockId
= Air
| Stone
| Grass
| Dirt
| Cobblestone
| WoodenPlank
| Sapling
| Bedrock
| Water
| StationaryWater
| Lava
| StationaryLava
| Sand
| Gravel
| Goldore
| Ironore
| Coalore
| Wood
| Leaves
| Sponge
| Glass
| LapisLazuliOre
| LapisLazuliBlock
| Dispenser
| Sandstone
| NoteBlock
| Wool
| YellowFlower
| RedRose
| BrownMushroom
| RedMushroom
| GoldBlock
| IronBlock
| DoubleStoneSlab
| StoneSlab
| Brick
| TNT
| Bookshelf
| MossStone
| Obsidian
| Torch
| Fire
| MonsterSpawner
| WoodenStairs
| Chest
| RedstoneWire
| DiamondOre
| DiamondBlock
| Workbench
| Crops
| Soil
| Furnace
| BurningFurnace
| SignPost
| WoodenDoor
| Ladder
| MinecartTracks
| CobblestoneStairs
| WallSign
| Lever
| StonePressurePlate
| IronDoor
| WoodenPressurePlate
| RedstoneOre
| GlowingRedstoneOre
| RedstoneTorchOff
| RedstoneTorchOn
| StoneButton
| Snow
| Ice
| SnowBlock
| Cactus
| Clay
| SugarCane
| Jukebox
| Fence
| Pumpkin
| Netherrack
| SoulSand
| Glowstone
| Portal
| JackOLantern
| Cake
| UnknownBlock Int8
deriving (Show, Read, Eq)
instance JavaBinary BlockId where
getJ = do
tag <- getJ
return $!
case tag :: Int8 of
0x00 -> Air
0x01 -> Stone
0x02 -> Grass
0x03 -> Dirt
0x04 -> Cobblestone
0x05 -> WoodenPlank
0x06 -> Sapling
0x07 -> Bedrock
0x08 -> Water
0x09 -> StationaryWater
0x0A -> Lava
0x0B -> StationaryLava
0x0C -> Sand
0x0D -> Gravel
0x0E -> Goldore
0x0F -> Ironore
0x10 -> Coalore
0x11 -> Wood
0x12 -> Leaves
0x13 -> Sponge
0x14 -> Glass
0x15 -> LapisLazuliOre
0x16 -> LapisLazuliBlock
0x17 -> Dispenser
0x18 -> Sandstone
0x19 -> NoteBlock
0x23 -> Wool
0x25 -> YellowFlower
0x26 -> RedRose
0x27 -> BrownMushroom
0x28 -> RedMushroom
0x29 -> GoldBlock
0x2A -> IronBlock
0x2B -> DoubleStoneSlab
0x2C -> StoneSlab
0x2D -> Brick
0x2E -> TNT
0x2F -> Bookshelf
0x30 -> MossStone
0x31 -> Obsidian
0x32 -> Torch
0x33 -> Fire
0x34 -> MonsterSpawner
0x35 -> WoodenStairs
0x36 -> Chest
0x37 -> RedstoneWire
0x38 -> DiamondOre
0x39 -> DiamondBlock
0x3A -> Workbench
0x3B -> Crops
0x3C -> Soil
0x3D -> Furnace
0x3E -> BurningFurnace
0x3F -> SignPost
0x40 -> WoodenDoor
0x41 -> Ladder
0x42 -> MinecartTracks
0x43 -> CobblestoneStairs
0x44 -> WallSign
0x45 -> Lever
0x46 -> StonePressurePlate
0x47 -> IronDoor
0x48 -> WoodenPressurePlate
0x49 -> RedstoneOre
0x4A -> GlowingRedstoneOre
0x4B -> RedstoneTorchOff
0x4C -> RedstoneTorchOn
0x4D -> StoneButton
0x4E -> Snow
0x4F -> Ice
0x50 -> SnowBlock
0x51 -> Cactus
0x52 -> Clay
0x53 -> SugarCane
0x54 -> Jukebox
0x55 -> Fence
0x56 -> Pumpkin
0x57 -> Netherrack
0x58 -> SoulSand
0x59 -> Glowstone
0x5A -> Portal
0x5B -> JackOLantern
0x5C -> Cake
_ -> trace ("Unknown block " ++ show tag) (UnknownBlock tag)
putJ Air = putJ (0x00 :: Int8)
putJ Stone = putJ (0x01 :: Int8)
putJ Grass = putJ (0x02 :: Int8)
putJ Dirt = putJ (0x03 :: Int8)
putJ Cobblestone = putJ (0x04 :: Int8)
putJ WoodenPlank = putJ (0x05 :: Int8)
putJ Sapling = putJ (0x06 :: Int8)
putJ Bedrock = putJ (0x07 :: Int8)
putJ Water = putJ (0x08 :: Int8)
putJ StationaryWater = putJ (0x09 :: Int8)
putJ Lava = putJ (0x0A :: Int8)
putJ StationaryLava = putJ (0x0B :: Int8)
putJ Sand = putJ (0x0C :: Int8)
putJ Gravel = putJ (0x0D :: Int8)
putJ Goldore = putJ (0x0E :: Int8)
putJ Ironore = putJ (0x0F :: Int8)
putJ Coalore = putJ (0x10 :: Int8)
putJ Wood = putJ (0x11 :: Int8)
putJ Leaves = putJ (0x12 :: Int8)
putJ Sponge = putJ (0x13 :: Int8)
putJ Glass = putJ (0x14 :: Int8)
putJ LapisLazuliOre = putJ (0x15 :: Int8)
putJ LapisLazuliBlock = putJ (0x16 :: Int8)
putJ Dispenser = putJ (0x17 :: Int8)
putJ Sandstone = putJ (0x18 :: Int8)
putJ NoteBlock = putJ (0x19 :: Int8)
putJ Wool = putJ (0x23 :: Int8)
putJ YellowFlower = putJ (0x25 :: Int8)
putJ RedRose = putJ (0x26 :: Int8)
putJ BrownMushroom = putJ (0x27 :: Int8)
putJ RedMushroom = putJ (0x28 :: Int8)
putJ GoldBlock = putJ (0x29 :: Int8)
putJ IronBlock = putJ (0x2A :: Int8)
putJ DoubleStoneSlab = putJ (0x2B :: Int8)
putJ StoneSlab = putJ (0x2C :: Int8)
putJ Brick = putJ (0x2D :: Int8)
putJ TNT = putJ (0x2E :: Int8)
putJ Bookshelf = putJ (0x2F :: Int8)
putJ MossStone = putJ (0x30 :: Int8)
putJ Obsidian = putJ (0x31 :: Int8)
putJ Torch = putJ (0x32 :: Int8)
putJ Fire = putJ (0x33 :: Int8)
putJ MonsterSpawner = putJ (0x34 :: Int8)
putJ WoodenStairs = putJ (0x35 :: Int8)
putJ Chest = putJ (0x36 :: Int8)
putJ RedstoneWire = putJ (0x37 :: Int8)
putJ DiamondOre = putJ (0x38 :: Int8)
putJ DiamondBlock = putJ (0x39 :: Int8)
putJ Workbench = putJ (0x3A :: Int8)
putJ Crops = putJ (0x3B :: Int8)
putJ Soil = putJ (0x3C :: Int8)
putJ Furnace = putJ (0x3D :: Int8)
putJ BurningFurnace = putJ (0x3E :: Int8)
putJ SignPost = putJ (0x3F :: Int8)
putJ WoodenDoor = putJ (0x40 :: Int8)
putJ Ladder = putJ (0x41 :: Int8)
putJ MinecartTracks = putJ (0x42 :: Int8)
putJ CobblestoneStairs = putJ (0x43 :: Int8)
putJ WallSign = putJ (0x44 :: Int8)
putJ Lever = putJ (0x45 :: Int8)
putJ StonePressurePlate = putJ (0x46 :: Int8)
putJ IronDoor = putJ (0x47 :: Int8)
putJ WoodenPressurePlate= putJ (0x48 :: Int8)
putJ RedstoneOre = putJ (0x49 :: Int8)
putJ GlowingRedstoneOre = putJ (0x4A :: Int8)
putJ RedstoneTorchOff = putJ (0x4B :: Int8)
putJ RedstoneTorchOn = putJ (0x4C :: Int8)
putJ StoneButton = putJ (0x4D :: Int8)
putJ Snow = putJ (0x4E :: Int8)
putJ Ice = putJ (0x4F :: Int8)
putJ SnowBlock = putJ (0x50 :: Int8)
putJ Cactus = putJ (0x51 :: Int8)
putJ Clay = putJ (0x52 :: Int8)
putJ SugarCane = putJ (0x53 :: Int8)
putJ Jukebox = putJ (0x54 :: Int8)
putJ Fence = putJ (0x55 :: Int8)
putJ Pumpkin = putJ (0x56 :: Int8)
putJ Netherrack = putJ (0x57 :: Int8)
putJ SoulSand = putJ (0x58 :: Int8)
putJ Glowstone = putJ (0x59 :: Int8)
putJ Portal = putJ (0x5A :: Int8)
putJ JackOLantern = putJ (0x5B :: Int8)
putJ Cake = putJ (0x5C :: Int8)
putJ (UnknownBlock tag) = putJ (tag :: Int8)
data Metadata = Metadata [(Int8, MetadataEntry)]
deriving (Show, Read)
data MetadataEntry
= MetadataByte Int8
| MetadataShort Int16
| MetadataInt Int32
| MetadataFloat Float
| MetadataString String
| MetadataTriple (Int16, Int8, Int16)
deriving (Show, Read)
instance JavaBinary Metadata where
getJ = Metadata <$> aux
where
aux =
do tag <- getJ :: Get Int8
if tag == 127 then return []
else do
let ix = tag .&. 0x1f
x <- case tag `shiftR` 5 of
0 -> MetadataByte <$> getJ
1 -> MetadataShort <$> getJ
2 -> MetadataInt <$> getJ
3 -> MetadataFloat <$> getJ
4 -> MetadataString <$> getJ
5 -> MetadataTriple <$> getJ
_ -> error $ "Unknown metadata tag " ++ show tag
xs <- aux
return ((ix,x) : xs)
putJ (Metadata xs) = traverse_ aux xs *> putJ (127 :: Int8)
where putTag fieldType ix = putJ (fieldType `shiftL` 5 .|. ix .&. 0x1f)
aux (ix, MetadataByte x) = putTag 0 ix *> putJ x
aux (ix, MetadataShort x) = putTag 1 ix *> putJ x
aux (ix, MetadataInt x) = putTag 2 ix *> putJ x
aux (ix, MetadataFloat x) = putTag 3 ix *> putJ x
aux (ix, MetadataString x) = putTag 4 ix *> putJ x
aux (ix, MetadataTriple x) = putTag 5 ix *> putJ x
data Action
= ActionCrouch
| ActionUncrouch
| ActionOther Int8
deriving (Show, Read)
instance JavaBinary Action where
getJ = do
tag <- getJ
return $! case tag of
1 -> ActionCrouch
2 -> ActionUncrouch
_ -> trace ("Unknown action " ++ show tag) (ActionOther tag)
putJ ActionCrouch = putJ (1 :: Int8)
putJ ActionUncrouch = putJ (2 :: Int8)
putJ (ActionOther tag) = putJ tag
data Animate
= NoAnimate
| SwingArm
| DamageAnimation
| Crouch
| Uncrouch
| OtherAnimate Int8
deriving (Show, Read)
instance JavaBinary Animate where
getJ = do
tag <- getJ
case tag of
0 -> return NoAnimate
1 -> return SwingArm
2 -> return DamageAnimation
104 -> return Crouch
105 -> return Uncrouch
_ -> return $ OtherAnimate tag
putJ NoAnimate = putJ (0 :: Int8)
putJ SwingArm = putJ (1 :: Int8)
putJ DamageAnimation = putJ (2 :: Int8)
putJ Crouch = putJ (104 :: Int8)
putJ Uncrouch = putJ (105 :: Int8)
putJ (OtherAnimate tag) = putJ (tag :: Int8)
data DiggingStatus
= StartedDigging
| Digging
| StoppedDigging
| BlockBroken
| DropItem
| OtherDigging Int8
deriving (Show, Read)
instance JavaBinary DiggingStatus where
getJ = do
tag <- getJ
return $! case tag :: Int8 of
0 -> StartedDigging
1 -> Digging
2 -> StoppedDigging
3 -> BlockBroken
4 -> DropItem
_ -> trace ("Unknown digging status " ++ show tag) (OtherDigging tag)
putJ StartedDigging = putJ (0 :: Int8)
putJ Digging = putJ (1 :: Int8)
putJ StoppedDigging = putJ (2 :: Int8)
putJ BlockBroken = putJ (3 :: Int8)
putJ DropItem = putJ (4 :: Int8)
putJ (OtherDigging tag) = putJ tag
data Face
= Y1 | Y2 | Z1 | Z2 | X1 | X2 | None
deriving (Show, Read)
instance JavaBinary Face where
getJ = do
tag <- getJ
return $! case tag :: Int8 of
-1 -> None
0 -> Y1
1 -> Y2
2 -> Z1
3 -> Z2
4 -> X1
5 -> X2
_ -> error ("Bad face " ++ show tag)
putJ None = putJ (-1 :: Int8)
putJ Y1 = putJ (0 :: Int8)
putJ Y2 = putJ (1 :: Int8)
putJ Z1 = putJ (2 :: Int8)
putJ Z2 = putJ (3 :: Int8)
putJ X1 = putJ (4 :: Int8)
putJ X2 = putJ (5 :: Int8)
-- | Get a lazy ByteString prefixed with a 32-bit length.
getLazyByteString32 :: Get ByteString
getLazyByteString32 = getLazyByteString . fromIntegral =<< getWord32be
getMaybe16 :: Get a -> Get (Maybe a)
getMaybe16 p = do
mb <- lookAheadM $ isNil <$> getJ
case mb of
Nothing -> Just <$> p
Just () -> return Nothing
where
isNil :: Int16 -> Maybe ()
isNil x = guard (x == (-1))
putMaybe16 :: (a -> Put) -> Maybe a -> Put
putMaybe16 = maybe (putJ (-1 :: Int16))
getMessage :: Get Message
getMessage = do
tag <- getJ
case tag :: Int8 of
0x00 -> autoGet KeepAliv
0x01 -> autoGet LoginRequest
0x02 -> autoGet Handshake
0x03 -> autoGet Chat
0x04 -> autoGet TimeUpdate
0x05 -> autoGet EntityEquipment
0x06 -> autoGet SpawnPosition
0x07 -> autoGet UseEntity
0x08 -> autoGet UpdateHealth
0x09 -> autoGet Respawn
0x0a -> autoGet Player
0x0b -> autoGet PlayerPosition
0x0c -> autoGet PlayerLook
0x0d -> autoGet PlayerPositionLook
0x0e -> autoGet PlayerDigging
0x0f -> PlayerBlockPlacement <$> getJ <*> getJ <*> getJ <*> getJ
<*> getMaybe16 getJ
0x10 -> autoGet HoldingChange
0x12 -> autoGet Animation
0x13 -> autoGet EntityAction
0x14 -> autoGet NamedEntitySpawn
0x15 -> autoGet PickupSpawn
0x16 -> autoGet CollectItem
0x17 -> autoGet AddObject
0x18 -> autoGet MobSpawn
0x19 -> autoGet Painting
0x1c -> autoGet EntityVelocity
0x1d -> autoGet DestroyEntity
0x1e -> autoGet Entity
0x1f -> autoGet EntityRelativeMove
0x20 -> autoGet EntityLook
0x21 -> autoGet EntityLookMove
0x22 -> autoGet EntityTeleport
0x26 -> autoGet EntityStatus
0x27 -> autoGet AttachEntity
0x28 -> autoGet EntityMetadata
0x32 -> autoGet Prechunk
0x33 -> do (x,y,z,sx,sy,sz) <- getJ
let block_count = (fromIntegral sx + 1)
* (fromIntegral sy + 1)
* (fromIntegral sz + 1)
len <- getJ :: Get Int32
compressed <- getLazyByteString (fromIntegral len)
return $ case safeDecompress compressed of
Left _ -> proxyChat "Bad chunk, attempting to continue"
Right uncompressed ->
let parser = Mapchunk x y z sx sy sz
<$> replicateM (fromIntegral block_count) getJ
<*> getLazyByteString (block_count `div` 2)
<*> getLazyByteString (block_count `div` 2)
<*> getLazyByteString (block_count `div` 2)
in runGet parser uncompressed
0x34 -> MultiblockChange <$> getJ <*> changes
where changes = do
sz <- getJ :: Get Int16
zip3 <$> (replicateM (fromIntegral sz) (splitCoord <$> getJ))
<*> replicateM (fromIntegral sz) getJ
<*> replicateM (fromIntegral sz) getJ
splitCoord :: Int16 -> (Int8, Int8, Int8)
splitCoord c = (fromIntegral (c' `shiftR` 12),
fromIntegral (c' .&. 0x7f),
fromIntegral (c' `shiftR` 8 .&. 0xf))
where c' :: Word16
c' = fromIntegral c
0x35 -> autoGet BlockChange
0x36 -> autoGet PlayNote
0x3c -> Explosion <$> getJ <*> getJ <*> getJ <*> getJ <*> coords
where coords = do
len <- getJ :: Get Int32
replicateM (fromIntegral len) getJ
0x64 -> autoGet OpenWindow
0x65 -> autoGet CloseWindow
0x66 -> WindowClick <$> getJ <*> getJ <*> getJ <*> getJ
<*> getMaybe16 getJ
0x67 -> SetSlot <$> getJ <*> getJ <*> getMaybe16 getJ
0x68 -> WindowItems <$> getJ <*> items
where items = do
count <- getJ :: Get Int16
replicateM (fromIntegral count) (getMaybe16 getJ)
0x69 -> autoGet UpdateProgressBar
0x6a -> autoGet Transaction
0x82 -> autoGet UpdateSign
0xff -> autoGet Disconnect
_ -> error $ "Unknown packet " ++ show tag
putMessage :: Message -> Put
putMessage KeepAliv = putJ (0x00 :: MessageTag)
putMessage (LoginRequest ver usr pw x y) = do
putJ (0x01 :: MessageTag)
putJ ver
putJ usr
putJ pw
putJ x
putJ y
putMessage (Handshake usr) = do
putJ (0x02 :: MessageTag)
putJ usr
putMessage (Chat msg) = do
putJ (0x03 :: MessageTag)
putJ msg
putMessage (TimeUpdate time) = do
putJ (0x04 :: MessageTag)
putJ time
putMessage (EntityEquipment eid slot iid damage) = do
putJ (0x05 :: MessageTag)
putJ eid
putJ slot
putJ iid
putJ damage
putMessage (SpawnPosition x y z) = do
putJ (0x06 :: MessageTag)
putJ x
putJ y
putJ z
putMessage (UseEntity user target left) = do
putJ (0x07 :: MessageTag)
putJ user
putJ target
putJ left
putMessage (UpdateHealth health) = do
putJ (0x08 :: MessageTag)
putJ health
putMessage Respawn = putJ (0x09 :: MessageTag)
putMessage (Player ground) = do
putJ (0x0a :: MessageTag)
putJ ground
putMessage (PlayerPosition x y stance z ground) = do
putJ (0x0b :: MessageTag)
putJ x
putJ y
putJ stance
putJ z
putJ ground
putMessage (PlayerLook yaw pitch ground) = do
putJ (0x0c :: MessageTag)
putJ yaw
putJ pitch