Difference between revisions of "Data types"

From wiki.vg
Jump to navigation Jump to search
(Fix particle not being transcluded into the main protocol article)
(Merge position (hopefully this is correct) and particles)
Line 269: Line 269:
  
 
* x: 26 MSBs
 
* x: 26 MSBs
* z: 26 LSBs
+
* z: 26 middle bits
* y: 12 bits between them
+
* y: 12 LSBs
  
 
Encoded as followed:
 
Encoded as followed:
  
  ((x & 0x3FFFFFF) << 38) | ((y & 0xFFF) << 26) | (z & 0x3FFFFFF)
+
  ((x & 0x3FFFFFF) << 38) | ((z & 0x3FFFFFF) << 12) | (y & 0xFFF)
  
 
And decoded as:
 
And decoded as:
Line 280: Line 280:
 
  val = read_unsigned_long();
 
  val = read_unsigned_long();
 
  x = val >> 38;
 
  x = val >> 38;
  y = (val >> 26) & 0xFFF;
+
  y = val & 0xFFF;
  z = val << 38 >> 38;
+
  z = (val << 26 >> 38);
  
 
Note: The details of bit shifting are rather language dependent; the above may work in Java but probably won't in other languages without some tweaking. In particular, you will usually receive positive numbers even if the actual coordinates are negative. This can be fixed by adding something like the following:
 
Note: The details of bit shifting are rather language dependent; the above may work in Java but probably won't in other languages without some tweaking. In particular, you will usually receive positive numbers even if the actual coordinates are negative. This can be fixed by adding something like the following:
Line 372: Line 372:
 
  | <code>minecraft:dripping_lava</code>
 
  | <code>minecraft:dripping_lava</code>
 
  | 9
 
  | 9
 +
| None
 +
|-
 +
| <code>minecraft:falling_lava</code>
 +
| 10
 +
| None
 +
|-
 +
| <code>minecraft:landing_lava</code>
 +
| 11
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:dripping_water</code>
 
  | <code>minecraft:dripping_water</code>
  | 10
+
  | 12
 +
| None
 +
|-
 +
| <code>minecraft:falling_water</code>
 +
| 13
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:dust</code>
 
  | <code>minecraft:dust</code>
  | 11
