/
request_ability.go
80 lines (74 loc) · 1.83 KB
/
request_ability.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
package packet
import (
"github.com/sandertv/gophertunnel/minecraft/protocol"
)
const (
AbilityBuild = iota
AbilityMine
AbilityDoorsAndSwitches
AbilityOpenContainers
AbilityAttackPlayers
AbilityAttackMobs
AbilityOperatorCommands
AbilityTeleport
AbilityInvulnerable
AbilityFlying
AbilityMayFly
AbilityInstantBuild
AbilityLightning
AbilityFlySpeed
AbilityWalkSpeed
AbilityMuted
AbilityWorldBuilder
AbilityNoClip
AbilityAbilityCount
)
// RequestAbility is a packet sent by the client to the server to request permission for a specific ability from the
// server. These abilities are defined above.
type RequestAbility struct {
// Ability is the ability that the client is requesting. This is one of the constants defined above.
Ability int32
// Value represents the value of the ability. This can either be a boolean or a float32, otherwise the writer/reader
// will panic.
Value any
}
// ID ...
func (*RequestAbility) ID() uint32 {
return IDRequestAbility
}
// Marshal ...
func (pk *RequestAbility) Marshal(w *protocol.Writer) {
w.Varint32(&pk.Ability)
switch val := pk.Value.(type) {
case bool:
valType, defaultVal := uint8(1), float32(0)
w.Uint8(&valType)
w.Bool(&val)
w.Float32(&defaultVal)
case float32:
valType, defaultVal := uint8(2), false
w.Uint8(&valType)
w.Bool(&defaultVal)
w.Float32(&val)
default:
w.InvalidValue(pk.Value, "ability value type", "must be bool or float32")
}
}
// Unmarshal ...
func (pk *RequestAbility) Unmarshal(r *protocol.Reader) {
valType, boolVal, floatVal := uint8(0), false, float32(0)
r.Varint32(&pk.Ability)
r.Uint8(&valType)
switch valType {
case 1:
r.Bool(&boolVal)
r.Float32(&floatVal)
pk.Value = boolVal
case 2:
r.Bool(&boolVal)
r.Float32(&floatVal)
pk.Value = floatVal
default:
r.InvalidValue(valType, "ability value type", "must be bool or float32")
}
}