-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
entity_link.go
83 lines (76 loc) · 2.67 KB
/
entity_link.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
package protocol
import (
"bytes"
"encoding/binary"
)
const (
// EntityLinkRemove is set to remove the link between two entities.
EntityLinkRemove = iota
// EntityLinkRider is set for entities that have control over the entity they're riding, such as in a
// minecart.
EntityLinkRider
// EntityLinkPassenger is set for entities being a passenger of a vehicle they enter, such as the back
// sit of a boat.
EntityLinkPassenger
)
// EntityLink is a link between two entities, typically being one entity riding another.
type EntityLink struct {
// RiddenEntityUniqueID is the entity unique ID of the entity that is being ridden. For a player sitting
// in a boat, this is the unique ID of the boat.
RiddenEntityUniqueID int64
// RiderEntityUniqueID is the entity unique ID of the entity that is riding. For a player sitting in a
// boat, this is the unique ID of the player.
RiderEntityUniqueID int64
// Type is one of the types above. It specifies the way the entity is linked to another entity.
Type byte
// Immediate is set to immediately dismount an entity from another. This should be set when the mount of
// an entity is killed.
Immediate bool
}
// EntityLinkAction reads a single entity link (action) from buffer src.
func EntityLinkAction(src *bytes.Buffer, x *EntityLink) error {
return chainErr(
Varint64(src, &x.RiddenEntityUniqueID),
Varint64(src, &x.RiderEntityUniqueID),
binary.Read(src, binary.LittleEndian, &x.Type),
binary.Read(src, binary.LittleEndian, &x.Immediate),
)
}
// EntityLinks reads a list of entity links from buffer src that are currently active.
func EntityLinks(src *bytes.Buffer, x *[]EntityLink) error {
var count uint32
if err := Varuint32(src, &count); err != nil {
return wrap(err)
}
if count > lowerLimit {
return LimitHitError{Limit: lowerLimit, Type: "entity link"}
}
*x = make([]EntityLink, count)
for i := uint32(0); i < count; i++ {
if err := EntityLinkAction(src, &(*x)[i]); err != nil {
return wrap(err)
}
}
return nil
}
// WriteEntityLinkAction writes a single entity link x to buffer dst.
func WriteEntityLinkAction(dst *bytes.Buffer, x EntityLink) error {
return chainErr(
WriteVarint64(dst, x.RiddenEntityUniqueID),
WriteVarint64(dst, x.RiderEntityUniqueID),
binary.Write(dst, binary.LittleEndian, x.Type),
binary.Write(dst, binary.LittleEndian, x.Immediate),
)
}
// WriteEntityLinks writes a list of entity links currently active to buffer dst.
func WriteEntityLinks(dst *bytes.Buffer, x []EntityLink) error {
if err := WriteVaruint32(dst, uint32(len(x))); err != nil {
return wrap(err)
}
for _, link := range x {
if err := WriteEntityLinkAction(dst, link); err != nil {
return wrap(err)
}
}
return nil
}