/
interact.go
62 lines (56 loc) · 1.72 KB
/
interact.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
package packet
import (
"bytes"
"encoding/binary"
"github.com/go-gl/mathgl/mgl32"
"github.com/sandertv/gophertunnel/minecraft/protocol"
)
const (
_ = iota
_
_
InteractActionLeaveVehicle
InteractActionMouseOverEntity
_
InteractActionOpenInventory
)
// Interact is sent by the client when it interacts with another entity in some way. It used to be used for
// normal entity and block interaction, but this is no longer the case now.
type Interact struct {
// Action type is the ID of the action that was executed by the player. It is one of the constants that
// may be found above.
ActionType byte
// TargetEntityRuntimeID is the runtime ID of the entity that the player interacted with. This is empty
// for the InteractActionOpenInventory action type.
TargetEntityRuntimeID uint64
// MouseOverPosition was the position relative to the entity moused over over which the player hovered
// with its mouse/touch. It is only set if ActionType is InteractActionMouseOverEntity.
MouseOverPosition mgl32.Vec3
}
// ID ...
func (*Interact) ID() uint32 {
return IDInteract
}
// Marshal ...
func (pk *Interact) Marshal(buf *bytes.Buffer) {
_ = binary.Write(buf, binary.LittleEndian, pk.ActionType)
_ = protocol.WriteVaruint64(buf, pk.TargetEntityRuntimeID)
switch pk.ActionType {
case InteractActionMouseOverEntity:
_ = protocol.WriteVec3(buf, pk.MouseOverPosition)
}
}
// Unmarshal ...
func (pk *Interact) Unmarshal(buf *bytes.Buffer) error {
if err := chainErr(
binary.Read(buf, binary.LittleEndian, &pk.ActionType),
protocol.Varuint64(buf, &pk.TargetEntityRuntimeID),
); err != nil {
return err
}
switch pk.ActionType {
case InteractActionMouseOverEntity:
return protocol.Vec3(buf, &pk.MouseOverPosition)
}
return nil
}