-
Notifications
You must be signed in to change notification settings - Fork 143
/
command.go
183 lines (170 loc) · 5.67 KB
/
command.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package session
import (
"github.com/df-mc/dragonfly/server/cmd"
"github.com/go-gl/mathgl/mgl64"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)
// SendCommandOutput sends the output of a command to the player. It will be shown to the caller of the
// command, which might be the player or a websocket server.
func (s *Session) SendCommandOutput(output *cmd.Output) {
messages := make([]protocol.CommandOutputMessage, 0, output.MessageCount()+output.ErrorCount())
for _, message := range output.Messages() {
messages = append(messages, protocol.CommandOutputMessage{
Success: true,
Message: message,
})
}
for _, err := range output.Errors() {
messages = append(messages, protocol.CommandOutputMessage{
Success: false,
Message: err.Error(),
})
}
s.writePacket(&packet.CommandOutput{
CommandOrigin: s.handlers[packet.IDCommandRequest].(*CommandRequestHandler).origin,
OutputType: packet.CommandOutputTypeAllOutput,
SuccessCount: uint32(output.MessageCount()),
OutputMessages: messages,
})
}
// sendAvailableCommands sends all available commands of the server. Once sent, they will be visible in the
// /help list and will be auto-completed.
func (s *Session) sendAvailableCommands() map[string]map[int]cmd.Runnable {
commands := cmd.Commands()
m := make(map[string]map[int]cmd.Runnable, len(commands))
pk := &packet.AvailableCommands{}
for alias, c := range commands {
if c.Name() != alias {
// Don't add duplicate entries for aliases.
continue
}
m[alias] = c.Runnables(s.c)
params := c.Params(s.c)
overloads := make([]protocol.CommandOverload, len(params))
for i, params := range params {
for _, paramInfo := range params {
t, enum := valueToParamType(paramInfo.Value, s.c)
t |= protocol.CommandArgValid
opt := byte(0)
if _, ok := paramInfo.Value.(bool); ok {
opt |= protocol.ParamOptionCollapseEnum
}
overloads[i].Parameters = append(overloads[i].Parameters, protocol.CommandParameter{
Name: paramInfo.Name,
Type: t,
Optional: paramInfo.Optional,
Options: opt,
Enum: enum,
Suffix: paramInfo.Suffix,
})
}
}
if len(params) > 0 {
pk.Commands = append(pk.Commands, protocol.Command{
Name: c.Name(),
Description: c.Description(),
Aliases: c.Aliases(),
Overloads: overloads,
})
}
}
s.writePacket(pk)
return m
}
// valueToParamType finds the command argument type of the value passed and returns it, in addition to creating
// an enum if applicable.
func valueToParamType(i interface{}, source cmd.Source) (t uint32, enum protocol.CommandEnum) {
switch i.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return protocol.CommandArgTypeInt, enum
case float32, float64:
return protocol.CommandArgTypeFloat, enum
case string:
return protocol.CommandArgTypeString, enum
case cmd.Varargs:
return protocol.CommandArgTypeRawText, enum
case cmd.Target, []cmd.Target:
return protocol.CommandArgTypeTarget, enum
case bool:
return 0, protocol.CommandEnum{
Type: "bool",
Options: []string{"true", "1", "false", "0"},
}
case mgl64.Vec3:
return protocol.CommandArgTypePosition, enum
}
if sub, ok := i.(cmd.SubCommand); ok {
return 0, protocol.CommandEnum{
Type: "SubCommand" + sub.SubName(),
Options: []string{sub.SubName()},
}
}
if enum, ok := i.(cmd.Enum); ok {
return 0, protocol.CommandEnum{
Type: enum.Type(),
Options: enum.Options(source),
Dynamic: true,
}
}
return protocol.CommandArgTypeValue, enum
}
// resendCommands resends all commands that a Session has access to if the map of runnable commands passed does not
// match with the commands that the Session is currently allowed to execute.
// True is returned if the commands were resent.
func (s *Session) resendCommands(before map[string]map[int]cmd.Runnable) (map[string]map[int]cmd.Runnable, bool) {
commands := cmd.Commands()
m := make(map[string]map[int]cmd.Runnable, len(commands))
for alias, c := range commands {
if c.Name() == alias {
m[alias] = c.Runnables(s.c)
}
}
if len(before) != len(m) {
return s.sendAvailableCommands(), true
}
for name, r := range m {
for k := range r {
if _, ok := before[name][k]; !ok {
return s.sendAvailableCommands(), true
}
}
}
return m, false
}
// enums returns a map of all enums exposed to the Session and records the values those enums currently hold.
func (s *Session) enums() (map[string]cmd.Enum, map[string][]string) {
enums, enumValues := make(map[string]cmd.Enum), make(map[string][]string)
for alias, c := range cmd.Commands() {
if c.Name() == alias {
for _, params := range c.Params(s.c) {
for _, paramInfo := range params {
if enum, ok := paramInfo.Value.(cmd.Enum); ok {
enums[enum.Type()] = enum
enumValues[enum.Type()] = enum.Options(s.c)
}
}
}
}
}
return enums, enumValues
}
// resendEnums checks the options of the enums passed against the values that were previously recorded. If they do not
// match, the enum is resent to the client and the values are updated in the before map.
func (s *Session) resendEnums(enums map[string]cmd.Enum, before map[string][]string) {
for name, enum := range enums {
valuesBefore := before[name]
values := enum.Options(s.c)
before[name] = values
if len(valuesBefore) != len(values) {
s.writePacket(&packet.UpdateSoftEnum{EnumType: name, Options: values, ActionType: packet.SoftEnumActionSet})
continue
}
for k, v := range values {
if valuesBefore[k] != v {
s.writePacket(&packet.UpdateSoftEnum{EnumType: name, Options: values, ActionType: packet.SoftEnumActionSet})
break
}
}
}
}