-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathDataTypes.cs
1581 lines (1441 loc) · 62.7 KB
/
DataTypes.cs
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
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using MinecraftClient.Inventory;
using MinecraftClient.Inventory.ItemPalettes;
using MinecraftClient.Mapping;
using MinecraftClient.Mapping.EntityPalettes;
using MinecraftClient.Protocol.Message;
namespace MinecraftClient.Protocol.Handlers
{
/// <summary>
/// Handle data types encoding / decoding
/// </summary>
class DataTypes
{
/// <summary>
/// Protocol version for adjusting data types
/// </summary>
private readonly int protocolversion;
/// <summary>
/// Initialize a new DataTypes instance
/// </summary>
/// <param name="protocol">Protocol version</param>
public DataTypes(int protocol)
{
protocolversion = protocol;
}
/// <summary>
/// Read some data from a cache of bytes and remove it from the cache
/// </summary>
/// <param name="offset">Amount of bytes to read</param>
/// <param name="cache">Cache of bytes to read from</param>
/// <returns>The data read from the cache as an array</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public byte[] ReadData(int offset, Queue<byte> cache)
{
byte[] result = new byte[offset];
for (int i = 0; i < offset; i++)
result[i] = cache.Dequeue();
return result;
}
/// <summary>
/// Read some data from a cache of bytes and remove it from the cache
/// </summary>
/// <param name="cache">Cache of bytes to read from</param>
/// <param name="dest">Storage results</param>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public void ReadDataReverse(Queue<byte> cache, Span<byte> dest)
{
for (int i = (dest.Length - 1); i >= 0; --i)
dest[i] = cache.Dequeue();
}
/// <summary>
/// Remove some data from the cache
/// </summary>
/// <param name="offset">Amount of bytes to drop</param>
/// <param name="cache">Cache of bytes to drop</param>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public void DropData(int offset, Queue<byte> cache)
{
while (offset-- > 0)
cache.Dequeue();
}
/// <summary>
/// Read a string from a cache of bytes and remove it from the cache
/// </summary>
/// <param name="cache">Cache of bytes to read from</param>
/// <returns>The string</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public string ReadNextString(Queue<byte> cache)
{
int length = ReadNextVarInt(cache);
if (length > 0)
{
return Encoding.UTF8.GetString(ReadData(length, cache));
}
else return "";
}
/// <summary>
/// Skip a string from a cache of bytes and remove it from the cache
/// </summary>
/// <param name="cache">Cache of bytes to read from</param>
public void SkipNextString(Queue<byte> cache)
{
int length = ReadNextVarInt(cache);
DropData(length, cache);
}
/// <summary>
/// Read a boolean from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The boolean value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public bool ReadNextBool(Queue<byte> cache)
{
return ReadNextByte(cache) != 0x00;
}
/// <summary>
/// Read a short integer from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The short integer value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public short ReadNextShort(Queue<byte> cache)
{
Span<byte> rawValue = stackalloc byte[2];
for (int i = (2 - 1); i >= 0; --i) //Endianness
rawValue[i] = cache.Dequeue();
return BitConverter.ToInt16(rawValue);
}
/// <summary>
/// Read an integer from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The integer value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public int ReadNextInt(Queue<byte> cache)
{
Span<byte> rawValue = stackalloc byte[4];
for (int i = (4 - 1); i >= 0; --i) //Endianness
rawValue[i] = cache.Dequeue();
return BitConverter.ToInt32(rawValue);
}
/// <summary>
/// Read a long integer from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The unsigned long integer value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public long ReadNextLong(Queue<byte> cache)
{
Span<byte> rawValue = stackalloc byte[8];
for (int i = (8 - 1); i >= 0; --i) //Endianness
rawValue[i] = cache.Dequeue();
return BitConverter.ToInt64(rawValue);
}
/// <summary>
/// Read an unsigned short integer from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The unsigned short integer value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public ushort ReadNextUShort(Queue<byte> cache)
{
Span<byte> rawValue = stackalloc byte[2];
for (int i = (2 - 1); i >= 0; --i) //Endianness
rawValue[i] = cache.Dequeue();
return BitConverter.ToUInt16(rawValue);
}
/// <summary>
/// Read an unsigned long integer from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The unsigned long integer value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public ulong ReadNextULong(Queue<byte> cache)
{
Span<byte> rawValue = stackalloc byte[8];
for (int i = (8 - 1); i >= 0; --i) //Endianness
rawValue[i] = cache.Dequeue();
return BitConverter.ToUInt64(rawValue);
}
/// <summary>
/// Read a Location encoded as an ulong field and remove it from the cache
/// </summary>
/// <returns>The Location value</returns>
public Location ReadNextLocation(Queue<byte> cache)
{
ulong locEncoded = ReadNextULong(cache);
int x, y, z;
if (protocolversion >= Protocol18Handler.MC_1_14_Version)
{
x = (int)(locEncoded >> 38);
y = (int)(locEncoded & 0xFFF);
z = (int)(locEncoded << 26 >> 38);
}
else
{
x = (int)(locEncoded >> 38);
y = (int)((locEncoded >> 26) & 0xFFF);
z = (int)(locEncoded << 38 >> 38);
}
if (x >= 0x02000000) // 33,554,432
x -= 0x04000000; // 67,108,864
if (y >= 0x00000800) // 2,048
y -= 0x00001000; // 4,096
if (z >= 0x02000000) // 33,554,432
z -= 0x04000000; // 67,108,864
return new Location(x, y, z);
}
/// <summary>
/// Read several little endian unsigned short integers at once from a cache of bytes and remove them from the cache
/// </summary>
/// <returns>The unsigned short integer value</returns>
public ushort[] ReadNextUShortsLittleEndian(int amount, Queue<byte> cache)
{
byte[] rawValues = ReadData(2 * amount, cache);
ushort[] result = new ushort[amount];
for (int i = 0; i < amount; i++)
result[i] = BitConverter.ToUInt16(rawValues, i * 2);
return result;
}
/// <summary>
/// Read a uuid from a cache of bytes and remove it from the cache
/// </summary>
/// <param name="cache">Cache of bytes to read from</param>
/// <returns>The uuid</returns>
public Guid ReadNextUUID(Queue<byte> cache)
{
Span<byte> javaUUID = stackalloc byte[16];
for (int i = 0; i < 16; ++i)
javaUUID[i] = cache.Dequeue();
Guid guid = new(javaUUID);
if (BitConverter.IsLittleEndian)
guid = guid.ToLittleEndian();
return guid;
}
/// <summary>
/// Read a byte array from a cache of bytes and remove it from the cache
/// </summary>
/// <param name="cache">Cache of bytes to read from</param>
/// <returns>The byte array</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public byte[] ReadNextByteArray(Queue<byte> cache)
{
int len = protocolversion >= Protocol18Handler.MC_1_8_Version
? ReadNextVarInt(cache)
: ReadNextShort(cache);
return ReadData(len, cache);
}
/// <summary>
/// Read a byte array with given length from a cache of bytes and remove it from the cache
/// </summary>
/// <param name="cache">Cache of bytes to read from</param>
/// <param name="length">Length of the bytes array</param>
/// <returns>The byte array</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public byte[] ReadNextByteArray(Queue<byte> cache, int length)
{
return ReadData(length, cache);
}
/// <summary>
/// Reads a length-prefixed array of unsigned long integers and removes it from the cache
/// </summary>
/// <returns>The unsigned long integer values</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public ulong[] ReadNextULongArray(Queue<byte> cache)
{
int len = ReadNextVarInt(cache);
ulong[] result = new ulong[len];
for (int i = 0; i < len; i++)
result[i] = ReadNextULong(cache);
return result;
}
/// <summary>
/// Read a double from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The double value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public double ReadNextDouble(Queue<byte> cache)
{
Span<byte> rawValue = stackalloc byte[8];
for (int i = (8 - 1); i >= 0; --i) //Endianness
rawValue[i] = cache.Dequeue();
return BitConverter.ToDouble(rawValue);
}
/// <summary>
/// Read a float from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The float value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public float ReadNextFloat(Queue<byte> cache)
{
Span<byte> rawValue = stackalloc byte[4];
for (int i = (4 - 1); i >= 0; --i) //Endianness
rawValue[i] = cache.Dequeue();
return BitConverter.ToSingle(rawValue);
}
/// <summary>
/// Read an integer from the network
/// </summary>
/// <returns>The integer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadNextVarIntRAW(SocketWrapper socket)
{
int i = 0;
int j = 0;
byte b;
while (true)
{
b = socket.ReadDataRAW(1)[0];
i |= (b & 0x7F) << j++ * 7;
if (j > 5) throw new OverflowException("VarInt too big");
if ((b & 0x80) != 128) break;
}
return i;
}
/// <summary>
/// Read an integer from a cache of bytes and remove it from the cache
/// </summary>
/// <param name="cache">Cache of bytes to read from</param>
/// <returns>The integer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public int ReadNextVarInt(Queue<byte> cache)
{
int i = 0;
int j = 0;
byte b;
do
{
b = cache.Dequeue();
i |= (b & 0x7F) << j++ * 7;
if (j > 5) throw new OverflowException("VarInt too big");
} while ((b & 0x80) == 128);
return i;
}
/// <summary>
/// Skip a VarInt from a cache of bytes with better performance
/// </summary>
/// <param name="cache">Cache of bytes to read from</param>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public void SkipNextVarInt(Queue<byte> cache)
{
while (true)
if ((ReadNextByte(cache) & 0x80) != 128)
break;
}
/// <summary>
/// Read an "extended short", which is actually an int of some kind, from the cache of bytes.
/// This is only done with forge. It looks like it's a normal short, except that if the high
/// bit is set, it has an extra byte.
/// </summary>
/// <param name="cache">Cache of bytes to read from</param>
/// <returns>The int</returns>
public int ReadNextVarShort(Queue<byte> cache)
{
ushort low = ReadNextUShort(cache);
byte high = 0;
if ((low & 0x8000) != 0)
{
low &= 0x7FFF;
high = ReadNextByte(cache);
}
return ((high & 0xFF) << 15) | low;
}
/// <summary>
/// Read a long from a cache of bytes and remove it from the cache
/// </summary>
/// <param name="cache">Cache of bytes to read from</param>
/// <returns>The long value</returns>
public long ReadNextVarLong(Queue<byte> cache)
{
int numRead = 0;
long result = 0;
byte read;
do
{
read = ReadNextByte(cache);
long value = (read & 0x7F);
result |= (value << (7 * numRead));
numRead++;
if (numRead > 10)
{
throw new OverflowException("VarLong is too big");
}
} while ((read & 0x80) != 0);
return result;
}
/// <summary>
/// Read a single byte from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The byte that was read</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public byte ReadNextByte(Queue<byte> cache)
{
byte result = cache.Dequeue();
return result;
}
/// <summary>
/// Read an uncompressed Named Binary Tag blob and remove it from the cache
/// </summary>
public Dictionary<string, object> ReadNextNbt(Queue<byte> cache)
{
return ReadNextNbt(cache, true);
}
/// <summary>
/// Read a single item slot from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The item that was read or NULL for an empty slot</returns>
public Item? ReadNextItemSlot(Queue<byte> cache, ItemPalette itemPalette)
{
// MC 1.13.2 and greater
if (protocolversion >= Protocol18Handler.MC_1_13_Version)
{
var itemPresent = ReadNextBool(cache);
if (!itemPresent)
return null;
var itemId = ReadNextVarInt(cache);
if (itemId == -1)
return null;
var type = itemPalette.FromId(itemId);
var itemCount = ReadNextByte(cache);
var nbt = ReadNextNbt(cache);
return new Item(type, itemCount, nbt);
}
else
{
var itemId = ReadNextShort(cache);
if (itemId == -1)
return null;
var itemCount = ReadNextByte(cache);
var data = ReadNextShort(cache);
var nbt = ReadNextNbt(cache);
// For 1.8 - 1.12.2 we combine Item Id and Item Data/Damage to a single value using: (id << 16) | data
return new Item(itemPalette.FromId((itemId << 16) | (ushort)data), itemCount, data, nbt);
}
}
/// <summary>
/// Read entity information from a cache of bytes and remove it from the cache
/// </summary>
/// <param name="entityPalette">Mappings for converting entity type Ids to EntityType</param>
/// <param name="living">TRUE for living entities (layout differs)</param>
/// <returns>Entity information</returns>
public Entity ReadNextEntity(Queue<byte> cache, EntityPalette entityPalette, bool living)
{
int entityID = ReadNextVarInt(cache);
Guid entityUUID = Guid.Empty;
// UUID field added in 1.9 +
if (protocolversion >= Protocol18Handler.MC_1_9_Version)
entityUUID = ReadNextUUID(cache);
// Entity type data type change from byte to varint in 1.13.2
EntityType entityType;
if (living)
{
// For living entities the Type field was changed to VarInt from Byte in 1.11 +
if (protocolversion >= Protocol18Handler.MC_1_11_Version)
entityType = entityPalette.FromId(ReadNextVarInt(cache), living);
else entityType = entityPalette.FromId(ReadNextByte(cache), living);
}
else
{
// For non-living entities the Type field was changed to VarInt from Byte in 1.13.2 +
if (protocolversion >= Protocol18Handler.MC_1_13_2_Version)
entityType = entityPalette.FromId(ReadNextVarInt(cache), living);
else entityType = entityPalette.FromId(ReadNextByte(cache), living);
}
Double entityX, entityY, entityZ;
if (protocolversion < Protocol18Handler.MC_1_9_Version)
{
entityX = ReadNextInt(cache) / 32.0D; // X
entityY = ReadNextInt(cache) / 32.0D; // Y
entityZ = ReadNextInt(cache) / 32.0D; // Z
}
else
{
entityX = ReadNextDouble(cache); // X
entityY = ReadNextDouble(cache); // Y
entityZ = ReadNextDouble(cache); // Z
}
int data = -1;
byte entityPitch, entityYaw;
if (living)
{
entityYaw = ReadNextByte(cache); // Yaw
entityPitch = ReadNextByte(cache); // Pitch
entityPitch = ReadNextByte(cache); // Head Pitch
}
else
{
entityPitch = ReadNextByte(cache); // Pitch
entityYaw = ReadNextByte(cache); // Yaw
if (protocolversion >= Protocol18Handler.MC_1_19_Version)
entityYaw = ReadNextByte(cache); // Head Yaw
// Data
data = protocolversion >= Protocol18Handler.MC_1_19_Version
? ReadNextVarInt(cache)
: ReadNextInt(cache);
}
// In 1.8 those 3 fields for Velocity are optional
if (protocolversion < Protocol18Handler.MC_1_9_Version)
{
if (data != 0)
{
ReadNextShort(cache);
ReadNextShort(cache);
ReadNextShort(cache);
}
}
else
{
ReadNextShort(cache);
ReadNextShort(cache);
ReadNextShort(cache);
}
return new Entity(entityID, entityType, new Location(entityX, entityY, entityZ), entityYaw, entityPitch,
data);
}
/// <summary>
/// Read an uncompressed Named Binary Tag blob and remove it from the cache (internal)
/// </summary>
private Dictionary<string, object> ReadNextNbt(Queue<byte> cache, bool root)
{
Dictionary<string, object> nbtData = new();
if (root)
{
if (cache.Peek() == 0) // TAG_End
{
cache.Dequeue();
return nbtData;
}
var nextId = cache.Dequeue();
if (protocolversion < Protocol18Handler.MC_1_20_2_Version)
{
if (nextId is not 10) // TAG_Compound
throw new System.IO.InvalidDataException(
"Failed to decode NBT: Does not start with TAG_Compound");
// NBT root name
var rootName = Encoding.ASCII.GetString(ReadData(ReadNextUShort(cache), cache));
if (!string.IsNullOrEmpty(rootName))
nbtData[""] = rootName;
}
// In 1.20.2 The root TAG_Compound doesn't have a name
// In 1.20.3+ The root can be TAG_Compound or TAG_String
else
{
if (nextId is not (10 or 8)) // TAG_Compound or TAG_String
throw new System.IO.InvalidDataException(
"Failed to decode NBT: Does not start with TAG_Compound or TAG_String");
// Read TAG_String
if (nextId is 8)
{
var byteArrayLength = ReadNextUShort(cache);
var result = Encoding.UTF8.GetString(ReadData(byteArrayLength, cache));
return new Dictionary<string, object>()
{
{ "", result }
};
}
}
}
while (true)
{
int fieldType = ReadNextByte(cache);
if (fieldType == 0) // TAG_End
return nbtData;
int fieldNameLength = ReadNextUShort(cache);
string fieldName = Encoding.ASCII.GetString(ReadData(fieldNameLength, cache));
object fieldValue = ReadNbtField(cache, fieldType);
// This will override previous tags with the same name
nbtData[fieldName] = fieldValue;
}
}
/// <summary>
/// Read a single Named Binary Tag field of the specified type and remove it from the cache
/// </summary>
private object ReadNbtField(Queue<byte> cache, int fieldType)
{
switch (fieldType)
{
case 1: // TAG_Byte
return ReadNextByte(cache);
case 2: // TAG_Short
return ReadNextShort(cache);
case 3: // TAG_Int
return ReadNextInt(cache);
case 4: // TAG_Long
return ReadNextLong(cache);
case 5: // TAG_Float
return ReadNextFloat(cache);
case 6: // TAG_Double
return ReadNextDouble(cache);
case 7: // TAG_Byte_Array
return ReadData(ReadNextInt(cache), cache);
case 8: // TAG_String
return Encoding.UTF8.GetString(ReadData(ReadNextUShort(cache), cache));
case 9: // TAG_List
int listType = ReadNextByte(cache);
int listLength = ReadNextInt(cache);
object[] listItems = new object[listLength];
for (int i = 0; i < listLength; i++)
listItems[i] = ReadNbtField(cache, listType);
return listItems;
case 10: // TAG_Compound
return ReadNextNbt(cache, false);
case 11: // TAG_Int_Array
listType = 3;
listLength = ReadNextInt(cache);
listItems = new object[listLength];
for (int i = 0; i < listLength; i++)
listItems[i] = ReadNbtField(cache, listType);
return listItems;
case 12: // TAG_Long_Array
listType = 4;
listLength = ReadNextInt(cache);
listItems = new object[listLength];
for (int i = 0; i < listLength; i++)
listItems[i] = ReadNbtField(cache, listType);
return listItems;
default:
throw new System.IO.InvalidDataException("Failed to decode NBT: Unknown field type " + fieldType);
}
}
/// <summary>
/// Read a Entity MetaData and remove it from the cache
/// </summary>
/// <param name="cache"></param>
/// <param name="itemPalette"></param>
/// <param name="metadataPalette"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
/// <exception cref="System.IO.InvalidDataException"></exception>
public Dictionary<int, object?> ReadNextMetadata(Queue<byte> cache, ItemPalette itemPalette,
EntityMetadataPalette metadataPalette)
{
Dictionary<int, object?> data = new();
byte key = ReadNextByte(cache);
byte terminteValue = protocolversion <= Protocol18Handler.MC_1_8_Version
? (byte)0x7f // 1.8 (https://wiki.vg/index.php?title=Entity_metadata&oldid=6220#Entity_Metadata_Format)
: (byte)0xff; // 1.9+
while (key != terminteValue)
{
int typeId = protocolversion <= Protocol18Handler.MC_1_8_Version
? key >> 5 // 1.8
: ReadNextVarInt(cache); // 1.9+
EntityMetaDataType type;
try
{
type = metadataPalette.GetDataType(typeId);
}
catch (KeyNotFoundException)
{
throw new System.IO.InvalidDataException("Unknown Metadata Type ID " + typeId +
". Is this up to date for new MC Version?");
}
if (protocolversion <= Protocol18Handler.MC_1_8_Version)
key = (byte)(key & 0x1f);
// Value's data type is depended on Type
object? value = null;
switch (type)
{
case EntityMetaDataType.Short: // 1.8 only
value = ReadNextShort(cache);
break;
case EntityMetaDataType.Int: // 1.8 only
value = ReadNextInt(cache);
break;
case EntityMetaDataType.Vector3Int: // 1.8 only
value = new List<int>()
{
ReadNextInt(cache),
ReadNextInt(cache),
ReadNextInt(cache),
};
break;
case EntityMetaDataType.Byte: // byte
value = ReadNextByte(cache);
break;
case EntityMetaDataType.VarInt: // VarInt
value = ReadNextVarInt(cache);
break;
case EntityMetaDataType.VarLong: // Long
value = ReadNextVarLong(cache);
break;
case EntityMetaDataType.Float: // Float
value = ReadNextFloat(cache);
break;
case EntityMetaDataType.String: // String
value = ReadNextString(cache);
break;
case EntityMetaDataType.Chat: // Chat
value = ReadNextChat(cache);
break;
case EntityMetaDataType.OptionalChat: // Optional Chat
if (ReadNextBool(cache))
value = ReadNextChat(cache);
break;
case EntityMetaDataType.Slot: // Slot
value = ReadNextItemSlot(cache, itemPalette);
break;
case EntityMetaDataType.Boolean: // Boolean
value = ReadNextBool(cache);
break;
case EntityMetaDataType.Rotation: // Rotation (3x floats)
value = new List<float>
{
ReadNextFloat(cache),
ReadNextFloat(cache),
ReadNextFloat(cache)
};
break;
case EntityMetaDataType.Position: // Position
value = ReadNextLocation(cache);
break;
case EntityMetaDataType.OptionalPosition: // Optional Position
if (ReadNextBool(cache))
{
value = ReadNextLocation(cache);
}
break;
case EntityMetaDataType.Direction: // Direction (VarInt)
value = ReadNextVarInt(cache);
break;
case EntityMetaDataType.OptionalUuid: // Optional UUID
if (ReadNextBool(cache))
{
value = ReadNextUUID(cache);
}
break;
case EntityMetaDataType.BlockId: // BlockID (VarInt)
value = ReadNextVarInt(cache);
break;
case EntityMetaDataType.OptionalBlockId: // Optional BlockID (VarInt)
value = ReadNextVarInt(cache);
break;
case EntityMetaDataType.Nbt: // NBT
value = ReadNextNbt(cache);
break;
case EntityMetaDataType.Particle: // Particle
// Skip data only, not used
ReadParticleData(cache, itemPalette);
break;
case EntityMetaDataType.VillagerData: // Villager Data (3x VarInt)
value = new List<int>
{
ReadNextVarInt(cache),
ReadNextVarInt(cache),
ReadNextVarInt(cache)
};
break;
case EntityMetaDataType.OptionalVarInt: // Optional VarInt
if (ReadNextBool(cache))
{
value = ReadNextVarInt(cache);
}
break;
case EntityMetaDataType.Pose: // Pose
value = ReadNextVarInt(cache);
break;
case EntityMetaDataType.CatVariant: // Cat Variant
value = ReadNextVarInt(cache);
break;
case EntityMetaDataType.FrogVariant: // Frog Varint
value = ReadNextVarInt(cache);
break;
case EntityMetaDataType.GlobalPosition: // GlobalPos
// Dimension and blockPos, currently not in use
value = new Tuple<string, Location>(ReadNextString(cache), ReadNextLocation(cache));
break;
case EntityMetaDataType.OptionalGlobalPosition:
// FIXME: wiki.vg is bool + string + location
// but minecraft-data is bool + string
if (ReadNextBool(cache))
{
// Dimension and blockPos, currently not in use
value = new Tuple<string, Location>(ReadNextString(cache), ReadNextLocation(cache));
}
break;
case EntityMetaDataType.PaintingVariant: // Painting Variant
value = ReadNextVarInt(cache);
break;
case EntityMetaDataType.SnifferState: // Sniffer state
value = ReadNextVarInt(cache);
break;
case EntityMetaDataType.Vector3: // Vector 3f
value = new List<float>
{
ReadNextFloat(cache),
ReadNextFloat(cache),
ReadNextFloat(cache)
};
break;
case EntityMetaDataType.Quaternion: // Quaternion
value = new List<float>
{
ReadNextFloat(cache),
ReadNextFloat(cache),
ReadNextFloat(cache),
ReadNextFloat(cache)
};
break;
}
data[key] = value;
key = ReadNextByte(cache);
}
return data;
}
/// <summary>
/// Currently not handled. Reading data only
/// </summary>
/// <param name="cache"></param>
/// <param name="itemPalette"></param>
public void ReadParticleData(Queue<byte> cache, ItemPalette itemPalette)
{
if (protocolversion < Protocol18Handler.MC_1_13_Version)
return;
var particleId = ReadNextVarInt(cache);
// Documentation:
// 1.19.3+ - https://wiki.vg/index.php?title=Data_types&oldid=17986
// 1.18 - https://wiki.vg/index.php?title=Data_types&oldid=17180
// 1.17 - https://wiki.vg/index.php?title=Data_types&oldid=16740
// 1.15 - https://wiki.vg/index.php?title=Data_types&oldid=15338
// 1.13 - https://wiki.vg/index.php?title=Data_types&oldid=14271
switch (particleId)
{
case 2:
// 1.18 +
if (protocolversion > Protocol18Handler.MC_1_17_1_Version)
ReadNextVarInt(cache); // Block state (minecraft:block)
break;
case 3:
if (protocolversion is < Protocol18Handler.MC_1_17_Version or > Protocol18Handler.MC_1_17_1_Version)
ReadNextVarInt(
cache); // Block State (minecraft:block before 1.18, minecraft:block_marker after 1.18)
break;
case 4:
if (protocolversion is Protocol18Handler.MC_1_17_Version or Protocol18Handler.MC_1_17_1_Version)
ReadNextVarInt(cache); // Block State (minecraft:block)
break;
case 11:
// 1.13 - 1.14.4
if (protocolversion < Protocol18Handler.MC_1_15_Version)
ReadDustParticle(cache);
break;
case 14:
// 1.15 - 1.16.5 and 1.18 - 1.19.4
if (protocolversion is >= Protocol18Handler.MC_1_15_Version and < Protocol18Handler.MC_1_17_Version
or > Protocol18Handler.MC_1_17_1_Version)
ReadDustParticle(cache);
break;
case 15:
switch (protocolversion)
{
case Protocol18Handler.MC_1_17_Version or Protocol18Handler.MC_1_17_1_Version:
ReadDustParticle(cache);
break;
case > Protocol18Handler.MC_1_17_1_Version:
ReadDustParticleColorTransition(cache);
break;
}
break;
case 16:
if (protocolversion is Protocol18Handler.MC_1_17_Version or Protocol18Handler.MC_1_17_1_Version)
ReadDustParticleColorTransition(cache);
break;
case 23:
// 1.15 - 1.16.5
if (protocolversion is >= Protocol18Handler.MC_1_15_Version and < Protocol18Handler.MC_1_17_Version)
ReadNextVarInt(cache); // Block State (minecraft:falling_dust)
break;
case 24:
// 1.18 - 1.19.2 onwards
if (protocolversion is > Protocol18Handler.MC_1_17_1_Version
and < Protocol18Handler.MC_1_19_3_Version)
ReadNextVarInt(cache); // Block State (minecraft:falling_dust)
break;
case 25:
// 1.17 - 1.17.1 and 1.19.3 onwards
if (protocolversion is Protocol18Handler.MC_1_17_Version or Protocol18Handler.MC_1_17_1_Version
or >= Protocol18Handler.MC_1_19_3_Version)
ReadNextVarInt(cache); // Block State (minecraft:falling_dust)
break;
case 27:
// 1.13 - 1.14.4
if (protocolversion < Protocol18Handler.MC_1_15_Version)
ReadNextItemSlot(cache, itemPalette); // Item (minecraft:item)
break;
case 30:
if (protocolversion >= Protocol18Handler.MC_1_19_3_Version)
ReadNextFloat(cache); // Roll (minecraft:sculk_charge)
break;
case 32:
// 1.15 - 1.16.5
if (protocolversion is >= Protocol18Handler.MC_1_15_Version and < Protocol18Handler.MC_1_17_Version)
ReadNextItemSlot(cache, itemPalette); // Item (minecraft:item)
break;
case 36:
switch (protocolversion)
{
// 1.17 - 1.17.1
case Protocol18Handler.MC_1_17_Version or Protocol18Handler.MC_1_17_1_Version:
ReadNextItemSlot(cache, itemPalette); // Item (minecraft:item)
break;
case > Protocol18Handler.MC_1_17_1_Version and < Protocol18Handler.MC_1_19_3_Version:
// minecraft:vibration
ReadNextLocation(cache); // Origin (Starting Position)
ReadNextLocation(cache); // Destination (Ending Position)
ReadNextVarInt(cache); // Ticks
break;
}
break;
case 37:
// minecraft:vibration
if (protocolversion is Protocol18Handler.MC_1_17_Version or Protocol18Handler.MC_1_17_1_Version)
{
ReadNextDouble(cache); // Origin X
ReadNextDouble(cache); // Origin Y
ReadNextDouble(cache); // Origin Z
ReadNextDouble(cache); // Destination X
ReadNextDouble(cache); // Destination Y
ReadNextDouble(cache); // Destination Z
ReadNextInt(cache); // Ticks
}
break;
case 39:
if (protocolversion >= Protocol18Handler.MC_1_19_3_Version)
ReadNextItemSlot(cache, itemPalette); // Item (minecraft:item)
break;
case 40:
if (protocolversion >= Protocol18Handler.MC_1_19_3_Version)
{
var positionSourceType = ReadNextString(cache);
switch (positionSourceType)
{
case "minecraft:block":
ReadNextLocation(cache);
break;
case "minecraft:entity":
ReadNextVarInt(cache);
ReadNextFloat(cache);
break;