Difference between revisions of "Data types"

From wiki.vg
Jump to navigation Jump to search
(added/updated sizes)
m (cleaned up table style and formatting; removed the 128-bit integer type as it seems not to be used anymore)
Line 4: Line 4:
  
 
{| class="wikitable"
 
{| class="wikitable"
|- class="row0"
+
|-
| class="col0" |
+
!
! class="col1" | Size
+
! Size
! class="col2" | Range
+
! Range
! class="col3" | Notes
+
! Notes
|- class="row1"
+
|-
! class="col0 centeralign" | bool
+
! bool
| class="col1 centeralign" | 1
+
| 1
| class="col2" | 0 or 1
+
| 0 or 1
| class="col3" | Value can be either true (0x01) or false (0x00)
+
| Value can be either true (<code>0x01</code>) or false (<code>0x00</code>)
|- class="row2"
+
|-
! class="col0 centeralign" | byte
+
! byte
| class="col1 centeralign" | 1
+
| 1
| class="col2" | -128 to 127
+
| -128 to 127
| class="col3" | Signed, two's complement
+
| Signed, two's complement
|- class="row3"
+
|-
! class="col0 centeralign" | short
+
! short
| class="col1 centeralign" | 2
+
| 2
| class="col2" | -32768 to 32767
+
| -32768 to 32767
| class="col3" | Signed, two's complement
+
| Signed, two's complement
|- class="row4"
+
|-
! class="col0 centeralign" | int
+
! int
| class="col1 centeralign" | 4
+
| 4
| class="col2" | -2147483648 to 2147483647
+
| -2147483648 to 2147483647
| class="col3" | Signed, two's complement
+
| Signed, two's complement
|- class="row5"
+
|-
! class="col0 centeralign" | long
+
! long
| class="col1 centeralign" | 8
+
| 8
| class="col2" | -9223372036854775808 to 9223372036854775807
+
| -9223372036854775808 to 9223372036854775807
| class="col3" | Signed, two's complement
+
| Signed, two's complement
|- class="row8"
+
|-
! class="col0 centeralign" | 128-bit integer
+
! float
| class="col1 centeralign" | 16
+
| 4
| class="col2" | 0 to 340282366920938463463374607431768211455
+
| See [http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3 this]
| class="col3" | Unsigned, two's complement
+
| Single-precision 32-bit IEEE 754 floating point
 +
|-
 +
! double
 +
| 8
 +
| See [http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3 this]
 +
| Double-precision 64-bit IEEE 754 floating point
 +
|-
 +
! string
 +
| ≥ 1 <br />≤ 2147483652
 +
| N/A
 +
| UTF-8 string prefixed with its length as a VarInt
 +
|-
 +
! VarInt
 +
| ≥ 1 <br />≤ 5
 +
| [http://developers.google.com/protocol-buffers/docs/encoding#varints Protocol Buffer 32-bit Varint]
 +
| Signed, two's complement
 +
|-
 +
! VarLong
 +
| ≥ 1 <br />≤ 10
 +
|
 +
| Like VarInt but for java longs
 +
|-
 +
! metadata
 +
| Varies
 +
| See [[Entities#Entity_Metadata_Format|this]]
 +
|
 +
|-
 +
! Slot Data
 +
| Varies
 +
| See [[Slot_Data|slot data]]
 +
|
 +
|-
 +
! Position
 +
| 8
 +
|
 +
| See below
 +
|-
 +
! UUID
 +
| 16
 +
| 0 to 340282366920938463463374607431768211455
 +
| The vanilla Minecraft server internally sends this as two longs.
  
Used in [http://wiki.vg/Protocol#Spawn_Global_Entity 0x2C] to transmit UUIDs.
+
<pre>this.writeLong(uuid.getMostSignificantBits());
 
+
this.writeLong(uuid.getLeastSignificantBits());</pre>
The vanilla Minecraft server internally sends this as two longs.
+
|}
|- class="row7"
 
! class="col0 centeralign" | float
 
| class="col1 centeralign" | 4
 
| class="col2" |
 
See [http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3 this]
 
| class="col3" | Single-precision 32-bit IEEE 754 floating point
 
|- class="row8"
 
! class="col0 centeralign" | double
 
| class="col1 centeralign" | 8
 
| class="col2" |
 
See [http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3 this]
 
| class="col3" | Double-precision 64-bit IEEE 754 floating point
 
|- class="row9"
 
! class="col0 centeralign" | string
 
| class="col1 centeralign" | ≥ 1 <br />≤ 2147483652
 
| class="col2" | N/A
 
| class="col3" | UTF-8 String prefixed with its length as a VarInt
 
|-
 
! class="centeralign" | VarInt
 
| class="centeralign" | ≥ 1 <br />≤ 5
 
| [http://developers.google.com/protocol-buffers/docs/encoding#varints Protocol Buffer 32-bit Varint]
 
| Signed, two's complement
 
|-
 
! VarLong
 
| ≥ 1 <br />≤ 10
 
|
 
| Like VarInt but for java longs
 
|- class="row9"
 
! class="col0 centeralign" | metadata
 
| class="col1 centeralign" | Varies
 
| class="col2" | See [[Entities#Entity_Metadata_Format|this]]
 
| class="Col3" |
 
|-
 
! class="centeralign" | Slot Data
 
| class="centeralign" | Varies
 
| See [[Slot_Data|slot data]]
 
|
 
|-
 
! Position
 
| 8
 
|
 
| See below
 
|-
 
! UUID
 
| 16
 
|
 
| Encoded as two longs this.writeLong(uuid.getMostSignificantBits()); this.writeLong(uuid.getLeastSignificantBits());
 
|}
 
  
 
=== Position ===
 
=== Position ===

Revision as of 17:13, 20 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.

Size Range Notes
bool 1 0 or 1 Value can be either true (0x01) or false (0x00)
byte 1 -128 to 127 Signed, two's complement
short 2 -32768 to 32767 Signed, two's complement
int 4 -2147483648 to 2147483647 Signed, two's complement
long 8 -9223372036854775808 to 9223372036854775807 Signed, two's complement
float 4 See this Single-precision 32-bit IEEE 754 floating point
double 8 See this Double-precision 64-bit IEEE 754 floating point
string ≥ 1
≤ 2147483652
N/A UTF-8 string prefixed with its length as a VarInt
VarInt ≥ 1
≤ 5
Protocol Buffer 32-bit Varint Signed, two's complement
VarLong ≥ 1
≤ 10
Like VarInt but for java longs
metadata Varies See this
Slot Data Varies See slot data
Position 8 See below
UUID 16 0 to 340282366920938463463374607431768211455 The vanilla Minecraft server internally sends this as two longs.
this.writeLong(uuid.getMostSignificantBits());
this.writeLong(uuid.getLeastSignificantBits());

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;