+
  | 14
 
  |  
 
  |  
 
  {| class="wikitable"
 
  {| class="wikitable"
Line 404: Line 416:
 
  |-
 
  |-
 
  | <code>minecraft:effect</code>
 
  | <code>minecraft:effect</code>
  | 12
+
  | 15
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:elder_guardian</code>
 
  | <code>minecraft:elder_guardian</code>
  | 13
+
  | 16
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:enchanted_hit</code>
 
  | <code>minecraft:enchanted_hit</code>
  | 14
+
  | 17
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:enchant</code>
 
  | <code>minecraft:enchant</code>
  | 15
+
  | 18
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:end_rod</code>
 
  | <code>minecraft:end_rod</code>
  | 16
+
  | 19
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:entity_effect</code>
 
  | <code>minecraft:entity_effect</code>
  | 17
+
  | 20
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:explosion_emitter</code>
 
  | <code>minecraft:explosion_emitter</code>
  | 18
+
  | 21
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:explosion</code>
 
  | <code>minecraft:explosion</code>
  | 19
+
  | 22
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:falling_dust</code>
 
  | <code>minecraft:falling_dust</code>
  | 20
+
  | 23
 
  |
 
  |
 
  {| class="wikitable"
 
  {| class="wikitable"
Line 449: Line 461:
 
  |-
 
  |-
 
  | <code>minecraft:firework</code>
 
  | <code>minecraft:firework</code>
  | 21
+
  | 24
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:fishing</code>
 
  | <code>minecraft:fishing</code>
  | 22
+
  | 25
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:flame</code>
 
  | <code>minecraft:flame</code>
  | 23
+
  | 26
 +
| None
 +
|-
 +
| <code>minecraft:flash</code>
 +
| 27
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:happy_villager</code>
 
  | <code>minecraft:happy_villager</code>
  | 24
+
  | 28
 +
| None
 +
|-
 +
| <code>minecraft:composter</code>
 +
| 29
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:heart</code>
 
  | <code>minecraft:heart</code>
  | 25
+
  | 30
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:instant_effect</code>
 
  | <code>minecraft:instant_effect</code>
  | 26
+
  | 31
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:item</code>
 
  | <code>minecraft:item</code>
  | 27
+
  | 32
 
  |
 
  |
 
  {| class="wikitable"
 
  {| class="wikitable"
Line 486: Line 506:
 
  |-
 
  |-
 
  | <code>minecraft:item_slime</code>
 
  | <code>minecraft:item_slime</code>
  | 28
+
  | 33
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:item_snowball</code>
 
  | <code>minecraft:item_snowball</code>
  | 29
+
  | 34
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:large_smoke</code>
 
  | <code>minecraft:large_smoke</code>
  | 30
+
  | 35
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:lava</code>
 
  | <code>minecraft:lava</code>
  | 31
+
  | 36
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:mycelium</code>
 
  | <code>minecraft:mycelium</code>
  | 32
+
  | 37
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:note</code>
 
  | <code>minecraft:note</code>
  | 33
+
  | 38
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:poof</code>
 
  | <code>minecraft:poof</code>
  | 34
+
  | 39
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:portal</code>
 
  | <code>minecraft:portal</code>
  | 35
+
  | 40
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:rain</code>
 
  | <code>minecraft:rain</code>
  | 36
+
  | 41
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:smoke</code>
 
  | <code>minecraft:smoke</code>
  | 37
+
  | 42
 +
| None
 +
|-
 +
| <code>minecraft:sneeze</code>
 +
| 43
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:spit</code>
 
  | <code>minecraft:spit</code>
  | 38
+
  | 44
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:squid_ink</code>
 
  | <code>minecraft:squid_ink</code>
  | 39
+
  | 45
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:sweep_attack</code>
 
  | <code>minecraft:sweep_attack</code>
  | 40
+
  | 46
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:totem_of_undying</code>
 
  | <code>minecraft:totem_of_undying</code>
  | 41
+
  | 47
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:underwater</code>
 
  | <code>minecraft:underwater</code>
  | 42
+
  | 48
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:splash</code>
 
  | <code>minecraft:splash</code>
  | 43
+
  | 49
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:witch</code>
 
  | <code>minecraft:witch</code>
  | 44
+
  | 50
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:bubble_pop</code>
 
  | <code>minecraft:bubble_pop</code>
  | 45
+
  | 51
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:current_down</code>
 
  | <code>minecraft:current_down</code>
  | 46
+
  | 52
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:bubble_column_up</code>
 
  | <code>minecraft:bubble_column_up</code>
  | 47
+
  | 53
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:nautilus</code>
 
  | <code>minecraft:nautilus</code>
  | 48
+
  | 54
 
  | None
 
  | None
 
  |-
 
  |-
 
  | <code>minecraft:dolphin</code>
 
  | <code>minecraft:dolphin</code>
  | 49
+
  | 55
 +
| None
 +
|-
 +
| <code>minecraft:campfire_cosy_smoke</code>
 +
| 56
 +
| None
 +
|-
 +
| <code>minecraft:campfire_signal_smoke</code>
 +
| 57
 
  | None
 
  | None
 
  |}
 
  |}

Revision as of 20:10, 22 August 2019

This article defines the data types used in the protocol. All data sent over the network (except for VarInt and VarLong) is big-endian, that is the bytes are sent from most significant byte to least significant byte. The majority of everyday computers are little-endian, therefore it may be necessary to change the endianness before sending data over the network.

Definitions

Name Size (bytes) Encodes Notes
Boolean 1 Either false or true True is encoded as 0x01, false as 0x00.
Byte 1 An integer between -128 and 127 Signed 8-bit integer, two's complement
Unsigned Byte 1 An integer between 0 and 255 Unsigned 8-bit integer
Short 2 An integer between -32768 and 32767 Signed 16-bit integer, two's complement
Unsigned Short 2 An integer between 0 and 65535 Unsigned 16-bit integer
Int 4 An integer between -2147483648 and 2147483647 Signed 32-bit integer, two's complement
Long 8 An integer between -9223372036854775808 and 9223372036854775807 Signed 64-bit integer, two's complement
Float 4 A single-precision 32-bit IEEE 754 floating point number
Double 8 A double-precision 64-bit IEEE 754 floating point number
String (n) ≥ 1
≤ (n×4) + 3
A sequence of Unicode scalar values UTF-8 string prefixed with its size in bytes as a VarInt. Maximum length of n characters, which varies by context; up to n × 4 bytes can be used to encode n characters and both of those limits are checked. Maximum n value is 32767. The + 3 is due to the max size of a valid length VarInt.
Chat ≥ 1
≤ (32767×4) + 3
See Chat Encoded as a String with max length of 32767.
Identifier ≥ 1
≤ (32767×4) + 3
See Identifier below Encoded as a String with max length of 32767.
VarInt ≥ 1
≤ 5
An integer between -2147483648 and 2147483647 Variable-length data encoding a two's complement signed 32-bit integer; more info in their section
VarLong ≥ 1
≤ 10
An integer between -9223372036854775808 and 9223372036854775807 Variable-length data encoding a two's complement signed 64-bit integer; more info in their section
Entity Metadata Varies Miscellaneous information about an entity See Entities#Entity Metadata Format
Slot Varies An item stack in an inventory or container See Slot Data
NBT Tag Varies Depends on context See NBT
Position 8 An integer/block position: x (-33554432 to 33554431), y (-2048 to 2047), z (-33554432 to 33554431) x as a 26-bit integer, followed by y as a 12-bit integer, followed by z as a 26-bit integer (all signed, two's complement). See also the section below.
Angle 1 A rotation angle in steps of 1/256 of a full turn Whether or not this is signed does not matter, since the resulting angles are the same.
UUID 16 A UUID Encoded as an unsigned 128-bit integer (or two unsigned 64-bit integers: the most significant 64 bits and then the least significant 64 bits)
Optional X 0 or size of X A field of type X, or nothing Whether or not the field is present must be known from the context.
Array of X count times size of X Zero or more fields of type X The count must be known from the context.
X Enum size of X A specific value from a given list The list of possible values and how each is encoded as an X must be known from the context. An invalid value sent by either side will usually result in the client being disconnected with an error or even crashing.
Byte Array Varies Depends on context This is just a sequence of zero or more bytes, its meaning should be explained somewhere else, e.g. in the packet description. The length must also be known from the context.

Identifier

Identifiers are a namespaced location, in the form of minecraft:thing. If the namespace is not provided, it defaults to minecraft (i.e. thing is minecraft:thing. Custom content should always be in its own namespace, not the default one. The namespace should only use the characters 01​​234​5​6​78​9abcdefghijklmnopqrstuvwxyz-_; actual names may contain more symbols. The naming convention is lower_case_with_underscores. More information.

VarInt and VarLong

Variable-length format such that smaller numbers use fewer bytes. These are very similar to Protocol Buffer Varints: the 7 least significant bits are used to encode the value and the most significant bit indicates whether there's another byte after it for the next part of the number. The least significant group is written first, followed by each of the more significant groups; thus, VarInts are effectively little endian (however, groups are 7 bits, not 8).

VarInts are never longer than 5 bytes, and VarLongs are never longer than 10 bytes.

Pseudocode to read and write VarInts and VarLongs:

public static int readVarInt() {
    int numRead = 0;
    int result = 0;
    byte read;
    do {
        read = readByte();
        int value = (read & 0b01111111);
        result |= (value << (7 * numRead));

        numRead++;
        if (numRead > 5) {
            throw new RuntimeException("VarInt is too big");
        }
    } while ((read & 0b10000000) != 0);

    return result;
}
public static long readVarLong() {
    int numRead = 0;
    long result = 0;
    byte read;
    do {
        read = readByte();
        int value = (read & 0b01111111);
        result |= (value << (7 * numRead));

        numRead++;
        if (numRead > 10) {
            throw new RuntimeException("VarLong is too big");
        }
    } while ((read & 0b10000000) != 0);

    return result;
}
public static void writeVarInt(int value) {
    do {
        byte temp = (byte)(value & 0b01111111);
        // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone
        value >>>= 7;
        if (value != 0) {
            temp |= 0b10000000;
        }
        writeByte(temp);
    } while (value != 0);
}
public static void writeVarLong(long value) {
    do {
        byte temp = (byte)(value & 0b01111111);
        // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone
        value >>>= 7;
        if (value != 0) {
            temp |= 0b10000000;
        }
        writeByte(temp);
    } while (value != 0);
}

Warning.png Note that Minecraft's VarInts are not encoded using Protocol Buffers; it's just similar. If you try to use Protocol Buffers Varints with Minecraft's VarInts, you'll get incorrect results in some cases. The major differences:

  • Minecraft's VarInts are all signed, but do not use the ZigZag encoding. Protocol buffers have 3 types of Varints: uint32 (normal encoding, unsigned), sint32 (ZigZag encoding, signed), and int32 (normal encoding, signed). Minecraft's are the int32 variety. Because Minecraft uses the normal encoding instead of ZigZag encoding, negative values always use the maximum number of bytes.
  • Minecraft's VarInts are never be longer than 5 bytes and its VarLongs will never be longer than 10 bytes, while Protocol Buffer Varints will always use 10 bytes when encoding negative numbers, even if it's an int32.

Sample VarInts:

Value Hex bytes Decimal bytes
0 0x00 0
1 0x01 1
2 0x02 2
127 0x7f 127
128 0x80 0x01 128 1
255 0xff 0x01 255 1
2147483647 0xff 0xff 0xff 0xff 0x07 255 255 255 255 7
-1 0xff 0xff 0xff 0xff 0x0f 255 255 255 255 15
-2147483648 0x80 0x80 0x80 0x80 0x08 128 128 128 128 8

Sample VarLongs:

Value Hex bytes Decimal bytes
0 0x00 0
1 0x01 1
2 0x02 2
127 0x7f 127
128 0x80 0x01 128 1
255 0xff 0x01 255 1
2147483647 0xff 0xff 0xff 0xff 0x07 255 255 255 255 7
9223372036854775807 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x7f 255 255 255 255 255 255 255 255 127
-1 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x01 255 255 255 255 255 255 255 255 255 1
-2147483648 0x80 0x80 0x80 0x80 0xf8 0xff 0xff 0xff 0xff 0x01 128 128 128 128 248 255 255 255 255 1
-9223372036854775808 0x80 0x80 0x80 0x80 0x80 0x80 0x80 0x80 0x80 0x01 128 128 128 128 128 128 128 128 128 1

Position

64-bit value split in to three parts

  • x: 26 MSBs
  • z: 26 middle bits
  • y: 12 LSBs

Encoded as followed:

((x & 0x3FFFFFF) << 38) | ((z & 0x3FFFFFF) << 12) | (y & 0xFFF)

And decoded as:

val = read_unsigned_long();
x = val >> 38;
y = val & 0xFFF;
z = (val << 26 >> 38);

Note: The details of bit shifting are rather language dependent; the above may work in Java but probably won't in other languages without some tweaking. In particular, you will usually receive positive numbers even if the actual coordinates are negative. This can be fixed by adding something like the following:

if x >= 2^25 { x -= 2^26 }
if y >= 2^11 { y -= 2^12 }
if z >= 2^25 { z -= 2^26 }

Fixed-point numbers

Some fields may be stored as fixed-point numbers, where a certain number of bits represents the signed integer part (number to the left of the decimal point) and the rest represents the fractional part (to the right). Floating points (float and double), in contrast, keep the number itself (mantissa) in one chunk, while the location of the decimal point (exponent) is stored beside it.

Essentially, while fixed-point numbers have lower range than floating points, their fractional precision is greater for higher values. This makes them ideal for representing global coordinates of an entity in Minecraft, as it's more important to store the integer part accurately than position them more precisely within a single block (or meter).

Coordinates are often represented as a 32-bit integer, where 5 of the least-significant bits are dedicated to the fractional part, and the rest store the integer part.

Java lacks support for fractional integers directly, but you can represent them as integers. To convert from a double to this integer representation, use the following formulas:

 abs_int = (int) (double * 32.0D);

And back again:

 double = (double) (abs_int / 32.0D);

Particle

Field Name Field Type Meaning
ID VarInt The ID of the particle type, see below.
Data Varies Varies based on the particle type, see below.
Particle Name Particle ID Data
minecraft:ambient_entity_effect 0 None
minecraft:angry_villager 1 None
minecraft:barrier 2 None
minecraft:block 3
Field Name Field Type Meaning
BlockState VarInt The ID of the block state.
minecraft:bubble 4 None
minecraft:cloud 5 None
minecraft:crit 6 None
minecraft:damage_indicator 7 None
minecraft:dragon_breath 8 None
minecraft:dripping_lava 9 None
minecraft:falling_lava 10 None
minecraft:landing_lava 11 None
minecraft:dripping_water 12 None
minecraft:falling_water 13 None
minecraft:dust 14
Field Name Field Type Meaning
Red Float Red value, 0-1
Green Float Green value, 0-1
Blue Float Blue value, 0-1
Scale Float The scale, will be clamped between 0.01 and 4.
minecraft:effect 15 None
minecraft:elder_guardian 16 None
minecraft:enchanted_hit 17 None
minecraft:enchant 18 None
minecraft:end_rod 19 None
minecraft:entity_effect 20 None
minecraft:explosion_emitter 21 None
minecraft:explosion 22 None
minecraft:falling_dust 23
Field Name Field Type Meaning
BlockState VarInt The ID of the block state.
minecraft:firework 24 None
minecraft:fishing 25 None
minecraft:flame 26 None
minecraft:flash 27 None
minecraft:happy_villager 28 None
minecraft:composter 29 None
minecraft:heart 30 None
minecraft:instant_effect 31 None
minecraft:item 32
Field Name Field Type Meaning
Item Slot The item that will be used.
minecraft:item_slime 33 None
minecraft:item_snowball 34 None
minecraft:large_smoke 35 None
minecraft:lava 36 None
minecraft:mycelium 37 None
minecraft:note 38 None
minecraft:poof 39 None
minecraft:portal 40 None
minecraft:rain 41 None
minecraft:smoke 42 None
minecraft:sneeze 43 None
minecraft:spit 44 None
minecraft:squid_ink 45 None
minecraft:sweep_attack 46 None
minecraft:totem_of_undying 47 None
minecraft:underwater 48 None
minecraft:splash 49 None
minecraft:witch 50 None
minecraft:bubble_pop 51 None
minecraft:current_down 52 None
minecraft:bubble_column_up 53 None
minecraft:nautilus 54 None
minecraft:dolphin 55 None
minecraft:campfire_cosy_smoke 56 None
minecraft:campfire_signal_smoke 57 None