forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
270 lines (249 loc) · 8.11 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package session
import (
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/cmd"
"github.com/Adrian8115/gophertunnel-Amethyst-Protocol/minecraft/protocol"
"github.com/Adrian8115/gophertunnel-Amethyst-Protocol/minecraft/protocol/packet"
"github.com/go-gl/mathgl/mgl64"
"math"
)
// 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) {
if s == Nop {
return
}
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{}
var enums []commandEnum
enumIndices := map[string]uint32{}
var dynamicEnums []commandEnum
dynamicEnumIndices := map[string]uint32{}
suffixIndices := map[string]uint32{}
for alias, c := range commands {
if c.Name() != alias {
// Don't add duplicate entries for aliases.
continue
}
if run := c.Runnables(s.c); len(run) > 0 {
m[alias] = run
} else {
continue
}
params := c.Params(s.c)
overloads := make([]protocol.CommandOverload, len(params))
aliasesIndex := uint32(math.MaxUint32)
if len(c.Aliases()) > 0 {
aliasesIndex = uint32(len(enumIndices))
enumIndices[c.Name()+"Aliases"] = aliasesIndex
enums = append(enums, commandEnum{Type: c.Name() + "Aliases", Options: c.Aliases()})
}
for i, params := range params {
for _, paramInfo := range params {
t, enum := valueToParamType(paramInfo, s.c)
t |= protocol.CommandArgValid
suffix := paramInfo.Suffix
opt := byte(0)
if _, ok := paramInfo.Value.(bool); ok {
opt |= protocol.ParamOptionCollapseEnum
}
if len(enum.Options) > 0 || enum.Type != "" {
if !enum.Dynamic {
index, ok := enumIndices[enum.Type]
if !ok {
index = uint32(len(enums))
enumIndices[enum.Type] = index
enums = append(enums, enum)
}
t |= protocol.CommandArgEnum | index
} else {
index, ok := dynamicEnumIndices[enum.Type]
if !ok {
index = uint32(len(dynamicEnums))
dynamicEnumIndices[enum.Type] = index
dynamicEnums = append(dynamicEnums, enum)
}
t |= protocol.CommandArgSoftEnum | index
}
}
if suffix != "" {
index, ok := suffixIndices[suffix]
if !ok {
index = uint32(len(pk.Suffixes))
suffixIndices[suffix] = index
pk.Suffixes = append(pk.Suffixes, suffix)
}
t |= protocol.CommandArgSuffixed | index
}
overloads[i].Parameters = append(overloads[i].Parameters, protocol.CommandParameter{
Name: paramInfo.Name,
Type: t,
Optional: paramInfo.Optional,
Options: opt,
})
}
}
pk.Commands = append(pk.Commands, protocol.Command{
Name: c.Name(),
Description: c.Description(),
AliasesOffset: aliasesIndex,
Overloads: overloads,
})
}
pk.DynamicEnums = make([]protocol.DynamicEnum, 0, len(dynamicEnums))
for _, e := range dynamicEnums {
pk.DynamicEnums = append(pk.DynamicEnums, protocol.DynamicEnum{Type: e.Type, Values: e.Options})
}
enumValueIndices := make(map[string]uint32, len(enums)*3)
pk.EnumValues = make([]string, 0, len(enumValueIndices))
pk.Enums = make([]protocol.CommandEnum, 0, len(enums))
for _, enum := range enums {
protoEnum := protocol.CommandEnum{Type: enum.Type}
for _, opt := range enum.Options {
index, ok := enumValueIndices[opt]
if !ok {
index = uint32(len(pk.EnumValues))
enumValueIndices[opt] = index
pk.EnumValues = append(pk.EnumValues, opt)
}
protoEnum.ValueIndices = append(protoEnum.ValueIndices, uint(index))
}
pk.Enums = append(pk.Enums, protoEnum)
}
s.writePacket(pk)
return m
}
type commandEnum struct {
Type string
Options []string
Dynamic bool
}
// valueToParamType finds the command argument type of the value passed and returns it, in addition to creating
// an enum if applicable.
func valueToParamType(i cmd.ParamInfo, source cmd.Source) (t uint32, enum commandEnum) {
switch i.Value.(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, commandEnum{
Type: "bool",
Options: []string{"true", "1", "false", "0"},
}
case mgl64.Vec3:
return protocol.CommandArgTypePosition, enum
case cmd.SubCommand:
return 0, commandEnum{
Type: "SubCommand" + i.Name,
Options: []string{i.Name},
}
}
if enum, ok := i.Value.(cmd.Enum); ok {
return 0, 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 {
if run := c.Runnables(s.c); len(run) > 0 {
m[alias] = run
}
}
}
if len(before) != len(m) {
return s.sendAvailableCommands(), true
}
// First check for commands that were newly added.
for name, r := range m {
for k := range r {
if _, ok := before[name][k]; !ok {
return s.sendAvailableCommands(), true
}
}
}
// Then check for commands that a player could execute before, but no longer can.
for name, r := range before {
for k := range r {
if _, ok := m[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
}
}
}
}