-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
block_pos.go
70 lines (61 loc) · 1.9 KB
/
block_pos.go
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
package protocol
import (
"bytes"
)
// BlockPos is the position of a block. It is composed of three integers, and is typically written as either
// 3 varint32s or a varint32, varuint32 and varint32.
type BlockPos [3]int32
// X returns the X coordinate of the block position. It is equivalent to BlockPos[0].
func (pos BlockPos) X() int32 {
return pos[0]
}
// Y returns the Y coordinate of the block position. It is equivalent to BlockPos[1].
func (pos BlockPos) Y() int32 {
return pos[1]
}
// Z returns the Z coordinate of the block position. It is equivalent to BlockPos[2].
func (pos BlockPos) Z() int32 {
return pos[2]
}
// BlockPosition reads a BlockPos from Buffer src and stores it to the BlockPos pointer passed.
func BlockPosition(src *bytes.Buffer, x *BlockPos) error {
if err := chainErr(
Varint32(src, &(*x)[0]),
Varint32(src, &(*x)[1]),
Varint32(src, &(*x)[2]),
); err != nil {
return wrap(err)
}
return nil
}
// WriteBlockPosition writes a BlockPos x to Buffer dst, composed of 3 varint32s.
func WriteBlockPosition(dst *bytes.Buffer, x BlockPos) error {
return chainErr(
WriteVarint32(dst, x[0]),
WriteVarint32(dst, x[1]),
WriteVarint32(dst, x[2]),
)
}
// UBlockPosition reads an unsigned BlockPos from Buffer src and stores it to the BlockPos pointer passed. The
// difference between this and BlockPosition is that the Y coordinate is read as a varuint32.
func UBlockPosition(src *bytes.Buffer, x *BlockPos) error {
var v uint32
if err := chainErr(
Varint32(src, &(*x)[0]),
Varuint32(src, &v),
Varint32(src, &(*x)[2]),
); err != nil {
return wrap(err)
}
(*x)[1] = int32(v)
return nil
}
// WriteUBlockPosition writes an unsigned BlockPos x to Buffer dst, composed of a varint32, varuint32 and a
// varint32.
func WriteUBlockPosition(dst *bytes.Buffer, x BlockPos) error {
return chainErr(
WriteVarint32(dst, x[0]),
WriteVaruint32(dst, uint32(x[1])),
WriteVarint32(dst, x[2]),
)
}