/
types.go
116 lines (95 loc) · 2.77 KB
/
types.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package protocol
import (
"fmt"
"io"
)
// Serializable is a type which can be serialized into a packet.
// This is used by protocol_builder when the struct tag 'as' is set
// to "raw".
type Serializable interface {
Serialize(w io.Writer) error
Deserialize(r io.Reader) error
}
//go:generate stringer -type=State
// State defined which state the protocol is in.
type State int
// States of the protocol.
// Handshaking is default.
const (
Handshaking State = 0
Play State = 1
Status State = 2
Login State = 3
)
const (
// SupportedProtocolVersion is current protocol version this package defines
SupportedProtocolVersion = 71
)
const (
clientbound = iota
serverbound
)
const maxPacketCount = 100
var packetCreator [4][2][maxPacketCount]func() Packet
// VarInt is a variable length integer with a cap of
// 32 bits
type VarInt int32
// VarLong is a variable length integer with a cap of
// 64 bits
type VarLong int64
// Position is a location in the world packed into a 64 bit integer
type Position uint64
// NewPosition creates a Position for the given location.
func NewPosition(x, y, z int) Position {
return ((Position(x) & 0x3FFFFFF) << 38) |
((Position(y) & 0xFFF) << 26) |
(Position(z) & 0x3FFFFFF)
}
// X returns the X component of the position
func (p Position) X() int {
return int(int64(p) >> 38)
}
// Y returns the Y component of the position
func (p Position) Y() int {
return int((int64(p) >> 26) & 0xFFF)
}
// Z returns the Z component of the position
func (p Position) Z() int {
return int(int64(p) << 38 >> 38)
}
// String returns a string representation of the position
func (p Position) String() string {
return fmt.Sprintf("%d,%d,%d", p.X(), p.Y(), p.Z())
}
// UUID is an unique identifier
type UUID [16]byte
// Serialize serializes the uuid into the writer
func (u *UUID) Serialize(w io.Writer) error {
_, err := w.Write(u[:])
return err
}
// Deserialize deserializes the uuid from the reader
func (u *UUID) Deserialize(r io.Reader) error {
_, err := io.ReadFull(r, u[:])
return err
}
// Packet is a structure that can be serialized or deserialized from
// Minecraft connection
type Packet interface {
write(io.Writer) error
read(io.Reader) error
id() int
}