Difference between revisions of "Data types"
(added “encodes” for float and double) |
(added some types mentioned in the protocol, changed the type names to match those used on Protocol) |
||
Line 10: | Line 10: | ||
! Notes | ! Notes | ||
|- | |- | ||
− | ! | + | ! Boolean |
| 1 | | 1 | ||
| false or true | | false or true | ||
| Value can be either true (<code>0x01</code>) or false (<code>0x00</code>) | | Value can be either true (<code>0x01</code>) or false (<code>0x00</code>) | ||
|- | |- | ||
− | ! | + | ! Byte |
| 1 | | 1 | ||
| -128 to 127 | | -128 to 127 | ||
| Signed 8-bit integer, [[wikipedia:Two's complement|two's complement]] | | Signed 8-bit integer, [[wikipedia:Two's complement|two's complement]] | ||
|- | |- | ||
− | ! | + | ! Unsigned Byte |
+ | | 1 | ||
+ | | 0 to 255 | ||
+ | | Unsigned 8-bit integer | ||
+ | |- | ||
+ | ! Short | ||
| 2 | | 2 | ||
| -32768 to 32767 | | -32768 to 32767 | ||
| Signed 16-bit integer, two's complement | | Signed 16-bit integer, two's complement | ||
|- | |- | ||
− | ! | + | ! Unsigned Short |
+ | | 2 | ||
+ | | 0 to 65535 | ||
+ | | Unsigned 16-bit integer | ||
+ | |- | ||
+ | ! Int | ||
| 4 | | 4 | ||
| -2147483648 to 2147483647 | | -2147483648 to 2147483647 | ||
| Signed 32-bit integer, two's complement | | Signed 32-bit integer, two's complement | ||
|- | |- | ||
− | ! | + | ! Long |
| 8 | | 8 | ||
| -9223372036854775808 to 9223372036854775807 | | -9223372036854775808 to 9223372036854775807 | ||
| Signed 64-bit integer, two's complement | | Signed 64-bit integer, two's complement | ||
|- | |- | ||
− | ! | + | ! Float |
| 4 | | 4 | ||
| [[wikipedia:Single-precision floating-point format|Single-precision 32-bit IEEE 754 floating point]] | | [[wikipedia:Single-precision floating-point format|Single-precision 32-bit IEEE 754 floating point]] | ||
| | | | ||
|- | |- | ||
− | ! | + | ! Double |
| 8 | | 8 | ||
| [[wikipedia:Double-precision floating-point format|Double-precision 64-bit IEEE 754 floating point]] | | [[wikipedia:Double-precision floating-point format|Double-precision 64-bit IEEE 754 floating point]] | ||
| | | | ||
|- | |- | ||
− | ! | + | ! String |
| ≥ 1 <br />≤ 2147483652 | | ≥ 1 <br />≤ 2147483652 | ||
| A sequence of [[wikipedia:Unicode|Unicode]] code points | | A sequence of [[wikipedia:Unicode|Unicode]] code points | ||
| [[wikipedia:UTF-8|UTF-8]] string prefixed with its length as a VarInt | | [[wikipedia:UTF-8|UTF-8]] string prefixed with its length as a VarInt | ||
+ | |- | ||
+ | ! Chat | ||
+ | | ≥ 1 <br />≤ 2147483652 | ||
+ | | See [[Chat]] | ||
+ | | Encoded as a String | ||
|- | |- | ||
! VarInt | ! VarInt | ||
Line 60: | Line 75: | ||
| [http://developers.google.com/protocol-buffers/docs/encoding#varints Protocol Buffer Varint], encoding a two's complement signed 64-bit integer | | [http://developers.google.com/protocol-buffers/docs/encoding#varints Protocol Buffer Varint], encoding a two's complement signed 64-bit integer | ||
|- | |- | ||
− | ! | + | ! Metadata |
| Varies | | Varies | ||
− | | See [[Entities# | + | | See [[Entities#Entity Metadata Format]] |
| | | | ||
|- | |- | ||
− | ! Slot | + | ! Slot |
| Varies | | Varies | ||
− | | See [[ | + | | See [[Slot Data]] |
+ | | | ||
+ | |- | ||
+ | ! Object Data | ||
+ | | 4 or 10 | ||
+ | | See [[Object Data]] | ||
+ | | | ||
+ | |- | ||
+ | ! NBT Tag | ||
+ | | Varies | ||
+ | | See [[NBT]] | ||
| | | | ||
|- | |- | ||
Line 82: | Line 107: | ||
<pre>this.writeLong(uuid.getMostSignificantBits()); | <pre>this.writeLong(uuid.getMostSignificantBits()); | ||
this.writeLong(uuid.getLeastSignificantBits());</pre> | this.writeLong(uuid.getLeastSignificantBits());</pre> | ||
+ | |- | ||
+ | ! 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. | ||
|} | |} | ||
Revision as of 03:11, 21 January 2015
All data sent over the network 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.
Other than 'String' and 'Metadata', which are decoded with a custom function, these data formats are identical to those provided by the Java classes DataInputStream and DataOutputStream.
Name | Size (bytes) | Encodes | Notes |
---|---|---|---|
Boolean | 1 | false or true | Value can be either true (0x01 ) or false (0x00 )
|
Byte | 1 | -128 to 127 | Signed 8-bit integer, two's complement |
Unsigned Byte | 1 | 0 to 255 | Unsigned 8-bit integer |
Short | 2 | -32768 to 32767 | Signed 16-bit integer, two's complement |
Unsigned Short | 2 | 0 to 65535 | Unsigned 16-bit integer |
Int | 4 | -2147483648 to 2147483647 | Signed 32-bit integer, two's complement |
Long | 8 | -9223372036854775808 to 9223372036854775807 | Signed 64-bit integer, two's complement |
Float | 4 | Single-precision 32-bit IEEE 754 floating point | |
Double | 8 | Double-precision 64-bit IEEE 754 floating point | |
String | ≥ 1 ≤ 2147483652 |
A sequence of Unicode code points | UTF-8 string prefixed with its length as a VarInt |
Chat | ≥ 1 ≤ 2147483652 |
See Chat | Encoded as a String |
VarInt | ≥ 1 ≤ 5 |
-2147483648 to 2147483647 | Protocol Buffer Varint, encoding a two's complement signed 32-bit integer |
VarLong | ≥ 1 ≤ 10 |
-9223372036854775808 to 9223372036854775807 | Protocol Buffer Varint, encoding a two's complement signed 64-bit integer |
Metadata | Varies | See Entities#Entity Metadata Format | |
Slot | Varies | See Slot Data | |
Object Data | 4 or 10 | See Object Data | |
NBT Tag | Varies | See NBT | |
Position | 8 | 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. |
UUID | 16 | A UUID | The vanilla Minecraft server internally sends this as two longs.
this.writeLong(uuid.getMostSignificantBits()); this.writeLong(uuid.getLeastSignificantBits()); |
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. |
Position
64-bit long split in to three parts
x: 26 MSBs
z: 26 LSBs
y: 12 bits between them
Encoded as followed:
((x & 0x3FFFFFF) << 38) | ((y & 0xFFF) << 26) | (z & 0x3FFFFFF)
And decoded as:
long val; // Encoded value x = val >> 38; y = (val >> 26) & 0xFFF z = val << 38 >> 38
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;
And back again:
double = (double)abs_int / 32;