• 欢迎来到Minecraft插件百科!
  • 对百科编辑一脸懵逼?帮助:快速入门带您快速熟悉百科编辑!
  • 因近日遭受攻击,百科现已限制编辑,有意编辑请加入插件百科企鹅群:223812289

“Data Types”的版本间的差异

来自Minecraft插件百科
跳转至: 导航搜索
(translate)
第3行: 第3行:
 
{| class="wikitable"
 
{| class="wikitable"
 
  |-
 
  |-
  ! Name
+
  ! 名称
  ! Size (bytes)
+
  ! 长度 (字节)
  ! Encodes
+
  ! 内容
  ! Notes
+
  ! 注释
 
  |-
 
  |-
 
  ! Boolean
 
  ! Boolean
 
  | 1
 
  | 1
  | false or true
+
  | false true
  | Value can be either true (<code>0x01</code>) or false (<code>0x00</code>)
+
  | 值可以是 true (<code>0x01</code>) false (<code>0x00</code>)
 
  |-
 
  |-
 
  ! Byte
 
  ! Byte
 
  | 1
 
  | 1
  | -128 to 127
+
  | -128 127
  | Signed 8-bit integer, [[wikipedia:Two's complement|two's complement]]
+
  | 有符号的 8 位整型,[[wikipedia:Two's complement|负数补码]]
 
  |-
 
  |-
 
  ! Unsigned Byte
 
  ! Unsigned Byte
 
  | 1
 
  | 1
 
  | 0 to 255
 
  | 0 to 255
  | Unsigned 8-bit integer
+
  | 无符号的 8 位整型
 
  |-
 
  |-
 
  ! Short
 
  ! Short
 
  | 2
 
  | 2
 
  | -32768 to 32767
 
  | -32768 to 32767
  | Signed 16-bit integer, two's complement
+
  | 有符号 16 位整型,负数补码
 
  |-
 
  |-
 
  ! Unsigned Short
 
  ! Unsigned Short
 
  | 2
 
  | 2
  | 0 to 65535
+
  | 0 65535
  | Unsigned 16-bit integer
+
  | 无符号 16 位整型
 
  |-
 
  |-
 
  ! Int
 
  ! Int
 
  | 4
 
  | 4
  | -2147483648 to 2147483647
+
  | -2147483648 2147483647
  | Signed 32-bit integer, two's complement
+
  | 有符号 32 位整型,负数补码
 
  |-
 
  |-
 
  ! Long
 
  ! Long
 
  | 8
 
  | 8
  | -9223372036854775808 to 9223372036854775807
+
  | -9223372036854775808 9223372036854775807
  | Signed 64-bit integer, two's complement
+
  | 有符号 64 位整型,负数补码
 
  |-
 
  |-
 
  ! Float
 
  ! Float
 
  | 4
 
  | 4
  | [[wikipedia:Single-precision floating-point format|Single-precision 32-bit IEEE 754 floating point]]
+
  | [[wikipedia:Single-precision floating-point format|单精度 32 IEEE 754 浮点数]]
 
  |  
 
  |  
 
  |-
 
  |-
 
  ! Double
 
  ! Double
 
  | 8
 
  | 8
  | [[wikipedia:Double-precision floating-point format|Double-precision 64-bit IEEE 754 floating point]]
+
  | [[wikipedia:Double-precision floating-point format|双精度 64 IEEE 754 浮点数]]
 
  |  
 
  |  
 
  |-
 
  |-
 
  ! String
 
  ! String
 
  | ≥ 1 <br />≤&nbsp;2147483652
 
  | ≥ 1 <br />≤&nbsp;2147483652
  | A sequence of [[wikipedia:Unicode|Unicode]] code points
+
  | 一个 [[wikipedia:Unicode|Unicode]] 编码序列
  | [[wikipedia:UTF-8|UTF-8]] string prefixed with its size in bytes as a VarInt
+
  | [[wikipedia:UTF-8|UTF-8]] string 开头用 VarInt 以 byte 为单位表示它的长度
 
  |-
 
  |-
 
  ! Chat
 
  ! Chat
 
  | ≥ 1 <br />≤&nbsp;2147483652
 
  | ≥ 1 <br />≤&nbsp;2147483652
  | See [[Chat]]
+
  | 详见 [[Chat]]
  | Encoded as a String
+
  | String 编码
 
  |-  
 
  |-  
 
  ! VarInt
 
  ! VarInt
 
  | ≥ 1 <br />≤ 5
 
  | ≥ 1 <br />≤ 5
 
  | -2147483648 to 2147483647
 
  | -2147483648 to 2147483647
  | [http://developers.google.com/protocol-buffers/docs/encoding#varints Protocol Buffer Varint], encoding a two's complement signed 32-bit integer
+
  | [http://developers.google.com/protocol-buffers/docs/encoding#varints Protocol Buffer Varint],编码了一个负数补码的有符号 32 位整型
 
  |-
 
  |-
 
  ! VarLong
 
  ! VarLong
 
  | ≥ 1 <br />≤ 10
 
  | ≥ 1 <br />≤ 10
 
  | -9223372036854775808 to 9223372036854775807
 
  | -9223372036854775808 to 9223372036854775807
  | [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],编码了一个负数补码的有符号 64 位整型
 
  |-
 
  |-
 
  ! Chunk
 
  ! Chunk
  | Varies
+
  | 可变
  | A vertical chunk column
+
  | 一个垂直区块列
  | See [[SMP Map Format#Data]]
+
  | 详见 [[SMP Map Format#Data]]
 
  |-
 
  |-
 
  ! Metadata
 
  ! Metadata
  | Varies
+
  | 可变
  | See [[Entities#Entity Metadata Format]]
+
  | 详见 [[Entities#Entity Metadata Format]]
 
  |  
 
  |  
 
  |-  
 
  |-  
 
  ! Slot
 
  ! Slot
  | Varies
+
  | 可变
  | See [[Slot Data]]
+
  | 详见 [[Slot Data]]
 
  |  
 
  |  
 
  |-
 
  |-
 
  ! Object Data
 
  ! Object Data
  | 4 or 10
+
  | 4 10
  | See [[Object Data]]
+
  | 详见 [[Object Data]]
 
  |  
 
  |  
 
  |-
 
  |-
 
  ! NBT Tag
 
  ! NBT Tag
  | Varies
+
  | 可变
  | See [[NBT]]
+
  | 详见 [[NBT]]
 
  |  
 
  |  
 
  |-
 
  |-
 
  ! Position  
 
  ! Position  
 
  | 8
 
  | 8
  | Integer/block position: x (-33554432 to 33554431), y (-2048 to 2047), z (-33554432 to 33554431)
+
  | 整型/方块坐标: x (-33554432 33554431), y (-2048 2047), z (-33554432 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 [[#Position|the section below]].
+
  | x 为一个 26 位整型,随着的是用 12 位整型表示的 y,然后是用 26 位整型表示的 z (均为有符号,负数补码)。同时参阅 [[#Position|下面的部分]].
 
  |-
 
  |-
 
  ! Angle
 
  ! Angle
 
  | 1
 
  | 1
  | Rotation angle in steps of 1/256 of a full turn
+
  | 以整圈的 1/256 为步长的旋转角度
  | Whether or not this is signed does not matter, since the resulting angles are the same.
+
  | 是否有符号没有关系因为结果的角度是相同的。
 
  |-
 
  |-
 
  ! UUID
 
  ! UUID
 
  | 16
 
  | 16
  | A [[wikipedia:Universally_unique_identifier|UUID]]
+
  | 一个 [[wikipedia:Universally_unique_identifier|通用唯一识别码]]
  | The vanilla Minecraft server internally sends this as two longs.
+
  | 官方版本的 Minecraft 服务器内部以两个 long 发送。
  
 
<pre>this.writeLong(uuid.getMostSignificantBits());
 
<pre>this.writeLong(uuid.getMostSignificantBits());
第117行: 第117行:
 
  |-
 
  |-
 
  ! Optional X
 
  ! Optional X
  | 0 or size of X
+
  | 0 X 的长度
  | A field of type X, or nothing
+
  | 一个 X 类型的域或者什么都不是
  | Whether or not the field is present must be known from the context.
+
  | 域是否存在必须从上下文中得知。
 
  |-
 
  |-
 
  ! Array of X
 
  ! Array of X
  | count times size of X
+
  | X 的长度的重复次数
  | Zero or more fields of type X
+
  | 零或以上个 X 类型的域
  | The count must be known from the context.
+
  | 域的数量必须从上下文中得知。
 
  |-
 
  |-
 
  ! Byte Array
 
  ! 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.
+
  | 这只是一个零或更多个 Byte 的序列,他的含义将会在其他地方被解释,例如:在包描述中。长度必须从上下文中得知。
 
  |}
 
  |}
  

2016年7月18日 (一) 22:22的版本

所有通过网络发送的数据均为大端序(big-endian,这意味着字节被从高位向低位发送。日常中大多数的计算机为小端序,所以可能需要在发送数据到网络之前将其转换为大端序。

名称 长度 (字节) 内容 注释
Boolean 1 false 或 true 值可以是 true (0x01) 或 false (0x00)
Byte 1 -128 到 127 有符号的 8 位整型,负数补码
Unsigned Byte 1 0 to 255 无符号的 8 位整型
Short 2 -32768 to 32767 有符号 16 位整型,负数补码
Unsigned Short 2 0 到 65535 无符号 16 位整型
Int 4 -2147483648 到 2147483647 有符号 32 位整型,负数补码
Long 8 -9223372036854775808 到 9223372036854775807 有符号 64 位整型,负数补码
Float 4 单精度 32 位 IEEE 754 浮点数
Double 8 双精度 64 位 IEEE 754 浮点数
String ≥ 1
≤ 2147483652
一个 Unicode 编码序列 UTF-8 string 开头用 VarInt 以 byte 为单位表示它的长度
Chat ≥ 1
≤ 2147483652
详见 Chat 以 String 编码
VarInt ≥ 1
≤ 5
-2147483648 to 2147483647 Protocol Buffer Varint,编码了一个负数补码的有符号 32 位整型
VarLong ≥ 1
≤ 10
-9223372036854775808 to 9223372036854775807 Protocol Buffer Varint,编码了一个负数补码的有符号 64 位整型
Chunk 可变 一个垂直区块列 详见 SMP Map Format#Data
Metadata 可变 详见 Entities#Entity Metadata Format
Slot 可变 详见 Slot Data
Object Data 4 或 10 详见 Object Data
NBT Tag 可变 详见 NBT
Position 8 整型/方块坐标: x (-33554432 到 33554431), y (-2048 到 2047), z (-33554432 到 33554431) x 为一个 26 位整型,随着的是用 12 位整型表示的 y,然后是用 26 位整型表示的 z (均为有符号,负数补码)。同时参阅 下面的部分.
Angle 1 以整圈的 1/256 为步长的旋转角度 是否有符号没有关系因为结果的角度是相同的。
UUID 16 一个 通用唯一识别码 官方版本的 Minecraft 服务器内部以两个 long 发送。
this.writeLong(uuid.getMostSignificantBits());
this.writeLong(uuid.getLeastSignificantBits());
Optional X 0 或 X 的长度 一个 X 类型的域或者什么都不是 域是否存在必须从上下文中得知。
Array of X X 的长度的重复次数 零或以上个 X 类型的域 域的数量必须从上下文中得知。
Byte Array 可变 依赖于上下文 这只是一个零或更多个 Byte 的序列,他的含义将会在其他地方被解释,例如:在包描述中。长度必须从上下文中得知。

位置

分成 3 部分的 64 个比特位

  • x: 26 MSBs
  • z: 26 LSBs
  • y: 12 bits between them

像这样编码:

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

像这样解码:

long val; // Encoded value
x = val >> 38;
y = (val >> 26) & 0xFFF
z = val << 38 >> 38

Note: 位移的细节是和编程语言相关的;以上代码若没有调整可能在 Java 中正确运行但在其他语言中无法正确运行????。

定点数据

有些域可能会被存储为 定点数(fixed point),即使用一定数量的比特位来存储小数的整数部分(小数点的左边),其余比特位表示小数部分 (小数点的右边)。浮点数 (float 和 double) 则相反,它将数字本身(底数)放在一个块里, 小数点的位置(指数)在其一旁存储。

实际上,虽然定点数的范围比浮点数小,但他们对于更高数值的精度更高。这使得他们成为在Minecraft中表示一个实体的全局坐标的理想选择,因为比起在一个方块(或一米)中精确的确定他们的位置而言,存储表示他们所在方块的整数部分显得更重要。

坐标通常被表示为一个 32 位的整型,低 5 位被用来存储小数部分,剩余的用来存储整数部分。

Java 缺乏对带小数部分的整型的直接支持,但你可以将他们表示为整型(integer)。如果要从一个 double 转换到这个整型表示,使用以下公式:

 abs_int = (int)double * 32;

逆向过程:

 double = (double)abs_int / 32;